from fastapi import APIRouter, UploadFile, File, HTTPException from services.stt_service import speech_to_text from models.stt import STTResponse import tempfile import os router = APIRouter(prefix="/stt", tags=["Speech To Text"]) @router.post("/", response_model=STTResponse) async def convert_speech_to_text(file: UploadFile = File(...)): """ Convert uploaded audio file to text (English only) """ # Check file type if not file.content_type or not file.content_type.startswith('audio/'): raise HTTPException(status_code=400, detail="File must be an audio file") # Create temporary file with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file: temp_path = temp_file.name content = await file.read() if len(content) == 0: os.unlink(temp_path) raise HTTPException(status_code=400, detail="Audio file is empty") temp_file.write(content) try: # Convert audio to text text = speech_to_text(temp_path) # Clean up os.unlink(temp_path) return STTResponse(text=text) except Exception as e: # Clean up on error if os.path.exists(temp_path): os.unlink(temp_path) raise HTTPException(status_code=500, detail=str(e))