Spaces:
Runtime error
Runtime error
File size: 1,665 Bytes
2c35026 8dba466 2c35026 8dba466 2c35026 8dba466 2c35026 8dba466 2c35026 8dba466 2c35026 8dba466 2c35026 |
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 |
import uvicorn
from fastapi import FastAPI, Form, Request
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from inference import inferenceAPI
# ------ MODELE --------------------------------------------------------------
# appel de la fonction inference, adaptee pour une entree txt
def summarize(text: str):
return " ".join(inferenceAPI(text))
# ----------------------------------------------------------------------------------
# -------- API ---------------------------------------------------------------------
app = FastAPI()
# static files pour envoi du css au navigateur
templates = Jinja2Templates(directory="templates")
app.mount("/templates", StaticFiles(directory="templates"), name="templates")
@app.get("/")
async def index(request: Request):
return templates.TemplateResponse("index.html.jinja", {"request": request})
# retourner le texte, les predictions et message d'erreur si formulaire envoye vide
@app.post("/")
async def prediction(request: Request, text: str = Form(None)):
if not text:
error = "Merci de saisir votre texte."
return templates.TemplateResponse(
"index.html.jinja", {"request": request, "text": error}
)
else:
summary = summarize(text)
return templates.TemplateResponse(
"index.html.jinja", {"request": request, "text": text, "summary": summary}
)
# ------------------------------------------------------------------------------------
# lancer le serveur et le recharger a chaque modification sauvegardee
if __name__ == "__main__":
uvicorn.run("api:app", port=8000, reload=True)
|