# ===== containerfile (podman & docker) for marker-api on this host  =====
#
# Targets AMD Radeon 8060S (GFX1151) with ROCm 7.2 native PyTorch 2.9.1.
# Ollama for OCR is on localhost:11435.
# Removed gfx906 base (Vega20) — now uses rocm/pytorch which natively supports gfx1151.

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 && \
    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 ----
COPY marker/ /app/marker/

# rocm/pytorch base already includes ROCm torch 2.9.1.
# Use --extra-index-url so pip prefers ROCm builds if torch gets reinstalled.
RUN pip install --no-cache-dir --break-system-packages \
    --extra-index-url https://download.pytorch.org/whl/rocm7.2 \
    -e "/app/marker[full]" && \
    pip install --no-cache-dir --break-system-packages flask gunicorn

# ---- font: download GoNotoCurrent-Regular.ttf at build time ----
RUN mkdir -p /app/marker/static/fonts && \
    curl -sL -o /app/marker/static/fonts/GoNotoCurrent-Regular.ttf \
    "https://models.datalab.to/artifacts/GoNotoCurrent-Regular.ttf" && \
    chown marker:marker /app/marker/static/fonts/GoNotoCurrent-Regular.ttf

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

RUN chmod +x /app/entrypoint.sh

ENV \
    HOME=/app \
    PYTHONUNBUFFERED=1 \
    PYTHONDONTWRITEBYTECODE=1 \
    MARKER_OUTPUT_DIR=/app/conversion_results \
    UPLOAD_DIR=/app/uploads \
    OLLAMA_HOST=http://localhost:11435 \
    DEESEEK_OCR_MODEL=deepseek-ocr \
    AMD_COMPUTE=true \
    TORCH_DEVICE= \
    MODEL_DTYPE=float32 \
    PORT=8000 \
    HOST=0.0.0.0 \
    LLM_SERVICE=marker.services.openai.OpenAIService \
    USE_LLM=false \
    OPENAI_BASE_URL=http://localhost:11435/v1 \
    OPENAI_MODEL= \
    GUNICORN_WORKERS=1

USER marker

EXPOSE 8000

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