File size: 1,011 Bytes
9aa985d
73d4f3c
 
9aa985d
 
c7fc3b6
 
 
9aa985d
 
 
73d4f3c
 
9aa985d
 
 
 
c7fc3b6
9aa985d
 
c7fc3b6
 
9aa985d
 
 
73d4f3c
 
9aa985d
 
 
 
 
 
73d4f3c
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 APIRouter, UploadFile, File, HTTPException
from services.tts_service import generate_tts
from services.stt_service import speech_to_text
from fastapi.responses import StreamingResponse
import io

router = APIRouter(prefix="/audio", tags=["Audio"])

# ======================
# TEXT TO SPEECH
# ======================
@router.post("/tts")
async def tts(text: str):
    try:
        audio_bytes = await generate_tts(text)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

    # Return as streaming response without saving file
    return StreamingResponse(io.BytesIO(audio_bytes), media_type="audio/wav")


# ======================
# SPEECH TO TEXT
# ======================
@router.post("/stt")
async def stt(file: UploadFile = File(...)):
    try:
        audio_bytes = await file.read()
        text = await speech_to_text(audio_bytes)
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))
    
    return {"text": text}