File size: 2,250 Bytes
60c56d7
 
 
 
 
 
 
8294547
60c56d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7396a26
 
60c56d7
 
 
4cd67d1
e314d38
680dd7d
bdef219
76c83b6
bdef219
76c83b6
 
 
60c56d7
 
 
 
 
 
 
 
8fa7f23
 
6108abf
 
 
 
 
 
60c56d7
 
 
 
 
 
 
 
 
779884f
 
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
65
66
67
FROM python:3.10-slim

# Set working directory
WORKDIR /app

# Install system dependencies
RUN apt-get update && apt-get install -y \
    libgl1 \
    libglib2.0-0 \
    libsm6 \
    libxext6 \
    libxrender-dev \
    libgomp1 \
    git \
    && rm -rf /var/lib/apt/lists/*

# Copy requirements first for better caching
COPY requirements.txt .

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

# Copy application code
COPY . .

# Create directories for uploads and results
# Note: /data is already writable in Hugging Face Spaces, no chmod needed
RUN mkdir -p /data/uploads /data/results

# Handle Firebase credentials from environment variable (for Hugging Face Spaces secrets)
# This allows the credentials to be passed as a secret and written to file at runtime
# Note: Python code will create /data/hf_cache with proper permissions
# Make Firebase credentials writing non-fatal (use || true to continue on error)
RUN echo '#!/bin/sh' > /entrypoint.sh && \
    echo 'mkdir -p /tmp/colorize_uploads /tmp/colorize_results /tmp/hf_cache || true' >> /entrypoint.sh && \
    echo 'if [ -n "$FIREBASE_CREDENTIALS" ]; then' >> /entrypoint.sh && \
    echo '  printf "%s" "$FIREBASE_CREDENTIALS" > /tmp/firebase-adminsdk.json 2>/dev/null || true' >> /entrypoint.sh && \
    echo 'fi' >> /entrypoint.sh && \
    echo 'exec "$@"' >> /entrypoint.sh && \
    chmod +x /entrypoint.sh

# Expose port (Hugging Face Spaces uses port 7860)
EXPOSE 7860

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV BASE_URL=${SPACE_HOST}
ENV PORT=7860
ENV DATA_DIR=/data
ENV OMP_NUM_THREADS=1
ENV HF_HOME=/tmp/hf_cache
ENV TRANSFORMERS_CACHE=/tmp/hf_cache
ENV HUGGINGFACE_HUB_CACHE=/tmp/hf_cache
ENV HF_HUB_CACHE=/tmp/hf_cache
ENV XDG_CACHE_HOME=/tmp/hf_cache
ENV MPLCONFIGDIR=/tmp/matplotlib_config

# Health check
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \
    CMD python -c "import requests; requests.get('http://localhost:7860/health', timeout=5)" || exit 1

# Set entrypoint
ENTRYPOINT ["/entrypoint.sh"]

# Run the application (port will be set via environment variable)
# Use SDXL version for text-guided colorization
CMD ["sh", "-c", "uvicorn app.main_sdxl:app --host 0.0.0.0 --port ${PORT:-7860}"]