A lightweight Laravel library that wraps AI-based toxicity detection engines to evaluate and moderate user-generated content within your application.
mohammed-abd-razaq/laravel-toxicity-filter is a Laravel package for a lightweight laravel library that wraps ai-based toxicity detection engines to evaluate and moderate user-generated content within your application..
It currently has 0 GitHub stars and 2 downloads on Packagist (latest version 1.1.1).
Install it with composer require mohammed-abd-razaq/laravel-toxicity-filter.
Discover more Laravel packages by mohammed-abd-razaq
or browse all Laravel packages to compare alternatives.
Last updated
A professional Laravel library that integrates AI-based toxicity detection engines to automatically evaluate, moderate, and filter user-generated content such as comments, posts, messages, and reviews within your application.
composer require mohammed-abd-razaq/laravel-toxicity-filter
Or if using the local package, update your root composer.json:
{
"require": {
"packages/toxicity-filter": "^1.0"
}
}
composer update
php artisan vendor:publish --tag=toxicity-filter-config
php artisan vendor:publish --tag=toxicity-filter-migrations
php artisan migrate
php artisan config:clear
Set up your AI provider API keys in .env:
# OpenAI Configuration
OPENAI_API_KEY=your_openai_api_key
OPENAI_MODERATION_MODEL=text-moderation-latest
# Google Perspective API Configuration
PERSPECTIVE_API_KEY=your_perspective_api_key
# Toxicity Thresholds (0.0 - 1.0)
TOXICITY_BLOCK_THRESHOLD=0.8
TOXICITY_FLAG_THRESHOLD=0.6
TOXICITY_WARN_THRESHOLD=0.4
# Language-Specific Thresholds
TOXICITY_ARABIC_BLOCK_THRESHOLD=0.8
TOXICITY_ARABIC_FLAG_THRESHOLD=0.6
TOXICITY_ARABIC_WARN_THRESHOLD=0.4
TOXICITY_ENGLISH_BLOCK_THRESHOLD=0.8
TOXICITY_ENGLISH_FLAG_THRESHOLD=0.6
TOXICITY_ENGLISH_WARN_THRESHOLD=0.4
# Caching
TOXICITY_CACHE_ENABLED=true
TOXICITY_CACHE_TTL=3600
# Logging
TOXICITY_LOGGING_ENABLED=true
TOXICITY_STORE_CONTENT=false
use Packages\ToxicityFilter\Facades\ToxicityFilter;
// Analyze English content
$result = ToxicityFilter::analyze("This is some content to check");
echo $result->getToxicityScore(); // 0.85
echo $result->getProvider(); // 'openai'
var_dump($result->getCategories()); // ['harassment', 'hate']
// Analyze Arabic content (automatic language detection)
$arabicResult = ToxicityFilter::analyze("ู
ุฑุญุจุง ุจุงูุนุงูู
");
// Analyze multilingual content
$multilingualResult = ToxicityFilter::analyze("Hello ู
ุฑุญุจุง world");
// Quick checks (uses language-specific thresholds)
if (ToxicityFilter::shouldBlock($content)) {
// Block the content
}
if (ToxicityFilter::shouldFlag($content)) {
// Flag for manual review
}
if (ToxicityFilter::shouldWarn($content)) {
// Show warning to user
}
// Use OpenAI specifically
$result = ToxicityFilter::analyze($content, 'openai');
// Use Perspective API specifically
$result = ToxicityFilter::analyze($content, 'perspective');
// Get available providers
$providers = ToxicityFilter::getAvailableProviders();
The package includes optional middleware for automatic content filtering. To use it, you need to manually register it first.
Add to your app/Http/Kernel.php:
// In app/Http/Kernel.php
protected $routeMiddleware = [
// ... other middleware
'toxicity-filter' => \Packages\ToxicityFilter\Middleware\ToxicityFilterMiddleware::class,
];
// In your routes file
Route::post('/comments', [CommentController::class, 'store'])
->middleware('toxicity-filter');
// Or specify fields to check
Route::post('/posts', [PostController::class, 'store'])
->middleware('toxicity-filter:title,content,description');
The middleware will:
use Packages\ToxicityFilter\Contracts\ToxicityFilterInterface;
class ContentModerationService
{
public function __construct(
private ToxicityFilterInterface $toxicityFilter
) {}
public function moderateComment(string $content, User $user): array
{
$result = $this->toxicityFilter->analyze($content);
$response = [
'allowed' => true,
'message' => null,
'requires_review' => false,
];
if ($result->shouldBlock(0.8)) {
$response['allowed'] = false;
$response['message'] = 'Content blocked due to inappropriate language';
} elseif ($result->shouldFlag(0.6)) {
$response['requires_review'] = true;
$response['message'] = 'Content flagged for review';
}
return $response;
}
}
The package includes native support for Arabic content with automatic language detection and text normalization:
// Arabic content is automatically detected
$arabicContent = "ู
ุฑุญุจุง ุจุงูุนุงูู
";
$result = ToxicityFilter::analyze($arabicContent);
// Language is automatically detected as 'ar'
// Multilingual content is supported
$mixedContent = "Hello ู
ุฑุญุจุง world";
$result = ToxicityFilter::analyze($mixedContent);
// Primary language is determined based on character count
The package automatically normalizes Arabic text for better analysis:
// Raw Arabic text with diacritics
$rawArabic = "ู
ูุฑูุญูุจุงู ุจูุงูุนูุงููู
ู";
// Package automatically normalizes for analysis
$result = ToxicityFilter::analyze($rawArabic);
Configure different toxicity thresholds for Arabic and English content:
// In config/toxicity-filter.php
'languages' => [
'thresholds' => [
'ar' => [
'block' => 0.8, // Arabic blocking threshold
'flag' => 0.6, // Arabic flagging threshold
'warn' => 0.4, // Arabic warning threshold
],
'en' => [
'block' => 0.8, // English blocking threshold
'flag' => 0.6, // English flagging threshold
'warn' => 0.4, // English warning threshold
],
],
],
You can also use the language detection service directly:
use Packages\ToxicityFilter\Services\LanguageDetectionService;
$detector = new LanguageDetectionService();
$language = $detector->detectLanguage("ู
ุฑุญุจุง ุจุงูุนุงูู
"); // 'ar'
$isArabic = $detector->isArabic("ู
ุฑุญุจุง"); // true
$isMultilingual = $detector->isMultilingual("Hello ู
ุฑุญุจุง"); // true
$normalized = $detector->normalizeArabicText("ู
ูุฑูุญูุจุงู"); // "ู
ุฑุญุจุง"
For async processing, you can dispatch jobs:
use Packages\ToxicityFilter\Jobs\AnalyzeToxicityJob;
// Process large content asynchronously
AnalyzeToxicityJob::dispatch($content, $userId, $options);
The package offers extensive configuration options:
The package creates a toxicity_detections table to log all analysis results:
id (primary key)
provider (string, indexed)
toxicity_score (decimal, indexed)
categories (json)
content_hash (text, indexed)
content (text, optional)
metadata (json)
action_taken (string, indexed)
user_id (bigint, nullable, indexed)
ip_address, user_agent, request_path
timestamps
Implement the ToxicityProviderInterface:
use Packages\ToxicityFilter\Contracts\ToxicityProviderInterface;
use Packages\ToxicityFilter\ValueObjects\ToxicityResult;
class CustomProvider implements ToxicityProviderInterface
{
public function analyze(string $content, array $options = []): ToxicityResult
{
// Implement your provider logic
}
public function getName(): string
{
return 'custom';
}
// ... implement other interface methods
}
# Run package tests
cd packages/toxicity-filter
composer test
# Run with coverage
composer test-coverage
# Run specific test file
vendor/bin/phpunit tests/Unit/ToxicityFilterServiceTest.php
# Run tests with debug output
vendor/bin/phpunit --debug
Create a .env.testing file for test environment:
TOXICITY_CACHE_ENABLED=false
TOXICITY_LOGGING_ENABLED=false
OPENAI_API_KEY=test_key
PERSPECTIVE_API_KEY=test_key
1. Configuration not loaded
php artisan config:clear
php artisan config:cache
2. Provider API errors
.env3. Migration issues
php artisan migrate:rollback
php artisan vendor:publish --tag=toxicity-filter-migrations --force
php artisan migrate
4. Cache issues
php artisan cache:clear
php artisan config:clear
Enable debug logging in your configuration:
'debug' => env('TOXICITY_DEBUG', false),
'log_level' => env('TOXICITY_LOG_LEVEL', 'info'),
Arabic Language Support
Initial Release
We welcome contributions! Please see our Contributing Guide for details.
composer install.env.example to .env and configurecomposer testThis package is open-sourced software licensed under the MIT License.
Mohammed Abd Razaq
โญ If you find this package helpful, please consider giving it a star on GitHub!