Upload 10 files
Browse files
providers/microsoft_azure_provider.py
CHANGED
|
@@ -74,4 +74,4 @@ class MicrosoftAzureProvider(APIProvider):
|
|
| 74 |
if not resp.ok:
|
| 75 |
print(f"Azure API error {resp.status_code}: {resp.text}")
|
| 76 |
resp.raise_for_status()
|
| 77 |
-
return resp.json().get("combinedPhrases", [{}])[0].get("text", "") or "."
|
|
|
|
| 74 |
if not resp.ok:
|
| 75 |
print(f"Azure API error {resp.status_code}: {resp.text}")
|
| 76 |
resp.raise_for_status()
|
| 77 |
+
return resp.json().get("combinedPhrases", [{}])[0].get("text", "") or "."
|
providers/reson8_provider.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import time
|
| 3 |
+
import threading
|
| 4 |
+
from typing import Optional
|
| 5 |
+
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
from . import APIProvider, register, PermanentError
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
AUTH_URL = "https://api.reson8.dev/v1/auth/token"
|
| 12 |
+
TRANSCRIBE_URL = "https://api.reson8.dev/v1/speech-to-text/prerecorded"
|
| 13 |
+
TOKEN_REFRESH_MARGIN_S = 30
|
| 14 |
+
MIN_AUDIO_DURATION_S = 0.16
|
| 15 |
+
|
| 16 |
+
_token_lock = threading.Lock()
|
| 17 |
+
_token_cache: dict = {"access_token": None, "expires_at": 0.0}
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def _get_access_token(api_key: str) -> str:
|
| 21 |
+
with _token_lock:
|
| 22 |
+
cached = _token_cache["access_token"]
|
| 23 |
+
if cached and _token_cache["expires_at"] - time.time() > TOKEN_REFRESH_MARGIN_S:
|
| 24 |
+
return cached
|
| 25 |
+
|
| 26 |
+
response = requests.post(
|
| 27 |
+
AUTH_URL,
|
| 28 |
+
headers={"Authorization": f"ApiKey {api_key}"},
|
| 29 |
+
timeout=30,
|
| 30 |
+
)
|
| 31 |
+
response.raise_for_status()
|
| 32 |
+
data = response.json()
|
| 33 |
+
_token_cache["access_token"] = data["access_token"]
|
| 34 |
+
_token_cache["expires_at"] = time.time() + float(data.get("expires_in", 600))
|
| 35 |
+
return _token_cache["access_token"]
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
@register("reson8")
|
| 39 |
+
class Reson8Provider(APIProvider):
|
| 40 |
+
def transcribe(
|
| 41 |
+
self,
|
| 42 |
+
model_variant: str,
|
| 43 |
+
audio_file_path: Optional[str],
|
| 44 |
+
sample: dict,
|
| 45 |
+
use_url: bool = False,
|
| 46 |
+
language: str = "en",
|
| 47 |
+
) -> str:
|
| 48 |
+
api_key = os.getenv("RESON8_API_KEY")
|
| 49 |
+
if not api_key or api_key == "your_api_key":
|
| 50 |
+
raise PermanentError("RESON8_API_KEY is not set (or still the placeholder)")
|
| 51 |
+
|
| 52 |
+
if use_url:
|
| 53 |
+
audio_duration = sample["row"]["audio_length_s"]
|
| 54 |
+
if audio_duration < MIN_AUDIO_DURATION_S:
|
| 55 |
+
return "."
|
| 56 |
+
audio_url = sample["row"]["audio"][0]["src"]
|
| 57 |
+
resp = requests.get(audio_url, timeout=60)
|
| 58 |
+
resp.raise_for_status()
|
| 59 |
+
audio_bytes = resp.content
|
| 60 |
+
else:
|
| 61 |
+
audio_duration = (
|
| 62 |
+
len(sample["audio"]["array"]) / sample["audio"]["sampling_rate"]
|
| 63 |
+
)
|
| 64 |
+
if audio_duration < MIN_AUDIO_DURATION_S:
|
| 65 |
+
return "."
|
| 66 |
+
with open(audio_file_path, "rb") as f:
|
| 67 |
+
audio_bytes = f.read()
|
| 68 |
+
|
| 69 |
+
params = {"encoding": "auto", "model": model_variant}
|
| 70 |
+
if language:
|
| 71 |
+
params["language"] = language
|
| 72 |
+
|
| 73 |
+
token = _get_access_token(api_key)
|
| 74 |
+
response = requests.post(
|
| 75 |
+
TRANSCRIBE_URL,
|
| 76 |
+
params=params,
|
| 77 |
+
headers={
|
| 78 |
+
"Authorization": f"Bearer {token}",
|
| 79 |
+
"Content-Type": "application/octet-stream",
|
| 80 |
+
},
|
| 81 |
+
data=audio_bytes,
|
| 82 |
+
timeout=300,
|
| 83 |
+
)
|
| 84 |
+
response.raise_for_status()
|
| 85 |
+
return response.json().get("text", "").strip()
|
providers/smallest_provider.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import requests
|
| 3 |
+
from typing import Optional
|
| 4 |
+
|
| 5 |
+
from . import APIProvider, PermanentError, register
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
@register("smallestai")
|
| 9 |
+
class SmallestAIProvider(APIProvider):
|
| 10 |
+
def transcribe(
|
| 11 |
+
self,
|
| 12 |
+
model_variant: str,
|
| 13 |
+
audio_file_path: Optional[str],
|
| 14 |
+
sample: dict,
|
| 15 |
+
use_url: bool = False,
|
| 16 |
+
language: str = "en",
|
| 17 |
+
) -> str:
|
| 18 |
+
api_key = os.getenv("SMALLESTAI_API_KEY")
|
| 19 |
+
if not api_key or api_key == "your_api_key":
|
| 20 |
+
raise ValueError(
|
| 21 |
+
"SMALLESTAI_API_KEY environment variable not set, get your key at https://console.smallest.ai"
|
| 22 |
+
)
|
| 23 |
+
endpoint = "https://api.smallest.ai/waves/v1/pulse/get_text"
|
| 24 |
+
if use_url:
|
| 25 |
+
audio_url = sample["row"]["audio"][0]["src"]
|
| 26 |
+
response = requests.post(
|
| 27 |
+
endpoint,
|
| 28 |
+
headers={
|
| 29 |
+
"Authorization": f"Bearer {api_key}",
|
| 30 |
+
"Content-Type": "application/json",
|
| 31 |
+
},
|
| 32 |
+
json={"url": audio_url},
|
| 33 |
+
params={"language": language},
|
| 34 |
+
timeout=300,
|
| 35 |
+
)
|
| 36 |
+
else:
|
| 37 |
+
with open(audio_file_path, "rb") as audio_file:
|
| 38 |
+
audio_data = audio_file.read()
|
| 39 |
+
response = requests.post(
|
| 40 |
+
endpoint,
|
| 41 |
+
headers={
|
| 42 |
+
"Authorization": f"Bearer {api_key}",
|
| 43 |
+
"Content-Type": "application/octet-stream",
|
| 44 |
+
},
|
| 45 |
+
data=audio_data,
|
| 46 |
+
params={"language": language},
|
| 47 |
+
timeout=300,
|
| 48 |
+
)
|
| 49 |
+
if response.status_code != 429 and 400 <= response.status_code < 500:
|
| 50 |
+
raise PermanentError(
|
| 51 |
+
f"Smallest AI API returned {response.status_code}: {response.text}"
|
| 52 |
+
)
|
| 53 |
+
response.raise_for_status()
|
| 54 |
+
return response.json().get("transcription", "") or "."
|