KillerKing93/Transformers-TextEngine-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 .51COPY auth.py .52COPY models.py .53COPY database.py .54COPY utils.py .55COPY seed_data.py .56COPY image_caption.py .57COPY marketplace_demo.html .58 59# Include client UI so root (/) can serve web/index.html60COPY web/ web/61COPY tests/ tests/62 63# Copy env template (users can override with volume or env)64COPY .env.example .env65 66# HF cache and optional model bake-in (skippable for huge GPU builds to avoid runner disk exhaustion)67ENV HF_HOME=/app/hf-cache68ENV TRANSFORMERS_CACHE=/app/hf-cache69RUN mkdir -p /app/hf-cache && \70 if [ "$BAKE_MODEL" = "1" ]; then \71 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.');"; \72 else \73 echo 'Skipping model bake-in (BAKE_MODEL=0). The server will prefetch to /app/hf-cache at startup.'; \74 fi75 76# Expose port77EXPOSE 300078 79# Health check80HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \81 CMD curl -f http://localhost:3000/health || exit 182 83# Run the server84CMD ["python", "main.py"]