File size: 2,412 Bytes
8a233a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0b27eb
 
8a233a9
 
 
 
 
 
 
e0b27eb
8a233a9
 
 
 
 
 
 
 
e0b27eb
 
 
8a233a9
e0b27eb
 
8a233a9
 
 
 
 
 
 
 
 
 
 
 
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 an official Python runtime as a parent image
FROM python:3.10-slim

# Set environment variables to prevent interactive prompts during apt-get install
ENV DEBIAN_FRONTEND=noninteractive

# Install system dependencies
# - Essential for Pillow/OpenCV: libgl1-mesa-glx, libglib2.0-0
# - Font handling: fontconfig
# - Installs Microsoft Core Fonts (including Arial): ttf-mscorefonts-installer
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
    libgl1-mesa-glx \
    libglib2.0-0 \
    fontconfig \
    # Accept the EULA for ttf-mscorefonts-installer non-interactively
    && echo "ttf-mscorefonts-installer msttcorefonts/accepted-mscorefonts-eula select true" | debconf-set-selections \
    && apt-get install -y --no-install-recommends ttf-mscorefonts-installer \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* \
    # Update font cache to make installed fonts available
    && fc-cache -f -v

# Set the working directory in the container
WORKDIR /app

# Create a non-root user and group for better security
# Using user ID 1000 as is common in HF Spaces
RUN groupadd -g 1000 appuser && useradd --no-log-init -u 1000 -g appuser appuser

# Create necessary application directories before copying and set ownership
# Ensures the user 'appuser' can write to generated_images
RUN mkdir -p /app/templates /app/generated_images && \
    chown -R appuser:appuser /app

# Copy the requirements file first for layer caching
# Ensure ownership is set correctly during copy
COPY --chown=appuser:appuser requirements.txt .

# Install Python dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy the rest of the application files into the container
# Ensure 'app.py', 'arial.ttf', and the 'templates' directory exist in your repo root
COPY --chown=appuser:appuser app.py .

# ---- IMPORTANT ----
# The following line requires 'arial.ttf' to be present in the root of your repository
COPY --chown=appuser:appuser arial.ttf .
# ---- IMPORTANT ----

# Copy the entire templates directory
COPY --chown=appuser:appuser templates ./templates

# Switch to the non-root user
USER appuser

# Define environment variable placeholder (Hugging Face Secrets will override this)
# You MUST set TELEGRAM_TOKEN in your Space's secrets settings.
ENV TELEGRAM_TOKEN=""

# Command to run the application when the container launches
CMD ["python", "app.py"]