File size: 1,507 Bytes
90260b6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Use Python 3.9 slim image for better performance
FROM python:3.9-slim

# Set working directory
WORKDIR /app

# Set environment variables
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV TRANSFORMERS_CACHE=/app/cache
ENV HF_HOME=/app/cache

# Install system dependencies
RUN apt-get update && apt-get install -y \
    build-essential \
    curl \
    git \
    && rm -rf /var/lib/apt/lists/*

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

# Create a non-root user early
RUN useradd --create-home --shell /bin/bash app

# Install Python dependencies as app user
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Create cache directory and set permissions
RUN mkdir -p /app/cache && \
    chown -R app:app /app

# Copy application code and set ownership
COPY --chown=app:app app.py .

# Switch to non-root user
USER app

# Pre-download models at build time (optional - comment out if you want faster builds)
# RUN python -c "
# from transformers import AutoTokenizer, AutoModel;
# models = ['Lyon28/Albert-Base-V2', 'Lyon28/GPT-2', 'Lyon28/Tinny-Llama'];
# [AutoTokenizer.from_pretrained(m) for m in models[:3]];
# [AutoModel.from_pretrained(m) for m in models[:3]];
# print('Sample models cached')
# "

# Expose port
EXPOSE 7860

# Health check
HEALTHCHECK --interval=30s --timeout=30s --start-period=60s --retries=3 \
    CMD curl -f http://localhost:7860/health || exit 1

# Run the application
CMD ["python", "app.py"]