Files
baserow/embeddings/_download_model.py
Davide SilvestriandBram Wiepjes 7c3525dc1a chore(embeddings): update the embeddings server and release it as 1.1.0 (#6300)
* chore(embeddings): replace transformers with tokenizers and drop pip from the image

* chore(embeddings): fix the dev health check and docs, add a changelog entry

* chore(embeddings): port the dependency and threading updates from #6379

Brings over the changes from #6379 that this branch didn't have yet:

- onnxruntime 1.26.0, tokenizers 0.23.3, starlette 1.8.0 and uvicorn 0.54.0,
  plus huggingface_hub 2.2.0 and transformers 5.19.0 in the build stage.
- The model download is pinned to its current revision.
- Inference uses as many threads as the container's CPU limit, which
  EMBEDDINGS_NUM_THREADS overrides.
- Inference runs in a worker thread behind a lock, so /health answers during
  long batches.
- An empty texts list returns 400 instead of 500.
- Helm, its README, Cloudron and the README point to baserow/embeddings:1.1.0.

Co-authored-by: Bram Wiepjes <bramw@protonmail.com>

* chore(embeddings): address review feedback

- The build stage writes tokenizer.json with the 512-token truncation and
  per-batch padding, so app.py only loads it. The build stage uses the
  tokenizers library for this and no longer installs transformers.
- tokenizers is installed with --no-deps. Its only dependency,
  huggingface-hub, is used by from_pretrained, which the app never calls.
- The README's local run installs the image's packages, downloads the model
  with _download_model.py into EMBEDDINGS_MODEL_DIR (default /model), and
  its snippet posts texts instead of text.

* chore(embeddings): fix the issues found while verifying the image

- requirements.txt pins all 13 runtime packages and is installed with
  --no-deps, so rebuilds are reproducible. The builder no longer installs
  git, and the build fails if the app can't be imported.
- Requests are processed 8 texts at a time and limited to 256 texts and a
  1 MiB body, so a single request can't OOM-kill the container anymore.
- Inference holds a lock in its worker thread, and a queued request whose
  client has gone is skipped.
- Malformed input, including lone surrogates, returns 400 instead of 500.
- The app refuses to start with a tokenizer.json that doesn't truncate at
  512 tokens, or with EMBEDDINGS_NUM_THREADS below 1.
- _download_model.py can be re-run.
- The README, docs, Cloudron manifest and changelog match the code, and the
  Cloudron manifest gets a 1.5 GiB memory limit and the /health check.

* chore(embeddings): correct the response size per text in a comment

---------

Co-authored-by: Bram Wiepjes <bramw@protonmail.com>
2026-10-15 14:18:02 +02:00

45 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""Download the ONNX embedding model and configure its tokenizer."""
import json
import os
import shutil
from huggingface_hub import snapshot_download
from tokenizers import Tokenizer
MODEL_ID = "optimum/all-MiniLM-L6-v2"
# Pinned so rebuilding the image can never change the stored vectors.
MODEL_REVISION = "10244843eba3d9e479b27a4b81c94b56d8e9f4f2"
OUTPUT_DIR = os.environ.get("EMBEDDINGS_MODEL_DIR", "/model")
print(f"Downloading {MODEL_ID} from Hugging Face...")
local_dir = snapshot_download(
repo_id=MODEL_ID,
revision=MODEL_REVISION,
allow_patterns=["*.onnx", "*.json", "*.txt"],
)
print("Copying ONNX model files...")
os.makedirs(OUTPUT_DIR, exist_ok=True)
for item in os.listdir(local_dir):
src = os.path.join(local_dir, item)
dst = os.path.join(OUTPUT_DIR, item)
if os.path.isfile(src):
# The cache's files are read-only, and copying that mode breaks re-runs.
shutil.copyfile(src, dst)
print("Configuring the tokenizer...")
with open(os.path.join(local_dir, "tokenizer_config.json")) as f:
tokenizer_config = json.load(f)
tokenizer = Tokenizer.from_file(os.path.join(local_dir, "tokenizer.json"))
# Override the file's 128-token settings to match how stored vectors were made.
tokenizer.enable_truncation(max_length=tokenizer_config["model_max_length"])
tokenizer.enable_padding(
pad_id=tokenizer.token_to_id(tokenizer_config["pad_token"]),
pad_token=tokenizer_config["pad_token"],
)
tokenizer.save(os.path.join(OUTPUT_DIR, "tokenizer.json"))
print("Done! ONNX model is ready at", OUTPUT_DIR)