File size: 1,356 Bytes
c45f0d6
2da4544
 
c45f0d6
a71355d
2da4544
 
 
 
c45f0d6
 
a71355d
c45f0d6
a71355d
c45f0d6
 
27ee35f
a71355d
c45f0d6
 
 
a71355d
 
 
 
 
c45f0d6
2da4544
c45f0d6
a71355d
c45f0d6
 
a71355d
c45f0d6
 
 
 
 
a71355d
c45f0d6
 
 
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
36
37
38
39
40
41
42
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))