malek-messaoudii
commited on
Commit
·
5406818
1
Parent(s):
2683fd8
update
Browse files- routes/tts_routes.py +25 -0
- services/tts_service.py +9 -0
routes/tts_routes.py
CHANGED
|
@@ -10,6 +10,7 @@ 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 |
Can return either Base64 or file based on return_base64 parameter.
|
| 14 |
"""
|
| 15 |
try:
|
|
@@ -83,5 +84,29 @@ async def generate_tts_base64(request: TTSRequest):
|
|
| 83 |
|
| 84 |
except ValueError as e:
|
| 85 |
raise HTTPException(status_code=400, detail=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
except Exception as e:
|
| 87 |
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 |
+
<<<<<<< Updated upstream
|
| 14 |
Can return either Base64 or file based on return_base64 parameter.
|
| 15 |
"""
|
| 16 |
try:
|
|
|
|
| 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))
|
services/tts_service.py
CHANGED
|
@@ -1,9 +1,15 @@
|
|
| 1 |
import uuid
|
|
|
|
| 2 |
import base64
|
| 3 |
from pathlib import Path
|
| 4 |
from gtts import gTTS
|
| 5 |
from fastapi import HTTPException
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def text_to_speech(
|
| 9 |
text: str,
|
|
@@ -13,7 +19,10 @@ def text_to_speech(
|
|
| 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")
|
|
|
|
| 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 |
+
>>>>>>> Stashed changes
|
| 13 |
|
| 14 |
def text_to_speech(
|
| 15 |
text: str,
|
|
|
|
| 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")
|