File size: 1,851 Bytes
5fe71b0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ── SentimentAI β€” Production Dockerfile ──────────────────────────────────────
# Hugging Face Spaces (sdk: docker) β€” Python 3.11 slim image
# Runs FastAPI via Gunicorn + 4 UvicornWorkers for high concurrency

FROM python:3.11-slim

# Metadata
LABEL maintainer="airzipm"
LABEL description="SentimentAI-v2 β€” MuRIL 3-class sentiment analysis API"

# System dependencies needed by PyTorch and HuggingFace tokenizers
RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    curl \
    && rm -rf /var/lib/apt/lists/*

# Working directory β€” all app files live here on HF Spaces
WORKDIR /app

# Install Python dependencies first (Docker layer caching: only re-runs if requirements change)
COPY requirements.txt .
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir -r requirements.txt

# Copy application files
COPY app.py .
# COPY index.html .

# HF Spaces runs as a non-root user; create one for security
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
USER appuser

# HF Spaces always exposes port 7860
EXPOSE 7860

# Environment variables
ENV HOST=0.0.0.0
ENV PORT=7860
ENV PYTHONUNBUFFERED=1      
# PYTHONUNBUFFERED=1 ensures logs are flushed immediately (no buffering)

# Production command:
# - 4 workers Γ— async = ~100+ concurrent connections on CPU
# - UvicornWorker = async ASGI workers (not sync wsgi)
# - timeout=120 allows slow model cold-starts
# - keepalive=5 reuses connections from load balancer
# Production command (Single line to prevent parsing errors)
CMD ["gunicorn", "app:app", "--workers", "4", "--worker-class", "uvicorn.workers.UvicornWorker", "--bind", "0.0.0.0:7860", "--timeout", "120", "--keep-alive", "5", "--access-logfile", "-", "--error-logfile", "-"]