File size: 2,033 Bytes
58f07b6
 
7e5ed44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58f07b6
7e5ed44
 
 
58f07b6
 
 
7e5ed44
 
 
 
 
 
 
 
 
 
58f07b6
7e5ed44
 
 
 
 
 
 
 
58f07b6
 
 
7e5ed44
 
 
58f07b6
 
 
 
7e5ed44
 
 
 
58f07b6
 
7e5ed44
58f07b6
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
# Base Image - use slim-bullseye for smaller size
FROM python:3.10-slim-bullseye


ENV DEBIAN_FRONTEND=noninteractive \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1


WORKDIR /code

# System Dependencies
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    git \
    curl \
    libopenblas-dev \
    libomp-dev \
    && rm -rf /var/lib/apt/lists/*


# Install dependencies first for better caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Install additional packages for monitoring and rate limiting
RUN pip install --no-cache-dir slowapi cachetools

# Hugging Face + model tools
RUN pip install --no-cache-dir huggingface-hub sentencepiece accelerate fasttext

# Hugging Face cache environment
ENV HF_HOME=/models/huggingface \
    TRANSFORMERS_CACHE=/models/huggingface \
    HUGGINGFACE_HUB_CACHE=/models/huggingface \
    HF_HUB_CACHE=/models/huggingface

# Created cache dir and set permissions
RUN mkdir -p /models/huggingface /logs && chmod -R 777 /models/huggingface /logs

# Note: Models are loaded lazily at runtime to reduce startup time and memory usage
# HuggingFace Spaces will cache models automatically
# Pre-downloading is skipped to keep build time and image size smaller

# Copy project files
COPY . .

# Create logs directory for error and usage logs
RUN mkdir -p /code/logs && chmod -R 777 /code/logs

# Expose FastAPI port
EXPOSE 7860

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

# Run FastAPI app with uvicorn (1 worker for CPU, single-threaded for memory efficiency)
# Set environment variables for CPU optimization
ENV OMP_NUM_THREADS=4 \
    MKL_NUM_THREADS=4 \
    NUMEXPR_NUM_THREADS=4 \
    PYTHONPATH=/code

CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860", "--workers", "1", "--timeout-keep-alive", "30", "--log-level", "info"]