File size: 1,157 Bytes
507c064
a64822b
 
 
 
 
 
 
 
 
 
 
507c064
a64822b
 
 
 
507c064
 
a64822b
 
507c064
a64822b
 
507c064
a64822b
507c064
 
 
a64822b
507c064
a64822b
507c064
a64822b
507c064
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
from fastapi import FastAPI, UploadFile, File, HTTPException
import speech_recognition as sr
import os
import uuid

app = FastAPI()

UPLOAD_DIR = "uploads"
os.makedirs(UPLOAD_DIR, exist_ok=True)

@app.post("/voice-to-text")
async def voice_to_text(file: UploadFile = File(...)):
    # Check for .wav file
    if not file.filename.endswith(".wav"):
        raise HTTPException(status_code=400, detail="Only .wav files are supported")

    # Save uploaded file
    file_path = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.wav")
    with open(file_path, "wb") as f:
        f.write(await file.read())

    # Speech recognition
    recognizer = sr.Recognizer()
    try:
        with sr.AudioFile(file_path) as source:
            audio_data = recognizer.record(source)
            recognized_text = recognizer.recognize_google(audio_data)
            return {"recognized_text": recognized_text}

    except sr.UnknownValueError:
        raise HTTPException(status_code=400, detail="Could not understand audio")
    except sr.RequestError as e:
        raise HTTPException(status_code=500, detail=f"Speech API error: {e}")
    finally:
        os.remove(file_path)