malek-messaoudii commited on
Commit
587fe4f
·
1 Parent(s): 209f7bb

CORRECT services

Browse files
Files changed (2) hide show
  1. services/stt_service.py +23 -10
  2. 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(audio_file_path: str) -> str:
6
- headers = {"Authorization": f"Bearer {GROQ_API_KEY}"}
7
- files = {"file": open(audio_file_path, "rb")}
8
- response = requests.post(
9
- f"https://api.groq.ai/v1/models/{GROQ_STT_MODEL}/predict",
10
- headers=headers,
11
- files=files
12
- )
13
- return response.json().get("text", "")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
- from pathlib import Path
2
- from config import client
 
3
 
4
- def text_to_speech(text: str, voice: str = "Aaliyah-PlayAI", fmt: str = "wav"):
5
- speech_path = Path("audio/temp/output." + fmt)
 
 
 
 
 
 
6
 
7
- response = client.audio.speech.create(
8
- model="playai-tts",
9
- voice=voice,
10
- response_format=fmt,
11
- input=text,
12
- )
13
 
14
- response.stream_to_file(speech_path)
15
- return speech_path
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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