from fastapi import APIRouter, UploadFile, File, Form from typing import Optional from services.orchestrator import orchestrator router = APIRouter() from fastapi.responses import StreamingResponse import asyncio import json @router.post("/chat") async def chat_endpoint( user_id: str = Form(...), message: str = Form(...), simulation_mode: str = Form("default"), file: Optional[UploadFile] = File(None) ): file_bytes = None filename = None if file: file_bytes = await file.read() filename = file.filename async def generate(): # Start the heavy processing in the background task = asyncio.create_task(orchestrator.process_request(user_id, message, simulation_mode, file_bytes, filename)) # Yield keep-alive spaces every 2 seconds to prevent Hugging Face proxy timeout while not task.done(): yield b" " await asyncio.sleep(2) # Once finished, yield the actual JSON try: result = task.result() yield json.dumps(result).encode("utf-8") except Exception as e: # Yield an error JSON if something crashes error_json = {"text": "I encountered an error while processing that.", "audio_url": "", "metrics": {"objectionScore": 0, "toneConfidence": 0}} yield json.dumps(error_json).encode("utf-8") return StreamingResponse(generate(), media_type="application/json")