malek-messaoudii
commited on
Commit
·
520a06a
1
Parent(s):
544d113
Update audio.py
Browse files- routes/audio.py +25 -3
routes/audio.py
CHANGED
|
@@ -22,17 +22,39 @@ async def tts(text: str):
|
|
| 22 |
# ------------------------
|
| 23 |
# Speech to Text
|
| 24 |
# ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
@router.post("/stt")
|
| 26 |
async def stt(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
try:
|
| 28 |
-
audio_bytes = await file.read()
|
| 29 |
-
text = await speech_to_text(audio_bytes)
|
| 30 |
except Exception as e:
|
| 31 |
raise HTTPException(status_code=500, detail=str(e))
|
| 32 |
-
|
| 33 |
return {"text": text}
|
| 34 |
|
| 35 |
|
|
|
|
| 36 |
# ------------------------
|
| 37 |
# Voice Chatbot: User sends voice → TTS reply
|
| 38 |
# ------------------------
|
|
|
|
| 22 |
# ------------------------
|
| 23 |
# Speech to Text
|
| 24 |
# ------------------------
|
| 25 |
+
|
| 26 |
+
# Allowed MIME types
|
| 27 |
+
ALLOWED_AUDIO_TYPES = {
|
| 28 |
+
"audio/wav",
|
| 29 |
+
"audio/x-wav",
|
| 30 |
+
"audio/mpeg", # mp3
|
| 31 |
+
"audio/mp3", # mp3
|
| 32 |
+
"audio/mp4", # sometimes m4a
|
| 33 |
+
"audio/m4a" # m4a
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
@router.post("/stt")
|
| 37 |
async def stt(file: UploadFile = File(...)):
|
| 38 |
+
"""
|
| 39 |
+
Accepts an uploaded audio file (wav, mp3, m4a) and returns the transcribed text.
|
| 40 |
+
"""
|
| 41 |
+
# Validate MIME type
|
| 42 |
+
if file.content_type not in ALLOWED_AUDIO_TYPES:
|
| 43 |
+
raise HTTPException(
|
| 44 |
+
status_code=400,
|
| 45 |
+
detail=f"Unsupported audio format: {file.content_type}. Supported: WAV, MP3, M4A"
|
| 46 |
+
)
|
| 47 |
+
|
| 48 |
try:
|
| 49 |
+
audio_bytes = await file.read() # Read uploaded file
|
| 50 |
+
text = await speech_to_text(audio_bytes) # Call your Gemini STT
|
| 51 |
except Exception as e:
|
| 52 |
raise HTTPException(status_code=500, detail=str(e))
|
| 53 |
+
|
| 54 |
return {"text": text}
|
| 55 |
|
| 56 |
|
| 57 |
+
|
| 58 |
# ------------------------
|
| 59 |
# Voice Chatbot: User sends voice → TTS reply
|
| 60 |
# ------------------------
|