|
2 | 2 | # Download the PIGuard ONNX model and tokenizer into docker/piguard/models/. |
3 | 3 | # This is a one-time step required before the PIGuard sidecar can start in |
4 | 4 | # Docker Compose. The exported model is ~735 MB. |
| 5 | +# |
| 6 | +# The export script in this directory is derived from the go-prompt-injection-guard |
| 7 | +# export script with trust_remote_code=True added so it can run non-interactively. |
5 | 8 |
|
6 | 9 | set -euo pipefail |
7 | 10 |
|
8 | 11 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
9 | 12 | MODEL_DIR="${SCRIPT_DIR}/models" |
| 13 | +EXPORT_SCRIPT="${SCRIPT_DIR}/export_onnx.py" |
10 | 14 | mkdir -p "${MODEL_DIR}" |
11 | 15 |
|
12 | 16 | if [ -f "${MODEL_DIR}/model.onnx" ] && [ -f "${MODEL_DIR}/tokenizer.json" ]; then |
13 | 17 | echo "PIGuard model already present in ${MODEL_DIR}; nothing to do." |
14 | 18 | exit 0 |
15 | 19 | fi |
16 | 20 |
|
17 | | -PIGUARD_REF="${PIGUARD_REF:-v1.0.0}" |
18 | | - |
19 | | -echo "Downloading and exporting PIGuard model (${PIGUARD_REF})..." |
20 | | -echo "Output directory: ${MODEL_DIR}" |
| 21 | +echo "Downloading and exporting PIGuard model into ${MODEL_DIR}..." |
21 | 22 |
|
22 | 23 | # Run the export in a disposable Python container so no host Python/toolchain is |
23 | | -# required. The guard repo's export_onnx.py downloads the model from HuggingFace |
24 | | -# and converts it to ONNX. |
| 24 | +# required. The export script downloads the model from HuggingFace and converts |
| 25 | +# it to ONNX. |
25 | 26 | # |
26 | 27 | # DEBIAN_FRONTEND=noninteractive keeps apt-get quiet on headless hosts. |
27 | | -# GIT_CONFIG_GLOBAL turns off the detached-HEAD advice and clone progress spam. |
28 | | -# HF_TRUST_REMOTE_CODE=1 allows the PIGuard model's custom modeling code to run |
29 | | -# without an interactive prompt. Pass through HF_TOKEN if set on the host. |
| 28 | +# HF_TRUST_REMOTE_CODE=1 is belt-and-suspenders. Pass HF_TOKEN through if set |
| 29 | +# on the host for higher HF rate limits. |
30 | 30 | DOCKER_ENVS=( |
31 | 31 | -e HF_HOME=/tmp |
32 | 32 | -e DEBIAN_FRONTEND=noninteractive |
33 | | - -e GIT_CONFIG_GLOBAL=/tmp/.gitconfig |
34 | 33 | -e HF_TRUST_REMOTE_CODE=1 |
35 | 34 | ) |
36 | 35 | if [ -n "${HF_TOKEN:-}" ]; then |
37 | 36 | DOCKER_ENVS+=(-e "HF_TOKEN=${HF_TOKEN}") |
38 | 37 | fi |
39 | 38 |
|
| 39 | +# Pinned versions from go-prompt-injection-guard/scripts/requirements.txt. |
| 40 | +PYTHON_REQS="torch==2.12.0 transformers==5.10.2 onnxscript==0.7.0" |
| 41 | + |
40 | 42 | docker run --rm \ |
41 | 43 | -v "${MODEL_DIR}:/out" \ |
| 44 | + -v "${EXPORT_SCRIPT}:/src/export_onnx.py:ro" \ |
42 | 45 | "${DOCKER_ENVS[@]}" \ |
43 | 46 | python:3.12-slim bash -c " |
44 | 47 | set -euo pipefail |
45 | 48 |
|
46 | | - # Silence git advice / progress noise inside the container. |
47 | | - printf '[advice]\ndetachedHead = false\n' > /tmp/.gitconfig |
48 | | -
|
49 | | - echo '==> Installing git and curl inside container...' |
50 | | - apt-get -qq update |
51 | | - apt-get -qq install -y --no-install-recommends git curl ca-certificates >/dev/null |
52 | | -
|
53 | | - echo '==> Cloning go-prompt-injection-guard (${PIGUARD_REF})...' |
54 | | - git clone --quiet --depth 1 --branch '${PIGUARD_REF}' \ |
55 | | - https://github.com/BackendStack21/go-prompt-injection-guard.git /src |
56 | | -
|
57 | | - cd /src |
58 | | -
|
59 | | - # The PIGuard model uses custom HF modeling code. The upstream export script |
60 | | - # does not pass trust_remote_code=True, so we patch it to avoid the interactive |
61 | | - # y/N prompt that would hang in a non-TTY container. |
62 | | - echo '==> Patching export script to allow custom HF modeling code...' |
63 | | - sed -i 's/revision=MODEL_REVISION)/revision=MODEL_REVISION, trust_remote_code=True)/g' scripts/export_onnx.py |
64 | | -
|
65 | | - echo '==> Installing Python export requirements (this may take a minute)...' |
66 | | - pip install --root-user-action=ignore --disable-pip-version-check --no-cache-dir --quiet -r scripts/requirements.txt |
| 49 | + echo '==> Installing Python export requirements (~2 GB, this may take several minutes)...' |
| 50 | + pip install --root-user-action=ignore --disable-pip-version-check --no-cache-dir --quiet ${PYTHON_REQS} |
67 | 51 |
|
68 | 52 | echo '==> Exporting PIGuard model from HuggingFace to ONNX (~735 MB, be patient)...' |
69 | | - python -u scripts/export_onnx.py |
| 53 | + python -u /src/export_onnx.py |
70 | 54 |
|
71 | 55 | echo '==> Copying exported model to host volume...' |
72 | 56 | cp ~/.cache/piguard/onnx/* /out/ |
|
0 commit comments