File size: 1,963 Bytes
1f215d0
 
 
769c5ec
 
 
 
1f215d0
 
 
 
 
 
 
 
 
 
 
769c5ec
1f215d0
 
769c5ec
5423de8
769c5ec
aceb02e
5423de8
769c5ec
aceb02e
5423de8
 
 
 
 
 
 
 
769c5ec
5423de8
1f215d0
e0b1399
 
 
1f215d0
b9a16d2
 
 
 
 
 
5423de8
1f215d0
 
5423de8
769c5ec
1f215d0
5423de8
1f215d0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 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"]