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)