A powerful FastAPI service that automatically detects harmful, illegal, or unwanted imagery using Azure Content Safety services.
π Quick Start β’ π Documentation β’ π§ Setup Guide β’ π‘ Examples
|
|
- π³ Docker Ready - One-command deployment
- π CORS Enabled - Frontend integration ready
- π Auto Documentation - Interactive API docs
- β‘ Fast & Scalable - Built on FastAPI
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
# 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
# Setup environment
cd backend
pip install -r requirements.txt
# Launch server
uvicorn app.main:app --host 0.0.0.0 --port 7000 --reloadπ₯οΈ 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
-
Copy environment template:
cp .env.example .env
-
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
π Step-by-step MongoDB Atlas guide
- π Visit MongoDB Atlas
- π Create free account
- ποΈ Create new cluster
- π€ Database Access β Create database user
- π Network Access β Add IP (0.0.0.0/0 for development)
- π Clusters β Connect β Connect your application
- π Copy connection string and replace
<password>
Connection string format:
mongodb+srv://username:password@cluster.mongodb.net/image_moderation
π Step-by-step Azure guide
- π Visit Azure Portal
- π³ Sign up (free $200 credit included)
- β Create a resource β Search "Content Safety"
- π Configure:
- Subscription: Your subscription
- Resource Group: Create new
- Region: Choose nearest
- Pricing: Free tier available
- π Keys and Endpoint β Copy
Key 1andEndpoint
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 |
| Token | Type | Capabilities |
|---|---|---|
admin-seed-token |
π Admin | Token management + moderation |
user-seed-token |
π€ User | Image moderation only |
π 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"
}
}| 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 |
| Method | Endpoint | Access | Description |
|---|---|---|---|
POST |
/moderate |
π Any | Analyze image content |
| Score Range | Meaning | Action |
|---|---|---|
0.0 - 0.2 |
π’ Safe content | β Allow |
0.2 - 0.5 |
π‘ Potentially questionable | |
0.5 - 0.8 |
π Likely harmful | π« Block |
0.8 - 1.0 |
π΄ High confidence harmful | π¨ Block immediately |
π Collections Structure
{
"_id": ObjectId,
"token": "uuid-string",
"isAdmin": boolean,
"createdAt": datetime
}{
"_id": ObjectId,
"token": "uuid-string",
"endpoint": "/moderate",
"timestamp": datetime,
"metadata": {
"filename": "string",
"filesize": number
}
}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
# Add new dependency
cd backend && pip install new-package
pip freeze > requirements.txt
# Run tests
pytest
# View logs
docker-compose logs backendπ Common Issues & Solutions
- β
Verify
MONGO_URIformat - β Check MongoDB Atlas network access
- β Ensure database user permissions
- β Verify API key and endpoint
- β Check Azure subscription status
- β Ensure service quota isn't exceeded
- β Check if Docker daemon is running
- β Verify ports 7000 and 80 are available
- β
Ensure
.envfile exists
- β
Include
Authorization: Bearer <token>header - β Verify token exists in database
- β Check if endpoint requires admin privileges
- π Use strong, unique tokens
- π Enable HTTPS/TLS
- β±οΈ Implement rate limiting
- ποΈ Use production MongoDB cluster
- π Restrict CORS origins
- π Use secure secret management
- π Application performance monitoring
- π° Azure Content Safety usage tracking
- ποΈ Database performance metrics
- π¨ Error alerting system
We welcome contributions! Here's how to get started:
- π΄ Fork the repository
- πΏ Create feature branch (
git checkout -b feature/amazing-feature) - πΎ Commit changes (
git commit -m 'Add amazing feature') - π€ Push to branch (
git push origin feature/amazing-feature) - π Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
π Documentation β’ π¬ Discussions β’ π Issues β’ π§ Contact
Made with β€οΈ for safer digital content