← Back to list

This AI Content Moderator Catches What Humans Miss — Here’s How I Built It

Discover the shocking truth behind how I used Python and open-source models to build a multi-modal AI system that detects hate, spam, NSFW…

Code with Margaret in Python in Plain English · 2025-07-21 20:31 · 14 claps · 4.4 min read
#ai-content-moderation #python-automation #machine-learning-projects #multi-modal-ai #openai-gpt
Open on Medium ↗
Wiki topics: LLM · Large Language Models ML · Machine Learning EDU · Education & Learning AIM · AI in Marketing 🔓 · Open Source

This AI Content Moderator Catches What Humans Miss — Here’s How I Built It

Discover the shocking truth behind how I used Python and open-source models to build a multi-modal AI system that detects hate, spam, NSFW content, and misinformation better than your average mod team.

The internet is noisy, chaotic, and filled with all kinds of user-generated content — some valuable, some toxic, and some outright illegal. In one of my recent projects, I decided to tackle a real-world problem that almost every platform faces: moderating content at scale using AI.

Unlike standard NLP classification tasks, moderation involves nuance: tone, sarcasm, context, and even image understanding. So I built a multi-modal content moderation system that detects text abuse, harmful imagery, spam, and misinformation.

Here’s a full breakdown of the architecture, tools, code, and the lessons I learned building this.

1. Project Blueprint — What Are We Trying to Solve?

Before jumping into code, I scoped out exactly what types of content needed moderation:

  • Toxic text (hate speech, threats, slurs)
  • Spam content (repetitive, meaningless messages)
  • Misinformation
  • NSFW or violent images
  • Prompted abuse (clever ways to bypass filters)

I planned a modular system where each content type (text/image) has its own handler, but shares a common pipeline and result aggregator.

2. Text Classification with Pretrained Transformers

To moderate text, I used **HuggingFace Transformers** and a fine-tuned BERT model (like cardiffnlp/twitter-roberta-base-offensive). Here’s the core code:

from transformers import AutoTokenizer, AutoModelForSequenceClassification
import torch
import torch.nn.functional as F

model_name = "cardiffnlp/twitter-roberta-base-offensive"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)

def classify_text(text):
    inputs = tokenizer(text, return_tensors="pt")
    outputs = model(**inputs)
    probs = F.softmax(outputs.logits, dim=-1)
    classes = ['non-offensive', 'offensive', 'hate']
    result = {classes[i]: probs[0][i].item() for i in range(len(classes))}
    return result

# Test
print(classify_text("I hate you and your people."))

Each result is passed through a confidence filter, and anything over 0.75 triggers a moderation alert.

3. Image Moderation with CLIP + NSFW Classifier

For image moderation, I used **CLIP embeddings and ran them through a small classifier trained on NSFW datasets**. Here’s how:

from PIL import Image
import requests
import torch
from transformers import CLIPProcessor, CLIPModel

model_name = "openai/clip-vit-base-patch32"
clip_model = CLIPModel.from_pretrained(model_name)
clip_processor = CLIPProcessor.from_pretrained(model_name)

def get_clip_embedding(image_path):
    image = Image.open(image_path).convert("RGB")
    inputs = clip_processor(images=image, return_tensors="pt")
    outputs = clip_model.get_image_features(**inputs)
    return outputs

embedding = get_clip_embedding("user_upload.jpg")

I trained a Logistic Regression classifier on embeddings generated from an NSFW-safe dataset:

from sklearn.linear_model import LogisticRegression
import pickle

# After training...
with open("nsfw_classifier.pkl", "rb") as f:
    clf = pickle.load(f)

is_nsfw = clf.predict(embedding.detach().numpy())

This approach gives solid results and is extremely fast at inference time.

4. Spam Detection Using Frequency Patterns + NLP

Spam isn’t just about offensive words. It’s repetition, meaningless content, or attempts to game ranking systems.

I used a hybrid approach: frequency tracking, word pattern detection, and entropy scoring.

import math
from collections import Counter

def shannon_entropy(text):
    counter = Counter(text)
    total = len(text)
    return -sum((count / total) * math.log2(count / total) for count in counter.values())

def is_spam(text):
    low_entropy = shannon_entropy(text) < 3.5
    repeated_phrases = text.count(text[:10]) > 3
    return low_entropy or repeated_phrases

# Test
print(is_spam("Buy now!!! Buy now!!! Buy now!!! Buy now!!!"))

This won’t catch everything, but it knocks out the low-hanging fruit.

5. Misinformation Detection with Fact-Check APIs

Fighting fake news with AI is tough. You need external fact verification, so I used Google Fact Check Tools API to compare statements to known falsehoods.

import requests

def fact_check(text):
    api_key = "your_google_factcheck_api"
    url = f"https://factchecktools.googleapis.com/v1alpha1/claims:search?query={text}&key={api_key}"
    response = requests.get(url)
    results = response.json()
    return results.get('claims', [])

# Test
print(fact_check("COVID vaccines cause magnetism"))

If claims return flagged responses with high confidence, we tag the content for manual review.

6. Prompt Injection & Jailbreak Detection

Smart users will try to prompt-inject or trick moderation filters by adding extra characters or using codewords.

To handle this, I added a pattern recognition + GPT-powered rephrasing system to normalize and analyze suspicious content.

from openai import OpenAI

client = OpenAI(api_key="your_api_key")

def normalize_input(text):
    prompt = f"Normalize this text to its original toxic intent if any:\n\n{text}\n\nReturn only the normalized version."
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content.strip()

# Example
print(normalize_input("I'm not saying he's stupid, but if IQ were money, he'd be bankrupt."))

This gives me a second pass to catch cleverly disguised hate or abuse.

7. Aggregator System — Combining All Pipelines

Each model runs in a modular microservice. Once results are ready, we aggregate them into a confidence-weighted score.

def aggregate_results(text_score, image_score, spam_score):
    score = 0
    if text_score['offensive'] > 0.75:
        score += 2
    if image_score == 1:
        score += 2
    if spam_score:
        score += 1

    if score >= 3:
        return "High Risk"
    elif score == 2:
        return "Review Needed"
    else:
        return "Safe"

# Example call
label = aggregate_results(
    classify_text("This group should be eliminated."),
    is_nsfw=1,
    spam_score=True
)
print(label)  # High Risk

All services report back to a Flask dashboard that logs content violations and decisions.

8. Real-Time Deployment & Scaling

For deployment, I containerized each component with Docker and deployed it on a Kubernetes cluster using FastAPI for async inference.

I also added webhooks for platforms like Discord, Slack, and forums to POST content for real-time moderation.

uvicorn app:moderator --host 0.0.0.0 --port 8000

Traffic spikes? No problem. Kubernetes handles horizontal scaling of the heavy models.

9. What I Learned (and What Broke)

  • False positives are your enemy. You need human fallback for sensitive decisions.
  • Multi-modal moderation is a must. Text-only systems miss crucial abuse signals.
  • Scaling models cost money. Optimize by batching and offloading image moderation unless needed.
  • Humor, sarcasm, and coded language break models. But clever prompt rewriters help a lot.

Final Thoughts

Moderating content is one of the hardest challenges in AI, not because it’s technically impossible — but because context is everything.

Building this system taught me a lot about AI’s strengths and blind spots. But with the right stack — Transformers, CLIP, entropy logic, and a sprinkle of LLM power — you can build something both fast and reliable.

This is a project I believe every AI developer should explore — not just to protect platforms, but to understand how AI interacts with human behavior in the wild.

Let me know if you want the full code repo or deployment setup. I’ll be happy to share more.

More AI projects coming soon.

Thank you for being a part of the community

Before you go:


메타데이터
post_id
a9802b415f71
slug
this-ai-content-moderator-catches-what-humans-miss-heres-how-i-built-it-a9802b415f71
url
https://python.plainenglish.io/this-ai-content-moderator-catches-what-humans-miss-heres-how-i-built-it-a9802b415f71
canonical_url
https://python.plainenglish.io/this-ai-content-moderator-catches-what-humans-miss-heres-how-i-built-it-a9802b415f71
author_url
https://medium.com/@currun95
status
ok
fetched_at
2026-06-27 07:40:21