← Journal
Use KeyBERT to extract key points without reading everything

Use KeyBERT to extract key points without reading everything

A record of running KeyBERT, which extracts keywords from text with an embedding model, while the network remained blocked. Hugging Face libraries tried to connect although the cache held the model. I fixed the problem with the standard environment variables.

Repository covered in this article

MaartenGr/KeyBERT

Minimal keyword extraction with BERT

4.2k Python MIT License

As meeting minutes and technical documents pile up, you lose the time to read each one and note its key points. Full-text search alone cannot tell you what a document is about. KeyBERT uses an embedding model to turn text into vectors, then extracts the keywords and key phrases that best represent it. I installed it and tested keyword extraction with the network disabled.

KeyBERT turns the document and its candidate words and phrases into embedding vectors, then selects the candidates with high cosine similarity as keywords. The project uses the MIT License (*1).

1. Install it

FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'keybert==0.9.0'
COPY warmup_model.py /tmp/warmup_model.py
RUN python /tmp/warmup_model.py
WORKDIR /work
# warmup_model.py (fetch the model during the build)
from keybert import KeyBERT

kw_model = KeyBERT(model="sentence-transformers/all-MiniLM-L6-v2")
kw_model.extract_keywords("warm up the model cache")
print("warmup ok")
podman build -t keybert-sandbox .

2. The blocker: It tries to connect despite a cached model

After I built the image, disabled the network, and ran keyword extraction, the process failed with this error.

'[Errno -3] Temporary failure in name resolution' thrown while requesting HEAD
https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/adapter_config.json
...
RuntimeError: Cannot send a request, as the client has been closed.

The build downloaded the model, but the Hugging Face libraries used inside KeyBERT queried the Hugging Face Hub on every run to check for PEFT-related adapter configuration. The libraries queried the Hub for that check despite a local model cache.

3. Fix: Force offline mode with standard environment variables

Hugging Face provides the official HF_HUB_OFFLINE and TRANSFORMERS_OFFLINE environment variables. Set both to force the libraries to use the local cache for all operations, including this check.

podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
  -e HF_HUB_OFFLINE=1 -e TRANSFORMERS_OFFLINE=1 \
  -v "$(pwd)":/work -w /work keybert-sandbox \
  python keyword_demo.py

-v makes the specified directory readable and writable from the container. Use a separate empty directory for the experiment.

I ran keyword extraction on a short passage about rootless containers and network isolation and got the following output.

0.623  rootless containers
0.455  rootless
0.449  untrusted build
0.418  run untrusted
0.415  root privileges
0.406  compromised dependency
0.389  containers
0.389  runaway malicious

Keyword extraction returned the passage's central topics, rootless containers, untrusted builds, root privileges, and compromised dependencies, with scores. You could get a sense of the content without reading the full text.

In the earlier argos-translate article, I wrapped a library-internal call in application code and fixed it. Both cases involved a library trying to connect despite a populated cache. For KeyBERT, I fixed the problem by setting Hugging Face's official HF_HUB_OFFLINE environment variable. I spent less effort because the library provides an official offline mode.

Screen a large document collection before you read it

The more documents you have, the less time you can spend reading each one. Run keyword extraction before reading, and you can decide which documents to read and which to defer without reading their contents. You can leave that read-or-defer decision to preprocessing.

Sources

*1 KeyBERT (GitHub), https://github.com/MaartenGr/KeyBERT, MIT License