# Dockerfile for Hugging Face Spaces # Based on HF Spaces Docker SDK documentation: https://huggingface.co/docs/hub/spaces-sdks-docker FROM python:3.10-slim # Set working directory WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ gcc \ g++ \ cmake \ libopenblas-dev \ libomp-dev \ curl \ && rm -rf /var/lib/apt/lists/* # Create app user with UID 1000 (fixes getpwuid(): uid not found: 1000 error) # Note: Hugging Face Spaces may run as root, but creating user prevents errors # Use || true to allow graceful failure if user already exists RUN (useradd -u 1000 -m -s /bin/bash appuser 2>/dev/null) || \ (groupadd -g 1000 appuser 2>/dev/null && useradd -u 1000 -g appuser -m -s /bin/bash appuser 2>/dev/null) || \ echo "User creation skipped (may already exist)" # Create cache directories with proper permissions # Hugging Face Spaces runs as root, but we ensure appuser can access RUN mkdir -p /tmp/huggingface_cache && \ chmod 777 /tmp/huggingface_cache && \ mkdir -p /tmp/logs && \ chmod 777 /tmp/logs && \ (chown -R appuser:appuser /tmp/huggingface_cache /tmp/logs 2>/dev/null || true) # Copy requirements file first (for better caching) COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir --upgrade pip && \ pip install --no-cache-dir -r requirements.txt # Copy application code COPY . . # Set ownership of application files (if running as root, this ensures appuser can access) # Use || true to allow graceful failure if chown not needed RUN chown -R appuser:appuser /app 2>/dev/null || true # Expose port 7860 (HF Spaces standard) EXPOSE 7860 # Set environment variables ENV PYTHONUNBUFFERED=1 ENV PORT=7860 # Set OMP_NUM_THREADS to valid integer (not empty string) ENV OMP_NUM_THREADS=4 ENV MKL_NUM_THREADS=4 ENV DB_PATH=/tmp/sessions.db ENV FAISS_INDEX_PATH=/tmp/embeddings.faiss ENV LOG_DIR=/tmp/logs ENV RATE_LIMIT_ENABLED=true # Cache directories - will be used by transformers and huggingface_hub ENV HF_HOME=/tmp/huggingface_cache ENV TRANSFORMERS_CACHE=/tmp/huggingface_cache # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=120s --retries=3 \ CMD curl -f http://localhost:7860/api/health || exit 1 # Run with Gunicorn production WSGI server (replaces Flask dev server) CMD ["gunicorn", "--bind", "0.0.0.0:7860", "--workers", "4", "--threads", "2", "--timeout", "120", "--access-logfile", "-", "--error-logfile", "-", "--log-level", "info", "flask_api_standalone:app"]