Spaces:
Running
Running
Commit ·
19604c7
1
Parent(s): 001c84a
Fix HF Spaces startup timeout by pre-caching models
Browse files- Dockerfile +10 -0
- scripts/preload_models.py +43 -0
Dockerfile
CHANGED
|
@@ -32,6 +32,16 @@ COPY requirements.txt .
|
|
| 32 |
RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
|
| 33 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 34 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
# Copy the current directory contents into the container at /app
|
| 36 |
COPY . .
|
| 37 |
|
|
|
|
| 32 |
RUN pip install --no-cache-dir torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
|
| 33 |
RUN pip install --no-cache-dir -r requirements.txt
|
| 34 |
|
| 35 |
+
# Increase Hugging Face Hub download timeout to prevent 504 errors
|
| 36 |
+
ENV HF_HUB_DOWNLOAD_TIMEOUT=120
|
| 37 |
+
|
| 38 |
+
# Create scripts directory and copy just the preload script first to leverage Docker cache
|
| 39 |
+
RUN mkdir -p scripts
|
| 40 |
+
COPY scripts/preload_models.py scripts/
|
| 41 |
+
|
| 42 |
+
# Pre-download HF model weights into the image to prevent container startup timeouts
|
| 43 |
+
RUN python scripts/preload_models.py
|
| 44 |
+
|
| 45 |
# Copy the current directory contents into the container at /app
|
| 46 |
COPY . .
|
| 47 |
|
scripts/preload_models.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import (
|
| 4 |
+
DetrImageProcessor,
|
| 5 |
+
DetrForObjectDetection,
|
| 6 |
+
ViTImageProcessor,
|
| 7 |
+
ViTForImageClassification,
|
| 8 |
+
CLIPProcessor,
|
| 9 |
+
CLIPModel,
|
| 10 |
+
AutoTokenizer,
|
| 11 |
+
AutoModel,
|
| 12 |
+
AutoModelForQuestionAnswering,
|
| 13 |
+
AutoModelForSeq2SeqLM,
|
| 14 |
+
BartForConditionalGeneration
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
# Set timeout
|
| 18 |
+
os.environ["HF_HUB_DOWNLOAD_TIMEOUT"] = "120"
|
| 19 |
+
|
| 20 |
+
def preload():
|
| 21 |
+
print("🚀 Starting model pre-loading...")
|
| 22 |
+
|
| 23 |
+
models = {
|
| 24 |
+
"detection": ("facebook/detr-resnet-50", DetrForObjectDetection, DetrImageProcessor),
|
| 25 |
+
"reid": ("google/vit-base-patch16-224", ViTForImageClassification, ViTImageProcessor),
|
| 26 |
+
"clip": ("openai/clip-vit-base-patch32", CLIPModel, CLIPProcessor),
|
| 27 |
+
"search": ("sentence-transformers/all-MiniLM-L6-v2", AutoModel, AutoTokenizer),
|
| 28 |
+
"qa": ("deepset/roberta-base-squad2", AutoModelForQuestionAnswering, AutoTokenizer),
|
| 29 |
+
"report": ("google/flan-t5-base", AutoModelForSeq2SeqLM, AutoTokenizer),
|
| 30 |
+
"summarizer": ("facebook/bart-large-cnn", BartForConditionalGeneration, AutoTokenizer),
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
for name, (model_id, model_cls, proc_cls) in models.items():
|
| 34 |
+
print(f"📦 Pre-loading {name}: {model_id}...")
|
| 35 |
+
try:
|
| 36 |
+
proc_cls.from_pretrained(model_id)
|
| 37 |
+
model_cls.from_pretrained(model_id)
|
| 38 |
+
print(f"✅ {name} loaded.")
|
| 39 |
+
except Exception as e:
|
| 40 |
+
print(f"❌ Failed to load {name}: {e}")
|
| 41 |
+
|
| 42 |
+
if __name__ == "__main__":
|
| 43 |
+
preload()
|