# Use Python 3.10 slim image FROM python:3.10-slim # Set working directory WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ gcc \ g++ \ curl \ && rm -rf /var/lib/apt/lists/* # Copy requirements first for better caching COPY requirements.txt . # Install Python dependencies RUN pip install --no-cache-dir -r requirements.txt # Create non-root user RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app # Copy application files COPY . . # Create necessary directories with proper permissions RUN mkdir -p templates property_db /tmp/hf_cache /tmp/chromadb_properties RUN mkdir -p /tmp/hf_cache/transformers /tmp/hf_cache/datasets /tmp/hf_cache/hub /tmp/hf_cache/chroma /tmp/hf_cache/onnx /tmp/hf_cache/sentence_transformers RUN chmod -R 755 /tmp/hf_cache /tmp/chromadb_properties RUN chown -R appuser:appuser /tmp/hf_cache /tmp/chromadb_properties # Set environment variables ENV PYTHONPATH=/app ENV FLASK_APP=app.py ENV PORT=7860 ENV HF_HOME=/tmp/hf_cache ENV TRANSFORMERS_CACHE=/tmp/hf_cache/transformers ENV HF_DATASETS_CACHE=/tmp/hf_cache/datasets ENV HF_HUB_CACHE=/tmp/hf_cache/hub ENV CHROMA_CACHE_DIR=/tmp/hf_cache/chroma ENV ONNXRUNTIME_CACHE_DIR=/tmp/hf_cache/onnx ENV SENTENCE_TRANSFORMERS_HOME=/tmp/hf_cache/sentence_transformers ENV CHROMADB_PERSIST_DIRECTORY=/tmp/chromadb_properties ENV CHROMADB_IS_PERSISTENT=true # Expose port (Hugging Face Spaces uses 7860) EXPOSE 7860 # Switch to non-root user USER appuser # Health check HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \ CMD curl -f http://localhost:7860/api/health || exit 1 # Run the application CMD ["python", "app.py"]