File size: 1,752 Bytes
75e2b6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
# Use Python 3.12 as the base image
FROM python:3.12-slim

# Set working directory in the container
WORKDIR /app

# Install system dependencies including Tesseract OCR
RUN apt-get update && apt-get install -y --no-install-recommends \
    gcc \
    python3-dev \
    tesseract-ocr \
    libtesseract-dev \
    tesseract-ocr-eng \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

# Create a non-root user and set ownership
RUN useradd -m -u 1000 appuser && \
    chown -R appuser:appuser /app

# Copy requirements first to leverage Docker cache
COPY pyproject.toml .

# Install Python dependencies
RUN pip install --no-cache-dir --upgrade pip && \
    pip install --no-cache-dir .

# Copy the rest of the application
COPY . .

# Create ALL needed directories with proper permissions
RUN mkdir -p temp uploads \
    /app/.cache \
    /app/nltk_data \
    /app/app/routers/temp \
    /app/app/config/temp && \
    chown -R appuser:appuser /app && \
    chmod -R 777 temp uploads /app/.cache /app/nltk_data /app/app/routers/temp /app/app/config/temp

# Set environment variables for cache directories and Tesseract
ENV HF_HOME=/app/.cache/huggingface \
    TRANSFORMERS_CACHE=/app/.cache/huggingface/transformers \
    PYTORCH_PRETRAINED_BERT_CACHE=/app/.cache/torch \
    NLTK_DATA=/app/nltk_data \
    XDG_CACHE_HOME=/app/.cache \
    TESSDATA_PREFIX=/usr/share/tesseract-ocr/5/tessdata \
    TESSERACT_CMD=/usr/bin/tesseract \
    PATH=/usr/bin:$PATH

# Verify Tesseract installation
RUN tesseract --version

# Switch to non-root user
USER appuser

# Expose the port that Hugging Face Spaces expects
EXPOSE 7860

# Command to run the application using Uvicorn on port 7860
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "7860"]