# Use official python slim image FROM python:3.10-slim # Set environment variables early ENV PADDLEOCR_HOME=/app/paddleocr_models \ MPLCONFIGDIR=/app/tmp/mpl_config WORKDIR /app # Install system dependencies RUN apt-get update && apt-get install -y \ build-essential \ libgl1-mesa-glx \ libglib2.0-0 \ libsm6 \ libxext6 \ libxrender-dev \ ffmpeg \ wget \ && rm -rf /var/lib/apt/lists/* # Create necessary folders RUN mkdir -p /app/uploads /app/paddleocr_models/det /app/paddleocr_models/rec /app/paddleocr_models/cls /app/tmp/mpl_config # Download PaddleOCR models manually RUN wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar -O det.tar \ && wget https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar -O rec.tar \ && wget https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar -O cls.tar \ && tar -xf det.tar -C /app/paddleocr_models/det --strip-components=1 \ && tar -xf rec.tar -C /app/paddleocr_models/rec --strip-components=1 \ && tar -xf cls.tar -C /app/paddleocr_models/cls --strip-components=1 \ && rm det.tar rec.tar cls.tar # Fix permissions to allow app user to write RUN chown -R 1000:1000 /app/uploads /app/paddleocr_models /app/tmp/mpl_config \ && chmod -R 755 /app/uploads /app/paddleocr_models /app/tmp/mpl_config # Copy requirements and install Python dependencies COPY requirements.txt . RUN pip install --upgrade pip && \ pip install --no-cache-dir --no-user -r requirements.txt # Switch to non-root user (recommended) USER 1000 # Set MPLCONFIGDIR for matplotlib cache to avoid permission errors ENV MPLCONFIGDIR=/app/tmp/mpl_config # Copy FastAPI app source code COPY app.py . # Expose FastAPI port (usually 7860 for Hugging Face spaces) EXPOSE 7860 # Run the FastAPI app with uvicorn, binding to all interfaces CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]