from fastapi import FastAPI, Query from fastapi.middleware.cors import CORSMiddleware from fastapi.staticfiles import StaticFiles from fastapi.responses import FileResponse from app.utils.embedding import get_embedding app = FastAPI() origins = [ "http://localhost:3000", "http://localhost:8000", "localhost:8000", "https://your-space-name.hf.space", ] app.add_middleware( CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"], ) # Mount static files app.mount( "/static", StaticFiles(directory="app/build/static", html=True), name="static" ) # Serve index.html at the root @app.get("/") def read_root(): return FileResponse("app/build/index.html") words_db = [] @app.get("/api/words", tags=["words"]) async def get_todos() -> dict: return {"data": words_db} @app.post("/api/add-word", tags=["words"]) async def add_word(word: dict) -> dict: words_db.append(word) word_to_embbed = word["id"] word_embedding = get_embedding(word_to_embbed)[:3] word["embedding"] = word_embedding.tolist() return {"data": {"Succesful"}} @app.delete("/api/delete-word/{word_id}", tags=["words"]) async def delete_word(word_id: str) -> dict: word_id = int(word_id) for word in words_db: if int(word["id"]) == word_id: print("found") words_db.remove(word) return {"data": {"Succesful"}} return {"data": {"Word not found"}} if __name__ == "__main__": import uvicorn uvicorn.run(app, host="0.0.0.0", port=8000)