File size: 1,587 Bytes
02a9316
54bac2e
02a9316
aae42e1
54bac2e
8765030
 
 
54bac2e
8765030
54bac2e
 
02a9316
 
 
54bac2e
 
 
 
 
 
02a9316
54bac2e
 
02a9316
8765030
 
 
02a9316
 
aae42e1
 
 
 
 
8765030
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
02a9316
 
 
 
54bac2e
02a9316
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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)