hoda.fakhar commited on
Commit
5ff59cc
·
1 Parent(s): 79134ab

Optimize Hugging Face build context and permissions

Browse files
Files changed (2) hide show
  1. .dockerignore +31 -0
  2. Dockerfile +32 -31
.dockerignore ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # EXCLUDE HEAVY FILES FROM DOCKER BUILD CONTEXT
2
+ # This fixes the "No Logs" hanging on Hugging Face during upload
3
+ .git
4
+ .github
5
+ venv
6
+ __pycache__
7
+ *.pyc
8
+ *.pyo
9
+ *.pyd
10
+ .Python
11
+ env/
12
+ .env
13
+
14
+ # REDUNDANT FOLDERS (Project Archive)
15
+ archive/
16
+ final_hf/
17
+ vision-ai-engine/
18
+ node_modules/
19
+
20
+ # LOCAL WEIGHTS (Will be downloaded during build for stability)
21
+ .deepface/weights/*
22
+ ! .deepface/weights/.gitkeep
23
+ models/*.h5
24
+
25
+ # LOGS & TEMPS
26
+ *.log
27
+ app/static/uploads/*
28
+ ! app/static/uploads/.gitkeep
29
+ .ipynb_checkpoints
30
+ .claude
31
+ .hf_token
Dockerfile CHANGED
@@ -1,51 +1,52 @@
1
- # VISION.AI - Hugging Face Spaces
2
  FROM python:3.10-slim
3
 
4
- # System dependencies for OpenCV
 
 
 
 
 
 
 
5
  RUN apt-get update && apt-get install -y \
6
  libgl1 \
7
  libglib2.0-0 \
8
  libsm6 \
9
  libxext6 \
10
  libxrender-dev \
 
11
  && rm -rf /var/lib/apt/lists/*
12
 
13
- WORKDIR /app
14
-
15
- ENV DEEPFACE_HOME=/app
16
- ENV PORT=7860
17
 
18
- COPY requirements.txt .
 
19
 
20
- # Step 1: upgrade pip
21
- RUN pip install --no-cache-dir --upgrade pip
 
22
 
23
- # Step 2: install tensorflow-cpu first (largest package, isolated for visibility)
24
- RUN pip install --no-cache-dir --timeout 180 --retries 5 --progress-bar on tensorflow-cpu==2.15.1
 
 
 
25
 
26
- # Step 3: install tf-keras (depends on tensorflow)
27
- RUN pip install --no-cache-dir --timeout 60 tf-keras
28
-
29
- # Step 4: install everything else
30
- RUN pip install --no-cache-dir --timeout 120 --retries 3 \
31
- flask>=3.0.0 \
32
- flask-cors>=4.0.0 \
33
- opencv-python-headless>=4.8.0 \
34
- deepface>=0.0.86 \
35
- "numpy>=1.24.0,<2.0" \
36
- pillow>=10.0.0 \
37
- python-dotenv>=1.0.0 \
38
- gunicorn>=21.2.0 \
39
- "werkzeug>=3.0.0" \
40
- "huggingface_hub>=0.20.0"
41
-
42
- # Copy only the build script, then download models
43
- COPY scripts/build_models.py scripts/build_models.py
44
  RUN python scripts/build_models.py
45
 
46
  # Copy the rest of the application
47
- COPY . .
 
 
 
48
 
49
  EXPOSE 7860
50
 
51
- CMD ["gunicorn", "-w", "1", "-b", "0.0.0.0:7860", "--timeout", "300", "--access-logfile", "-", "--error-logfile", "-", "run:app"]
 
 
1
+ # VISION.AI - Hugging Face Spaces (Optimized)
2
  FROM python:3.10-slim
3
 
4
+ # Set environment variables for non-root user
5
+ ENV DEEPFACE_HOME=/home/user
6
+ ENV MPLCONFIGDIR=/home/user/.matplotlib
7
+ ENV HF_HOME=/home/user/.cache/huggingface
8
+ ENV PORT=7860
9
+ ENV PYTHONUNBUFFERED=1
10
+
11
+ # Install system dependencies for OpenCV and DeepFace
12
  RUN apt-get update && apt-get install -y \
13
  libgl1 \
14
  libglib2.0-0 \
15
  libsm6 \
16
  libxext6 \
17
  libxrender-dev \
18
+ git \
19
  && rm -rf /var/lib/apt/lists/*
20
 
21
+ # Setup non-root user (Standard for Hugging Face Spaces)
22
+ RUN useradd -m -u 1000 user
23
+ USER user
24
+ WORKDIR /home/user/app
25
 
26
+ # Set PATH for the new user
27
+ ENV PATH="/home/user/.local/bin:${PATH}"
28
 
29
+ # Prepare directories with correct permissions (Hugging Face /home/user is writable)
30
+ RUN mkdir -p /home/user/.deepface/weights \
31
+ && mkdir -p /home/user/app/static/uploads
32
 
33
+ # Install core dependencies first (TensorFlow is the largest)
34
+ COPY --chown=user:user requirements.txt .
35
+ RUN pip install --no-cache-dir --upgrade pip \
36
+ && pip install --no-cache-dir tensorflow-cpu==2.15.1 \
37
+ && pip install --no-cache-dir -r requirements.txt
38
 
39
+ # Pre-download models during BUILD phase (Ensures runtime stability)
40
+ COPY --chown=user:user scripts/build_models.py scripts/build_models.py
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41
  RUN python scripts/build_models.py
42
 
43
  # Copy the rest of the application
44
+ COPY --chown=user:user . .
45
+
46
+ # Ensure the upload folder is definitely writable by the app
47
+ RUN chmod 777 /home/user/app/static/uploads
48
 
49
  EXPOSE 7860
50
 
51
+ # CMD to start the Gunicorn server
52
+ CMD ["gunicorn", "-w", "1", "-b", "0.0.0.0:7860", "--timeout", "300", "run:app"]