malek-messaoudii
commited on
Commit
·
587fe4f
1
Parent(s):
209f7bb
CORRECT services
Browse files- services/stt_service.py +23 -10
- services/tts_service.py +31 -12
services/stt_service.py
CHANGED
|
@@ -1,13 +1,26 @@
|
|
| 1 |
-
# stt_service.py
|
| 2 |
import requests
|
| 3 |
from config import GROQ_API_KEY, GROQ_STT_MODEL
|
| 4 |
|
| 5 |
-
def speech_to_text(
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
files = {"file": open(audio_file, "rb")}
|
| 18 |
+
|
| 19 |
+
url = f"https://api.groq.ai/v1/models/{GROQ_STT_MODEL}/predict"
|
| 20 |
+
|
| 21 |
+
response = requests.post(url, headers=headers, files=files)
|
| 22 |
+
response.raise_for_status()
|
| 23 |
+
|
| 24 |
+
result = response.json()
|
| 25 |
+
# Supposons que l'API retourne le texte sous 'text'
|
| 26 |
+
return result.get("text", "")
|
services/tts_service.py
CHANGED
|
@@ -1,15 +1,34 @@
|
|
| 1 |
-
|
| 2 |
-
|
|
|
|
| 3 |
|
| 4 |
-
def text_to_speech(text: str,
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
input=text,
|
| 12 |
-
)
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# services/tts_service.py
|
| 2 |
+
import requests
|
| 3 |
+
from config import GROQ_API_KEY, GROQ_TTS_MODEL, GROQ_TTS_VOICE, GROQ_TTS_FORMAT
|
| 4 |
|
| 5 |
+
def text_to_speech(text: str, output_file: str):
|
| 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 |
+
"text": text,
|
| 21 |
+
"voice": GROQ_TTS_VOICE,
|
| 22 |
+
"format": GROQ_TTS_FORMAT
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
url = f"https://api.groq.ai/v1/models/{GROQ_TTS_MODEL}/predict"
|
| 26 |
+
|
| 27 |
+
response = requests.post(url, headers=headers, json=payload)
|
| 28 |
+
response.raise_for_status() # Si erreur, lève une exception
|
| 29 |
+
|
| 30 |
+
audio_content = response.content
|
| 31 |
+
with open(output_file, "wb") as f:
|
| 32 |
+
f.write(audio_content)
|
| 33 |
+
|
| 34 |
+
return output_file
|