Spaces:
Sleeping
Sleeping
Fix build error: Remove build-time model preloading and optimize for HF Spaces
Browse files- Remove build-time model preloading to avoid resource constraints
- Move model downloads to runtime for better HF Spaces compatibility
- Use SD 1.5 by default in HF Spaces for faster loading
- Update cache directories to use /tmp for better compatibility
- Add fallback logic detection for HF Spaces environment
- Dockerfile +8 -10
- app.py +2 -1
- models/image_generator.py +14 -5
Dockerfile
CHANGED
|
@@ -14,16 +14,14 @@ RUN pip install --no-cache-dir -r requirements.txt
|
|
| 14 |
# Copy application code
|
| 15 |
COPY . .
|
| 16 |
|
| 17 |
-
# Set environment variables for HuggingFace caching
|
| 18 |
-
ENV HF_HOME=/
|
| 19 |
-
ENV TRANSFORMERS_CACHE=/
|
| 20 |
-
ENV HF_DATASETS_CACHE=/
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
# Pre-download models during build time for faster startup
|
| 26 |
-
RUN python preload_models.py
|
| 27 |
|
| 28 |
EXPOSE 7860
|
| 29 |
|
|
|
|
| 14 |
# Copy application code
|
| 15 |
COPY . .
|
| 16 |
|
| 17 |
+
# Set environment variables for HuggingFace caching (runtime download)
|
| 18 |
+
ENV HF_HOME=/tmp/huggingface
|
| 19 |
+
ENV TRANSFORMERS_CACHE=/tmp/transformers_cache
|
| 20 |
+
ENV HF_DATASETS_CACHE=/tmp/huggingface_datasets
|
| 21 |
+
ENV TORCH_HOME=/tmp/torch
|
| 22 |
+
|
| 23 |
+
# Note: Models will be downloaded at runtime for better compatibility with Hugging Face Spaces
|
| 24 |
+
# This avoids build-time resource constraints and allows for proper authentication
|
|
|
|
|
|
|
| 25 |
|
| 26 |
EXPOSE 7860
|
| 27 |
|
app.py
CHANGED
|
@@ -12,10 +12,11 @@ import asyncio
|
|
| 12 |
from typing import Optional
|
| 13 |
from contextlib import asynccontextmanager
|
| 14 |
|
| 15 |
-
# Set cache directories to writable locations
|
| 16 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
| 17 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 18 |
os.environ["TORCH_HOME"] = "/tmp/torch"
|
|
|
|
| 19 |
|
| 20 |
import uvicorn
|
| 21 |
from fastapi import FastAPI, File, UploadFile, HTTPException, BackgroundTasks
|
|
|
|
| 12 |
from typing import Optional
|
| 13 |
from contextlib import asynccontextmanager
|
| 14 |
|
| 15 |
+
# Set cache directories to writable locations (Hugging Face Spaces compatible)
|
| 16 |
os.environ["TRANSFORMERS_CACHE"] = "/tmp/transformers_cache"
|
| 17 |
os.environ["HF_HOME"] = "/tmp/huggingface"
|
| 18 |
os.environ["TORCH_HOME"] = "/tmp/torch"
|
| 19 |
+
os.environ["HF_DATASETS_CACHE"] = "/tmp/huggingface_datasets"
|
| 20 |
|
| 21 |
import uvicorn
|
| 22 |
from fastapi import FastAPI, File, UploadFile, HTTPException, BackgroundTasks
|
models/image_generator.py
CHANGED
|
@@ -24,10 +24,19 @@ class ImageGenerator:
|
|
| 24 |
def load_model(self):
|
| 25 |
"""Load the Stable Diffusion model with optimized caching"""
|
| 26 |
try:
|
| 27 |
-
logger.info(f"π Loading
|
| 28 |
|
| 29 |
-
#
|
| 30 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
# Optimize caching for faster subsequent loads
|
| 33 |
cache_dir = os.environ.get("HF_HOME", "/tmp/huggingface_cache")
|
|
@@ -72,9 +81,9 @@ class ImageGenerator:
|
|
| 72 |
self.pipeline.enable_sequential_cpu_offload()
|
| 73 |
|
| 74 |
if self.device.type == "cuda":
|
| 75 |
-
logger.info(f"β
|
| 76 |
else:
|
| 77 |
-
logger.info("β
|
| 78 |
|
| 79 |
except Exception as e:
|
| 80 |
logger.error(f"β Failed to load Stability AI model: {str(e)}")
|
|
|
|
| 24 |
def load_model(self):
|
| 25 |
"""Load the Stable Diffusion model with optimized caching"""
|
| 26 |
try:
|
| 27 |
+
logger.info(f"π Loading Stable Diffusion model on {self.device}...")
|
| 28 |
|
| 29 |
+
# For Hugging Face Spaces, start with a more lightweight model
|
| 30 |
+
# Check if we're running in HF Spaces environment
|
| 31 |
+
is_hf_spaces = os.environ.get('SPACE_ID') is not None
|
| 32 |
+
|
| 33 |
+
if is_hf_spaces:
|
| 34 |
+
# Use SD 1.5 for faster loading in HF Spaces
|
| 35 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 36 |
+
logger.info("π Running in HF Spaces - using SD 1.5 for optimal performance")
|
| 37 |
+
else:
|
| 38 |
+
# Use SDXL for local/other deployments
|
| 39 |
+
model_id = "stabilityai/stable-diffusion-xl-base-1.0"
|
| 40 |
|
| 41 |
# Optimize caching for faster subsequent loads
|
| 42 |
cache_dir = os.environ.get("HF_HOME", "/tmp/huggingface_cache")
|
|
|
|
| 81 |
self.pipeline.enable_sequential_cpu_offload()
|
| 82 |
|
| 83 |
if self.device.type == "cuda":
|
| 84 |
+
logger.info(f"β
Stable Diffusion model loaded on GPU: {torch.cuda.get_device_name(0)}")
|
| 85 |
else:
|
| 86 |
+
logger.info("β
Stable Diffusion model loaded on CPU")
|
| 87 |
|
| 88 |
except Exception as e:
|
| 89 |
logger.error(f"β Failed to load Stability AI model: {str(e)}")
|