# ===== containerfile (podman & docker) for marker-api on powermac  =====
#
# Targets 2x AMD Vega 20 (GFX906) with ROCm using gfx906-patched PyTorch.
# deepseek-ocr and GPT services are on the same host at :8082 / :8080.

FROM mixa3607/pytorch-gfx906:v2.7.1-rocm-6.3.3

# 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/

# We use --break-system-packages and make sure we do NOT use --ignore-installed so that pre-installed
# gfx906 patched torch, torchvision, and torchaudio are preserved and NOT overwritten by PyPI releases.
RUN pip install --no-cache-dir --break-system-packages -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:8082 \
    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:8082/v1 \
    OPENAI_MODEL= \
    GUNICORN_WORKERS=1

USER marker

EXPOSE 8000

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