import logging
from fastapi import APIRouter, HTTPException, UploadFile, File, Depends
from app.api.dependencies import ServiceContainer
from app.api import schemas
from app.core.services.stt import STTService
# Configure logging
logger = logging.getLogger(__name__)
def create_stt_router(services: ServiceContainer) -> APIRouter:
"""
Creates and configures the API router for Speech-to-Text (STT) functionality.
"""
router = APIRouter(prefix="/stt", tags=["STT"])
@router.post(
"/transcribe",
summary="Transcribe audio to text",
response_description="The transcribed text from the audio file.",
response_model=schemas.STTResponse
)
async def transcribe_audio_to_text(
audio_file: UploadFile = File(...)
):
"""
Transcribes an uploaded audio file into text using the configured STT service.
The audio file is expected to be a common audio format like WAV or MP3,
though the specific provider implementation will determine supported formats.
"""
logger.info(f"Received transcription request for file: {audio_file.filename}")
if not audio_file.content_type.startswith("audio/"):
logger.warning(f"Invalid file type uploaded: {audio_file.content_type}")
raise HTTPException(
status_code=415,
detail="Unsupported media type. Please upload an audio file."
)
try:
# Read the audio bytes from the uploaded file
audio_bytes = await audio_file.read()
# Use the STT service to get the transcript
transcript = await services.stt_service.transcribe(audio_bytes)
# Return the transcript in a simple JSON response
return schemas.STTResponse(transcript=transcript)
except HTTPException:
# Re-raise Fast API exceptions so they're handled correctly
raise
except Exception as e:
logger.error(f"Failed to transcribe audio file: {e}")
raise HTTPException(
status_code=500, detail=f"Failed to transcribe audio: {e}"
) from e
return router