Spaces:
Sleeping
Sleeping
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) | |
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) | |