A Python application that demonstrates real-time text content moderation using the KoalaAI/Text-Moderation model with a Gradio web interface.
-
Text Classification: Real-time detection of potentially harmful content across 9 categories:
- Sexual content (S)
- Hate speech (H)
- Violence (V)
- Harassment (HR)
- Self-harm (SH)
- Sexual content involving minors (S3)
- Hate with threats (H2)
- Graphic violence (V2)
- Safe content (OK)
-
Performance Testing: Built-in tools to measure and visualize model performance:
- Configurable number of requests (1-1000)
- Adjustable text length per request
- Real-time latency metrics visualization
- Detailed performance statistics (avg, p50, p95, p99)
- Install dependencies:
pip install -r requirements.txt- Start the application:
python frontend.py- Open your web browser (I recommend Chrome, since on Firefox I spotted that some empty fields did not properly render their predefined value) and navigate to the local address shown in the terminal.
- Enter text in the input box or use the "Generate Random Text" button
- Click "Classify Text" to analyze the content
- View the classification results showing confidence scores for each category
- Navigate to the "Performance Testing" tab
- Adjust test parameters:
- Number of Requests (1-1000)
- Maximum Characters per Request (5-500)
- Click "Run Performance Test" to start
- View results in:
- Text summary with detailed statistics
- Interactive plot showing latency metrics across test runs
frontend.py: Gradio web interface implementationbackend.py: Core functionality including model integration and performance testingrequirements.txt: Project dependencies
The performance testing suite provides the following metrics:
- Average latency
- Median (P50) latency
- 95th percentile (P95)
- 99th percentile (P99)
- Minimum and maximum latencies
- Total execution time