SentinelAI / readme copy.md
sajith-0701's picture
initial deployment for HF Spaces
71c1ad2
metadata
title: Hubble AI Engine
emoji: πŸ”
colorFrom: blue
colorTo: indigo
sdk: docker
app_port: 7860
pinned: false

Hubble AI Engine β€” Cyberbullying Detection Pipeline

A production-grade, layered AI content moderation system for detecting cyberbullying across text, image, and video inputs. Designed as a universal safety tool for social media platforms.


πŸ—οΈ Architecture Overview

User Input (text/image/video)
  β†’ Preprocessing (normalization, frame extraction)
  β†’ Fast AI Filter (RoBERTa text, EfficientNet image) β€” ONNX optimized
  β†’ Risk Scoring Engine (0-100 composite score)
  β†’ LangGraph Router
      β”œβ”€ LOW (0-30)    β†’ Allow βœ…
      β”œβ”€ MEDIUM (31-65) β†’ Warning ⚠️
      └─ HIGH (66-100)  β†’ Deep Analysis πŸ”΄
          β”œβ”€ CLIP multimodal alignment
          β”œβ”€ Gemini reasoning (via LangChain)
          └─ Final verdict
  β†’ Decision Engine (severity + user history + rules)
  β†’ Response + Logging

Key Components

Layer Technology Purpose
Fast Filter RoBERTa (ONNX), EfficientNet (ONNX) Sub-200ms first-pass classification
Risk Scoring Custom weighted engine Composite score with category weights + user history
Routing LangGraph state machine Conditional deep analysis for HIGH-risk content only
Deep Analysis CLIP + Gemini (LangChain) Multimodal alignment + LLM contextual reasoning
Decision Engine Rule-based system ALLOWED / WARNING / BLOCKED with escalation logic
Observability LangSmith + structlog Full pipeline tracing and structured logging
Storage MongoDB (motor) + Redis Moderation logs, user history, result caching

πŸ“ Project Structure

Ai/
β”œβ”€β”€ app/
β”‚   β”œβ”€β”€ main.py                  # FastAPI app factory + lifespan
β”‚   β”œβ”€β”€ config.py                # Pydantic Settings (.env)
β”‚   β”œβ”€β”€ dependencies.py          # FastAPI dependency injection
β”‚   β”œβ”€β”€ api/                     # API layer
β”‚   β”‚   β”œβ”€β”€ router.py            # Route aggregator
β”‚   β”‚   β”œβ”€β”€ v1/
β”‚   β”‚   β”‚   β”œβ”€β”€ analyze.py       # POST /analyze/text, /image, /video
β”‚   β”‚   β”‚   β”œβ”€β”€ health.py        # GET /health
β”‚   β”‚   β”‚   └── history.py       # GET /history/{user_id}
β”‚   β”‚   └── schemas/
β”‚   β”‚       β”œβ”€β”€ requests.py      # Request models
β”‚   β”‚       └── responses.py     # Response models
β”‚   β”œβ”€β”€ pipeline/                # Core moderation pipeline
β”‚   β”‚   β”œβ”€β”€ preprocessor.py      # Input normalization
β”‚   β”‚   β”œβ”€β”€ fast_filter.py       # RoBERTa + EfficientNet inference
β”‚   β”‚   β”œβ”€β”€ risk_scorer.py       # Composite risk scoring
β”‚   β”‚   β”œβ”€β”€ deep_analyzer.py     # CLIP + Gemini deep analysis
β”‚   β”‚   β”œβ”€β”€ decision_engine.py   # Rule-based verdicts
β”‚   β”‚   └── workflow.py          # LangGraph state machine
β”‚   β”œβ”€β”€ models/                  # ML model management
β”‚   β”‚   β”œβ”€β”€ model_registry.py    # Singleton model loader
β”‚   β”‚   β”œβ”€β”€ text_model.py        # RoBERTa (ONNX)
β”‚   β”‚   β”œβ”€β”€ image_model.py       # EfficientNet (ONNX)
β”‚   β”‚   β”œβ”€β”€ clip_model.py        # OpenCLIP
β”‚   β”‚   └── onnx_utils.py        # ONNX export/inference
β”‚   β”œβ”€β”€ services/                # External integrations
β”‚   β”‚   β”œβ”€β”€ gemini_service.py    # Gemini via LangChain
β”‚   β”‚   β”œβ”€β”€ mongo_service.py     # MongoDB (async)
β”‚   β”‚   └── redis_service.py     # Redis (async)
β”‚   β”œβ”€β”€ observability/           # Monitoring
β”‚   β”‚   β”œβ”€β”€ langsmith.py         # LangSmith tracing
β”‚   β”‚   └── logging.py           # structlog config
β”‚   └── utils/                   # Helpers
β”‚       β”œβ”€β”€ image_utils.py       # Image preprocessing
β”‚       └── video_utils.py       # Video frame extraction
β”œβ”€β”€ tests/                       # Test suite
β”œβ”€β”€ model_cache/                 # Downloaded models (gitignored)
β”œβ”€β”€ _legacy/                     # Old code (preserved for reference)
β”œβ”€β”€ .env.example                 # Environment template
β”œβ”€β”€ requirements.txt             # Python dependencies
└── README.md                    # This file

