Skip to content

Txbish/image-moderation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ›‘οΈ Image Moderation API

Intelligent Content Safety with Azure AI

A powerful FastAPI service that automatically detects harmful, illegal, or unwanted imagery using Azure Content Safety services.

FastAPI Python MongoDB Azure Docker

πŸš€ Quick Start β€’ πŸ“– Documentation β€’ πŸ”§ Setup Guide β€’ πŸ’‘ Examples


✨ Features

πŸ” Smart Content Analysis

  • 🚫 Hate symbols & content detection
  • ⚠️ Self-harm content identification
  • πŸ”ž Sexual content recognition
  • βš”οΈ Violence & graphic content analysis

πŸ” Security & Management

  • πŸ”‘ Token-based authentication
  • πŸ‘‘ Admin & user role system
  • πŸ“Š Complete usage tracking
  • πŸ—„οΈ MongoDB integration

πŸ—οΈ Additional Benefits

  • 🐳 Docker Ready - One-command deployment
  • 🌐 CORS Enabled - Frontend integration ready
  • πŸ“ Auto Documentation - Interactive API docs
  • ⚑ Fast & Scalable - Built on FastAPI

πŸ—οΈ System Architecture

graph TB
    A[🌐 Frontend UI] --> B[⚑ FastAPI Backend]
    B --> C[πŸ—„οΈ MongoDB Database]
    B --> D[🧠 Azure Content Safety]
    B --> E[πŸ“Š Usage Analytics]

    style A fill:#e1f5fe
    style B fill:#f3e5f5
    style C fill:#e8f5e8
    style D fill:#fff3e0
    style E fill:#fce4ec
Loading

πŸš€ Quick Start

🐳 Option 1: Docker (Recommended)

# Clone and run in 3 commands
git clone https://github.com/Txbish/image-moderation.git
cd image-moderation
docker-compose up --build

πŸŽ‰ That's it! Your API is running at http://localhost:7000

🐍 Option 2: Python Development

# Setup environment
cd backend
pip install -r requirements.txt

# Launch server
uvicorn app.main:app --host 0.0.0.0 --port 7000 --reload

πŸ”§ Setup Guide

πŸ“‹ Prerequisites

πŸ–₯️ Required Software
  • βœ… Python 3.8+
  • βœ… Docker & Docker Compose
  • βœ… Git
  • βœ… MongoDB (local or Atlas)
☁️ Cloud Services
  • πŸƒ MongoDB Atlas Account (free tier available)
  • πŸ”΅ Azure Account with Content Safety service

βš™οΈ Environment Configuration

  1. Copy environment template:

    cp .env.example .env
  2. Fill in your credentials:

    # MongoDB connection
    MONGO_URI=your_mongodb_connection_string
    
    # Azure Content Safety
    CONTENT_SAFETY_KEY=your_azure_content_safety_key
    CONTENT_SAFETY_ENDPOINT=your_azure_endpoint_url

πŸ”‘ Getting API Keys

πŸƒ MongoDB Atlas Setup

πŸ“– Step-by-step MongoDB Atlas guide
  1. 🌐 Visit MongoDB Atlas
  2. πŸ“ Create free account
  3. πŸ—„οΈ Create new cluster
  4. πŸ‘€ Database Access β†’ Create database user
  5. 🌍 Network Access β†’ Add IP (0.0.0.0/0 for development)
  6. πŸ”— Clusters β†’ Connect β†’ Connect your application
  7. πŸ“‹ Copy connection string and replace <password>

Connection string format:

mongodb+srv://username:password@cluster.mongodb.net/image_moderation

πŸ”΅ Azure Content Safety Setup

πŸ“– Step-by-step Azure guide
  1. 🌐 Visit Azure Portal
  2. πŸ’³ Sign up (free $200 credit included)
  3. βž• Create a resource β†’ Search "Content Safety"
  4. πŸ“ Configure:
    • Subscription: Your subscription
    • Resource Group: Create new
    • Region: Choose nearest
    • Pricing: Free tier available
  5. πŸ”‘ Keys and Endpoint β†’ Copy Key 1 and Endpoint

🌐 Access Points

Once running, access these services:

Service URL Description
πŸ–₯️ Frontend UI http://localhost:80 Web interface
⚑ Backend API http://localhost:7000 Main API server
πŸ“– API Docs http://localhost:7000/docs Interactive documentation
πŸ“š ReDoc http://localhost:7000/redoc Alternative documentation

πŸ’‘ Examples

πŸ”‘ Pre-seeded Tokens

Token Type Capabilities
admin-seed-token πŸ‘‘ Admin Token management + moderation
user-seed-token πŸ‘€ User Image moderation only

πŸ“ API Examples

πŸ” Check Admin Status
curl -X GET "http://localhost:7000/auth/tokens/is_admin" \
     -H "Authorization: Bearer admin-seed-token"

Response:

{
  "is_admin": true,
  "token": "admin-seed-token"
}
βž• Create New Token
curl -X POST "http://localhost:7000/auth/tokens" \
     -H "Authorization: Bearer admin-seed-token" \
     -H "Content-Type: application/json" \
     -d '{"is_admin": false}'

Response:

{
  "token": "new-uuid-token-here",
  "is_admin": false,
  "created_at": "2025-05-26T10:30:00Z"
}
πŸ–ΌοΈ Moderate Image
curl -X POST "http://localhost:7000/moderate" \
     -H "Authorization: Bearer user-seed-token" \
     -F "file=@/path/to/your/image.jpg"

Response:

{
  "result": {
    "hate": 0.14,
    "self_harm": 0.0,
    "sexual": 0.29,
    "violence": 0.43
  },
  "analysis": {
    "timestamp": "2025-05-26T10:30:00Z",
    "filename": "image.jpg",
    "status": "analyzed"
  }
}

πŸ“Š API Reference

πŸ” Authentication Endpoints

Method Endpoint Access Description
POST /auth/tokens πŸ‘‘ Admin Create new token
GET /auth/tokens πŸ‘‘ Admin List all tokens
GET /auth/tokens/is_admin πŸ”‘ Any Check admin status
DELETE /auth/tokens/{token} πŸ‘‘ Admin Delete token

πŸ›‘οΈ Moderation Endpoints

Method Endpoint Access Description
POST /moderate πŸ”‘ Any Analyze image content

πŸ“ˆ Score Interpretation

Score Range Meaning Action
0.0 - 0.2 🟒 Safe content βœ… Allow
0.2 - 0.5 🟑 Potentially questionable ⚠️ Review
0.5 - 0.8 🟠 Likely harmful 🚫 Block
0.8 - 1.0 πŸ”΄ High confidence harmful 🚨 Block immediately

πŸ—„οΈ Database Schema

πŸ“‹ Collections Structure

tokens Collection

{
  "_id": ObjectId,
  "token": "uuid-string",
  "isAdmin": boolean,
  "createdAt": datetime
}

usages Collection

{
  "_id": ObjectId,
  "token": "uuid-string",
  "endpoint": "/moderate",
  "timestamp": datetime,
  "metadata": {
    "filename": "string",
    "filesize": number
  }
}

πŸ”§ Development

πŸ“ Project Structure

image-moderation/
β”œβ”€β”€ πŸš€ backend/
β”‚   β”œβ”€β”€ πŸ“¦ app/
β”‚   β”‚   β”œβ”€β”€ 🏠 main.py
β”‚   β”‚   β”œβ”€β”€ πŸ” auth.py
β”‚   β”‚   β”œβ”€β”€ πŸ›‘οΈ moderate.py
β”‚   β”‚   β”œβ”€β”€ βš™οΈ middleware.py
β”‚   β”‚   └── πŸ—„οΈ db.py
β”‚   β”œβ”€β”€ πŸ“‹ requirements.txt
β”‚   └── 🐳 Dockerfile
β”œβ”€β”€ 🌐 frontend/
β”‚   └── πŸ“„ index.html
β”œβ”€β”€ 🐳 docker-compose.yml
β”œβ”€β”€ βš™οΈ .env.example
└── πŸ“– README.md

πŸ”§ Development Commands

# Add new dependency
cd backend && pip install new-package
pip freeze > requirements.txt

# Run tests
pytest

# View logs
docker-compose logs backend

🚨 Troubleshooting

πŸ” Common Issues & Solutions

πŸ—„οΈ MongoDB Connection Failed

  • βœ… Verify MONGO_URI format
  • βœ… Check MongoDB Atlas network access
  • βœ… Ensure database user permissions

πŸ”΅ Azure Content Safety Errors

  • βœ… Verify API key and endpoint
  • βœ… Check Azure subscription status
  • βœ… Ensure service quota isn't exceeded

🐳 Docker Issues

  • βœ… Check if Docker daemon is running
  • βœ… Verify ports 7000 and 80 are available
  • βœ… Ensure .env file exists

πŸ”‘ Authentication Errors

  • βœ… Include Authorization: Bearer <token> header
  • βœ… Verify token exists in database
  • βœ… Check if endpoint requires admin privileges

πŸš€ Production Deployment

πŸ”’ Security Checklist

  • πŸ” Use strong, unique tokens
  • 🌐 Enable HTTPS/TLS
  • ⏱️ Implement rate limiting
  • πŸ—„οΈ Use production MongoDB cluster
  • 🌍 Restrict CORS origins
  • πŸ” Use secure secret management

πŸ“Š Monitoring

  • πŸ“ˆ Application performance monitoring
  • πŸ’° Azure Content Safety usage tracking
  • πŸ—„οΈ Database performance metrics
  • 🚨 Error alerting system

🀝 Contributing

We welcome contributions! Here's how to get started:

  1. 🍴 Fork the repository
  2. 🌿 Create feature branch (git checkout -b feature/amazing-feature)
  3. πŸ’Ύ Commit changes (git commit -m 'Add amazing feature')
  4. πŸ“€ Push to branch (git push origin feature/amazing-feature)
  5. πŸ”ƒ Open a Pull Request

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.


πŸ’¬ Support & Community

Need Help?

πŸ“– Documentation β€’ πŸ’¬ Discussions β€’ πŸ› Issues β€’ πŸ“§ Contact

Useful Resources


Made with ❀️ for safer digital content

About

A powerful FastAPI service that automatically detects harmful, illegal, or unwanted imagery using Azure Content Safety services.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors