-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from StephanAkkerman/feat/backend-overhaul
Backend overhaul
- Loading branch information
Showing
108 changed files
with
494 additions
and
874 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import argparse | ||
|
||
import uvicorn | ||
from fastapi import FastAPI | ||
from fastapi.middleware.cors import CORSMiddleware | ||
|
||
from fluentai.api.routes.anki import anki_router | ||
from fluentai.api.routes.create_card import create_card_router | ||
from fluentai.utils.load_models import download_all_models | ||
|
||
# Initialize FastAPI app | ||
app = FastAPI() | ||
|
||
# Configure CORS middleware | ||
app.add_middleware( | ||
CORSMiddleware, | ||
allow_origins=[ | ||
"http://localhost:3000", | ||
"https://akkerman.ai", | ||
], | ||
allow_credentials=True, | ||
allow_methods=["*"], | ||
allow_headers=["*"], | ||
) | ||
|
||
app.include_router(anki_router) | ||
app.include_router(create_card_router) | ||
|
||
|
||
def main(): | ||
"""Start the FastAPI application.""" | ||
# Start by downloading all models | ||
download_all_models() | ||
|
||
parser = argparse.ArgumentParser(description="") | ||
parser.add_argument( | ||
"--host", type=str, default="127.0.0.1", help="Hosting default: 127.0.0.1" | ||
) | ||
parser.add_argument("--port", type=int, default=8000) | ||
|
||
args = parser.parse_args() | ||
|
||
uvicorn.run("app:app", host=args.host, port=args.port) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import httpx | ||
from fastapi import APIRouter, Request | ||
from fastapi.responses import JSONResponse | ||
|
||
anki_router = APIRouter() | ||
|
||
|
||
@anki_router.post("/api/anki") | ||
async def anki_proxy(request: Request): | ||
""" | ||
Proxy API endpoint for forwarding requests to the Anki server. | ||
This function receives a JSON request from the client, forwards it to the Anki | ||
server running on localhost, and returns the response back to the client. | ||
HACK: This uses the backend as a proxy for when the frontend is deployed in GH Pages | ||
Parameters | ||
---------- | ||
request : Request | ||
The incoming HTTP request object containing the JSON payload to be forwarded. | ||
Returns | ||
------- | ||
JSONResponse | ||
A JSON response containing the Anki server response or an error message if | ||
the request fails. | ||
""" | ||
try: | ||
# Forward the incoming request body to the Anki server | ||
request_body = await request.json() | ||
|
||
async with httpx.AsyncClient() as client: | ||
response = await client.post( | ||
"http://127.0.0.1:8765", # Assuming Anki is running on localhost with default port | ||
json=request_body, | ||
) | ||
|
||
# Return the JSON response from Anki server | ||
return JSONResponse(content=response.json(), status_code=response.status_code) | ||
|
||
except httpx.RequestError as e: | ||
return JSONResponse( | ||
content={"error": "Failed to connect to Anki server.", "details": str(e)}, | ||
status_code=500, | ||
) | ||
except Exception as e: | ||
return JSONResponse( | ||
content={"error": "An unexpected error occurred.", "details": str(e)}, | ||
status_code=500, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import json | ||
|
||
from fluentai.constants.config import config | ||
|
||
with open(config.get("G2P").get("LANGUAGE_JSON")) as f: | ||
G2P_LANGCODES = json.load(f) | ||
G2P_LANGUAGES: dict = dict(map(reversed, G2P_LANGCODES.items())) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.