Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

28 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

OffensEval NLP Banner

๐Ÿšจ OffensEval NLP

Transformer-Based Offensive Language Detection with Sentiment-Aware Modeling

A cloud-deployed NLP application for real-time offensive language detection using DistilBERT sentence embeddings, VADER sentiment features, and a Logistic Regression classifier.

The system is implemented using a production-style architecture with a FastAPI inference backend, an interactive Streamlit frontend, Docker containers, Azure Container Registry, and Azure Container Apps.


๐ŸŒ Live Application

Frontend

https://offenseval-frontend.icysea-bc6cd350.centralindia.azurecontainerapps.io/

Backend API Documentation

https://offenseval-backend.icysea-bc6cd350.centralindia.azurecontainerapps.io/docs

Backend Health Endpoint

https://offenseval-backend.icysea-bc6cd350.centralindia.azurecontainerapps.io/health

The live application provides real-time offensive language classification, confidence scores, cleaned-text visibility, and sentiment-aware output.


๐Ÿ“Œ Project Overview

OffensEval NLP is an end-to-end machine learning application designed to identify offensive language in user-provided text.

The project combines transformer-based semantic representations with sentiment information to improve contextual understanding. DistilBERT sentence embeddings are generated through SentenceTransformers, while VADER sentiment scores are added as an additional numerical feature. The combined feature vector is passed to a Logistic Regression classifier for binary prediction.

The application is divided into two independently deployed services:

  • A FastAPI backend responsible for preprocessing, model loading, sentiment analysis, and prediction
  • A Streamlit frontend responsible for user interaction and result presentation

Both services are containerized using Docker, stored in Azure Container Registry, and deployed through Azure Container Apps.


๐ŸŽฏ Key Features

  • ๐Ÿ” DistilBERT sentence embeddings through SentenceTransformers
  • ๐Ÿ˜Š Sentiment-aware feature augmentation using VADER
  • โš–๏ธ Interpretable Logistic Regression classifier
  • ๐Ÿ“Š Prediction confidence and sentiment distribution
  • ๐Ÿงน Centralized text preprocessing
  • โšก FastAPI REST inference service
  • ๐ŸŒ Interactive Streamlit frontend
  • ๐Ÿณ Dockerized frontend and backend
  • โ˜๏ธ Deployment through Azure Container Apps
  • ๐Ÿ“ฆ Container storage through Azure Container Registry
  • ๐Ÿงช Automated testing with PyTest
  • โค๏ธ Backend health monitoring
  • ๐Ÿ“ Modular production-style repository structure

๐Ÿ—๏ธ System Architecture

                         User
                           โ”‚
                           โ–ผ
                  Streamlit Frontend
                  Azure Container App
                           โ”‚
                    HTTPS REST API
                           โ”‚
                           โ–ผ
                   FastAPI Backend
                 Azure Container App
                           โ”‚
             Text Cleaning and Normalization
                           โ”‚
                           โ–ผ
          DistilBERT Sentence Embeddings (768D)
                           โ”‚
                   โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                   โ”‚               โ”‚
                   โ–ผ               โ–ผ
           Semantic Features   VADER Sentiment
                   โ”‚               โ”‚
                   โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                           โ–ผ
             Feature Concatenation (769D)
                           โ”‚
                           โ–ผ
              Logistic Regression Model
                           โ”‚
                           โ–ผ
           Prediction, Confidence, Sentiment

๐Ÿง  Machine Learning Pipeline

Input Text
   โ†“
Text Cleaning and Normalization
   โ†“
DistilBERT Sentence Embedding
   โ†“
VADER Sentiment Feature
   โ†“
Feature Concatenation
   โ†“
Logistic Regression Classification
   โ†“
Offensive / Not Offensive Prediction

Feature Dimensions

Component Dimensions
DistilBERT sentence embedding 768
VADER compound sentiment score 1
Final feature vector 769

๐Ÿ› ๏ธ Technology Stack

Category Technology
Programming Language Python 3.12
Backend Framework FastAPI
Frontend Framework Streamlit
ASGI Server Uvicorn
Machine Learning Scikit-learn
Embeddings SentenceTransformers
Transformer Model DistilBERT
Sentiment Analysis VADER
Validation Pydantic
Testing PyTest
Containerization Docker
Local Orchestration Docker Compose
Container Registry Azure Container Registry
Cloud Deployment Azure Container Apps
Version Control Git and GitHub

๐Ÿ“Š Dataset

  • Dataset: TweetEval โ€“ Offensive Language

  • Task: Binary text classification

  • Classes:

    • Offensive
    • Not Offensive

TweetEval provides benchmark datasets for evaluating language models on social media classification tasks. This project uses the offensive language subset for supervised model development and validation.


