# Dockerfile for Hugging Face Spaces with FastAPI + Gradio # Use an official Python runtime as a parent image FROM python:3.9-slim # Set the working directory inside the container WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ gcc \ g++ \ curl \ && rm -rf /var/lib/apt/lists/* # Copy the requirements file and install dependencies first for better caching COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # Set up cache directory with proper permissions RUN mkdir -p /app/.cache && chmod 777 /app/.cache ENV HF_HOME=/app/.cache ENV TRANSFORMERS_CACHE=/app/.cache ENV HF_HUB_CACHE=/app/.cache # Copy all your application code into the container COPY . . # IMPORTANT: Download and cache the models during the build process. # This makes the application start much faster when the Space wakes up. RUN python -c "import os; os.environ['HF_HOME']='/app/.cache'; from classifier import classify_with_model; classify_with_model('test')" RUN python -c "import os; os.environ['HF_HOME']='/app/.cache'; from ner import get_ner_pipeline; get_ner_pipeline()" RUN python -c "import os; os.environ['HF_HOME']='/app/.cache'; from sentiment import get_emotion_classifier; get_emotion_classifier()" RUN python -c "import os; os.environ['HF_HOME']='/app/.cache'; from translate import get_translator; get_translator()" # Create startup script with model pre-loading RUN echo '#!/bin/bash\n\ export HF_HOME=/app/.cache\n\ export TRANSFORMERS_CACHE=/app/.cache\n\ export HF_HUB_CACHE=/app/.cache\n\ echo "🚀 Starting FastAPI server on port 8000..."\n\ python -m uvicorn api:app --host 0.0.0.0 --port 8000 &\n\ echo "⏳ Waiting for FastAPI to start..."\n\ sleep 10\n\ echo "🔥 Warming up models..."\n\ curl -X POST http://localhost:8000/warmup || echo "Warmup failed, continuing..."\n\ echo "🌊 Starting Gradio web interface on port 7860..."\n\ python app.py' > start_services.sh RUN chmod +x start_services.sh # Expose ports for both services EXPOSE 7860 EXPOSE 8000 # Health check HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=3 \ CMD curl -f http://localhost:8000/health || exit 1 # Start both services CMD ["./start_services.sh"]