-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
34 lines (26 loc) · 856 Bytes
/
main.py
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
from fastapi import FastAPI, Request
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse
from fastapi.staticfiles import StaticFiles
from predictor import get_top_predictions
app = FastAPI()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get("/")
async def get_root():
with open("static/index.html") as f:
html_content = f.read()
return HTMLResponse(content=html_content, status_code=200)
@app.post("/predict")
async def predict(request: Request):
data = await request.json()
text = data.get("text", "")
text += " <mask>"
suggestions = get_top_predictions(text)
return {"suggestions": suggestions}