malek-messaoudii
commited on
Commit
·
1e7709f
1
Parent(s):
27ee35f
update services
Browse files- routes/tts_routes.py +6 -5
- services/stt_service.py +13 -13
- services/tts_service.py +13 -16
routes/tts_routes.py
CHANGED
|
@@ -8,11 +8,12 @@ router = APIRouter(prefix="/tts", tags=["Text To Speech"])
|
|
| 8 |
|
| 9 |
@router.post("/")
|
| 10 |
async def generate_tts(request: TTSRequest):
|
| 11 |
-
output_path
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
return FileResponse(
|
| 18 |
output_path,
|
|
|
|
| 8 |
|
| 9 |
@router.post("/")
|
| 10 |
async def generate_tts(request: TTSRequest):
|
| 11 |
+
output_path = text_to_speech(
|
| 12 |
+
text=request.text,
|
| 13 |
+
voice=request.voice,
|
| 14 |
+
fmt=request.format
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
|
| 18 |
return FileResponse(
|
| 19 |
output_path,
|
services/stt_service.py
CHANGED
|
@@ -1,26 +1,26 @@
|
|
| 1 |
-
# services/stt_service.py
|
| 2 |
import requests
|
| 3 |
from config import GROQ_API_KEY, GROQ_STT_MODEL
|
| 4 |
|
| 5 |
def speech_to_text(audio_file: str) -> str:
|
| 6 |
-
"""
|
| 7 |
-
Convertit un fichier audio en texte via l'API Groq STT.
|
| 8 |
-
audio_file : str -> chemin du fichier audio
|
| 9 |
-
"""
|
| 10 |
if not GROQ_API_KEY:
|
| 11 |
raise RuntimeError("GROQ_API_KEY is not set in config")
|
| 12 |
|
|
|
|
|
|
|
| 13 |
headers = {
|
| 14 |
"Authorization": f"Bearer {GROQ_API_KEY}"
|
| 15 |
}
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
return result.get("text", "")
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
from config import GROQ_API_KEY, GROQ_STT_MODEL
|
| 3 |
|
| 4 |
def speech_to_text(audio_file: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
if not GROQ_API_KEY:
|
| 6 |
raise RuntimeError("GROQ_API_KEY is not set in config")
|
| 7 |
|
| 8 |
+
url = "https://api.groq.ai/openai/v1/audio/transcriptions"
|
| 9 |
+
|
| 10 |
headers = {
|
| 11 |
"Authorization": f"Bearer {GROQ_API_KEY}"
|
| 12 |
}
|
| 13 |
|
| 14 |
+
with open(audio_file, "rb") as f:
|
| 15 |
+
files = {
|
| 16 |
+
"file": (audio_file, f, "audio/wav")
|
| 17 |
+
}
|
| 18 |
+
data = {
|
| 19 |
+
"model": GROQ_STT_MODEL
|
| 20 |
+
}
|
| 21 |
|
| 22 |
+
response = requests.post(url, headers=headers, data=data, files=files)
|
| 23 |
+
response.raise_for_status()
|
| 24 |
|
| 25 |
+
result = response.json()
|
| 26 |
+
return result.get("text", "")
|
|
|
services/tts_service.py
CHANGED
|
@@ -1,34 +1,31 @@
|
|
| 1 |
-
# services/tts_service.py
|
| 2 |
import requests
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
Convertit du texte en audio via l'API Groq TTS.
|
| 8 |
-
text : str -> texte à convertir
|
| 9 |
-
output_file : str -> chemin du fichier de sortie (ex: 'output.wav')
|
| 10 |
-
"""
|
| 11 |
if not GROQ_API_KEY:
|
| 12 |
raise RuntimeError("GROQ_API_KEY is not set in config")
|
| 13 |
|
|
|
|
|
|
|
| 14 |
headers = {
|
| 15 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 16 |
"Content-Type": "application/json"
|
| 17 |
}
|
| 18 |
|
| 19 |
payload = {
|
| 20 |
-
"
|
| 21 |
-
"voice":
|
| 22 |
-
"format":
|
|
|
|
| 23 |
}
|
| 24 |
|
| 25 |
-
|
| 26 |
|
| 27 |
response = requests.post(url, headers=headers, json=payload)
|
| 28 |
-
response.raise_for_status()
|
| 29 |
|
| 30 |
-
audio_content = response.content
|
| 31 |
with open(output_file, "wb") as f:
|
| 32 |
-
f.write(
|
| 33 |
|
| 34 |
return output_file
|
|
|
|
|
|
|
| 1 |
import requests
|
| 2 |
+
import uuid
|
| 3 |
+
from config import GROQ_API_KEY, GROQ_TTS_MODEL
|
| 4 |
+
|
| 5 |
+
def text_to_speech(text: str, voice: str, fmt: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
if not GROQ_API_KEY:
|
| 7 |
raise RuntimeError("GROQ_API_KEY is not set in config")
|
| 8 |
|
| 9 |
+
url = "https://api.groq.ai/openai/v1/audio/speech"
|
| 10 |
+
|
| 11 |
headers = {
|
| 12 |
"Authorization": f"Bearer {GROQ_API_KEY}",
|
| 13 |
"Content-Type": "application/json"
|
| 14 |
}
|
| 15 |
|
| 16 |
payload = {
|
| 17 |
+
"model": GROQ_TTS_MODEL,
|
| 18 |
+
"voice": voice,
|
| 19 |
+
"format": fmt,
|
| 20 |
+
"input": text
|
| 21 |
}
|
| 22 |
|
| 23 |
+
output_file = f"audio_{uuid.uuid4()}.{fmt}"
|
| 24 |
|
| 25 |
response = requests.post(url, headers=headers, json=payload)
|
| 26 |
+
response.raise_for_status()
|
| 27 |
|
|
|
|
| 28 |
with open(output_file, "wb") as f:
|
| 29 |
+
f.write(response.content)
|
| 30 |
|
| 31 |
return output_file
|