File size: 2,101 Bytes
2b350e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
53
54
55
56
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"])

@router.post("/process")
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)}")