LocalMod

February 10, 2026 · View on GitHub

Python 3.8+ License: MIT Tests

Fully offline content moderation API — Free, self-hosted, and private. Your data never leaves your infrastructure.

LocalMod Architecture

LocalMod Examples


Benchmark Results

Toxicity Detection

Benchmarked using CHI 2025 "Lost in Moderation" methodology (HateXplain, Civil Comments, SBIC datasets):

SystemBalanced AccuracyType
OpenAI Moderation API0.83Commercial
Azure Content Moderator0.81Commercial
LocalMod0.75Open Source
Amazon Comprehend0.74Commercial
Perspective API0.62Commercial
FeatureSystemPrecisionRecallF1FPFNnDatasetEval accuracy (balanced acc)
PIILocalMod1.00001.00001.0000002000synthetic_pii_v1 (balanced)1.0000
ToxicityLocalMod0.60070.83730.697312133555924HateXplain (1924) + Civil Comments (2000) + SBIC (2000); macro-avg P/R/F1, summed FP/FN0.6500
Prompt InjectionLocalMod0.93240.85250.8907651552101S-Labs/prompt-injection-dataset (test, threshold=0.10)0.8953
SpamLocalMod0.98611.00000.993010500ucirvine/sms_spam (train)0.9988
NSFW TextLocalMod0.60340.95330.739018814600Proxy: Maxx0/Texting_sex (NSFW) vs ag_news (SFW), balanced (threshold=0.60)0.6633
NSFW ImageLocalMod1.00000.95000.97440280Proxy: x1101/nsfw (pos) vs cifar10 (neg), balanced0.9750

Installation

git clone https://github.com/KOKOSde/localmod.git
cd localmod
pip install -e .

# Download ML models (~3.5GB - includes image model)
python scripts/download_models.py

Quick Start

# Run demo
python examples/demo.py

Python Usage

from localmod import SafetyPipeline

pipeline = SafetyPipeline()
report = pipeline.analyze("Check this text for safety issues")

print(f"Flagged: {report.flagged}")
print(f"Severity: {report.severity}")

API Server

localmod serve --port 8000

# Test
curl -X POST http://localhost:8000/analyze \
  -H "Content-Type: application/json" \
  -d '{"text": "Hello world", "classifiers": ["toxicity", "pii"]}'

Docker

docker build -f docker/Dockerfile -t localmod:latest .
docker run -p 8000:8000 localmod:latest

Discord Bot 🆕

# Install Discord dependency
pip install -e ".[discord]"

# Set your bot token
export DISCORD_BOT_TOKEN=your_token_here

# Run the bot
python examples/discord_bot.py

Features: Real-time text & image moderation, auto-delete, timeout, logging.


Classifiers

Text Moderation

ClassifierDetectsModel
PIIEmails, phones, SSNs, credit cardsRegex + Validation
ToxicityHate speech, harassment, threatsWeighted Ensemble (4 models)
Prompt InjectionLLM jailbreaks, instruction overrideDeBERTa
SpamPromotional content, scamsRoBERTa
NSFW TextSexual content, adult themesNSFW Classifier

Image Moderation 🆕

ClassifierDetectsModel
NSFW ImageExplicit/adult imagesFalconsai/nsfw_image_detection (ViT)

71M+ downloads on HuggingFace • Apache 2.0 license

Toxicity Ensemble

ModelWeightPurpose
unitary/toxic-bert50%Multi-label toxicity
Hate-speech-CNERG/dehatebert-mono-english20%Hate speech
s-nlp/roberta_toxicity_classifier15%Toxicity
facebook/roberta-hate-speech-dynabench-r4-target15%Adversarial robustness

API Endpoints

Text Moderation

EndpointMethodDescription
/analyzePOSTAnalyze single text
/analyze/batchPOSTAnalyze multiple texts
/redactPOSTRedact PII from text

Image Moderation 🆕

EndpointMethodDescription
/analyze/imagePOSTAnalyze image from URL
/analyze/image/uploadPOSTAnalyze uploaded image file

System

EndpointMethodDescription
/healthGETHealth check
/classifiersGETList text classifiers
/classifiers/imageGETList image classifiers

Example: Text Analysis

POST /analyze
{
  "text": "You are an idiot!",
  "classifiers": ["toxicity"]
}

Response:
{
  "flagged": true,
  "results": [{"classifier": "toxicity", "flagged": true, "confidence": 0.72, "severity": "high"}],
  "processing_time_ms": 85.3
}

Example: Image Analysis 🆕

POST /analyze/image
{
  "image_url": "https://example.com/image.jpg"
}

Response:
{
  "flagged": false,
  "results": [{"classifier": "nsfw_image", "flagged": false, "confidence": 0.02, "severity": "none"}],
  "processing_time_ms": 120.5
}

Offline Mode

# Download models once
python scripts/download_models.py --model-dir /path/to/models

# Run offline
export LOCALMOD_MODEL_DIR=/path/to/models
export LOCALMOD_OFFLINE=1
localmod serve

Docker:

docker run -p 8000:8000 \
  -v /path/to/models:/models \
  -e LOCALMOD_MODEL_DIR=/models \
  -e LOCALMOD_OFFLINE=1 \
  localmod:latest

Configuration

VariableDefaultDescription
LOCALMOD_MODEL_DIR~/.cache/localmod/modelsModel directory
LOCALMOD_OFFLINEfalseForce offline mode
LOCALMOD_DEVICEautocpu, cuda, or auto

Performance

ClassifierCPU LatencyGPU LatencyMemory
PII (regex)<1ms<1msMinimal
Single ML model (text)~50-200ms~10-30ms~1GB
Toxicity ensemble (4 models)~200-500ms~30-80ms~3GB
NSFW Image (ViT)~100-300ms~20-50ms~500MB

Performance varies by hardware. GPU recommended for production workloads.


Development

pip install -e ".[dev]"
pytest tests/ -v

License

MIT License — see LICENSE.

Acknowledgments

Models from HuggingFace. Benchmark methodology from CHI 2025 "Lost in Moderation" (Hartmann et al.).