Spaces:
Sleeping
Sleeping
| # ============================================================ | |
| # 🍽️ Trainera Food Recognition API | |
| # Production-Ready Multilingual AI Food Recognition | |
| # ============================================================ | |
| FROM python:3.11-slim | |
| # Metadata | |
| LABEL maintainer="Trainera Team" | |
| LABEL description="AI Food Recognition API with OpenAI translations (101+ food categories)" | |
| LABEL version="2.0.0" | |
| # Create non-root user for security (HF Spaces requirement) | |
| RUN useradd -m -u 1000 user | |
| # Set working directory | |
| WORKDIR /app | |
| # Install system dependencies for ML and image processing | |
| RUN apt-get update && apt-get install -y --no-install-recommends \ | |
| gcc \ | |
| g++ \ | |
| libglib2.0-0 \ | |
| libsm6 \ | |
| libxext6 \ | |
| libxrender-dev \ | |
| libgomp1 \ | |
| libgl1-mesa-dev \ | |
| libglib2.0-dev \ | |
| curl \ | |
| && rm -rf /var/lib/apt/lists/* \ | |
| && apt-get clean | |
| # Copy requirements first (Docker layer caching optimization) | |
| COPY --chown=user:user requirements.txt . | |
| # Install Python dependencies | |
| # Step 1: Install NumPy 1.x first (transformers compatibility) | |
| RUN pip install --no-cache-dir --upgrade pip && \ | |
| pip install --no-cache-dir "numpy>=1.24.0,<2.0.0" | |
| # Step 2: Install remaining dependencies | |
| RUN pip install --no-cache-dir -r requirements.txt | |
| # Copy application code | |
| COPY --chown=user:user app.py . | |
| # Create cache directories with correct permissions | |
| RUN mkdir -p \ | |
| /home/user/.cache \ | |
| /tmp/transformers \ | |
| /tmp/huggingface \ | |
| /tmp/torch \ | |
| && chown -R user:user /home/user/.cache /tmp/transformers /tmp/huggingface /tmp/torch | |
| # Switch to non-root user (security best practice) | |
| USER user | |
| # Environment Variables | |
| # ============================================================ | |
| # Python configuration | |
| ENV PYTHONUNBUFFERED=1 | |
| ENV PYTHONDONTWRITEBYTECODE=1 | |
| # Port configuration (7860 = HF Spaces standard) | |
| ENV PORT=7860 | |
| # User home | |
| ENV HOME=/home/user | |
| # Hugging Face cache directories | |
| ENV HF_HOME=/tmp/huggingface | |
| ENV TRANSFORMERS_CACHE=/tmp/transformers | |
| ENV XDG_CACHE_HOME=/tmp | |
| ENV TORCH_HOME=/tmp/torch | |
| ENV HF_HUB_DISABLE_TELEMETRY=1 | |
| ENV HF_HUB_ENABLE_HF_TRANSFER=0 | |
| # OpenAI API Key (set via HF Spaces secrets or docker run -e) | |
| ENV OPENAI_API_KEY=${OPENAI_API_KEY:-} | |
| # USDA API Keys (optional - defaults to DEMO_KEY) | |
| ENV USDA_API_KEY=${USDA_API_KEY:-DEMO_KEY} | |
| # Performance optimizations | |
| ENV TOKENIZERS_PARALLELISM=false | |
| ENV OMP_NUM_THREADS=2 | |
| ENV MKL_NUM_THREADS=2 | |
| # Expose port | |
| EXPOSE 7860 | |
| # Health check (monitors API health every 30s) | |
| HEALTHCHECK --interval=30s --timeout=10s --start-period=90s --retries=3 \ | |
| CMD curl -f http://localhost:7860/health || exit 1 | |
| # Run the application | |
| CMD ["python", "app.py"] | |