Commit
·
6108abf
1
Parent(s):
5e6062c
Fix cache directory permissions and OMP_NUM_THREADS warnings - Set HF cache env vars before imports - Set MPLCONFIGDIR for matplotlib - Fix OMP_NUM_THREADS in Dockerfile
Browse files- Dockerfile +6 -0
- app/colorize_model.py +12 -1
- app/main.py +12 -7
Dockerfile
CHANGED
|
@@ -48,6 +48,12 @@ ENV BASE_URL=${SPACE_HOST}
|
|
| 48 |
ENV PORT=7860
|
| 49 |
ENV DATA_DIR=/data
|
| 50 |
ENV OMP_NUM_THREADS=1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
# Health check
|
| 53 |
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
|
|
|
| 48 |
ENV PORT=7860
|
| 49 |
ENV DATA_DIR=/data
|
| 50 |
ENV OMP_NUM_THREADS=1
|
| 51 |
+
ENV HF_HOME=/tmp/hf_cache
|
| 52 |
+
ENV TRANSFORMERS_CACHE=/tmp/hf_cache
|
| 53 |
+
ENV HUGGINGFACE_HUB_CACHE=/tmp/hf_cache
|
| 54 |
+
ENV HF_HUB_CACHE=/tmp/hf_cache
|
| 55 |
+
ENV XDG_CACHE_HOME=/tmp/hf_cache
|
| 56 |
+
ENV MPLCONFIGDIR=/tmp/matplotlib_config
|
| 57 |
|
| 58 |
# Health check
|
| 59 |
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
|
app/colorize_model.py
CHANGED
|
@@ -9,6 +9,15 @@ import logging
|
|
| 9 |
import os
|
| 10 |
from typing import Tuple
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
import torch
|
| 13 |
from PIL import Image
|
| 14 |
from fastai.vision.all import *
|
|
@@ -20,15 +29,17 @@ logger = logging.getLogger(__name__)
|
|
| 20 |
|
| 21 |
|
| 22 |
def _ensure_cache_dir() -> str:
|
| 23 |
-
cache_dir = os.environ.get("HF_HOME"
|
| 24 |
try:
|
| 25 |
os.makedirs(cache_dir, exist_ok=True)
|
| 26 |
except Exception as exc:
|
| 27 |
logger.warning("Could not create cache directory %s: %s", cache_dir, exc)
|
|
|
|
| 28 |
os.environ["HF_HOME"] = cache_dir
|
| 29 |
os.environ["TRANSFORMERS_CACHE"] = cache_dir
|
| 30 |
os.environ["HUGGINGFACE_HUB_CACHE"] = cache_dir
|
| 31 |
os.environ["HF_HUB_CACHE"] = cache_dir
|
|
|
|
| 32 |
return cache_dir
|
| 33 |
|
| 34 |
|
|
|
|
| 9 |
import os
|
| 10 |
from typing import Tuple
|
| 11 |
|
| 12 |
+
# Ensure cache directory is set before any HF imports
|
| 13 |
+
# (main.py should have set these, but ensure they're set here too)
|
| 14 |
+
cache_dir = os.environ.get("HF_HOME", "/tmp/hf_cache")
|
| 15 |
+
os.environ["HF_HOME"] = cache_dir
|
| 16 |
+
os.environ["TRANSFORMERS_CACHE"] = cache_dir
|
| 17 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = cache_dir
|
| 18 |
+
os.environ["HF_HUB_CACHE"] = cache_dir
|
| 19 |
+
os.environ["XDG_CACHE_HOME"] = cache_dir
|
| 20 |
+
|
| 21 |
import torch
|
| 22 |
from PIL import Image
|
| 23 |
from fastai.vision.all import *
|
|
|
|
| 29 |
|
| 30 |
|
| 31 |
def _ensure_cache_dir() -> str:
|
| 32 |
+
cache_dir = os.environ.get("HF_HOME", "/tmp/hf_cache")
|
| 33 |
try:
|
| 34 |
os.makedirs(cache_dir, exist_ok=True)
|
| 35 |
except Exception as exc:
|
| 36 |
logger.warning("Could not create cache directory %s: %s", cache_dir, exc)
|
| 37 |
+
# Ensure all cache env vars point to this directory
|
| 38 |
os.environ["HF_HOME"] = cache_dir
|
| 39 |
os.environ["TRANSFORMERS_CACHE"] = cache_dir
|
| 40 |
os.environ["HUGGINGFACE_HUB_CACHE"] = cache_dir
|
| 41 |
os.environ["HF_HUB_CACHE"] = cache_dir
|
| 42 |
+
os.environ["XDG_CACHE_HOME"] = cache_dir
|
| 43 |
return cache_dir
|
| 44 |
|
| 45 |
|
app/main.py
CHANGED
|
@@ -3,8 +3,17 @@ FastAPI application for image colorization using ColorizeNet model
|
|
| 3 |
with Firebase App Check integration
|
| 4 |
"""
|
| 5 |
import os
|
|
|
|
| 6 |
# Set OMP_NUM_THREADS before any torch imports to avoid libgomp warnings
|
| 7 |
-
os.environ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
import uuid
|
| 10 |
import logging
|
|
@@ -24,13 +33,9 @@ import io
|
|
| 24 |
from app.colorize_model import ColorizeModel
|
| 25 |
from app.config import settings
|
| 26 |
|
| 27 |
-
#
|
| 28 |
-
os.environ.setdefault("HF_HOME", "/tmp/hf_cache")
|
| 29 |
-
os.environ.setdefault("TRANSFORMERS_CACHE", "/tmp/hf_cache")
|
| 30 |
-
os.environ.setdefault("HF_HUB_CACHE", "/tmp/hf_cache")
|
| 31 |
-
os.environ.setdefault("HUGGINGFACE_HUB_CACHE", "/tmp/hf_cache")
|
| 32 |
-
os.environ.setdefault("XDG_CACHE_HOME", "/tmp/hf_cache")
|
| 33 |
Path("/tmp/hf_cache").mkdir(parents=True, exist_ok=True)
|
|
|
|
| 34 |
Path("/tmp/colorize_uploads").mkdir(parents=True, exist_ok=True)
|
| 35 |
Path("/tmp/colorize_results").mkdir(parents=True, exist_ok=True)
|
| 36 |
|
|
|
|
| 3 |
with Firebase App Check integration
|
| 4 |
"""
|
| 5 |
import os
|
| 6 |
+
# Set environment variables BEFORE any imports to ensure they're used
|
| 7 |
# Set OMP_NUM_THREADS before any torch imports to avoid libgomp warnings
|
| 8 |
+
os.environ["OMP_NUM_THREADS"] = "1"
|
| 9 |
+
# Set HF cache directories to writable /tmp location BEFORE any HF imports
|
| 10 |
+
os.environ["HF_HOME"] = "/tmp/hf_cache"
|
| 11 |
+
os.environ["TRANSFORMERS_CACHE"] = "/tmp/hf_cache"
|
| 12 |
+
os.environ["HF_HUB_CACHE"] = "/tmp/hf_cache"
|
| 13 |
+
os.environ["HUGGINGFACE_HUB_CACHE"] = "/tmp/hf_cache"
|
| 14 |
+
os.environ["XDG_CACHE_HOME"] = "/tmp/hf_cache"
|
| 15 |
+
# Set matplotlib config directory to writable location
|
| 16 |
+
os.environ["MPLCONFIGDIR"] = "/tmp/matplotlib_config"
|
| 17 |
|
| 18 |
import uuid
|
| 19 |
import logging
|
|
|
|
| 33 |
from app.colorize_model import ColorizeModel
|
| 34 |
from app.config import settings
|
| 35 |
|
| 36 |
+
# Create writable directories (env vars already set above)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
Path("/tmp/hf_cache").mkdir(parents=True, exist_ok=True)
|
| 38 |
+
Path("/tmp/matplotlib_config").mkdir(parents=True, exist_ok=True)
|
| 39 |
Path("/tmp/colorize_uploads").mkdir(parents=True, exist_ok=True)
|
| 40 |
Path("/tmp/colorize_results").mkdir(parents=True, exist_ok=True)
|
| 41 |
|