Skip to content

Commit

Permalink
better python things (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
codekansas authored Sep 22, 2024
1 parent 56db27d commit 1c336f6
Show file tree
Hide file tree
Showing 19 changed files with 58 additions and 44 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ all:
# ------------------------ #

start-backend:
@python 'linguaphoto/main.py'
@uvicorn linguaphoto.main:app --reload --port 8080 --host localhost

start-frontend:
@cd frontend && npm start
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/api/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ export const read_me = async (token: string): Promise<Response | null> => {
}
};
export const signin = async (data: SigninData): Promise<Response> => {
console.log("signing in... url:", `${API_URL}/signin`);
const response = await axios.post(`${API_URL}/signin`, data);
console.log("signing in... response:", response.data);
return response.data;
};
export const social_facebook_login = async (
Expand Down
1 change: 1 addition & 0 deletions linguaphoto/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = "0.0.1"
5 changes: 3 additions & 2 deletions linguaphoto/ai/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

from openai import AsyncOpenAI
from PIL import Image
from transcribe import transcribe_image
from tts import synthesize_text

from linguaphoto.ai.transcribe import transcribe_image
from linguaphoto.ai.tts import synthesize_text

logger = logging.getLogger(__name__)

Expand Down
6 changes: 2 additions & 4 deletions linguaphoto/ai/transcribe.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
"""Uses the OpenAI API to transcribe an image to text."""

# import argparse

# import asyncio
import base64
import logging
from io import BytesIO

import aiohttp
from models import TranscriptionResponse
from openai import AsyncOpenAI
from PIL import Image

from linguaphoto.models import TranscriptionResponse

logger = logging.getLogger(__name__)

PROMPT = """
Expand Down
3 changes: 2 additions & 1 deletion linguaphoto/ai/tts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
from pathlib import Path
from typing import AsyncIterator

from ai.transcribe import TranscriptionResponse
from openai import AsyncOpenAI

from linguaphoto.ai.transcribe import TranscriptionResponse

logger = logging.getLogger(__name__)


Expand Down
3 changes: 2 additions & 1 deletion linguaphoto/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
to handle routing for the entire API.
"""

from api import collection, image, subscription, user
from fastapi import APIRouter

from linguaphoto.api import collection, image, subscription, user

# Create a new API router
router = APIRouter()

Expand Down
11 changes: 6 additions & 5 deletions linguaphoto/api/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from typing import List

from crud.collection import CollectionCrud
from errors import NotAuthorizedError
from fastapi import APIRouter, Depends
from models import Collection
from schemas.collection import CollectionCreateFragment, CollectionEditFragment
from utils.auth import get_current_user_id

from linguaphoto.crud.collection import CollectionCrud
from linguaphoto.errors import NotAuthorizedError
from linguaphoto.models import Collection
from linguaphoto.schemas.collection import CollectionCreateFragment, CollectionEditFragment
from linguaphoto.utils.auth import get_current_user_id

router = APIRouter()

Expand Down
9 changes: 5 additions & 4 deletions linguaphoto/api/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

from typing import Annotated, List

from crud.collection import CollectionCrud
from crud.image import ImageCrud
from fastapi import APIRouter, Depends, File, Form, HTTPException, UploadFile
from models import Image
from pydantic import BaseModel
from utils.auth import get_current_user_id, subscription_validate

from linguaphoto.crud.collection import CollectionCrud
from linguaphoto.crud.image import ImageCrud
from linguaphoto.models import Image
from linguaphoto.utils.auth import get_current_user_id, subscription_validate


class TranslateFramgement(BaseModel):
Expand Down
7 changes: 4 additions & 3 deletions linguaphoto/api/subscription.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Collection API."""

import stripe
from crud.user import UserCrud
from fastapi import APIRouter, Depends, HTTPException
from settings import settings
from utils.auth import get_current_user_id

from linguaphoto.crud.user import UserCrud
from linguaphoto.settings import settings
from linguaphoto.utils.auth import get_current_user_id

router = APIRouter()

Expand Down
7 changes: 4 additions & 3 deletions linguaphoto/api/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

from datetime import timedelta

from crud.user import UserCrud
from fastapi import APIRouter, Depends, HTTPException
from schemas.user import (

from linguaphoto.crud.user import UserCrud
from linguaphoto.schemas.user import (
UserSigninFragment,
UserSigninRespondFragment,
UserSignupFragment,
)
from utils.auth import create_access_token, decode_access_token, oauth2_schema
from linguaphoto.utils.auth import create_access_token, decode_access_token, oauth2_schema

router = APIRouter()

Expand Down
3 changes: 2 additions & 1 deletion linguaphoto/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
import aioboto3
import aioboto3.session
from botocore.exceptions import NoCredentialsError
from settings import settings

from linguaphoto.settings import settings


async def get_dynamodb_resource() -> Any: # noqa: ANN401
Expand Down
7 changes: 4 additions & 3 deletions linguaphoto/crud/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import aioboto3
from boto3.dynamodb.conditions import ComparisonCondition, Key
from botocore.exceptions import ClientError
from errors import InternalError, ItemNotFoundError
from models import BaseModel, LinguaBaseModel
from settings import settings
from types_aiobotocore_dynamodb.service_resource import DynamoDBServiceResource
from types_aiobotocore_s3.service_resource import S3ServiceResource

from linguaphoto.errors import InternalError, ItemNotFoundError
from linguaphoto.models import BaseModel, LinguaBaseModel
from linguaphoto.settings import settings

T = TypeVar("T", bound=BaseModel)

TABLE_NAME = settings.dynamodb_table_name
Expand Down
4 changes: 2 additions & 2 deletions linguaphoto/crud/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from typing import List

from crud.base import BaseCrud
from models import Collection
from linguaphoto.crud.base import BaseCrud
from linguaphoto.models import Collection


class CollectionCrud(BaseCrud):
Expand Down
15 changes: 8 additions & 7 deletions linguaphoto/crud/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
from typing import List

import requests
from ai.transcribe import transcribe_image
from ai.tts import synthesize_text
from boto3.dynamodb.conditions import Key
from crud.base import BaseCrud
from errors import ItemNotFoundError
from fastapi import HTTPException, UploadFile
from models import Collection, Image
from openai import AsyncOpenAI
from settings import settings
from utils.cloudfront_url_signer import CloudFrontUrlSigner

from linguaphoto.ai.transcribe import transcribe_image
from linguaphoto.ai.tts import synthesize_text
from linguaphoto.crud.base import BaseCrud
from linguaphoto.errors import ItemNotFoundError
from linguaphoto.models import Collection, Image
from linguaphoto.settings import settings
from linguaphoto.utils.cloudfront_url_signer import CloudFrontUrlSigner

key_pair_id = settings.key_pair_id
media_hosting_server = settings.media_hosting_server
Expand Down
6 changes: 3 additions & 3 deletions linguaphoto/crud/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

from typing import List

from crud.base import BaseCrud
from models import User
from schemas.user import UserSigninFragment, UserSignupFragment
from linguaphoto.crud.base import BaseCrud
from linguaphoto.models import User
from linguaphoto.schemas.user import UserSigninFragment, UserSignupFragment


class UserCrud(BaseCrud):
Expand Down
5 changes: 3 additions & 2 deletions linguaphoto/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
"""Defines the main entrypoint for the FastAPI app."""

import uvicorn
from api.api import router
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from settings import settings

from linguaphoto.api.api import router
from linguaphoto.settings import settings

app = FastAPI()

Expand Down
3 changes: 2 additions & 1 deletion linguaphoto/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

from bcrypt import checkpw, gensalt, hashpw
from pydantic import BaseModel
from schemas.user import UserSignupFragment

from linguaphoto.schemas.user import UserSignupFragment


class LinguaBaseModel(BaseModel):
Expand Down
3 changes: 2 additions & 1 deletion linguaphoto/utils/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
from typing import Union

import jwt
from crud.user import UserCrud
from fastapi import Depends, HTTPException
from fastapi.security import OAuth2PasswordBearer

from linguaphoto.crud.user import UserCrud

SECRET_KEY = "your_secret_key"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
Expand Down

0 comments on commit 1c336f6

Please sign in to comment.