File size: 1,263 Bytes
9a5e266
 
ad53b25
 
9a5e266
ad53b25
 
9a5e266
 
 
 
 
 
 
ad53b25
 
 
 
 
 
 
 
9a5e266
 
ad53b25
9a5e266
ad53b25
 
9a5e266
ad53b25
 
9a5e266
ad53b25
 
 
9a5e266
ad53b25
 
9a5e266
 
ad53b25
9a5e266
 
 
 
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
FROM python:3.9

# Workdir for build steps
WORKDIR /code

# Install system dependencies (Run as ROOT)
# This ensures we have permission to install system packages
USER root
RUN apt-get update && apt-get install -y \
    ffmpeg \
    libsm6 \
    libxext6 \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements file
COPY requirements.txt /code/requirements.txt

# Install Python dependencies globally (Run as ROOT)
# Installing as root avoids permission issues with /home/user/.local
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

# Create a non-root user (Required by Hugging Face Spaces)
RUN useradd -m -u 1000 user

# Switch to the non-root user for the application runtime
USER user
ENV HOME=/home/user \
    PATH=/home/user/.local/bin:$PATH

# Set working directory to the user's home/app
WORKDIR $HOME/app

# Copy the application code (Run as USER)
# We change ownership to the user so they can read/write if needed
COPY --chown=user . $HOME/app

# Download models (Run as USER)
# This ensures models are saved to /home/user/.u2net, which the app can read
RUN python download_models.py

# Expose the application port
EXPOSE 7860

# Command to run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]