๐Ÿ“ˆ Model Performance

Metric Validation Score
Macro F1 ~0.72
ROC-AUC ~0.81

The reported values are based on a held-out validation set used during model development.


๐ŸŒ Web Application Features

The Streamlit frontend provides:

  • Preprocessed text preview
  • Offensive or non-offensive prediction
  • Prediction confidence
  • VADER compound sentiment score
  • Positive, neutral, and negative sentiment percentages
  • Sentiment category
  • Color-coded sentiment visualizations
  • Backend availability check
  • Error handling for failed API requests
  • Clear usage disclaimer

โšก REST API

The FastAPI backend exposes the following endpoints:

Method Endpoint Description
GET / API information
GET /health Backend and model health check
POST /predict Offensive language prediction

๐Ÿ“ฅ Example Prediction Request

curl -X POST \
  "https://offenseval-backend.icysea-bc6cd350.centralindia.azurecontainerapps.io/predict" \
  -H "Content-Type: application/json" \
  -d '{
    "text": "You are an idiot."
  }'

๐Ÿ“ค Example Prediction Response

{
  "prediction": "Offensive",
  "confidence": 0.94,
  "cleaned_text": "you are an idiot",
  "sentiment_score": -0.73,
  "sentiment_category": "Negative",
  "positive_score": 0.0,
  "neutral_score": 0.29,
  "negative_score": 0.71
}

Exact prediction values depend on the trained model and input text.


๐Ÿ“ Repository Structure

offenseval-nlp/
โ”‚
โ”œโ”€โ”€ assets/
โ”‚   โ””โ”€โ”€ offenseval-nlp-banner.png
โ”‚
โ”œโ”€โ”€ backend/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ config.py
โ”‚   โ”œโ”€โ”€ inference.py
โ”‚   โ”œโ”€โ”€ main.py
โ”‚   โ”œโ”€โ”€ preprocessing.py
โ”‚   โ””โ”€โ”€ schemas.py
โ”‚
โ”œโ”€โ”€ frontend/
โ”‚   โ””โ”€โ”€ app.py
โ”‚
โ”œโ”€โ”€ embeddings/
โ”‚   โ”œโ”€โ”€ X_train_distil.npy
โ”‚   โ”œโ”€โ”€ X_val_distil.npy
โ”‚   โ”œโ”€โ”€ X_test_distil.npy
โ”‚   โ”œโ”€โ”€ X_train_minilm.npy
โ”‚   โ”œโ”€โ”€ X_val_minilm.npy
โ”‚   โ”œโ”€โ”€ X_test_minilm.npy
โ”‚   โ”œโ”€โ”€ y_train.npy
โ”‚   โ”œโ”€โ”€ y_val.npy
โ”‚   โ””โ”€โ”€ y_test.npy
โ”‚
โ”œโ”€โ”€ model/
โ”‚   โ”œโ”€โ”€ final_model_tuned_distilbert.joblib
โ”‚   โ””โ”€โ”€ label_encoder.json
โ”‚
โ”œโ”€โ”€ tests/
โ”‚   โ”œโ”€โ”€ __init__.py
โ”‚   โ”œโ”€โ”€ pytest.ini
โ”‚   โ”œโ”€โ”€ test_api.py
โ”‚   โ”œโ”€โ”€ test_inference.py
โ”‚   โ””โ”€โ”€ test_preprocessing.py
โ”‚
โ”œโ”€โ”€ Dockerfile.backend
โ”œโ”€โ”€ Dockerfile.frontend
โ”œโ”€โ”€ docker-compose.yml
โ”œโ”€โ”€ requirements.txt
โ”œโ”€โ”€ Model_Training_and_Evaluation.ipynb
โ”œโ”€โ”€ .dockerignore
โ”œโ”€โ”€ .gitignore
โ”œโ”€โ”€ LICENSE
โ””โ”€โ”€ README.md

๐Ÿš€ Running the Project Locally

1. Clone the repository

git clone https://github.com/ArunavaKumar/offenseval-nlp.git
cd offenseval-nlp

2. Create a virtual environment

python -m venv .venv
source .venv/bin/activate

For Windows:

.venv\Scripts\activate

3. Install dependencies

pip install --upgrade pip
pip install -r requirements.txt

4. Start the FastAPI backend

uvicorn backend.main:app --reload

The backend will be available at:

http://127.0.0.1:8000

API documentation:

http://127.0.0.1:8000/docs

5. Start the Streamlit frontend

Open another terminal and run:

streamlit run frontend/app.py

The frontend will usually be available at:

http://localhost:8501

๐Ÿณ Running with Docker Compose

Build and start both services:

docker compose up --build

The services will be available at:

Service Local Address
Streamlit frontend http://localhost:8501
FastAPI backend http://localhost:8000
FastAPI documentation http://localhost:8000/docs

Stop the services with:

docker compose down

๐Ÿงช Automated Testing

Run the complete test suite:

python -m pytest -v

The tests validate:

  • Root endpoint availability
  • Health endpoint response
  • Prediction endpoint response
  • Model loading
  • Request and response schema compatibility
  • Text preprocessing
  • Prediction confidence range
  • Sentiment score output

โ˜๏ธ Azure Deployment Architecture

Local Development
       โ”‚
       โ–ผ
Docker Images
       โ”‚
       โ–ผ
Azure Container Registry
       โ”‚
       โ”œโ”€โ”€ offenseval-backend:v1
       โ”‚
       โ””โ”€โ”€ offenseval-frontend:v1
       โ”‚
       โ–ผ
Azure Container Apps Environment
       โ”‚
       โ”œโ”€โ”€ FastAPI Backend Container App
       โ”‚
       โ””โ”€โ”€ Streamlit Frontend Container App
       โ”‚
       โ–ผ
Public HTTPS Endpoints

Deployed Azure Resources

Resource Name
Resource Group offenseval-rg
Container Registry offensevalacr
Container Apps Environment offenseval-env
Backend Container App offenseval-backend
Frontend Container App offenseval-frontend
Azure Region Central India

๐Ÿ”„ Deployment Workflow

After modifying the application, rebuild and push the updated Docker image.

Backend example

docker build \
  -f Dockerfile.backend \
  -t offensevalacr.azurecr.io/offenseval-backend:v2 \
  .
docker push offensevalacr.azurecr.io/offenseval-backend:v2
az containerapp update \
  --name offenseval-backend \
  --resource-group offenseval-rg \
  --image offensevalacr.azurecr.io/offenseval-backend:v2

Frontend example

docker build \
  -f Dockerfile.frontend \
  -t offensevalacr.azurecr.io/offenseval-frontend:v2 \
  .
docker push offensevalacr.azurecr.io/offenseval-frontend:v2
az containerapp update \
  --name offenseval-frontend \
  --resource-group offenseval-rg \
  --image offensevalacr.azurecr.io/offenseval-frontend:v2

๐Ÿฉบ Health Monitoring

The backend exposes a health endpoint:

GET /health

Example response:

{
  "status": "healthy",
  "model_ready": true
}

This endpoint verifies that:

  • The FastAPI service is running
  • The trained model has loaded successfully
  • The backend is ready to accept prediction requests

๐Ÿ” Environment Configuration

The frontend connects to the backend through the API_URL environment variable.

Local default:

http://127.0.0.1:8000

Azure deployment value:

https://offenseval-backend.icysea-bc6cd350.centralindia.azurecontainerapps.io

Example:

export API_URL=https://offenseval-backend.icysea-bc6cd350.centralindia.azurecontainerapps.io

โš ๏ธ Disclaimer and Limitations

This application predicts offensive language based on patterns learned from social media text.

Known limitations include:

  • Sarcasm and humor may be interpreted incorrectly
  • Cultural and regional language differences may be missed
  • Indirect insults may not always be detected
  • Quoted offensive language may be classified without understanding intent
  • Very short or ambiguous text may produce uncertain predictions
  • Sentiment does not always correspond directly to offensiveness
  • Model outputs may reflect biases present in the training data

The application is intended to support content analysis and experimentation. It should not replace human review in high-impact moderation or disciplinary decisions.


๐Ÿ”ฎ Future Improvements

  • Multi-class toxicity detection
  • Hate-speech category classification
  • Explainable AI using SHAP or LIME
  • Batch prediction endpoint
  • Authentication and rate limiting
  • GitHub Actions CI/CD
  • Azure Monitor and Application Insights
  • Structured application logging
  • Model version tracking
  • Drift monitoring
  • Database-backed prediction history
  • Kubernetes deployment
  • Transformer fine-tuning instead of fixed embeddings

๐Ÿ‘จโ€๐Ÿ’ป Developer

Arunava Kumar Chakraborty

Data Analyst | Machine Learning Enthusiast


๐Ÿ“œ License

This project is licensed under the MIT License.

See the LICENSE file for details.


โญ Acknowledgements

  • Hugging Face
  • SentenceTransformers
  • DistilBERT
  • VADER Sentiment Analysis
  • TweetEval
  • FastAPI
  • Streamlit
  • Docker
  • Microsoft Azure

About

Transformer-based offensive language detection using DistilBERT embeddings, sentiment analysis, CI/CD testing, and an interactive Streamlit web application.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages