August 14, 2026 · 15 min read
Content moderation is essential for any platform accepting user-generated content. Chinese AI models offer powerful moderation capabilities at a fraction of the cost of Western alternatives. This guide covers building production moderation pipelines using DeepSeek, GLM-4, and Qwen through TokenEase.
| Category | Description | Action |
|---|---|---|
| Hate speech | Attacks on protected groups | Block + flag |
| Harassment | Targeting individuals | Block + warn |
| Self-harm | Suicide, eating disorders | Block + resources |
| Sexual content | Adult content, CSAM | Block + escalate |
| Violence | Graphic violence, threats | Block + review |
| Misinformation | False health, political claims | Flag + reduce reach |
| Spam | Unwanted promotional content | Filter + rate limit |
| PII | Personal information exposure | Redact + notify |
import requests
import json
TOKEN = "your_tokenease_api_key"
BASE_URL = "https://tokenease.io/v1"
MODERATION_CATEGORIES = [
"hate_speech", "harassment", "self_harm", "sexual_content",
"violence", "misinformation", "spam", "pii_exposure"
]
def moderate_text(text, model="deepseek-v4"):
"""Multi-category text moderation"""
prompt = f"""Analyze the following text for content policy violations.
For each category, provide a score 0-1 (1 = severe violation) and brief reasoning.
Categories: {', '.join(MODERATION_CATEGORIES)}
Text:
{text[:2000]}
Return ONLY JSON in this exact format:
{{
"categories": {{
"hate_speech": {{"score": 0.0, "reason": "string"}},
"harassment": {{"score": 0.0, "reason": "string"}},
"self_harm": {{"score": 0.0, "reason": "string"}},
"sexual_content": {{"score": 0.0, "reason": "string"}},
"violence": {{"score": 0.0, "reason": "string"}},
"misinformation": {{"score": 0.0, "reason": "string"}},
"spam": {{"score": 0.0, "reason": "string"}},
"pii_exposure": {{"score": 0.0, "reason": "string"}}
}},
"overall_score": 0.0,
"action": "allow|flag|block",
"explanation": "string"
}}"""
response = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Authorization": f"Bearer {TOKEN}"},
json={
"model": model,
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.1,
"response_format": {"type": "json_object"}
}
)
return json.loads(response.json()["choices"][0]["message"]["content"])
# Test moderation
result = moderate_text("You are so stupid and worthless. Nobody likes you.")
print(json.dumps(result, indent=2))
# Output: harassment score ~0.9, action: block
class ModerationPipeline:
def __init__(self):
self.thresholds = {
"auto_block": 0.85, # Automatically block
"auto_flag": 0.60, # Flag for human review
"auto_allow": 0.20 # Automatically allow
}
def process(self, content):
"""Process content through moderation pipeline"""
# Tier 1: AI moderation
ai_result = moderate_text(content)
max_score = max(
c["score"] for c in ai_result["categories"].values()
)
# Tier 2: Decision routing
if max_score >= self.thresholds["auto_block"]:
return {
"action": "block",
"reason": ai_result["explanation"],
"categories": self.get_violations(ai_result),
"human_review": False
}
elif max_score >= self.thresholds["auto_flag"]:
return {
"action": "flag",
"reason": ai_result["explanation"],
"categories": self.get_violations(ai_result),
"human_review": True
}
elif max_score <= self.thresholds["auto_allow"]:
return {
"action": "allow",
"reason": "No violations detected",
"human_review": False
}
else:
# Ambiguous - route to human
return {
"action": "review",
"reason": "Uncertain classification",
"ai_result": ai_result,
"human_review": True
}
def get_violations(self, result):
"""Extract categories with significant scores"""
return {
cat: data["score"]
for cat, data in result["categories"].items()
if data["score"] > 0.5
}
# Usage
pipeline = ModerationPipeline()
user_posts = [
"Great article! Thanks for sharing.",
"You're all idiots. Go back to your caves.",
"Check out my amazing weight loss pills!!! Buy now!!!"
]
for post in user_posts:
result = pipeline.process(post)
print(f"Post: {post[:50]}...")
print(f"Action: {result['action']}")
if result.get('categories'):
print(f"Violations: {result['categories']}")
print()
def detect_pii(text):
"""Detect and classify PII in text"""
prompt = f"""Identify all personally identifiable information (PII) in this text.
For each PII item, provide the type and value.
PII types: email, phone, address, name, ssn, credit_card, date_of_birth, ip_address
Text:
{text}
Return JSON:
{{
"pii_found": true|false,
"items": [
{{"type": "email", "value": "user@example.com", "start": 10, "end": 26}}
],
"redacted_text": "text with PII replaced by [REDACTED]"
}}"""
response = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Authorization": f"Bearer {TOKEN}"},
json={
"model": "glm-4",
"messages": [{"role": "user", "content": prompt}],
"temperature": 0.1,
"response_format": {"type": "json_object"}
}
)
return json.loads(response.json()["choices"][0]["message"]["content"])
# Example
text_with_pii = "Contact John Smith at john.smith@email.com or call 555-123-4567."
pii_result = detect_pii(text_with_pii)
print(pii_result["redacted_text"])
# Output: "Contact [REDACTED] at [REDACTED] or call [REDACTED]."
import concurrent.futures
def batch_moderate(texts, max_workers=10):
"""Moderate multiple items in parallel"""
def moderate_single(text):
try:
result = moderate_text(text)
max_score = max(c["score"] for c in result["categories"].values())
return {
"text": text[:100],
"action": "block" if max_score > 0.85 else "flag" if max_score > 0.60 else "allow",
"max_score": max_score,
"categories": {k: v["score"] for k, v in result["categories"].items() if v["score"] > 0.5}
}
except Exception as e:
return {"text": text[:100], "action": "error", "error": str(e)}
with concurrent.futures.ThreadPoolExecutor(max_workers=max_workers) as executor:
results = list(executor.map(moderate_single, texts))
return results
# Moderate forum posts
forum_posts = load_forum_posts(limit=1000)
results = batch_moderate(forum_posts)
blocked = [r for r in results if r["action"] == "block"]
flagged = [r for r in results if r["action"] == "flag"]
allowed = [r for r in results if r["action"] == "allow"]
print(f"Processed: {len(results)}")
print(f"Blocked: {len(blocked)} ({len(blocked)/len(results)*100:.1f}%)")
print(f"Flagged: {len(flagged)} ({len(flagged)/len(results)*100:.1f}%)")
print(f"Allowed: {len(allowed)} ({len(allowed)/len(results)*100:.1f}%)")
class HumanReviewQueue:
def __init__(self, db_connection):
self.db = db_connection
def add_to_queue(self, content_id, content, ai_result, priority="normal"):
"""Add item to human review queue"""
# Priority based on severity
max_score = max(c["score"] for c in ai_result["categories"].values())
if max_score > 0.90:
priority = "urgent"
elif max_score > 0.75:
priority = "high"
self.db.execute("""
INSERT INTO moderation_queue
(content_id, content, ai_result, priority, status, created_at)
VALUES (?, ?, ?, ?, 'pending', ?)
""", (content_id, content, json.dumps(ai_result), priority, datetime.now()))
# Notify reviewers for urgent items
if priority == "urgent":
notify_reviewers(content_id, content, ai_result)
def get_next_item(self, reviewer_id):
"""Get next item for reviewer"""
# Prioritize by priority and age
item = self.db.execute("""
SELECT * FROM moderation_queue
WHERE status = 'pending'
ORDER BY
CASE priority
WHEN 'urgent' THEN 1
WHEN 'high' THEN 2
ELSE 3
END,
created_at ASC
LIMIT 1
""").fetchone()
if item:
self.db.execute("""
UPDATE moderation_queue
SET status = 'in_review', reviewer_id = ?
WHERE id = ?
""", (reviewer_id, item["id"]))
return item
def resolve(self, item_id, decision, reviewer_notes=""):
"""Resolve a moderation item"""
self.db.execute("""
UPDATE moderation_queue
SET status = 'resolved',
decision = ?,
reviewer_notes = ?,
resolved_at = ?
WHERE id = ?
""", (decision, reviewer_notes, datetime.now(), item_id))
# Update model with feedback for continuous improvement
self.log_feedback(item_id, decision)
def notify_reviewers(content_id, content, ai_result):
"""Send urgent notification to human reviewers"""
# Implementation depends on your notification system
pass
import base64
def moderate_image(image_path):
"""Moderate image content using vision model"""
with open(image_path, "rb") as f:
image_data = base64.b64encode(f.read()).decode()
response = requests.post(
f"{BASE_URL}/chat/completions",
headers={"Authorization": f"Bearer {TOKEN}"},
json={
"model": "qwen-vl",
"messages": [
{
"role": "user",
"content": [
{"type": "text", "text": "Analyze this image for content policy violations. Check for: nudity, violence, hate symbols, drugs, and text containing PII. Return JSON with categories and overall safety score 0-1."},
{"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_data}"}}
]
}
],
"response_format": {"type": "json_object"}
}
)
return json.loads(response.json()["choices"][0]["message"]["content"])
# Moderate user-uploaded image
result = moderate_image("user_upload.jpg")
if result["overall_score"] > 0.7:
block_upload("user_upload.jpg", result)
| Model | Latency (p95) | Accuracy (F1) | Cost/1K items |
|---|---|---|---|
| DeepSeek-V4 | 800ms | 0.89 | $0.50 |
| GLM-4 | 950ms | 0.87 | $0.70 |
| Qwen-Max | 700ms | 0.88 | $0.50 |
| Reference: OpenAI Moderation | 200ms | 0.91 | $2.00 |
Moderating 1 million user posts:
For security guides, see API security best practices.
Last updated: August 2026. Moderation capabilities evolve with model updates.