KillerKing93/Transformers-InferenceServer-OpenAPI
0
1# Use Python 3.12 slim for smaller image2FROM python:3.12-slim3 4# Install system deps for image/video processing and HF5RUN apt-get update && apt-get install -y --no-install-recommends \6 git \7 curl \8 libglib2.0-0 \9 libgomp1 \10 && apt-get clean \11 && rm -rf /var/lib/apt/lists/*12 13# Set working directory14WORKDIR /app15 16# Copy requirements first for better caching17COPY requirements.txt .18 19# Backend selector: cpu | nvidia | amd20ARG BACKEND=cpu21# Pin torch versions per backend index22# - CPU index publishes newer (2.9.0 ok)23# - CUDA cu124 index publishes up to 2.6.0 (auto-resolves to +cu124)24# - ROCm 6.2 index publishes up to 2.5.1+rocm6.2 (must include local tag)25ARG TORCH_VER_CPU=2.9.026ARG TORCHVISION_VER_CPU=0.24.027ARG TORCH_VER_NVIDIA=2.6.028ARG TORCH_VER_AMD=2.5.1+rocm6.229 30# Control whether to bake the model into the image (1) or skip and download at runtime (0)31ARG BAKE_MODEL=032 33ENV BACKEND=${BACKEND}34ENV BAKE_MODEL=${BAKE_MODEL}35ENV PIP_NO_CACHE_DIR=136 37# Install appropriate PyTorch for the selected backend, then the rest38RUN if [ "$BACKEND" = "cpu" ]; then \39 pip install --no-cache-dir --index-url https://download.pytorch.org/whl/cpu torch==${TORCH_VER_CPU} torchvision==${TORCHVISION_VER_CPU}; \40 elif [ "$BACKEND" = "nvidia" ]; then \41 pip install --no-cache-dir --index-url https://download.pytorch.org/whl/cu124 torch==${TORCH_VER_NVIDIA}; \42 elif [ "$BACKEND" = "amd" ]; then \43 pip install --no-cache-dir --index-url https://download.pytorch.org/whl/rocm6.2 "torch==${TORCH_VER_AMD}"; \44 else \45 echo "Unsupported BACKEND: $BACKEND" && exit 1; \46 fi && \47 pip install --no-cache-dir -r requirements.txt48 49# Copy source code50COPY main.py .51# Include client UI so root (/) can serve web/index.html52COPY web/ web/53COPY tests/ tests/54 55# Copy env template (users can override with volume or env)56COPY .env.example .env57 58# HF cache and optional model bake-in (skippable for huge GPU builds to avoid runner disk exhaustion)59ENV HF_HOME=/app/hf-cache60ENV TRANSFORMERS_CACHE=/app/hf-cache61RUN mkdir -p /app/hf-cache && \62 if [ "$BAKE_MODEL" = "1" ]; then \63 python -c "import os; from huggingface_hub import snapshot_download; repo_id='unsloth/Qwen3-4B-Instruct-2507'; token=os.getenv('HF_TOKEN'); print(f'Downloading {repo_id}...'); snapshot_download(repo_id, token=token, local_dir='/app/hf-cache/unsloth_Qwen3-4B-Instruct-2507', local_dir_use_symlinks=False); print('Model downloaded.');"; \64 else \65 echo 'Skipping model bake-in (BAKE_MODEL=0). The server will prefetch to /app/hf-cache at startup.'; \66 fi67 68# Expose port69EXPOSE 300070 71# Health check72HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \73 CMD curl -f http://localhost:3000/health || exit 174 75# Run the server76CMD ["python", "main.py"]