malek-messaoudii
commited on
Commit
·
218c6a3
1
Parent(s):
d4b6133
Update audio.py
Browse files- routes/audio.py +18 -14
routes/audio.py
CHANGED
|
@@ -1,12 +1,21 @@
|
|
| 1 |
from fastapi import APIRouter, UploadFile, File, HTTPException
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
import io
|
| 4 |
-
import mimetypes
|
| 5 |
from services.tts_service import generate_tts
|
| 6 |
from services.stt_service import speech_to_text
|
| 7 |
|
| 8 |
router = APIRouter(prefix="/audio", tags=["Audio"])
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
# ------------------------
|
| 11 |
# Text to Speech
|
| 12 |
# ------------------------
|
|
@@ -15,6 +24,12 @@ async def tts(text: str):
|
|
| 15 |
"""
|
| 16 |
Convert text to speech and return audio.
|
| 17 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
try:
|
| 19 |
audio_bytes = await generate_tts(text)
|
| 20 |
except Exception as e:
|
|
@@ -26,17 +41,6 @@ async def tts(text: str):
|
|
| 26 |
# ------------------------
|
| 27 |
# Speech to Text
|
| 28 |
# ------------------------
|
| 29 |
-
|
| 30 |
-
# Allowed MIME types
|
| 31 |
-
ALLOWED_AUDIO_TYPES = {
|
| 32 |
-
"audio/wav",
|
| 33 |
-
"audio/x-wav",
|
| 34 |
-
"audio/mpeg", # mp3
|
| 35 |
-
"audio/mp3", # mp3
|
| 36 |
-
"audio/mp4", # sometimes m4a
|
| 37 |
-
"audio/m4a" # m4a
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
@router.post("/stt")
|
| 41 |
async def stt(file: UploadFile = File(...)):
|
| 42 |
"""
|
|
@@ -51,7 +55,7 @@ async def stt(file: UploadFile = File(...)):
|
|
| 51 |
|
| 52 |
try:
|
| 53 |
audio_bytes = await file.read()
|
| 54 |
-
text = await speech_to_text(audio_bytes, file.filename)
|
| 55 |
except Exception as e:
|
| 56 |
raise HTTPException(status_code=500, detail=str(e))
|
| 57 |
|
|
@@ -84,4 +88,4 @@ async def chatbot(file: UploadFile = File(...)):
|
|
| 84 |
except Exception as e:
|
| 85 |
raise HTTPException(status_code=500, detail=str(e))
|
| 86 |
|
| 87 |
-
return StreamingResponse(io.BytesIO(audio_response), media_type="audio/wav")
|
|
|
|
| 1 |
from fastapi import APIRouter, UploadFile, File, HTTPException
|
| 2 |
from fastapi.responses import StreamingResponse
|
| 3 |
import io
|
|
|
|
| 4 |
from services.tts_service import generate_tts
|
| 5 |
from services.stt_service import speech_to_text
|
| 6 |
|
| 7 |
router = APIRouter(prefix="/audio", tags=["Audio"])
|
| 8 |
|
| 9 |
+
# Allowed MIME types
|
| 10 |
+
ALLOWED_AUDIO_TYPES = {
|
| 11 |
+
"audio/wav",
|
| 12 |
+
"audio/x-wav",
|
| 13 |
+
"audio/mpeg", # mp3
|
| 14 |
+
"audio/mp3", # mp3
|
| 15 |
+
"audio/mp4", # sometimes m4a
|
| 16 |
+
"audio/m4a" # m4a
|
| 17 |
+
}
|
| 18 |
+
|
| 19 |
# ------------------------
|
| 20 |
# Text to Speech
|
| 21 |
# ------------------------
|
|
|
|
| 24 |
"""
|
| 25 |
Convert text to speech and return audio.
|
| 26 |
"""
|
| 27 |
+
if not text or len(text) > 500:
|
| 28 |
+
raise HTTPException(
|
| 29 |
+
status_code=400,
|
| 30 |
+
detail="Text must be between 1 and 500 characters"
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
try:
|
| 34 |
audio_bytes = await generate_tts(text)
|
| 35 |
except Exception as e:
|
|
|
|
| 41 |
# ------------------------
|
| 42 |
# Speech to Text
|
| 43 |
# ------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
@router.post("/stt")
|
| 45 |
async def stt(file: UploadFile = File(...)):
|
| 46 |
"""
|
|
|
|
| 55 |
|
| 56 |
try:
|
| 57 |
audio_bytes = await file.read()
|
| 58 |
+
text = await speech_to_text(audio_bytes, file.filename)
|
| 59 |
except Exception as e:
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
|
|
|
|
| 88 |
except Exception as e:
|
| 89 |
raise HTTPException(status_code=500, detail=str(e))
|
| 90 |
|
| 91 |
+
return StreamingResponse(io.BytesIO(audio_response), media_type="audio/wav")
|