Spaces:
Sleeping
Sleeping
..
Browse files- pipeline/transcriber.py +28 -1
pipeline/transcriber.py
CHANGED
|
@@ -30,6 +30,8 @@ except ImportError: # pragma: no cover
|
|
| 30 |
|
| 31 |
import requests
|
| 32 |
import soundfile as sf
|
|
|
|
|
|
|
| 33 |
|
| 34 |
from config import (
|
| 35 |
TRANSCRIBE_MODEL_ID,
|
|
@@ -60,6 +62,8 @@ class Transcriber:
|
|
| 60 |
self._lock = threading.Lock()
|
| 61 |
self._loaded = False
|
| 62 |
self._use_remote = False
|
|
|
|
|
|
|
| 63 |
|
| 64 |
def transcribe(self, sample_rate: int, audio: np.ndarray) -> str:
|
| 65 |
"""
|
|
@@ -295,8 +299,26 @@ class Transcriber:
|
|
| 295 |
)
|
| 296 |
return ""
|
| 297 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 298 |
try:
|
| 299 |
-
endpoint = "https://api-inference.huggingface.co/models/openai/whisper-small"
|
| 300 |
headers = {
|
| 301 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 302 |
"Accept": "application/json",
|
|
@@ -317,6 +339,11 @@ class Transcriber:
|
|
| 317 |
if isinstance(payload, dict) and "text" in payload:
|
| 318 |
return payload["text"].strip()
|
| 319 |
return str(payload)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 320 |
except Exception as exc:
|
| 321 |
logger.error(f"Remote transcription failed: {exc}", exc_info=True)
|
| 322 |
return ""
|
|
|
|
| 30 |
|
| 31 |
import requests
|
| 32 |
import soundfile as sf
|
| 33 |
+
import socket
|
| 34 |
+
from requests.exceptions import ConnectionError as RequestsConnectionError
|
| 35 |
|
| 36 |
from config import (
|
| 37 |
TRANSCRIBE_MODEL_ID,
|
|
|
|
| 62 |
self._lock = threading.Lock()
|
| 63 |
self._loaded = False
|
| 64 |
self._use_remote = False
|
| 65 |
+
# None = unknown, True = reachable, False = unreachable
|
| 66 |
+
self._remote_ok: Optional[bool] = None
|
| 67 |
|
| 68 |
def transcribe(self, sample_rate: int, audio: np.ndarray) -> str:
|
| 69 |
"""
|
|
|
|
| 299 |
)
|
| 300 |
return ""
|
| 301 |
|
| 302 |
+
# Quick host resolution check: avoid repeated DNS failures
|
| 303 |
+
endpoint = "https://api-inference.huggingface.co/models/openai/whisper-small"
|
| 304 |
+
hf_host = "api-inference.huggingface.co"
|
| 305 |
+
|
| 306 |
+
if self._remote_ok is False:
|
| 307 |
+
logger.debug("Remote HF host previously unreachable; skipping remote transcription.")
|
| 308 |
+
return ""
|
| 309 |
+
|
| 310 |
+
if self._remote_ok is None:
|
| 311 |
+
try:
|
| 312 |
+
socket.getaddrinfo(hf_host, 443)
|
| 313 |
+
self._remote_ok = True
|
| 314 |
+
except Exception:
|
| 315 |
+
logger.warning(
|
| 316 |
+
"Cannot resolve Hugging Face inference host; disabling remote transcription until next restart."
|
| 317 |
+
)
|
| 318 |
+
self._remote_ok = False
|
| 319 |
+
return ""
|
| 320 |
+
|
| 321 |
try:
|
|
|
|
| 322 |
headers = {
|
| 323 |
"Authorization": f"Bearer {HF_TOKEN}",
|
| 324 |
"Accept": "application/json",
|
|
|
|
| 339 |
if isinstance(payload, dict) and "text" in payload:
|
| 340 |
return payload["text"].strip()
|
| 341 |
return str(payload)
|
| 342 |
+
except RequestsConnectionError as exc:
|
| 343 |
+
# Connection errors often indicate network/DNS issues — stop retrying
|
| 344 |
+
logger.error(f"Remote transcription connection failed: {exc}", exc_info=True)
|
| 345 |
+
self._remote_ok = False
|
| 346 |
+
return ""
|
| 347 |
except Exception as exc:
|
| 348 |
logger.error(f"Remote transcription failed: {exc}", exc_info=True)
|
| 349 |
return ""
|