| from fastapi import APIRouter, UploadFile, File |
| from services.tts_service import generate_tts |
| from services.stt_service import speech_to_text |
| from fastapi.responses import FileResponse |
| import uuid |
|
|
| router = APIRouter(prefix="/audio", tags=["Audio"]) |
|
|
|
|
| @router.post("/tts") |
| async def tts(text: str): |
| audio_bytes = await generate_tts(text) |
|
|
| filename = f"tts_{uuid.uuid4()}.wav" |
| with open(filename, "wb") as f: |
| f.write(audio_bytes) |
|
|
| return FileResponse(filename, media_type="audio/wav", filename=filename) |
|
|
|
|
| @router.post("/stt") |
| async def stt(file: UploadFile = File(...)): |
| audio_bytes = await file.read() |
| text = await speech_to_text(audio_bytes) |
| return {"text": text} |
|
|