Introduction
In recent months, there's been a significant shift in how digital platforms approach AI-generated content. What used to be called 'AI slop' - low-quality, automated content that floods the internet - is now being actively flagged and banned by major platforms. This tutorial will teach you how to build a simple AI content detection tool that can help identify potentially problematic AI-generated text. This is an important skill for content creators, moderators, and anyone working with digital content.
By the end of this tutorial, you'll have built a basic AI content detector that can analyze text for signs of AI generation and provide a confidence score.
Prerequisites
To follow this tutorial, you'll need:
- A computer with internet access
- Basic understanding of Python programming
- Python 3.7 or higher installed
- Access to a Python IDE or code editor
Note: This tutorial uses open-source libraries that are freely available and don't require API keys or paid services.
Step-by-Step Instructions
Step 1: Set Up Your Python Environment
First, we need to create a new Python project folder and install the required libraries. Open your terminal or command prompt and run these commands:
mkdir ai_content_detector
cd ai_content_detector
pip install transformers torch scikit-learn
Why: These libraries provide the tools we need - transformers for accessing pre-trained language models, torch for machine learning operations, and scikit-learn for additional analysis tools.
Step 2: Create Your Main Python File
Create a new file called detector.py in your project folder. This will be our main program file. Start by importing the necessary libraries:
import torch
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
class AIContentDetector:
def __init__(self):
# Initialize our detector
pass
if __name__ == "__main__":
detector = AIContentDetector()
print("AI Content Detector initialized!")
Why: This sets up the basic structure of our program. We're creating a class that will contain all our detection methods, making it easy to organize and expand our code later.
Step 3: Implement Basic Text Analysis
Now let's add a simple text analysis function that can detect certain patterns commonly found in AI-generated content:
def analyze_text_complexity(self, text):
# Split text into sentences
sentences = text.split('. ')
# Calculate average sentence length
avg_length = np.mean([len(sentence.split()) for sentence in sentences if sentence])
# Count unique words
words = text.lower().split()
unique_words = len(set(words))
# Calculate word diversity
diversity = unique_words / len(words) if words else 0
return {
'avg_sentence_length': avg_length,
'word_diversity': diversity,
'total_words': len(words)
}
Why: AI-generated text often has predictable patterns. For example, it might have unusually consistent sentence lengths or lower word diversity compared to human-written text. This analysis helps us identify these patterns.
Step 4: Add AI Detection Logic
Let's create a method that uses a pre-trained model to detect AI-generated content:
def detect_ai_content(self, text):
# Load a pre-trained model for text classification
try:
classifier = pipeline("text-classification",
model="facebook/bart-large-mnli")
# Simple classification - we'll check for certain patterns
# This is a simplified approach for beginners
# Check for common AI patterns
patterns = [
"this is a", "this article discusses", "in this paper",
"the purpose of this", "as mentioned above"
]
pattern_count = sum(1 for pattern in patterns if pattern in text.lower())
# Simple scoring system
score = min(pattern_count / 5.0, 1.0) # Max score of 1.0
# Analyze complexity
complexity = self.analyze_text_complexity(text)
return {
'confidence': score,
'is_ai_generated': score > 0.3,
'complexity': complexity
}
except Exception as e:
print(f"Error in AI detection: {e}")
return {'confidence': 0.0, 'is_ai_generated': False, 'complexity': None}
Why: We're using a simple but effective approach here. Instead of trying to build a complex model from scratch, we're leveraging existing pre-trained models and looking for specific linguistic patterns that are common in AI-generated content.
Step 5: Create a Test Function
Let's add a function that allows us to test our detector with sample texts:
def test_detector(self):
# Sample texts - one human-written, one AI-generated
human_text = "The weather today is quite pleasant. I went for a walk in the park and enjoyed the fresh air. The trees were beautiful and the birds were singing."
ai_text = "This article discusses the fundamental principles of artificial intelligence. The purpose of this paper is to examine how machine learning algorithms can be applied to natural language processing. As mentioned above, the development of neural networks has revolutionized the field of computer science."
print("Testing Human Text:")
result1 = self.detect_ai_content(human_text)
print(f"Confidence: {result1['confidence']:.2f}")
print(f"AI Generated: {result1['is_ai_generated']}")
print("\nTesting AI Text:")
result2 = self.detect_ai_content(ai_text)
print(f"Confidence: {result2['confidence']:.2f}")
print(f"AI Generated: {result2['is_ai_generated']}")
Why: Testing is crucial to understand how our tool performs. By using known examples, we can verify that our detector is working as expected and adjust our approach if needed.
Step 6: Complete Your Main Program
Update your main program to run the tests:
if __name__ == "__main__":
detector = AIContentDetector()
print("AI Content Detector initialized!")
# Run tests
detector.test_detector()
# Example of using with your own text
user_input = input("\nEnter text to analyze (or press Enter to skip): ")
if user_input:
result = detector.detect_ai_content(user_input)
print(f"\nAnalysis Result:")
print(f"Confidence: {result['confidence']:.2f}")
print(f"AI Generated: {result['is_ai_generated']}")
Why: This final step makes our tool interactive, allowing you to test it with your own text samples. This is how you'd actually use the tool in practice.
Summary
In this tutorial, you've built a basic AI content detection tool that can help identify potentially AI-generated text. While this is a simplified version, it demonstrates the core concepts behind content detection systems that major platforms are using to combat AI slop.
The key concepts you learned include:
- Using pre-trained models for text analysis
- Identifying linguistic patterns common in AI-generated content
- Creating a scoring system to measure confidence
- Testing your detection system with sample data
As you continue learning, you can expand this tool by adding more sophisticated machine learning models, incorporating more complex linguistic analysis, or connecting it to real-time content moderation systems. This foundation gives you the skills to understand and work with the technology that's reshaping how platforms handle AI-generated content.



