robo / main.py
abdullahalioo's picture
Create main.py
a64822b verified
raw
history blame
1.19 kB
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)