An LLM-based pipeline to detect toxic speech.
Project description
Toxicity Detector
An LLM-based pipeline to detect toxic speech using language models.
Setup
This project uses uv for dependency management.
Prerequisites
- Python 3.12 or higher
- uv package manager
Installation
-
Install uv (if not already installed):
-
Clone the repository:
git clone https://github.com/debatelab/toxicity-detector.git cd toxicity-detector
-
Install dependencies:
uv syncThis will create a virtual environment and install all dependencies specified in
pyproject.toml. -
Install development dependencies (optional):
uv sync --group dev
Environment Variables
Create a .env file in the project root with the following variables:
# API Keys (by the names as specified in the model config files)
# Optional: Custom app config file path
TOXICITY_DETECTOR_APP_CONFIG_FILE=./config/app_config.yaml
Configuration of the Pipeline
#TODO: Add section on configuring the pipeline using YAML files in the config/ directory.
Running the Pipeline
from toxicity_detector import detect_toxicity, PipelineConfig
# Load pipeline configuration from YAML file
pipeline_config = PipelineConfig.from_file('./config/pipeline_config.yaml')
# The text to analyze for toxicity
input_text = 'Peter is dumn.'
# Run toxicity detection
result = detect_toxicity(
input_text=input_text, # The text to be analyzed
user_input_source=None, # Optional: identifier for the source of the input (e.g., 'chat', 'comment')
toxicity_type='personalized_toxicity', # Type of toxicity analysis to perform
context_info=None, # Optional: additional context about the conversation or situation
pipeline_config=pipeline_config, # Configuration specifying model, paths, and behavior
serialize_result=True, # If True, saves the result to disk as YAML
)
# Display the analysis result and toxicity verdict
print(result.answer['contains_toxicity'])
We also provide an example notebook that demonstrates how to run the toxicity detection pipeline with a Hugging Face API key.
Running the Gradio App
The project includes a Gradio web interface for interactive toxicity detection.
Basic Usage
Run the app using uv:
uv run python src/toxicity_detector/app.py
The app will start and be accessible at http://localhost:7860 by default.
Alternative: Using the uv shell
You can also activate the virtual environment and run the app directly:
# Activate the virtual environment
source .venv/bin/activate # On Linux/Mac
# or
.venv\Scripts\activate # On Windows
# Run the app
python src/toxicity_detector/app.py
# or (enables live reloading)
gradio src/toxicity_detector/app.py
Developer Mode
To enable developer mode with additional configuration options, update your config/app_config.yaml:
developer_mode: true
Project Structure
toxicity-detector/
├── config/ # Configuration files
│ ├── app_config.yaml # App configuration
│ └── default_model_config_*.yaml # Model configurations
├── src/
│ └── toxicity_detector/
│ ├── __init__.py
│ ├── app.py # Gradio web interface
│ ├── backend.py # Core detection logic
│ └── chains.py # LangChain pipelines
├── logs/ # Application logs
├── notebooks/ # Jupyter notebooks for testing
├── pyproject.toml # Project dependencies
└── README.md # This file
Development
Code Style
The project follows PEP 8 guidelines with a maximum line length of 88 characters.
Run linting checks:
uv run flake8 src/
Running Tests
Run all tests:
uv run pytest
Run tests with verbose output:
uv run pytest -v
Run a specific test file:
uv run pytest tests/test_config.py
Run tests with coverage report:
uv run pytest --cov=src/toxicity_detector
Alternative: Using the activated virtual environment:
# Activate the virtual environment first
source .venv/bin/activate # On Linux/Mac
# or
.venv\Scripts\activate # On Windows
# Then run pytest directly
pytest tests/
pytest tests/test_config.py -v
Working with Notebooks
To use Jupyter notebooks for development:
# Install dev dependencies if not already done
uv sync --group dev
# Start Jupyter
uv run jupyter notebook notebooks/
License
See LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file toxicity_detector-0.1.0b1.tar.gz.
File metadata
- Download URL: toxicity_detector-0.1.0b1.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
569cea98a987a01c2c2db1fc35c732cc350e3d0a0acafb10f5446f2ca09da9de
|
|
| MD5 |
c1f8d2937d27cc466c20a5e3a8fe2704
|
|
| BLAKE2b-256 |
1ecd705ac76bc961cec8b2cacfa64c11eb81a80ae7f8fad245c18c9db3033e1d
|
File details
Details for the file toxicity_detector-0.1.0b1-py3-none-any.whl.
File metadata
- Download URL: toxicity_detector-0.1.0b1-py3-none-any.whl
- Upload date:
- Size: 25.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb554835962e52dc4929c3c01c2c6f1551805d308bcb6e3635d67146f26ccb33
|
|
| MD5 |
ef916833df3e1707a47c5ab09305ac09
|
|
| BLAKE2b-256 |
5976e1817de3c2aba195d18d14a60287bf2c401b2218dd6761981d3f8f9d3d29
|