Skip to content
This repository was archived by the owner on Jan 13, 2026. It is now read-only.

wolffbe/twitter-hate-speech-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Hate Speech Detection on Twitter API

Overview

This project provides a Dockerized Flask service for detecting hate speech in tweets using the cardiffnlp/twitter-roberta-base-hate-latest model. The API accepts a tweet in the request body and returns 0 for non-hate speech and 1 for hate speech.

Features

  • Flask backend for serving the hate speech detection model.
  • Preprocessing to clean and standardize tweets before analysis.
  • Truncation Handling: Tweets longer than 512 tokens are automatically truncated.
  • Docker Support: Easily deployable as a container.

Requirements

  • Docker (for containerized execution)
  • Alternatively, Python 3.9+ with dependencies installed

Installation & Setup

Note: Downloading dependencies can take up to 20 minutes, depending on your internet speed.

Docker Installation

  1. Clone this repository:
    git clone https://github.com/wolffbe/twitter-hate-speech-api.git
    cd twitter-hate-speech-api
  2. Build the Docker image:
    docker build -t twitter-hate-detector .
  3. Run the container:
    docker run -p 8000:8000 twitter-hate-detector

Local Installation (without Docker)

  1. Install dependencies:
    pip install -r requirements.txt
  2. Start the Flask server:
    python main.py

API Usage

Endpoint: POST /detect

Request Body:

{
  "tweet": "Your tweet text here"
}

Response:

{
  "hate_speech": 0  // or 1 for hate speech
}

Example Requests

Hate Speech Example

An example of a hate speech detection request:

Linux/macOS:
curl -X POST http://localhost:8000/detect -H "Content-Type: application/json" -d '{"tweet": "Illegal immigrants steal!"}'
Windows Command Prompt:
curl -X POST http://localhost:8000/detect -H "Content-Type: application/json" -d "{\"tweet\":\"Illegal immigrants steal!\"}"
Expected Response
{
  "hate_speech": 1
}

Non-Hate Speech Example

An example of a non-hate speech detection request:

Linux/macOS:
curl -X POST http://localhost:8000/detect -H "Content-Type: application/json" -d '{"tweet": "I hope you have a wonderful day!"}'
Windows Command Prompt:
curl -X POST http://localhost:8000/detect -H "Content-Type: application/json" -d "{\"tweet\":\"I hope you have a wonderful day!\"}"
Expected Response
{
  "hate_speech": 0
}

About

Hate speech detection using locally hosted LLMs

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors