Spaces:
Sleeping
Sleeping
File size: 1,186 Bytes
a64822b |
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, File, UploadFile, HTTPException
from fastapi.responses import JSONResponse
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 file type
if not file.filename.endswith(".wav"):
raise HTTPException(status_code=400, detail="Only .wav files are supported")
# Save uploaded file
temp_filename = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.wav")
with open(temp_filename, "wb") as f:
f.write(await file.read())
# Recognize speech
recognizer = sr.Recognizer()
try:
with sr.AudioFile(temp_filename) as source:
audio_data = recognizer.record(source)
text = recognizer.recognize_google(audio_data)
return JSONResponse(content={"text": text})
except sr.UnknownValueError:
raise HTTPException(status_code=400, detail="Speech not recognized")
except sr.RequestError as e:
raise HTTPException(status_code=500, detail=f"API error: {e}")
finally:
os.remove(temp_filename)
|