File size: 3,819 Bytes
c83a172
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ─────────────────────────────────────────────────────────────────────────────
# Dockerfile β€” Groq Audience Proxy (HuggingFace Space)
#
# Build & run locally:
#   docker build -t groq-proxy .
#   docker run -p 7860:7860 -e GROQ_API_KEY=gsk_... groq-proxy
#
# On HuggingFace Spaces:
#   1. Push this file + groq_proxy_service.py to the Space repo root.
#   2. Set GROQ_API_KEY as an HF Secret in the Space settings.
#      (Never hardcode the key β€” the Space injects it at runtime.)
#   3. HF will auto-detect this Dockerfile and build the Space.
#
# The service exposes port 7860 (HuggingFace's standard public port).
# Endpoint: POST /estimate_audience  β€” used by music_chart_server's
#           _AudienceSlotPool._call_hf_space()
# ─────────────────────────────────────────────────────────────────────────────

FROM python:3.11-slim

# ── System deps ───────────────────────────────────────────────────────────────
RUN apt-get update \
    && apt-get install -y --no-install-recommends \
        build-essential \
        curl \
    && rm -rf /var/lib/apt/lists/*

# ── Working directory ─────────────────────────────────────────────────────────
WORKDIR /app

# ── Python dependencies ───────────────────────────────────────────────────────
# Copy requirements first to exploit Docker layer caching
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# ── Application code ──────────────────────────────────────────────────────────
COPY groq_proxy_service.py .

# ── Non-root user (HuggingFace Spaces requirement) ───────────────────────────
RUN useradd -m -u 1000 hfuser
USER hfuser

# ── Runtime configuration ─────────────────────────────────────────────────────
# GROQ_API_KEY   β€” injected as an HF Secret; never set here
# GROQ_MODEL     β€” override to change the Groq model (default: llama-3.1-8b-instant)
# SLOT_INTERVAL  β€” seconds between Groq calls (default: 1.0  β†’  60 RPM)
# MAX_TOKENS     β€” max tokens in Groq response (default: 256)
# PORT           β€” HTTP port (default: 7860)

ENV PORT=7860 \
    SLOT_INTERVAL=1.0 \
    MAX_TOKENS=256 \
    AUDIENCE_GROQ_MODEL=llama-3.1-8b-instant \
    PYTHONUNBUFFERED=1

EXPOSE 7860

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

# ── Entrypoint ────────────────────────────────────────────────────────────────
CMD ["python", "-m", "uvicorn", "groq_proxy_service:app", \
     "--host", "0.0.0.0", "--port", "7860", "--log-level", "info"]