Yassine Mhirsi
commited on
Commit
·
9930d31
1
Parent(s):
5406818
fix
Browse files- routes/tts_routes.py +3 -54
- services/tts_service.py +1 -10
routes/tts_routes.py
CHANGED
|
@@ -10,18 +10,17 @@ router = APIRouter(prefix="/tts", tags=["Text To Speech"])
|
|
| 10 |
async def generate_tts(request: TTSRequest):
|
| 11 |
"""
|
| 12 |
Convert text to speech using the free gTTS backend (MP3 only).
|
| 13 |
-
<<<<<<< Updated upstream
|
| 14 |
Can return either Base64 or file based on return_base64 parameter.
|
| 15 |
"""
|
| 16 |
try:
|
| 17 |
# Option 1: Return Base64
|
| 18 |
-
if request
|
| 19 |
result = text_to_speech_base64(
|
| 20 |
text=request.text,
|
| 21 |
voice=request.voice,
|
| 22 |
fmt=request.format,
|
| 23 |
)
|
| 24 |
-
|
| 25 |
return {
|
| 26 |
"success": True,
|
| 27 |
"audio_base64": result["audio_base64"],
|
|
@@ -31,7 +30,7 @@ async def generate_tts(request: TTSRequest):
|
|
| 31 |
"size_bytes": result["size_bytes"],
|
| 32 |
"size_base64": result["size_base64"]
|
| 33 |
}
|
| 34 |
-
|
| 35 |
# Option 2: Return file (default behavior)
|
| 36 |
else:
|
| 37 |
audio_path = text_to_speech(
|
|
@@ -58,55 +57,5 @@ async def generate_tts(request: TTSRequest):
|
|
| 58 |
raise HTTPException(status_code=400, detail=str(e))
|
| 59 |
except Exception as e:
|
| 60 |
raise HTTPException(status_code=500, detail=str(e))
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
@router.post("/base64")
|
| 64 |
-
async def generate_tts_base64(request: TTSRequest):
|
| 65 |
-
"""
|
| 66 |
-
Convert text to speech and return as Base64.
|
| 67 |
-
"""
|
| 68 |
-
try:
|
| 69 |
-
result = text_to_speech_base64(
|
| 70 |
-
text=request.text,
|
| 71 |
-
voice=request.voice,
|
| 72 |
-
fmt=request.format,
|
| 73 |
-
)
|
| 74 |
-
|
| 75 |
-
return {
|
| 76 |
-
"success": True,
|
| 77 |
-
"audio_base64": result["audio_base64"],
|
| 78 |
-
"mime_type": result["mime_type"],
|
| 79 |
-
"format": result["format"],
|
| 80 |
-
"filename": result["filename"],
|
| 81 |
-
"size_bytes": result["size_bytes"],
|
| 82 |
-
"size_base64": result["size_base64"]
|
| 83 |
-
}
|
| 84 |
-
|
| 85 |
-
except ValueError as e:
|
| 86 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 87 |
-
=======
|
| 88 |
-
"""
|
| 89 |
-
try:
|
| 90 |
-
audio_path = text_to_speech(
|
| 91 |
-
text=request.text,
|
| 92 |
-
voice=request.voice,
|
| 93 |
-
fmt=request.format,
|
| 94 |
-
)
|
| 95 |
-
|
| 96 |
-
if not Path(audio_path).exists():
|
| 97 |
-
raise HTTPException(status_code=500, detail="Audio file generation failed")
|
| 98 |
-
|
| 99 |
-
media_type = "audio/mpeg"
|
| 100 |
-
|
| 101 |
-
return FileResponse(
|
| 102 |
-
path=audio_path,
|
| 103 |
-
filename=f"speech.{request.format}",
|
| 104 |
-
media_type=media_type,
|
| 105 |
-
headers={
|
| 106 |
-
"Content-Disposition": f"attachment; filename=speech.{request.format}"
|
| 107 |
-
},
|
| 108 |
-
)
|
| 109 |
-
|
| 110 |
-
>>>>>>> Stashed changes
|
| 111 |
except Exception as e:
|
| 112 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
| 10 |
async def generate_tts(request: TTSRequest):
|
| 11 |
"""
|
| 12 |
Convert text to speech using the free gTTS backend (MP3 only).
|
|
|
|
| 13 |
Can return either Base64 or file based on return_base64 parameter.
|
| 14 |
"""
|
| 15 |
try:
|
| 16 |
# Option 1: Return Base64
|
| 17 |
+
if getattr(request, "return_base64", False):
|
| 18 |
result = text_to_speech_base64(
|
| 19 |
text=request.text,
|
| 20 |
voice=request.voice,
|
| 21 |
fmt=request.format,
|
| 22 |
)
|
| 23 |
+
|
| 24 |
return {
|
| 25 |
"success": True,
|
| 26 |
"audio_base64": result["audio_base64"],
|
|
|
|
| 30 |
"size_bytes": result["size_bytes"],
|
| 31 |
"size_base64": result["size_base64"]
|
| 32 |
}
|
| 33 |
+
|
| 34 |
# Option 2: Return file (default behavior)
|
| 35 |
else:
|
| 36 |
audio_path = text_to_speech(
|
|
|
|
| 57 |
raise HTTPException(status_code=400, detail=str(e))
|
| 58 |
except Exception as e:
|
| 59 |
raise HTTPException(status_code=500, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
raise HTTPException(status_code=500, detail=str(e))
|
services/tts_service.py
CHANGED
|
@@ -1,15 +1,9 @@
|
|
| 1 |
import uuid
|
| 2 |
-
<<<<<<< Updated upstream
|
| 3 |
import base64
|
| 4 |
from pathlib import Path
|
| 5 |
-
from gtts import gTTS
|
| 6 |
-
from fastapi import HTTPException
|
| 7 |
-
|
| 8 |
-
=======
|
| 9 |
-
from pathlib import Path
|
| 10 |
from config import GROQ_TTS_API_KEY, GROQ_TTS_MODEL
|
| 11 |
from gtts import gTTS
|
| 12 |
-
|
| 13 |
|
| 14 |
def text_to_speech(
|
| 15 |
text: str,
|
|
@@ -19,10 +13,7 @@ def text_to_speech(
|
|
| 19 |
"""
|
| 20 |
Convert text to speech using gTTS (Google Translate, free).
|
| 21 |
Only MP3 is supported.
|
| 22 |
-
<<<<<<< Updated upstream
|
| 23 |
Returns file path.
|
| 24 |
-
=======
|
| 25 |
-
>>>>>>> Stashed changes
|
| 26 |
"""
|
| 27 |
if not text or not text.strip():
|
| 28 |
raise ValueError("Text cannot be empty")
|
|
|
|
| 1 |
import uuid
|
|
|
|
| 2 |
import base64
|
| 3 |
from pathlib import Path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from config import GROQ_TTS_API_KEY, GROQ_TTS_MODEL
|
| 5 |
from gtts import gTTS
|
| 6 |
+
from fastapi import HTTPException
|
| 7 |
|
| 8 |
def text_to_speech(
|
| 9 |
text: str,
|
|
|
|
| 13 |
"""
|
| 14 |
Convert text to speech using gTTS (Google Translate, free).
|
| 15 |
Only MP3 is supported.
|
|
|
|
| 16 |
Returns file path.
|
|
|
|
|
|
|
| 17 |
"""
|
| 18 |
if not text or not text.strip():
|
| 19 |
raise ValueError("Text cannot be empty")
|