Pick an on-device PII model
Use the committed registry to build an offline shortlist. Treat the language
default as the safety baseline, but never treat model size or format as proof
of recall.
Procedure
- Identify the input language and script before choosing a model.
- Choose the runtime:
pytorch for local CPU/GPU and mobile export sources,
mlx-fp or mlx-8bit for Apple Silicon.
- Read
get_default_pii_model(language) as the baseline.
- Filter
get_pii_models_by_language(language) by runtime and device budget.
- Prefer the baseline when it fits; otherwise select a compatible candidate.
- Benchmark the candidate on direct identifiers, critical leakage, scripts,
and the target quantization before shipping.
Runnable offline shortlist
This snippet reads only the bundled manifest; it does not download weights.
from openmed import get_default_pii_model, get_pii_models_by_language
LANGUAGE = "en"
TARGET_FORMAT = "mlx-fp" # Use "pytorch" for CPU or as an export source.
MAX_PARAMETERS_M = 150
baseline_id = get_default_pii_model(LANGUAGE)
models = get_pii_models_by_language(LANGUAGE)
shortlist = [
(key, info)
for key, info in models.items()
if TARGET_FORMAT in info.formats
and info.size_mb is not None
and info.size_mb <= MAX_PARAMETERS_M
]
shortlist.sort(
key=lambda item: (
item[1].model_id != baseline_id,
item[1].size_mb,
item[0],
)
)
if not shortlist:
raise RuntimeError("No compatible PII model fits the requested budget")
registry_key, selected = shortlist[0]
print(
{
"registry_key": registry_key,
"model_id": selected.model_id,
"format": TARGET_FORMAT,
"parameters_m": selected.size_mb,
"recommended_confidence": selected.recommended_confidence,
"is_language_default": selected.model_id == baseline_id,
}
)
print("Benchmark this candidate against the language default before release.")
For Android, Core ML, ONNX, or browser deployment, select a compatible
pytorch source and use the target export workflow. Re-run PII recall after
conversion or quantization.
Selection rules
- Reject an unsupported language instead of silently falling back to English.
- Prefer audited script coverage over a model's name or marketing description.
- Treat parameter count as a rough capacity signal, not download size, latency,
peak memory, or quality.
- Measure latency and peak memory on the real target device.
- Fail closed when conversion or quantization drops direct-identifier recall or
introduces residual critical leakage.
- Cache approved weights locally and set offline mode for steady-state use.
Repository example
Read
the PII model comparison example for
registry inspection and model-by-model inference.
1---2name: pick-a-pii-model3description: Select an on-device OpenMed PII model from the committed registry by language, runtime format, and size budget, then require recall validation before deployment. Use when an agent must choose a local PII detector for CPU, Apple Silicon, or a mobile export without relying on live model discovery.4---56# Pick an on-device PII model78Use the committed registry to build an offline shortlist. Treat the language9default as the safety baseline, but never treat model size or format as proof10of recall.1112## Procedure13141. Identify the input language and script before choosing a model.152. Choose the runtime: `pytorch` for local CPU/GPU and mobile export sources,16 `mlx-fp` or `mlx-8bit` for Apple Silicon.173. Read `get_default_pii_model(language)` as the baseline.184. Filter `get_pii_models_by_language(language)` by runtime and device budget.195. Prefer the baseline when it fits; otherwise select a compatible candidate.206. Benchmark the candidate on direct identifiers, critical leakage, scripts,21 and the target quantization before shipping.2223## Runnable offline shortlist2425This snippet reads only the bundled manifest; it does not download weights.2627```python28from openmed import get_default_pii_model, get_pii_models_by_language2930LANGUAGE = "en"31TARGET_FORMAT = "mlx-fp" # Use "pytorch" for CPU or as an export source.32MAX_PARAMETERS_M = 1503334baseline_id = get_default_pii_model(LANGUAGE)35models = get_pii_models_by_language(LANGUAGE)3637shortlist = [38 (key, info)39 for key, info in models.items()40 if TARGET_FORMAT in info.formats41 and info.size_mb is not None42 and info.size_mb <= MAX_PARAMETERS_M43]44shortlist.sort(45 key=lambda item: (46 item[1].model_id != baseline_id,47 item[1].size_mb,48 item[0],49 )50)5152if not shortlist:53 raise RuntimeError("No compatible PII model fits the requested budget")5455registry_key, selected = shortlist[0]56print(57 {58 "registry_key": registry_key,59 "model_id": selected.model_id,60 "format": TARGET_FORMAT,61 "parameters_m": selected.size_mb,62 "recommended_confidence": selected.recommended_confidence,63 "is_language_default": selected.model_id == baseline_id,64 }65)66print("Benchmark this candidate against the language default before release.")67```6869For Android, Core ML, ONNX, or browser deployment, select a compatible70`pytorch` source and use the target export workflow. Re-run PII recall after71conversion or quantization.7273## Selection rules7475- Reject an unsupported language instead of silently falling back to English.76- Prefer audited script coverage over a model's name or marketing description.77- Treat parameter count as a rough capacity signal, not download size, latency,78 peak memory, or quality.79- Measure latency and peak memory on the real target device.80- Fail closed when conversion or quantization drops direct-identifier recall or81 introduces residual critical leakage.82- Cache approved weights locally and set offline mode for steady-state use.8384## Repository example8586Read87[the PII model comparison example](../../examples/pii_model_comparison.py) for88registry inspection and model-by-model inference.