# ===== Containerfile for marker-api =====
#
# Uses ROCm PyTorch base for broad GPU support (AMD + NVIDIA via CUDA).
# Ollama defaults to localhost:11434; override with env vars at runtime.

FROM rocm/pytorch:rocm7.2.4_ubuntu24.04_py3.12_pytorch_release_2.9.1

# install system deps & remove system-installed python3-blinker to avoid pip upgrade conflicts
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        curl ca-certificates tini procps git gcc g++ zlib1g-dev libjpeg-dev \
        libpango-1.0-0 libharfbuzz0b libpangoft2-1.0-0 \
        poppler-utils pandoc && \
    apt-get remove -y python3-blinker || true && \
    rm -rf /var/lib/apt/lists/*

# ---- runtime ----
RUN groupadd -r marker && (groupadd -r render || true) && (groupadd -r video || true) && useradd -r -g marker -G render,video marker && \
    mkdir -p /app/conversion_results /app/uploads /app/.cache && \
    chown -R marker:marker /app

WORKDIR /app

# ---- deps ----
# Install marker-pdf from GitHub. For local builds with a cloned marker/
# directory, replace the line below with: -e /app/marker[full]
RUN pip install --no-cache-dir --break-system-packages \
    --extra-index-url https://download.pytorch.org/whl/rocm7.2 \
    "marker-pdf[full] @ git+https://github.com/VikParuchuri/marker.git" && \
    pip install --no-cache-dir --break-system-packages flask gunicorn

# ---- font: download GoNotoCurrent-Regular.ttf at build time ----
RUN FONT_DIR=$(python3 -c "from marker.settings import settings; import os; print(os.path.dirname(settings.FONT_PATH))") && \
    mkdir -p "$FONT_DIR" && \
    curl -sL -o "$FONT_DIR/GoNotoCurrent-Regular.ttf" \
    "https://models.datalab.to/artifacts/GoNotoCurrent-Regular.ttf" && \
    chown -R marker:marker "$FONT_DIR"

# ---- final image ----
COPY app.py           /app/
COPY request_store.py /app/
COPY deepseek_ocr.py  /app/
COPY gunicorn.conf.py /app/
COPY entrypoint.sh    /app/entrypoint.sh

RUN chmod +x /app/entrypoint.sh

ENV HOME=/app
ENV PYTHONUNBUFFERED=1
ENV PYTHONDONTWRITEBYTECODE=1
ENV MARKER_OUTPUT_DIR=/app/conversion_results
ENV UPLOAD_DIR=/app/uploads
ENV RESULTS_DIR=/app/conversion_results
ENV SELF_URL=http://localhost:8001
ENV OLLAMA_HOST=http://localhost:11434
ENV DEESEEK_OCR_MODEL=deepseek-ocr
ENV AMD_COMPUTE=false
ENV TORCH_DEVICE=
ENV MODEL_DTYPE=float32
ENV PORT=8001
ENV HOST=0.0.0.0
ENV LLM_SERVICE=marker.services.ollama.OllamaService
ENV USE_LLM=false
ENV OPENAI_BASE_URL=
ENV OPENAI_API_KEY=
ENV OPENAI_MODEL=
ENV OCR_BACKEND=marker
ENV DEEPSEEK_OLLAMA_HOST=http://localhost:11434
ENV DEEPSEEK_OCR_MODEL=deepseek-ocr:latest
ENV DEEPSEEK_OCR_PROMPT="<|grounding|>Free OCR."
ENV API_KEY=
ENV GUNICORN_WORKERS=3

USER marker

EXPOSE 8001

ENTRYPOINT ["/app/entrypoint.sh"]
CMD ["gunicorn", "-c", "gunicorn.conf.py", "app:app_instance"]
