Spaces:
Sleeping
Sleeping
the model tries to cache on startup to boost performance
Browse files- README.md +1 -0
- app.py +9 -1
- config.py +2 -1
- pipeline/transcriber.py +80 -0
README.md
CHANGED
|
@@ -54,6 +54,7 @@ TeeleAgentHF is an AI-powered telecalling agent built for a Hugging Face competi
|
|
| 54 |
## Deployment (Hugging Face Spaces)
|
| 55 |
|
| 56 |
- Ensure `app.py` listens on 0.0.0.0:7860 (config.py already uses these defaults)
|
|
|
|
| 57 |
- Provide model files or configure download/autoload in `hf_config.json`
|
| 58 |
- Verify VRAM budget and use quantized GGUF models to fit resource limits
|
| 59 |
|
|
|
|
| 54 |
## Deployment (Hugging Face Spaces)
|
| 55 |
|
| 56 |
- Ensure `app.py` listens on 0.0.0.0:7860 (config.py already uses these defaults)
|
| 57 |
+
- For Moonshine ASR, leave `TRANSCRIBE_LOCAL_ONLY` unset or set it to `0` in the environment so the model can be downloaded automatically on first run.
|
| 58 |
- Provide model files or configure download/autoload in `hf_config.json`
|
| 59 |
- Verify VRAM budget and use quantized GGUF models to fit resource limits
|
| 60 |
|
app.py
CHANGED
|
@@ -371,7 +371,15 @@ def build_app() -> gr.Blocks:
|
|
| 371 |
# ── Entry point ───────────────────────────────────────────────────────────────
|
| 372 |
|
| 373 |
if __name__ == "__main__":
|
| 374 |
-
logger.info("Starting Gradio app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 375 |
app = build_app()
|
| 376 |
app.launch(
|
| 377 |
server_name = SERVER_NAME,
|
|
|
|
| 371 |
# ── Entry point ───────────────────────────────────────────────────────────────
|
| 372 |
|
| 373 |
if __name__ == "__main__":
|
| 374 |
+
logger.info("Starting Gradio app; prefetching deployed ASR model if needed...")
|
| 375 |
+
try:
|
| 376 |
+
get_transcriber().prefetch()
|
| 377 |
+
except Exception as exc:
|
| 378 |
+
logger.error(
|
| 379 |
+
"ASR prefetch failed at startup; continuing with lazy loading: %s",
|
| 380 |
+
exc,
|
| 381 |
+
)
|
| 382 |
+
|
| 383 |
app = build_app()
|
| 384 |
app.launch(
|
| 385 |
server_name = SERVER_NAME,
|
config.py
CHANGED
|
@@ -22,7 +22,8 @@ HF_TOKEN = os.environ.get("HF_TOKEN", "")
|
|
| 22 |
TRANSCRIBE_MODEL_ID = "UsefulSensors/moonshine-tiny"
|
| 23 |
TRANSCRIBE_LANGUAGE = "en" # Moonshine Tiny is English ASR
|
| 24 |
TRANSCRIBE_DEVICE = "cpu" # CPU-only for zero-gpu deployment
|
| 25 |
-
TRANSCRIBE_LOCAL_ONLY = os.environ.get("TRANSCRIBE_LOCAL_ONLY", "
|
|
|
|
| 26 |
|
| 27 |
# Intent Parser: Qwen2.5-7B-Instruct (GGUF via llama-cpp-python)
|
| 28 |
# Using q2_k quantization for smaller size on CPU (~2.3 GB).
|
|
|
|
| 22 |
TRANSCRIBE_MODEL_ID = "UsefulSensors/moonshine-tiny"
|
| 23 |
TRANSCRIBE_LANGUAGE = "en" # Moonshine Tiny is English ASR
|
| 24 |
TRANSCRIBE_DEVICE = "cpu" # CPU-only for zero-gpu deployment
|
| 25 |
+
TRANSCRIBE_LOCAL_ONLY = os.environ.get("TRANSCRIBE_LOCAL_ONLY", "0").lower() in ("1", "true", "yes")
|
| 26 |
+
# Set TRANSCRIBE_LOCAL_ONLY=1 to require a pre-downloaded Moonshine cache and disable remote Hugging Face Hub downloads.
|
| 27 |
|
| 28 |
# Intent Parser: Qwen2.5-7B-Instruct (GGUF via llama-cpp-python)
|
| 29 |
# Using q2_k quantization for smaller size on CPU (~2.3 GB).
|
pipeline/transcriber.py
CHANGED
|
@@ -196,6 +196,77 @@ class Transcriber:
|
|
| 196 |
|
| 197 |
self._load()
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
def _load(self):
|
| 200 |
"""Pull Moonshine from Hugging Face and move it to the best device."""
|
| 201 |
import os
|
|
@@ -214,6 +285,15 @@ class Transcriber:
|
|
| 214 |
)
|
| 215 |
local_only = TRANSCRIBE_LOCAL_ONLY or os.path.exists(hf_model_cache)
|
| 216 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 217 |
if local_only and not os.path.exists(hf_model_cache):
|
| 218 |
logger.error(
|
| 219 |
"Local Moonshine model cache not found; offline-only transcription "
|
|
|
|
| 196 |
|
| 197 |
self._load()
|
| 198 |
|
| 199 |
+
def prefetch(self) -> None:
|
| 200 |
+
"""Download Moonshine into the local HF cache before first inference."""
|
| 201 |
+
if TRANSCRIBE_LOCAL_ONLY:
|
| 202 |
+
logger.info(
|
| 203 |
+
"TRANSCRIBE_LOCAL_ONLY is enabled; skipping startup prefetch."
|
| 204 |
+
)
|
| 205 |
+
return
|
| 206 |
+
|
| 207 |
+
import os
|
| 208 |
+
|
| 209 |
+
hf_model_cache = os.path.expanduser(
|
| 210 |
+
f"~/.cache/huggingface/hub/models--{TRANSCRIBE_MODEL_ID.replace('/', '--')}"
|
| 211 |
+
)
|
| 212 |
+
|
| 213 |
+
if os.path.exists(hf_model_cache):
|
| 214 |
+
logger.info("Moonshine ASR cache already exists; skipping startup prefetch.")
|
| 215 |
+
return
|
| 216 |
+
|
| 217 |
+
logger.info(
|
| 218 |
+
"Prefetching Moonshine ASR model into the Hugging Face cache..."
|
| 219 |
+
)
|
| 220 |
+
|
| 221 |
+
if not _TORCH_AVAILABLE:
|
| 222 |
+
try:
|
| 223 |
+
from huggingface_hub import snapshot_download
|
| 224 |
+
|
| 225 |
+
snapshot_download(
|
| 226 |
+
repo_id=TRANSCRIBE_MODEL_ID,
|
| 227 |
+
use_auth_token=HF_TOKEN or None,
|
| 228 |
+
local_files_only=False,
|
| 229 |
+
repo_type="model",
|
| 230 |
+
)
|
| 231 |
+
logger.info("Moonshine ASR cache download completed.")
|
| 232 |
+
except Exception as exc:
|
| 233 |
+
logger.error(
|
| 234 |
+
"Failed to prefetch Moonshine model cache without PyTorch: %s",
|
| 235 |
+
exc,
|
| 236 |
+
exc_info=True,
|
| 237 |
+
)
|
| 238 |
+
return
|
| 239 |
+
|
| 240 |
+
try:
|
| 241 |
+
self._install_torchvision_import_stub_if_needed()
|
| 242 |
+
from transformers import AutoFeatureExtractor, AutoTokenizer
|
| 243 |
+
from transformers import MoonshineForConditionalGeneration
|
| 244 |
+
|
| 245 |
+
token_kwargs = {"token": HF_TOKEN} if HF_TOKEN else {}
|
| 246 |
+
|
| 247 |
+
AutoFeatureExtractor.from_pretrained(
|
| 248 |
+
TRANSCRIBE_MODEL_ID,
|
| 249 |
+
local_files_only=False,
|
| 250 |
+
**token_kwargs,
|
| 251 |
+
)
|
| 252 |
+
AutoTokenizer.from_pretrained(
|
| 253 |
+
TRANSCRIBE_MODEL_ID,
|
| 254 |
+
local_files_only=False,
|
| 255 |
+
**token_kwargs,
|
| 256 |
+
)
|
| 257 |
+
MoonshineForConditionalGeneration.from_pretrained(
|
| 258 |
+
TRANSCRIBE_MODEL_ID,
|
| 259 |
+
local_files_only=False,
|
| 260 |
+
**token_kwargs,
|
| 261 |
+
)
|
| 262 |
+
logger.info("Moonshine ASR prefetch completed successfully.")
|
| 263 |
+
except Exception as exc:
|
| 264 |
+
logger.error(
|
| 265 |
+
"Startup prefetch of Moonshine failed: %s",
|
| 266 |
+
exc,
|
| 267 |
+
exc_info=True,
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
def _load(self):
|
| 271 |
"""Pull Moonshine from Hugging Face and move it to the best device."""
|
| 272 |
import os
|
|
|
|
| 285 |
)
|
| 286 |
local_only = TRANSCRIBE_LOCAL_ONLY or os.path.exists(hf_model_cache)
|
| 287 |
|
| 288 |
+
if TRANSCRIBE_LOCAL_ONLY:
|
| 289 |
+
logger.info(
|
| 290 |
+
"Offline-only transcription is enabled; Moonshine must already be cached locally."
|
| 291 |
+
)
|
| 292 |
+
elif not os.path.exists(hf_model_cache):
|
| 293 |
+
logger.info(
|
| 294 |
+
"Moonshine cache not found locally; downloading from Hugging Face Hub."
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
if local_only and not os.path.exists(hf_model_cache):
|
| 298 |
logger.error(
|
| 299 |
"Local Moonshine model cache not found; offline-only transcription "
|