πŸš€ Quick Start

1. Setup Python Environment

cd Ai
python -m venv venv
venv\Scripts\activate          # Windows
# source venv/bin/activate     # macOS/Linux
pip install -r requirements.txt

2. Configure Environment

cp .env.example .env
# Edit .env with your API keys and database URIs

3. Start Services (MongoDB + Redis)

# Using Docker
docker run -d -p 27017:27017 --name hubble-mongo mongo:7
docker run -d -p 6379:6379 --name hubble-redis redis:7-alpine

4. Run the Server

cd Ai
python -m app.main
# Or: uvicorn app.main:app --reload --port 8000

5. Test the API

# Health check
curl http://localhost:8000/health

# Analyze text
curl -X POST http://localhost:8000/api/v1/analyze/text \
  -H "Content-Type: application/json" \
  -d '{"text": "You are worthless", "user_id": "user123"}'

# Analyze image
curl -X POST http://localhost:8000/api/v1/analyze/image \
  -F "file=@test.jpg" -F "user_id=user123"

πŸ“‘ API Endpoints

Method Endpoint Description
GET /health Health check with model/service status
GET /health/ping Lightweight liveness probe
POST /api/v1/analyze/text Analyze text for cyberbullying
POST /api/v1/analyze/image Analyze image for harmful content
POST /api/v1/analyze/video Analyze video (frame extraction)
GET /api/v1/history/{user_id} Get moderation history
GET /api/v1/history/{user_id}/summary Get aggregated user stats

Response Schema

All /analyze/* endpoints return a unified AnalysisResponse:

{
  "request_id": "req_abc123",
  "input_type": "text",
  "status": "WARNING",
  "risk_level": "MEDIUM",
  "risk_score": 45.2,
  "categories": ["insult", "toxic"],
  "confidence": 0.82,
  "decision": {
    "action": "WARNING",
    "reason": "Content flagged as potentially harmful",
    "severity": "medium",
    "should_alert_parent": false
  },
  "processing_time_ms": 156,
  "cached": false
}

πŸ§ͺ Running Tests

cd Ai
python -m pytest tests/ -v

πŸ“Š Models Used

Model Purpose Size Backend
unitary/toxic-bert Text toxicity (6 labels) ~450 MB ONNX
google/efficientnet-b0 Image classification ~20 MB ONNX
openai/clip-vit-base-patch32 Multimodal alignment ~600 MB PyTorch
gemini-2.0-flash Deep contextual reasoning Cloud API LangChain

πŸ”’ Security Notes

  • API keys loaded from .env only (never hardcoded)
  • CORS restricted in production mode
  • User data isolated by user_id
  • All moderation events logged for audit

Built for the National Hackathon β€” SentinelAI Project