In this blog post, I’ll walk through how to build, deploy, and host an AI image detector. The project classifies images as either AI generated or real using a pre trained model, containerized with Docker, and deployed locally. You can find the complete source code on GitHub.


Overview

This project uses a Flask based application to serve a web interface where users can upload images or provide image URLs to determine their authenticity. The model used is Organika/sdxl-detector, a pre-trained model from Hugging Face, tailored for classifying AI generated and real images.


How the Model Works

The Organika/sdxl-detector model is a pre trained binary classification model designed to distinguish between AI generated and real images.

1. Model Architecture

  • EfficientNet Backbone: Utilizes EfficientNet for efficient and accurate feature extraction from images, focusing on hierarchical patterns like textures and edges.
  • Fine Tuning: The model is adapted for AI detection by training on datasets that include both real and AI generated images from tools like Stable Diffusion.

2. Data Flow

  1. Preprocessing:
    • The image is resized, normalized, and converted into tensors using Hugging Face’s AutoProcessor.
  2. Model Inference:
    • The processed image passes through the model, producing logits for:
      • Class 0: AI-generated.
      • Class 1: Real.
    • Logits are converted to probabilities using a softmax function.
  3. Classification Logic:
    • If Class 0‘s probability > 0.5, the image is classified as AI generated. Otherwise, it’s classified as real.

This model excels at identifying patterns unique to AI generated images, making it suitable for advanced detection tasks.


Project Structure

The project is structured as follows:

.
├── Dockerfile
├── docker-compose.yml
└── files
├── app.py
├── requirements.txt
└── templates
└── index.html

Key Files:

  • Dockerfile: Builds the Docker image.
  • docker-compose.yml: Simplifies local deployments.
  • files/app.py: Flask application backend.
  • files/requirements.txt: Python dependencies for the app.
  • files/templates/index.html: Simple HTML frontend for interacting with the app.

Step by Step Guide

1. Cloning the Repository

Start by cloning the repository:

git clone https://github.com/mattclemons/ai-image-detector.git
cd ai-image-detector

2. Building the Docker Image

To build the Docker image locally, use the following command:

docker build -t ai-image-detector .

The Dockerfile installs all dependencies and sets up the Flask application:

FROM python:3.9-slim

# Install system dependencies
RUN apt-get update && apt-get install -y --no-install-recommends libgl1-mesa-glx && apt-get clean

# Set the working directory
WORKDIR /app

# Copy the project files
COPY files/ /app/files/
COPY files/templates /app/files/templates

# Install Python dependencies
RUN pip install --no-cache-dir -r /app/files/requirements.txt

# Expose the application port
EXPOSE 5002

# Run the Flask application
CMD ["python", "/app/files/app.py"]

3. Running Locally

Once the image is built, you can run the container locally:

docker run -p 5002:5002 ai-image-detector

Visit http://localhost:5002 in your browser to access the app. You can upload images or input image URLs for analysis.


4. Using Docker Compose

The included docker-compose.yml simplifies deployment by abstracting common Docker commands:

services:
ai-image-detector:
build:
context: .
dockerfile: Dockerfile
image: ai-image-detector
container_name: ai-image-detector
ports:
- "5002:5002"
volumes:
- ./files:/app/files

command: ["python", "/app/files/app.py"]
restart: unless-stopped

To start the container using Docker Compose:

docker-compose up --build

5. Application Features

  • Upload or Provide Image URLs: Users can either upload images directly or provide URLs for analysis.
  • AI Detection: The app uses Organika/sdxl-detector to classify images as either AI generated or real.
  • Simple Frontend: A minimal HTML form enables easy interaction.

Frontend (index.html):

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI Image Detector</title>
</head>
<body>
<h1>AI Image Detector</h1>
<form action="/analyze" method="post" enctype="multipart/form-data">
<label for="file">Upload Image:</label>
<input type="file" id="file" name="file"><br><br>
<label for="url">Or Provide Image URL:</label>
<input type="text" id="url" name="url"><br><br>
<button type="submit">Analyze</button>
</form>
</body>
</html>

6. Results Example

Here’s an example of results returned by the /analyze endpoint via curl:

curl -X POST -F "file=@image.jpg" http://localhost:5002/analyze

Response:

{
"is_ai_generated": true,
"confidence": 0.895
}
Categories: Programming

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *