r/opensource just banned AI, non-English speakers, and more
Back to Tutorials
aiTutorialbeginner

r/opensource just banned AI, non-English speakers, and more

October 25, 202657 views5 min read

Learn to create a Python script that detects AI-generated content by analyzing text patterns and structures.

Introduction

In this tutorial, you'll learn how to create a simple Python script that can detect whether a text contains AI-generated content. This is a practical skill that helps you identify when content might have been generated by AI systems like ChatGPT, Claude, or similar tools. Understanding these detection methods is crucial as AI becomes more prevalent in online communities and content creation.

Prerequisites

  • Basic understanding of Python programming
  • Python 3.x installed on your computer
  • Internet connection to install required packages

Step-by-step instructions

Step 1: Set up your Python environment

First, you'll need to create a new Python file for our project. Open your code editor and create a new file named ai_detector.py. This will be our main script for detecting AI-generated content.

Why this step?

Creating a dedicated file ensures we have a clean workspace to build our AI detection tool. This makes it easier to organize our code and test different approaches.

Step 2: Install required packages

Before we can start detecting AI content, we need to install some packages. Open your terminal or command prompt and run:

pip install transformers torch

Why this step?

We're installing the Hugging Face Transformers library, which provides pre-trained models for natural language processing. These models can help us analyze text patterns that are characteristic of AI-generated content.

Step 3: Import necessary libraries

At the top of your ai_detector.py file, add the following imports:

import torch
from transformers import pipeline

def detect_ai_content(text):
    # We'll implement our detection logic here
    pass

Why this step?

These imports give us access to the machine learning models that can analyze text patterns. The transformers library provides ready-to-use models that we can use to examine content characteristics.

Step 4: Create a basic detection function

Replace the placeholder function with this implementation:

def detect_ai_content(text):
    # Initialize the text classification pipeline
    classifier = pipeline("text-classification", model="facebook/bart-large-mnli")
    
    # Define some patterns that might indicate AI-generated content
    ai_indicators = [
        "the following is a", 
        "in conclusion", 
        "in summary", 
        "it is important to note that",
        "based on the information provided"
    ]
    
    # Check for these patterns
    score = 0
    for indicator in ai_indicators:
        if indicator in text.lower():
            score += 1
    
    # Simple scoring system
    if score >= 2:
        return True
    else:
        return False

Why this step?

This basic function looks for common phrases that frequently appear in AI-generated text. While not perfect, it provides a starting point for detecting potentially AI-created content.

Step 5: Test with sample text

Add this test code at the bottom of your file:

# Test cases
sample_texts = [
    "The following is a comprehensive analysis of modern programming practices.",
    "In conclusion, we have demonstrated that machine learning models can be effectively used in software development.",
    "Python is a versatile programming language that is widely used in data science.",
    "Based on the information provided, we can conclude that this approach is effective."
]

for text in sample_texts:
    result = detect_ai_content(text)
    print(f"Text: {text[:50]}...")
    print(f"AI detected: {result}\n")

Why this step?

Testing with sample texts helps us understand how our detection function works and whether it correctly identifies patterns we've identified as potentially AI-generated.

Step 6: Run the detection script

Save your file and run it in the terminal:

python ai_detector.py

Why this step?

Running the script allows us to see how our AI detection tool performs with different types of text samples. This helps us understand the current limitations and strengths of our basic approach.

Step 7: Improve detection with more advanced techniques

Let's enhance our detection by adding a more sophisticated approach:

def advanced_detect_ai_content(text):
    # Check for unusual text patterns
    sentences = text.split('. ')
    
    # Check for excessive use of certain words
    common_words = ['important', 'conclusion', 'summary', 'based', 'utilize']
    word_count = sum(1 for word in common_words if word in text.lower())
    
    # Check sentence structure
    avg_sentence_length = sum(len(sentence.split()) for sentence in sentences) / len(sentences)
    
    # Simple scoring
    score = 0
    if word_count > 3:
        score += 2
    if avg_sentence_length > 20:
        score += 1
    
    return score > 2

Why this step?

This improved version looks at more complex patterns like sentence structure and word frequency, which can be indicators of AI-generated content. Real AI systems often produce text with certain statistical characteristics.

Step 8: Test the improved function

Replace the test code with this enhanced version:

# Enhanced test cases
enhanced_texts = [
    "The following is a comprehensive analysis of modern programming practices. It is important to note that machine learning models can be effectively used in software development. In conclusion, we have demonstrated that this approach is effective.",
    "Python is a versatile programming language that is widely used in data science. It is easy to learn and has a large community of developers.",
    "Based on the information provided, we can conclude that this approach is effective. The implementation involves several key steps. First, we need to understand the requirements. Second, we should consider the constraints. Third, we must ensure proper testing."
]

for text in enhanced_texts:
    result = advanced_detect_ai_content(text)
    print(f"Text: {text[:50]}...")
    print(f"AI detected (advanced): {result}\n")

Why this step?

Testing with the enhanced function shows how adding more sophisticated analysis improves our detection capabilities. This demonstrates how you can gradually build more accurate detection methods.

Summary

In this tutorial, you've learned how to create a basic AI content detection tool using Python. You started with simple pattern matching and progressed to more sophisticated analysis of text structure and word usage. While this is a simplified approach, it demonstrates the fundamental concepts behind AI content detection.

Remember that AI-generated content detection is still an evolving field. Real-world applications require more complex models and larger datasets. This tutorial gives you a foundation to understand how such systems work and how you might build upon these concepts.

Source: The Verge AI

Related Articles