CoolFace
Modelpublic

Pommu/threat-detection-jigsaw

sourceHugging Facemitupdated 4mo agoView on Hugging Face
0likes8downloads
Model Card

๐Ÿ›ก๏ธ Context-Aware Threat & Compliance Detection in Conversational Text

NLP Course Project โ€” Individual Assignment Model: Fine-tuned roberta-base | Task: 6-class Multi-label Classification Dataset: google/jigsaw_toxicity_pred (~150K samples)


๐ŸŒ Live Demo

ResourceURL
๐Ÿ–ฅ๏ธ Interactive UI (HuggingFace Spaces)https://huggingface.co/spaces/Pommu/threat-detection-jigsaw
๐Ÿค– Fine-tuned Modelhttps://huggingface.co/Pommu/threat-detection-jigsaw
๐Ÿ”Œ Local APIhttp://localhost:8000/predict (POST)

๐Ÿ“Œ Problem Statement

Modern organizations face increasing risks from implicit threats and workplace harassment. Unlike simple slur-detectors, this project focuses on intent-based detection. It uses the Jigsaw Toxic Comment dataset to identify professional and personal risk across 6 binary categories:

LabelDescriptionRisk Level
ThreateningViolent intent, intimidation, physical danger๐Ÿ”ด HIGH
Hate SpeechIdentity-based attacks, protected groups๐Ÿ”ด HIGH
Highly SevereExtreme toxicity, highly disruptive๐Ÿ”ด HIGH
ToxicRude, disrespectful, unprofessional๐ŸŸก MEDIUM
InsultPersonal attacks, non-violent harassment๐ŸŸก MEDIUM
ProfanityObscene language, compliance violation๐ŸŸก LOW

๐Ÿ—๏ธ Architecture: The 2-Layer Safety Net

[User Input: Text / Chat Transcript]
         โ”‚
         โ–ผ
   [Gradio UI] โ”€โ”€ HTTP POST /predict โ”€โ”€โ–ถ [FastAPI Backend]
                                                โ”‚
                                   โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                                   โ”‚                         โ”‚
                             [Layer 1]                 [Layer 2]
                       Fine-tuned RoBERTa          Lexical Booster
                       (6 Jigsaw labels)       (keyword pattern match)
                                   โ”‚                         โ”‚
                                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                                โ”‚
                                  {label, confidence, risk_level}
  1. 1.Layer 1 โ€” Weighted RoBERTa: Multi-label model fine-tuned with 50x class weights on the rare threat class (0.3% of data) to maximize recall.
  2. 2.Layer 2 โ€” Lexical Booster: Backend safety layer that catches ominous linguistic patterns (e.g., "watch your back", "you'll regret") unconditionally, regardless of model score.

๐Ÿ“Š Evaluation Results

Design Choice: We prioritize Recall over Precision for threats. A false positive (flagging safe text) is far less costly than a false negative (missing a real threat).

๐Ÿš€ API Usage

bash
# Test implicit threat detection locally
curl -X POST http://localhost:8000/predict \
     -H "Content-Type: application/json" \
     -d '{"text": "I will find you and you will regret every decision you have made."}'

JSON Response:

json
{
  "prediction": "Threatening",
  "confidence": 0.85,
  "is_threat": true,
  "risk_level": "HIGH",
  "all_scores": [
    {"label": "Toxic", "confidence": 0.0009},
    {"label": "Threatening", "confidence": 0.85},
    ...
  ]
}

๐Ÿ“‚ Project Structure

NLP Course Project/
โ”œโ”€โ”€ notebook/
โ”‚   โ””โ”€โ”€ train_3.ipynb         โ† Colab training notebook (WeightedTrainer)
โ”œโ”€โ”€ app/
โ”‚   โ”œโ”€โ”€ main.py               โ† FastAPI backend + Lexical Booster
โ”‚   โ”œโ”€โ”€ model_loader.py       โ† Singleton model loader (thread-safe)
โ”‚   โ”œโ”€โ”€ schemas.py            โ† Pydantic request/response models
โ”‚   โ””โ”€โ”€ demo.py               โ† Gradio UI (standalone OR API-backed)
โ”œโ”€โ”€ spaces/
โ”‚   โ”œโ”€โ”€ app.py                โ† HuggingFace Spaces entry point
โ”‚   โ””โ”€โ”€ requirements.txt
โ”œโ”€โ”€ requirements.txt          โ† API/demo dependencies
โ”œโ”€โ”€ requirements_training.txt โ† Training dependencies (Colab)
โ”œโ”€โ”€ setup_local.bat           โ† Windows one-click environment setup
โ””โ”€โ”€ README.md

โš™๏ธ Local Setup (Windows)

bat
# Option A: one-click setup
setup_local.bat

# Option B: manual
python -m venv .venv
.venv\Scripts\activate
pip install -r requirements.txt

Set your .env file:

env
HF_TOKEN=your_token_here
MODEL_NAME=Pommu/threat-detection-jigsaw

๐Ÿƒ Running Locally

bash
# Terminal 1 โ€” Start the FastAPI backend
uvicorn app.main:app --reload

# Terminal 2 โ€” Launch the Gradio demo
python app/demo.py

Open http://localhost:7860 to see the UI.


โ˜๏ธ Training on Colab

  1. 1.Open notebook/train_3.ipynb in Google Colab
  2. 2.Enable GPU accelerator (Tesla T4)
  3. 3.Add HF_Token to Colab Secrets (๐Ÿ”‘ icon)
  4. 4.Run all cells โ€” model auto-pushes to HuggingFace Hub

๐ŸŒ Deploy to HuggingFace Spaces

  1. 1.Go to huggingface.co/new-space โ†’ SDK: Gradio
  2. 2.In Space Settings โ†’ Variables: MODEL_NAME = Pommu/threat-detection-jigsaw
  3. 3.Push spaces/ contents to the Space repo

๐Ÿ“š Technologies Used

ComponentTechnology
Modelroberta-base (HuggingFace Transformers)
TrainingHuggingFace Trainer API + WeightedTrainer (custom)
Datasetgoogle/jigsaw_toxicity_pred (150K samples, 6 labels)
Backend APIFastAPI + Uvicorn
UIGradio
DeploymentHuggingFace Spaces
Evaluationscikit-learn (Precision/Recall/F1/Confusion Matrix)