-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranscriber.py
More file actions
46 lines (37 loc) · 1.49 KB
/
Copy pathtranscriber.py
File metadata and controls
46 lines (37 loc) · 1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import time
import requests
GROQ_API_URL = "https://api.groq.com/openai/v1/audio/transcriptions"
MODEL = "whisper-large-v3"
class Transcriber:
"""Transcribes audio via Groq's cloud Whisper API."""
def __init__(self, api_key: str, language: str | None = None):
self.api_key = api_key
self.language = language
def transcribe(self, audio_wav: bytes) -> str | None:
"""Send WAV audio bytes to Groq and return the transcribed text."""
if not self.api_key:
raise ValueError("Groq API key is not set")
files = {"file": ("recording.wav", audio_wav, "audio/wav")}
data = {"model": MODEL, "response_format": "json"}
if self.language:
data["language"] = self.language
headers = {"Authorization": f"Bearer {self.api_key}"}
for attempt in range(3):
try:
resp = requests.post(
GROQ_API_URL,
headers=headers,
files=files,
data=data,
timeout=30,
)
resp.raise_for_status()
result = resp.json()
return result.get("text", "").strip()
except requests.exceptions.Timeout:
if attempt < 2:
time.sleep(1)
continue
raise
except requests.exceptions.RequestException as e:
raise RuntimeError(f"Transcription failed: {e}") from e