File size: 2,234 Bytes
af35098
4f13c94
af35098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d943d7
d96e928
af35098
 
d96e928
 
 
af35098
a787193
af35098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a787193
 
 
 
 
af35098
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Use a stable, official Python base image
FROM python:3.14-slim

# Set environment variables
# PYTHONUNBUFFERED=1 ensures console logs are printed immediately
# PYTHONDONTWRITEBYTECODE=1 prevents python from writing .pyc files
# PORT=7860 is the default port for Hugging Face Spaces
# HOME=/home/user sets the home folder for the non-root user
ENV PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    PORT=7860 \
    HOST=0.0.0.0 \
    HOME=/home/user

# Install system dependencies required by OpenCV, MediaPipe, and other libraries
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    libgl1 \
    libgles2 \
    libegl1 \
    libglib2.0-0 \
    libgomp1 \
    libsm6 \
    libxext6 \
    libxrender1 \
    sed \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user with UID 1000 (Hugging Face Spaces runs as UID 1000)
RUN useradd -m -u 1000 user
WORKDIR /app

# Copy requirements.txt first for build caching
COPY --chown=user:user requirements.txt /app/

# Remove the custom local PyTorch wheels from requirements.txt to install standard stable versions
# from PyPI, supporting both CPU and GPU workloads automatically.
RUN sed -i '/torch==/d' requirements.txt && \
    sed -i '/torchvision==/d' requirements.txt && \
    pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir torch torchvision && \
    pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application files
COPY --chown=user:user . /app/

# Ensure the actual binary model is downloaded, overwriting any Git LFS pointer files
RUN mkdir -p /app/assets && \
    wget -qO /app/assets/blaze_face_short_range.tflite "https://storage.googleapis.com/mediapipe-models/face_detector/blaze_face_short_range/float16/1/blaze_face_short_range.tflite" && \
    chown user:user /app/assets/blaze_face_short_range.tflite

# Setup a writeable Hugging Face cache directory inside the home folder of user 1000
RUN mkdir -p /home/user/.cache/huggingface && chown -R user:user /home/user

# Switch to the non-root user
USER user

# Expose the default port (Hugging Face Spaces automatically forwards traffic to 7860)
EXPOSE 7860

# Run the FastAPI server
CMD ["python", "main.py"]