# Hugging Face Space adapter for official chenyme/grok2api (thin image mode). # # WHY THIS EXISTS # Upstream publishes a multi-arch image to GHCR on every push to main # (chenyme/grok2api/.github/workflows/ghcr-image.yml). That image is already # compiled, tested (go test / go vet / pnpm lint) and labelled with the exact # upstream revision. This adapter re-bases that artifact and adds the Hugging # Face runtime contract (port 7860, /data persistent storage, config seeding). # # Building the Space therefore takes seconds and cannot break because upstream # changed a build script, bumped Go/Node, or added a file the compiler needs. # That reliability is what makes unattended auto-updates safe. # # PINNING # The auto-update workflow always patches GROK2API_IMAGE below to an immutable # digest (ghcr.io/chenyme/grok2api@sha256:...) and GROK2API_CONFIG_REF to the # matching upstream commit, so every deployed Space is reproducible and can be # rolled back by re-deploying an older .hf-sync record. # # Build mode "source" (./Dockerfile) remains available as a fallback if # upstream ever stops publishing images. ARG GROK2API_IMAGE=ghcr.io/chenyme/grok2api@sha256:d24ca6a7efdbeaf5c7aa98f90973768aac444df1170f221d383ebfe6376f77be FROM ${GROK2API_IMAGE} ARG GROK2API_IMAGE ARG GROK2API_CONFIG_REF=47810328db6dc939efe04f45b627ada61cd7517a ARG GROK2API_VERSION=v3.1.6 ARG GROK2API_ADAPTER_HASH=b6eb9fc32d8a12e551db01a96e1ec0d5850b091ee0ae565fc5f1306f3ecc6cd9 ENV TZ=Asia/Shanghai \ SERVER_PORT=7860 \ DATA_DIR=/data \ GROK2API_CONFIG_SOURCE=/data/config.yaml # The upstream image ships /app/grok2api and /app/frontend/dist but NOT # config.example.yaml, because its own entrypoint expects the operator to mount # a config. start.sh seeds ${DATA_DIR}/config.yaml from that example on first # boot, so fetch it from the exact upstream revision this image was built from. # Also assert the layout we depend on, so a future upstream restructure fails # the build loudly instead of deploying a broken Space. RUN set -eux; \ command -v wget >/dev/null; \ test -x /app/grok2api; \ test -d /app/frontend/dist; \ url="https://raw.githubusercontent.com/chenyme/grok2api/${GROK2API_CONFIG_REF}/config.example.yaml"; \ ok=0; \ for attempt in 1 2 3; do \ if wget -qO /app/config.example.yaml "$url"; then ok=1; break; fi; \ echo "config.example.yaml fetch attempt ${attempt} failed" >&2; \ sleep 3; \ done; \ test "$ok" = 1; \ test -s /app/config.example.yaml # sqlite3 CLI powers the boot-time database guard (sqlite-preflight.sh), which # verifies /data/backend.db and repairs it if a rebuild left it malformed. RUN set -eux; \ ok=0; \ for attempt in 1 2 3; do \ if apk add --no-cache sqlite; then ok=1; break; fi; \ echo "apk add sqlite attempt ${attempt} failed" >&2; \ sleep 5; \ done; \ test "$ok" = 1; \ sqlite3 --version LABEL org.opencontainers.image.source="https://github.com/chenyme/grok2api" \ io.viciy.adapter="huggingface/Dockerfile.image" \ io.viciy.upstream.ref="${GROK2API_CONFIG_REF}" \ io.viciy.upstream.image="${GROK2API_IMAGE}" # Deployment marker served by the app itself (upstream's frontend handler serves # any existing file under frontend/dist). The deploy workflow polls this to # prove which upstream revision the *running container* actually is, instead of # trusting Hugging Face's build-stage API, which can report RUNNING on the old # container for a short window after a push. # # "adapter" is the fingerprint of every adapter file, so a change that only # touches the adapter (and leaves the upstream revision alone) still changes the # marker and therefore still forces verification to wait for the rebuild. RUN set -eux; \ printf 'revision=%s\nversion=%s\nimage=%s\nbuild_mode=image\nadapter=%s\n' \ "${GROK2API_CONFIG_REF}" "${GROK2API_VERSION}" "${GROK2API_IMAGE}" "${GROK2API_ADAPTER_HASH}" \ > /app/frontend/dist/__deployed-revision.txt; \ cat /app/frontend/dist/__deployed-revision.txt COPY --chmod=0755 start.sh /usr/local/bin/hf-start.sh COPY --chmod=0644 sqlite-preflight.sh /usr/local/bin/sqlite-preflight.sh EXPOSE 7860 HEALTHCHECK --interval=30s --timeout=5s --start-period=30s --retries=3 \ CMD wget -qO- "http://127.0.0.1:${SERVER_PORT}/healthz" >/dev/null || exit 1 ENTRYPOINT ["/usr/local/bin/hf-start.sh"] CMD ["/app/grok2api", "--config", "/app/config.yaml", "--listen", "0.0.0.0:7860"]