import logging
from fastapi import HTTPException
from app.core.providers.base import STTProvider
# Configure logging
logger = logging.getLogger(__name__)
class STTService:
"""
Service class for transcribing audio into text using an STT provider.
"""
def __init__(self, stt_provider: STTProvider):
"""
Initializes the STTService with a concrete STT provider.
"""
self.stt_provider = stt_provider
async def transcribe(self, audio_bytes: bytes) -> str:
"""
Transcribes the provided audio bytes into text using the STT provider.
"""
logger.info(f"Starting transcription for audio data ({len(audio_bytes)} bytes).")
if not audio_bytes:
logger.warning("No audio data provided for transcription.")
raise HTTPException(status_code=400, detail="No audio data provided.")
try:
transcript = await self.stt_provider.transcribe_audio(audio_bytes)
if not transcript:
logger.warning("STT provider returned an empty transcript.")
raise HTTPException(status_code=500, detail="Failed to transcribe audio.")
logger.info(f"Successfully transcribed audio. Transcript length: {len(transcript)} characters.")
return transcript
except HTTPException:
raise # Pass through existing HTTPException without wrapping
except Exception as e:
logger.error(f"Unexpected error during transcription: {e}")
raise HTTPException(
status_code=500,
detail=f"Error during transcription: {e}"
) from e