visionattend-api / Dockerfile
Shevilll's picture
feat: implement robust FaceNet weights download from Hugging Face to bypass unreliable DeepFace upstream sources
1e6e2fc
# Use the official Python 3.10 image as a base
FROM python:3.10-slim
# Install necessary system dependencies for OpenCV and other heavy ML tools
# DeepFace and OpenCV require libgl1 and libglib2.0
RUN apt-get update && apt-get install -y \
libgl1 \
libglib2.0-0 \
&& rm -rf /var/lib/apt/lists/*
# Set the working directory to /app
WORKDIR /app
# Copy requirement files first to cache dependencies correctly
COPY requirements.txt .
# Install dependencies using pip
RUN pip install --no-cache-dir -r requirements.txt
# Create a non-root user required by Hugging Face Spaces environment
# Since HF Spaces run on port 7860 by default under non-root
RUN useradd -m -u 1000 user
USER user
ENV HOME=/home/user \
PATH=/home/user/.local/bin:$PATH
WORKDIR $HOME/app
# Bake the FaceNet weights into the image so runtime requests do not depend on
# DeepFace's broken GitHub release URL during container startup.
RUN python - <<'PY'
import os
import tempfile
import urllib.request
weights_dir = os.path.join(os.environ["HOME"], ".deepface", "weights")
weights_path = os.path.join(weights_dir, "facenet_weights.h5")
weights_url = "https://huggingface.co/junjiang/GestureFace/resolve/main/facenet_weights.h5"
min_bytes = 80 * 1024 * 1024
os.makedirs(weights_dir, exist_ok=True)
with tempfile.NamedTemporaryFile(delete=False, dir=weights_dir, suffix=".tmp") as temp_file:
temp_path = temp_file.name
try:
with urllib.request.urlopen(weights_url, timeout=120) as response, open(temp_path, "wb") as output_file:
while True:
chunk = response.read(1024 * 1024)
if not chunk:
break
output_file.write(chunk)
if os.path.getsize(temp_path) < min_bytes:
raise RuntimeError("Downloaded FaceNet weights file is incomplete.")
os.replace(temp_path, weights_path)
finally:
if os.path.exists(temp_path):
os.remove(temp_path)
PY
RUN python -c "from deepface import DeepFace; DeepFace.build_model('Facenet')"
# Copy the rest of the application files in the directory
COPY --chown=user . $HOME/app
# Hugging Face Spaces expose port 7860 by default
EXPOSE 7860
# Run uvicorn on port 7860, bound to 0.0.0.0
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]