add fast API for chatbot
Browse files
app.py
CHANGED
|
@@ -359,6 +359,51 @@ with gr.Blocks(theme=gr.themes.Soft(), css=".gradio-container {background-color:
|
|
| 359 |
outputs=[error_box, login_block, chatbot_block, id_extraction_block],
|
| 360 |
)
|
| 361 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
# --- Lancement serveur
|
| 363 |
if __name__ == "__main__":
|
| 364 |
-
app.launch()
|
|
|
|
| 359 |
outputs=[error_box, login_block, chatbot_block, id_extraction_block],
|
| 360 |
)
|
| 361 |
|
| 362 |
+
# -- section FAST API
|
| 363 |
+
app_api = FastAPI()
|
| 364 |
+
|
| 365 |
+
# Permettre les appels depuis ton application mobile
|
| 366 |
+
app_api.add_middleware(
|
| 367 |
+
CORSMiddleware,
|
| 368 |
+
allow_origins=["*"], # Change ça pour plus de sécurité ensuite
|
| 369 |
+
allow_credentials=True,
|
| 370 |
+
allow_methods=["*"],
|
| 371 |
+
allow_headers=["*"],
|
| 372 |
+
)
|
| 373 |
+
|
| 374 |
+
# Schéma pour requêtes
|
| 375 |
+
class ChatRequest(BaseModel):
|
| 376 |
+
message: str
|
| 377 |
+
history: list
|
| 378 |
+
domain: str
|
| 379 |
+
model_name: str = "gpt-3.5-turbo"
|
| 380 |
+
max_tokens: int = 500
|
| 381 |
+
temperature: float = 0.7
|
| 382 |
+
top_p: float = 1.0
|
| 383 |
+
|
| 384 |
+
@app_api.post("/chat")
|
| 385 |
+
def chat(request: ChatRequest):
|
| 386 |
+
try:
|
| 387 |
+
response = respond(
|
| 388 |
+
message=request.message,
|
| 389 |
+
history=request.history,
|
| 390 |
+
system_message="",
|
| 391 |
+
domain=request.domain,
|
| 392 |
+
model_name=request.model_name,
|
| 393 |
+
max_tokens=request.max_tokens,
|
| 394 |
+
temperature=request.temperature,
|
| 395 |
+
top_p=request.top_p
|
| 396 |
+
)
|
| 397 |
+
return {"history": response}
|
| 398 |
+
except Exception as e:
|
| 399 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 400 |
+
|
| 401 |
+
def run_fastapi():
|
| 402 |
+
uvicorn.run(app_api, host="0.0.0.0", port=8000)
|
| 403 |
+
|
| 404 |
+
threading.Thread(target=run_fastapi, daemon=True).start()
|
| 405 |
+
|
| 406 |
+
|
| 407 |
# --- Lancement serveur
|
| 408 |
if __name__ == "__main__":
|
| 409 |
+
app.launch(server_name="0.0.0.0", server_port=7860)
|