Skip to content

Commit d49bbb5

Browse files
author
fkalghan
committed
Add demo script for quick testing
1 parent 9fca0ec commit d49bbb5

1 file changed

Lines changed: 94 additions & 0 deletions

File tree

examples/demo.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
#!/usr/bin/env python
2+
"""Demo script to test LocalMod functionality."""
3+
4+
import json
5+
import sys
6+
import time
7+
8+
# Check if running with models downloaded
9+
def main():
10+
print("=" * 60)
11+
print("LocalMod Demo - Content Moderation API")
12+
print("=" * 60)
13+
14+
from localmod import SafetyPipeline
15+
from localmod.classifiers import PIIDetector
16+
17+
# Initialize pipeline (PII only for quick demo - no model downloads needed)
18+
print("\n[1] Initializing PII detector (no ML model needed)...")
19+
pii = PIIDetector()
20+
pii.load()
21+
print(" ✓ PII detector ready")
22+
23+
# Test PII Detection
24+
print("\n[2] Testing PII Detection...")
25+
test_cases = [
26+
"My email is john.doe@example.com",
27+
"Call me at 555-123-4567",
28+
"SSN: 123-45-6789",
29+
"Credit card: 4111-1111-1111-1111",
30+
"Hello, how are you today?", # No PII
31+
]
32+
33+
for text in test_cases:
34+
result = pii.predict(text)
35+
status = "🚨 FLAGGED" if result.flagged else "✅ Safe"
36+
print(f" {status}: \"{text[:40]}...\"")
37+
if result.flagged:
38+
print(f" Types: {result.categories}")
39+
40+
# Test PII Redaction
41+
print("\n[3] Testing PII Redaction...")
42+
text = "Contact John at john@email.com or 555-123-4567"
43+
redacted, _ = pii.redact(text)
44+
print(f" Original: {text}")
45+
print(f" Redacted: {redacted}")
46+
47+
# Full pipeline test (requires model downloads)
48+
print("\n[4] Testing Full Pipeline (requires models)...")
49+
try:
50+
pipeline = SafetyPipeline(classifiers=["pii"]) # Just PII for quick test
51+
report = pipeline.analyze(
52+
"My email is test@example.com and I hate everyone!",
53+
include_explanation=True
54+
)
55+
print(f" Flagged: {report.flagged}")
56+
print(f" Severity: {report.severity.value}")
57+
print(f" Summary: {report.summary}")
58+
print(f" Time: {report.processing_time_ms:.2f}ms")
59+
except Exception as e:
60+
print(f" ⚠ Full pipeline test skipped: {e}")
61+
62+
# Test with ML models if available
63+
print("\n[5] Testing ML-based classifiers (if models are downloaded)...")
64+
try:
65+
from localmod.classifiers import ToxicityClassifier
66+
toxicity = ToxicityClassifier(device="cpu")
67+
toxicity.load()
68+
69+
toxic_texts = [
70+
"You're a complete idiot!",
71+
"I hope you have a wonderful day!",
72+
"Die in a fire you moron",
73+
]
74+
75+
print(" Toxicity classifier loaded!")
76+
for text in toxic_texts:
77+
result = toxicity.predict(text)
78+
status = "🚨 TOXIC" if result.flagged else "✅ Safe"
79+
print(f" {status} ({result.confidence:.2%}): \"{text[:40]}\"")
80+
81+
except Exception as e:
82+
print(f" ⚠ ML classifiers not available: {type(e).__name__}")
83+
print(" Run 'python -m localmod.cli download' to download models")
84+
85+
print("\n" + "=" * 60)
86+
print("Demo Complete!")
87+
print("=" * 60)
88+
89+
return 0
90+
91+
92+
if __name__ == "__main__":
93+
sys.exit(main())
94+

0 commit comments

Comments
 (0)