MIMEDefang ML model servers

This directory has two small Python HTTP servers. Each keeps a machine-learning model loaded in memory and answers classification requests from Mail::MIMEDefang::ML:

| Server | Model | Mail::MIMEDefang::ML backend | Default address | |------------------------------|--------------------------------------------------------------|--------------------------------|--------------------| | mimedefang-laya-server | Laya decision model | laya | 127.0.0.1:8687 | | mimedefang-gliclass-server | GLiClass zero-shot classifier | gliclass | 127.0.0.1:8688 |

Both models are encoder classifiers. They answer in a single forward pass, which is fast enough to check every message. You only need the server for the backend you use. The third backend, openai, talks to a server you already run, such as Ollama, llama.cpp or vLLM, and needs nothing from this directory.

Files:

Requirements

Installation

The steps below install the modules into a virtualenv at /opt/mimedefang-ml/venv, which is where the systemd units look for them. Current distributions don't let pip install into the system Python (PEP 668), and a virtualenv also keeps torch and transformers away from the Python modules your distribution installs.

1. Create the virtualenv

python3 -m venv /opt/mimedefang-ml/venv
/opt/mimedefang-ml/venv/bin/pip install --upgrade pip

2. Install torch

On a CPU-only host, install torch from PyTorch's CPU wheel index first. Otherwise pip downloads the CUDA build, which is several GB larger:

/opt/mimedefang-ml/venv/bin/pip install torch \
    --index-url https://download.pytorch.org/whl/cpu

To use a GPU, skip this step and let the next one install the default (CUDA) torch.

3. Install the server's modules

From this directory, install one or both sets:

# Laya
/opt/mimedefang-ml/venv/bin/pip install -r requirements-laya.txt

# GLiClass
/opt/mimedefang-ml/venv/bin/pip install -r requirements-gliclass.txt

To check that they import:

/opt/mimedefang-ml/venv/bin/python3 -c 'import laya, flask, waitress'
/opt/mimedefang-ml/venv/bin/python3 -c 'import gliclass, transformers, flask, waitress'

4. Install the server scripts

The servers are not installed by default. To have make install put mimedefang-laya-server and mimedefang-gliclass-server into the same bin directory as the other MIMEDefang programs (/usr/bin for the systemd units), enable them when configuring the build:

./configure --enable-ml-servers ...      # autoconf build
perl Makefile.PL ML_SERVERS=1 ...        # ExtUtils::MakeMaker build

Or copy them by hand:

install -m 755 mimedefang-laya-server mimedefang-gliclass-server /usr/bin/

The scripts start with #!/usr/bin/env python3, so run them with the virtualenv's Python: either /opt/mimedefang-ml/venv/bin/python3 /usr/bin/mimedefang-gliclass-server ..., or with the virtualenv activated. The systemd units do the former.

5. Download the model

Each server downloads its model from Hugging Face the first time it starts. To do this ahead of time, run as the same user and with the same cache directory as the systemd units (defang, /var/lib/MIMEDefang/ml/huggingface):

install -d -m 755 /var/lib/MIMEDefang       # if it doesn't exist yet
install -d -o defang -g defang /var/lib/MIMEDefang/ml

# GLiClass (use the model name you will run with --model)
sudo -u defang HF_HOME=/var/lib/MIMEDefang/ml/huggingface \
    /opt/mimedefang-ml/venv/bin/python3 -c \
    'from huggingface_hub import snapshot_download; snapshot_download("knowledgator/gliclass-modern-base-v2.0")'

# Laya (all stock checkpoints)
sudo -u defang HF_HOME=/var/lib/MIMEDefang/ml/huggingface \
    /opt/mimedefang-ml/venv/bin/python3 -c \
    'from laya import Router; Router(preload=True)'

Running

By hand

/opt/mimedefang-ml/venv/bin/python3 mimedefang-gliclass-server --port 8688
/opt/mimedefang-ml/venv/bin/python3 mimedefang-laya-server --port 8687 --preload english,multilingual

Useful options (both servers; --help lists them all):

| Option | Meaning | |--------------------|------------------------------------------------------------------------| | --host, --port | Address to listen on (default 127.0.0.1), see Security | | --threads | Requests handled at once, see Sizing | | --torch-threads | CPU threads per request, see Sizing | | --min-confidence | Logging threshold, keep it equal to $Mail::MIMEDefang::ML::Config{min_confidence} | | --daemon | Fork into the background and log to syslog |

GLiClass also takes --model (a Hugging Face name or a local path; the default is knowledgator/gliclass-modern-base-v2.0, and knowledgator/gliclass-modern-large-v2.0 is more accurate but slower) and --device (cpu, or e.g. cuda:0) and --max-length (tokens of labels plus message text read, default 4096; the rest is cut, the gliclass-modern-* checkpoints take up to 8192) and --add-prefix-space (for the checkpoints whose model card loads the tokenizer with add_prefix_space=True, e.g. gliclass-x-base). Laya also takes --preload, --checkpoint and --attach-language.

Multilingual GLiClass models

The default GLiClass checkpoint is English. For mail in other languages use a multilingual one, no other change is needed: they are loaded through the same GLiClass classes and the labels MIMEDefang sends stay in English, the models score text and labels in different languages. Knowledgator's multilingual line is knowledgator/gliclass-multilang-mini and knowledgator/gliclass-multilang-ultra (trained on 20 languages), and knowledgator/gliclass-x-base is an older one on mDeBERTa. Set it with --model, with GLICLASS_MODEL in the sysconfig/default file of the unit or in the environment of the Docker image, and download it as shown in the installation above (snapshot_download with its name). Then:

