Spaces:
Sleeping
Sleeping
fix: resolve HF Spaces deployment timeout
Browse files- Fix port mismatch: update README app_port from 8000 to 7860
to match Dockerfile EXPOSE and PORT env var
- Fix slow startup: move heavyweight transformers imports inside
methods (get_whisper, get_nllb, get_tts) instead of module level,
reducing ~5min import time to near-instant
- README.md +1 -1
- backend/models.py +6 -11
README.md
CHANGED
|
@@ -4,7 +4,7 @@ emoji: 🌾
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
-
app_port:
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
|
|
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: gray
|
| 6 |
sdk: docker
|
| 7 |
+
app_port: 7860
|
| 8 |
pinned: false
|
| 9 |
---
|
| 10 |
|
backend/models.py
CHANGED
|
@@ -4,14 +4,6 @@ import numpy as np
|
|
| 4 |
import soundfile as sf
|
| 5 |
import threading
|
| 6 |
import gc
|
| 7 |
-
from transformers import (
|
| 8 |
-
pipeline,
|
| 9 |
-
AutoModelForSeq2SeqLM,
|
| 10 |
-
AutoTokenizer,
|
| 11 |
-
VitsModel,
|
| 12 |
-
WhisperProcessor,
|
| 13 |
-
WhisperForConditionalGeneration
|
| 14 |
-
)
|
| 15 |
|
| 16 |
# Optimize Torch for CPU-only environments like HF Spaces
|
| 17 |
if not torch.cuda.is_available():
|
|
@@ -55,15 +47,15 @@ class ModelManager:
|
|
| 55 |
def get_whisper(self, size="base"):
|
| 56 |
with self.lock:
|
| 57 |
if size not in self.whisper_pipe:
|
|
|
|
|
|
|
| 58 |
model_id = f"openai/whisper-{size}"
|
| 59 |
print(f"[*] Loading STT model {model_id} from {self.cache_dir} on {self.device}...")
|
| 60 |
|
| 61 |
try:
|
| 62 |
-
# Load processor & model from local cache
|
| 63 |
processor = WhisperProcessor.from_pretrained(model_id, cache_dir=self.cache_dir, local_files_only=True)
|
| 64 |
model = WhisperForConditionalGeneration.from_pretrained(model_id, cache_dir=self.cache_dir, local_files_only=True)
|
| 65 |
|
| 66 |
-
# Pipeline does chunking automatically for long files
|
| 67 |
self.whisper_pipe[size] = pipeline(
|
| 68 |
"automatic-speech-recognition",
|
| 69 |
model=model,
|
|
@@ -75,7 +67,6 @@ class ModelManager:
|
|
| 75 |
print(f"[✓] Whisper-{size} loaded successfully.")
|
| 76 |
except Exception as e:
|
| 77 |
print(f"[!] Error loading Whisper-{size}: {e}")
|
| 78 |
-
# Try without local_files_only as fallback
|
| 79 |
self.whisper_pipe[size] = pipeline(
|
| 80 |
"automatic-speech-recognition",
|
| 81 |
model=model_id,
|
|
@@ -88,6 +79,8 @@ class ModelManager:
|
|
| 88 |
def get_nllb(self):
|
| 89 |
with self.lock:
|
| 90 |
if self.nllb_model is None:
|
|
|
|
|
|
|
| 91 |
model_id = "facebook/nllb-200-distilled-600M"
|
| 92 |
print(f"[*] Loading NLLB-200 translation model from {self.cache_dir} on {self.device}...")
|
| 93 |
try:
|
|
@@ -103,6 +96,8 @@ class ModelManager:
|
|
| 103 |
def get_tts(self, lang):
|
| 104 |
with self.lock:
|
| 105 |
if lang not in self.tts_models:
|
|
|
|
|
|
|
| 106 |
model_id = {
|
| 107 |
"Hindi": "facebook/mms-tts-hin",
|
| 108 |
"Marathi": "facebook/mms-tts-mar",
|
|
|
|
| 4 |
import soundfile as sf
|
| 5 |
import threading
|
| 6 |
import gc
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Optimize Torch for CPU-only environments like HF Spaces
|
| 9 |
if not torch.cuda.is_available():
|
|
|
|
| 47 |
def get_whisper(self, size="base"):
|
| 48 |
with self.lock:
|
| 49 |
if size not in self.whisper_pipe:
|
| 50 |
+
from transformers import WhisperProcessor, WhisperForConditionalGeneration, pipeline
|
| 51 |
+
|
| 52 |
model_id = f"openai/whisper-{size}"
|
| 53 |
print(f"[*] Loading STT model {model_id} from {self.cache_dir} on {self.device}...")
|
| 54 |
|
| 55 |
try:
|
|
|
|
| 56 |
processor = WhisperProcessor.from_pretrained(model_id, cache_dir=self.cache_dir, local_files_only=True)
|
| 57 |
model = WhisperForConditionalGeneration.from_pretrained(model_id, cache_dir=self.cache_dir, local_files_only=True)
|
| 58 |
|
|
|
|
| 59 |
self.whisper_pipe[size] = pipeline(
|
| 60 |
"automatic-speech-recognition",
|
| 61 |
model=model,
|
|
|
|
| 67 |
print(f"[✓] Whisper-{size} loaded successfully.")
|
| 68 |
except Exception as e:
|
| 69 |
print(f"[!] Error loading Whisper-{size}: {e}")
|
|
|
|
| 70 |
self.whisper_pipe[size] = pipeline(
|
| 71 |
"automatic-speech-recognition",
|
| 72 |
model=model_id,
|
|
|
|
| 79 |
def get_nllb(self):
|
| 80 |
with self.lock:
|
| 81 |
if self.nllb_model is None:
|
| 82 |
+
from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
|
| 83 |
+
|
| 84 |
model_id = "facebook/nllb-200-distilled-600M"
|
| 85 |
print(f"[*] Loading NLLB-200 translation model from {self.cache_dir} on {self.device}...")
|
| 86 |
try:
|
|
|
|
| 96 |
def get_tts(self, lang):
|
| 97 |
with self.lock:
|
| 98 |
if lang not in self.tts_models:
|
| 99 |
+
from transformers import AutoTokenizer, VitsModel
|
| 100 |
+
|
| 101 |
model_id = {
|
| 102 |
"Hindi": "facebook/mms-tts-hin",
|
| 103 |
"Marathi": "facebook/mms-tts-mar",
|