Spaces:
Sleeping
Sleeping
Create main.py
Browse files
main.py
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile, HTTPException
|
2 |
+
from fastapi.responses import JSONResponse
|
3 |
+
import speech_recognition as sr
|
4 |
+
import os
|
5 |
+
import uuid
|
6 |
+
|
7 |
+
app = FastAPI()
|
8 |
+
|
9 |
+
UPLOAD_DIR = "uploads"
|
10 |
+
os.makedirs(UPLOAD_DIR, exist_ok=True)
|
11 |
+
|
12 |
+
@app.post("/voice-to-text")
|
13 |
+
async def voice_to_text(file: UploadFile = File(...)):
|
14 |
+
# Check file type
|
15 |
+
if not file.filename.endswith(".wav"):
|
16 |
+
raise HTTPException(status_code=400, detail="Only .wav files are supported")
|
17 |
+
|
18 |
+
# Save uploaded file
|
19 |
+
temp_filename = os.path.join(UPLOAD_DIR, f"{uuid.uuid4()}.wav")
|
20 |
+
with open(temp_filename, "wb") as f:
|
21 |
+
f.write(await file.read())
|
22 |
+
|
23 |
+
# Recognize speech
|
24 |
+
recognizer = sr.Recognizer()
|
25 |
+
try:
|
26 |
+
with sr.AudioFile(temp_filename) as source:
|
27 |
+
audio_data = recognizer.record(source)
|
28 |
+
text = recognizer.recognize_google(audio_data)
|
29 |
+
return JSONResponse(content={"text": text})
|
30 |
+
except sr.UnknownValueError:
|
31 |
+
raise HTTPException(status_code=400, detail="Speech not recognized")
|
32 |
+
except sr.RequestError as e:
|
33 |
+
raise HTTPException(status_code=500, detail=f"API error: {e}")
|
34 |
+
finally:
|
35 |
+
os.remove(temp_filename)
|