Under systemd

cp ../../systemd-units/mimedefang-gliclass-server.service /etc/systemd/system/
cp ../../systemd-units/mimedefang-laya-server.service     /etc/systemd/system/
systemctl daemon-reload
systemctl enable --now mimedefang-gliclass-server   # and/or mimedefang-laya-server

The units run the server as the defang user. They start it before the MIMEDefang multiplexor when both are enabled, and keep the model cache in /var/lib/MIMEDefang/ml. systemd creates it, together with /var/lib/MIMEDefang if that doesn't exist yet, and gives only the ml subdirectory to defang. Settings can be overridden in /etc/sysconfig/mimedefang-<name>-server (Fedora/RHEL) or /etc/default/mimedefang-<name>-server (Debian/Ubuntu), as NAME=value lines:

| Variable | Default | Unit | |---|---|---| | ML_PYTHON | /opt/mimedefang-ml/venv/bin/python3 | both | | HF_HOME | /var/lib/MIMEDefang/ml/huggingface | both | | HF_HUB_OFFLINE | unset; 1 stops contacting huggingface.co | both | | GLICLASS_HOST, GLICLASS_PORT | 127.0.0.1, 8688 | gliclass | | GLICLASS_THREADS, GLICLASS_TORCH_THREADS | 8, 2 | gliclass | | GLICLASS_MODEL | knowledgator/gliclass-modern-base-v2.0 | gliclass | | GLICLASS_DEVICE | cpu | gliclass | | GLICLASS_MAX_LENGTH | 4096 | gliclass | | GLICLASS_EXTRA_ARGS | unset; further server options, e.g. --add-prefix-space | gliclass | | LAYA_HOST, LAYA_PORT | 127.0.0.1, 8687 | laya | | LAYA_THREADS, LAYA_TORCH_THREADS | 8, 2 | laya | | LAYA_PRELOAD | unset (all stock checkpoints) | laya | | LAYA_CHECKPOINT, LAYA_ATTACH_LANGUAGE | unset | laya |

If you installed the modules another way, point ML_PYTHON at the Python that has them, e.g. ML_PYTHON=/usr/bin/python3 for distribution packages.

Checking that it works

curl http://127.0.0.1:8688/health
curl -s http://127.0.0.1:8688/predict -H 'Content-Type: application/json' -d '{
  "text": "Subject: You won!\n\nClaim your prize, send your bank details today.",
  "labels": {"is_spam": "scam email: fake invoice, fake order, fake prize, advance-fee or investment fraud",
             "_ham0": "personal or business email between people who know each other"}
}'

curl http://127.0.0.1:8687/health
curl -s http://127.0.0.1:8687/predict -H 'Content-Type: application/json' -d '{
  "state": {"subject": "You won!", "body": "Claim your prize, send your bank details today."},
  "questions": {"is_spam": {"type": "noul", "instructions": "Is this email spam?"}}
}'

The servers load the model before they start listening, so connections are refused until loading has finished. That can take a minute, longer on the first start while the model downloads.

Docker images

Both servers are also published as images, mimedefang/laya and mimedefang/gliclass on Docker Hub (built from https://github.com/The-McGrail-Foundation/MIMEDefang-Docker). They listen on the same ports as above. To run the live tests of t/ml.t against them, run this from the top of the source tree:

sh t/run-tests.sh ml

Model cache and offline use

Models are stored in the Hugging Face cache, $HF_HOME (the units use /var/lib/MIMEDefang/ml/huggingface; by hand the default is ~/.cache/huggingface). Once the model is downloaded, set HF_HUB_OFFLINE=1 so the server starts without contacting huggingface.co. The server then fails to start if the model is missing.

GPU

Training a model

The stock models are zero-shot: they classify mail they were never trained on, from the label texts alone. Their accuracy varies from site to site, and a model trained on your own mail usually does much better. Of the three backends, only GLiClass can be easily trained:

Sizing

MIMEDefang calls the server synchronously, once per message, from every worker that reaches filter_end. Under a burst of mail, a server can get as many requests at once as the multiplexor has workers (its -x/MX_MAXIMUM setting). So:

Security

The servers have no authentication, and every request carries message content. The default 127.0.0.1 address keeps them reachable only from the same host. If MIMEDefang runs on other hosts, listen on a network address with --host and limit access with a firewall, so that only the MIMEDefang hosts can reach the port. Never expose the servers to the Internet. The same applies to Ollama, llama.cpp or vLLM when used with the openai backend.

Configuring MIMEDefang

In /etc/mail/mimedefang-filter:

use Mail::MIMEDefang::ML;

$Mail::MIMEDefang::ML::Config{backend}              = 'gliclass';
$Mail::MIMEDefang::ML::Config{gliclass}{server_url} = 'http://127.0.0.1:8688';
# or
# $Mail::MIMEDefang::ML::Config{backend}          = 'laya';
# $Mail::MIMEDefang::ML::Config{laya}{server_url} = 'http://127.0.0.1:8687';

See examples/example-filter-with-ml for a complete filter, and perldoc Mail::MIMEDefang::ML for every setting. To try a filter on a single message without running MIMEDefang:

mimedefang-test-mail -f /etc/mail/mimedefang-filter message.eml