File size: 1,003 Bytes
42c727a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import FastAPI, UploadFile, File, Request
from fastapi.middleware.cors import CORSMiddleware
from app.agent import process_text
from app.speech_to_text import transcribe_audio
from app.text_to_speech import synthesize_speech
from fastapi.responses import StreamingResponse, JSONResponse
import io

app = FastAPI()

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_methods=["*"],
    allow_headers=["*"],
)

@app.post("/transcribe")
async def transcribe(file: UploadFile = File(...)):
    audio_bytes = await file.read()
    text = transcribe_audio(audio_bytes)
    return {"transcription": text}

@app.post("/query")
async def query_agent(request: Request):
    data = await request.json()
    input_text = data.get("input_text", "")
    response = process_text(input_text)
    return {"response": response}

@app.get("/speak")
async def speak(text: str):
    audio = synthesize_speech(text)
    return StreamingResponse(io.BytesIO(audio), media_type="audio/wav")