File size: 1,782 Bytes
af35098
4f13c94
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
# 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 \
    libglib2.0-0 \
    libgomp1 \
    sed \
    && 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/

# 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"]