Spaces:
Sleeping
Sleeping
| from fastapi import APIRouter, UploadFile, File, HTTPException | |
| from src.agents.orchestrator import workflow | |
| import os | |
| import tempfile | |
| import logging | |
| router = APIRouter(prefix="/analysis", tags=["analysis"]) | |
| async def process_inputs(audio: UploadFile = File(None), image: UploadFile = File(None)): | |
| """ | |
| Process audio and/or image inputs to generate transcription, analysis, and synthesized speech. | |
| """ | |
| try: | |
| # Validate inputs | |
| if not audio and not image: | |
| raise HTTPException(status_code=400, detail="At least one input (audio or image) must be provided.") | |
| # Save uploaded files temporarily | |
| audio_filepath = None | |
| image_filepath = None | |
| if audio: | |
| audio_suffix = os.path.splitext(audio.filename)[1] | |
| with tempfile.NamedTemporaryFile(suffix=audio_suffix, delete=False) as temp_audio: | |
| temp_audio.write(await audio.read()) | |
| audio_filepath = temp_audio.name | |
| if image: | |
| image_suffix = os.path.splitext(image.filename)[1] | |
| with tempfile.NamedTemporaryFile(suffix=image_suffix, delete=False) as temp_image: | |
| temp_image.write(await image.read()) | |
| image_filepath = temp_image.name | |
| # Run the workflow | |
| state = {"audio": audio_filepath, "image": image_filepath} | |
| result = workflow.invoke(state) | |
| # Clean up temporary files | |
| if audio_filepath and os.path.exists(audio_filepath): | |
| os.remove(audio_filepath) | |
| if image_filepath and os.path.exists(image_filepath): | |
| os.remove(image_filepath) | |
| # Prepare response | |
| response = { | |
| "speech_to_text": result.get("text", ""), | |
| "doctor_response": result.get("analysis", "No analysis available."), | |
| "audio_output": result.get("speech", None) | |
| } | |
| return response | |
| except Exception as e: | |
| logging.error(f"Error processing inputs: {e}") | |
| raise HTTPException(status_code=500, detail=f"Error processing inputs: {str(e)}") |