Spaces:
Running
on
Zero
Running
on
Zero
Add application file
Browse files- README.md +42 -263
- README_.md +276 -0
README.md
CHANGED
|
@@ -1,276 +1,55 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-

|
| 7 |
-

|
| 8 |
|
| 9 |
-
##
|
| 10 |
|
| 11 |
-
-
|
| 12 |
-
-
|
| 13 |
-
-
|
| 14 |
-
-
|
| 15 |
-
-
|
| 16 |
-
-
|
| 17 |
-
- **Real-time Streaming**: Process audio in real-time as you speak
|
| 18 |
-
- **Adjustable Sensitivity**: Fine-tune profanity detection threshold
|
| 19 |
-
- **Visual Highlighting**: Instantly identify problematic words with visual highlighting
|
| 20 |
-
- **Toxicity Classification**: Automatically categorize content from "No Toxicity" to "Severe Toxicity"
|
| 21 |
-
- **Performance Optimization**: Half-precision support for improved GPU memory efficiency
|
| 22 |
|
| 23 |
-
##
|
| 24 |
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
-
|
| 28 |
-
2. **Content Refinement**: `s-nlp/t5-paranmt-detox` - A T5-based model for rephrasing offensive language
|
| 29 |
-
3. **Speech-to-Text**: OpenAI's `Whisper` (large) - For transcribing spoken audio
|
| 30 |
-
4. **Text-to-Speech**: Microsoft's `SpeechT5` - For converting rephrased text back to audio
|
| 31 |
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
| 35 |
|
| 36 |
-
|
| 37 |
-
- CUDA-compatible GPU recommended (but CPU mode works too)
|
| 38 |
-
- FFmpeg for audio processing
|
| 39 |
|
| 40 |
-
|
|
|
|
|
|
|
| 41 |
|
| 42 |
-
|
| 43 |
-
# Clone the repository
|
| 44 |
-
git clone https://github.com/yourusername/profanity-detection.git
|
| 45 |
-
cd profanity-detection
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
conda create -n profanity-detection python=3.10
|
| 53 |
-
conda activate profanity-detection
|
| 54 |
-
|
| 55 |
-
# Install PyTorch with CUDA support (adjust CUDA version if needed)
|
| 56 |
-
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
|
| 57 |
-
|
| 58 |
-
# Install FFmpeg for audio processing
|
| 59 |
-
conda install -c conda-forge ffmpeg
|
| 60 |
-
|
| 61 |
-
# Install Pillow properly to avoid DLL errors
|
| 62 |
-
conda install -c conda-forge pillow
|
| 63 |
-
|
| 64 |
-
# Install additional dependencies
|
| 65 |
-
pip install -r requirements.txt
|
| 66 |
-
|
| 67 |
-
# Set environment variable to avoid OpenMP conflicts (recommended)
|
| 68 |
-
conda env config vars set KMP_DUPLICATE_LIB_OK=TRUE
|
| 69 |
-
conda activate profanity-detection # Re-activate to apply the variable
|
| 70 |
-
```
|
| 71 |
-
|
| 72 |
-
### Option 2: Using Docker
|
| 73 |
-
|
| 74 |
-
```bash
|
| 75 |
-
# Clone the repository
|
| 76 |
-
git clone https://github.com/yourusername/profanity-detection.git
|
| 77 |
-
cd profanity-detection
|
| 78 |
-
|
| 79 |
-
# Build and run the Docker container
|
| 80 |
-
docker-compose build --no-cache
|
| 81 |
-
|
| 82 |
-
docker-compose up
|
| 83 |
-
```
|
| 84 |
-
|
| 85 |
-
## π Usage
|
| 86 |
-
|
| 87 |
-
### Running the Application
|
| 88 |
-
|
| 89 |
-
```bash
|
| 90 |
-
# Set environment variable to avoid OpenMP conflicts (if not set in conda config)
|
| 91 |
-
# For Windows:
|
| 92 |
-
set KMP_DUPLICATE_LIB_OK=TRUE
|
| 93 |
-
|
| 94 |
-
# For Linux/Mac:
|
| 95 |
-
export KMP_DUPLICATE_LIB_OK=TRUE
|
| 96 |
-
|
| 97 |
-
# Run the application
|
| 98 |
-
python profanity_detector.py
|
| 99 |
-
```
|
| 100 |
-
|
| 101 |
-
The Gradio interface will be accessible at http://127.0.0.1:7860 in your browser.
|
| 102 |
-
|
| 103 |
-
### Using the Interface
|
| 104 |
-
|
| 105 |
-
1. **Initialise Models**
|
| 106 |
-
- Click the "Initialize Models" button when you first open the interface
|
| 107 |
-
- Wait for all models to load (this may take a few minutes on first run)
|
| 108 |
-
|
| 109 |
-
2. **Text Analysis Tab**
|
| 110 |
-
- Enter text into the text box
|
| 111 |
-
- Adjust the "Profanity Detection Sensitivity" slider if needed
|
| 112 |
-
- Click "Analyze Text"
|
| 113 |
-
- View results including profanity score, toxicity classification, and rephrased content
|
| 114 |
-
- See highlighted profane words in the text
|
| 115 |
-
- Listen to the audio version of the rephrased content
|
| 116 |
-
|
| 117 |
-
3. **Audio Analysis Tab**
|
| 118 |
-
- Upload an audio file or record directly using your microphone
|
| 119 |
-
- Click "Analyze Audio"
|
| 120 |
-
- View transcription, profanity analysis, and rephrased content
|
| 121 |
-
- Listen to the cleaned audio version of the rephrased content
|
| 122 |
-
|
| 123 |
-
4. **Real-time Streaming Tab**
|
| 124 |
-
- Click "Start Real-time Processing"
|
| 125 |
-
- Speak into your microphone
|
| 126 |
-
- Watch as your speech is transcribed, analyzed, and rephrased in real-time
|
| 127 |
-
- Listen to the clean audio output
|
| 128 |
-
- Click "Stop Real-time Processing" when finished
|
| 129 |
-
|
| 130 |
-
## π§ Deployment Options
|
| 131 |
-
|
| 132 |
-
### Local Deployment with Conda
|
| 133 |
-
|
| 134 |
-
For the best development experience with fine-grained control:
|
| 135 |
-
|
| 136 |
-
```bash
|
| 137 |
-
# Create and configure environment
|
| 138 |
-
conda env create -f environment.yml
|
| 139 |
-
conda activate llm_project
|
| 140 |
-
|
| 141 |
-
# Run with sharing enabled (accessible from other devices)
|
| 142 |
-
python profanity_detector.py
|
| 143 |
-
```
|
| 144 |
-
|
| 145 |
-
### Docker Deployment (Production)
|
| 146 |
-
|
| 147 |
-
For containerised deployment with predictable environment:
|
| 148 |
-
|
| 149 |
-
#### Basic CPU Deployment
|
| 150 |
-
```bash
|
| 151 |
-
docker-compose up --build
|
| 152 |
-
```
|
| 153 |
-
|
| 154 |
-
#### GPU-Accelerated Deployment
|
| 155 |
-
```bash
|
| 156 |
-
# Automatic detection (recommended)
|
| 157 |
-
docker-compose up --build
|
| 158 |
-
|
| 159 |
-
# Or explicitly request GPU mode
|
| 160 |
-
docker-compose up --build profanity-detector-gpu
|
| 161 |
-
```
|
| 162 |
-
|
| 163 |
-
No need to edit any configuration files - the system will automatically detect and use your GPU if available.
|
| 164 |
-
|
| 165 |
-
#### Custom Port Configuration
|
| 166 |
-
To change the default port (7860):
|
| 167 |
-
1. Edit docker-compose.yml and change the port mapping (e.g., "8080:7860")
|
| 168 |
-
2. Run `docker-compose up --build`
|
| 169 |
-
|
| 170 |
-
## β οΈ Troubleshooting
|
| 171 |
-
|
| 172 |
-
### OpenMP Runtime Conflict
|
| 173 |
-
|
| 174 |
-
If you encounter this error:
|
| 175 |
-
```
|
| 176 |
-
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
|
| 177 |
-
```
|
| 178 |
-
|
| 179 |
-
**Solutions:**
|
| 180 |
-
|
| 181 |
-
1. **Temporary fix**: Set environment variable before running:
|
| 182 |
-
```bash
|
| 183 |
-
set KMP_DUPLICATE_LIB_OK=TRUE # Windows
|
| 184 |
-
export KMP_DUPLICATE_LIB_OK=TRUE # Linux/Mac
|
| 185 |
-
```
|
| 186 |
-
|
| 187 |
-
2. **Code-based fix**: Add to the beginning of your script:
|
| 188 |
-
```python
|
| 189 |
-
import os
|
| 190 |
-
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
|
| 191 |
-
```
|
| 192 |
-
|
| 193 |
-
3. **Permanent fix for Conda environment**:
|
| 194 |
-
```bash
|
| 195 |
-
conda env config vars set KMP_DUPLICATE_LIB_OK=TRUE -n profanity-detection
|
| 196 |
-
conda deactivate
|
| 197 |
-
conda activate profanity-detection
|
| 198 |
-
```
|
| 199 |
-
|
| 200 |
-
### GPU Memory Issues
|
| 201 |
-
|
| 202 |
-
If you encounter CUDA out of memory errors:
|
| 203 |
-
|
| 204 |
-
1. Use smaller models:
|
| 205 |
-
```python
|
| 206 |
-
# Change Whisper from "large" to "medium" or "small"
|
| 207 |
-
whisper_model = whisper.load_model("medium").to(device)
|
| 208 |
-
|
| 209 |
-
# Keep the TTS model on CPU to save GPU memory
|
| 210 |
-
tts_model = SpeechT5ForTextToSpeech.from_pretrained(TTS_MODEL) # CPU mode
|
| 211 |
-
```
|
| 212 |
-
|
| 213 |
-
2. Run some models on CPU instead of GPU:
|
| 214 |
-
```python
|
| 215 |
-
# Remove .to(device) to keep model on CPU
|
| 216 |
-
t5_model = AutoModelForSeq2SeqLM.from_pretrained(T5_MODEL) # CPU mode
|
| 217 |
-
```
|
| 218 |
-
|
| 219 |
-
3. Use Docker with specific GPU memory limits:
|
| 220 |
-
```yaml
|
| 221 |
-
# In docker-compose.yml
|
| 222 |
-
deploy:
|
| 223 |
-
resources:
|
| 224 |
-
reservations:
|
| 225 |
-
devices:
|
| 226 |
-
- driver: nvidia
|
| 227 |
-
count: 1
|
| 228 |
-
capabilities: [gpu]
|
| 229 |
-
options:
|
| 230 |
-
memory: 4G # Limit to 4GB of GPU memory
|
| 231 |
-
```
|
| 232 |
-
|
| 233 |
-
### Docker-Specific Issues
|
| 234 |
-
|
| 235 |
-
1. **Permission issues with mounted volumes**:
|
| 236 |
-
```bash
|
| 237 |
-
# Fix permissions (Linux/Mac)
|
| 238 |
-
sudo chown -R $USER:$USER .
|
| 239 |
-
```
|
| 240 |
-
|
| 241 |
-
2. **No GPU access in container**:
|
| 242 |
-
- Verify NVIDIA Container Toolkit installation
|
| 243 |
-
- Check GPU driver compatibility
|
| 244 |
-
- Run `nvidia-smi` on the host to confirm GPU availability
|
| 245 |
-
|
| 246 |
-
### First-Time Slowness
|
| 247 |
-
|
| 248 |
-
When first run, the application downloads all models, which may take time. Subsequent runs will be faster as models are cached locally. The text-to-speech model requires additional download time on first use.
|
| 249 |
-
|
| 250 |
-
## π Project Structure
|
| 251 |
-
|
| 252 |
-
```
|
| 253 |
-
profanity-detection/
|
| 254 |
-
βββ profanity_detector.py # Main application file
|
| 255 |
-
βββ Dockerfile # For containerised deployment
|
| 256 |
-
βββ docker-compose.yml # Container orchestration
|
| 257 |
-
βββ requirements.txt # Python dependencies
|
| 258 |
-
βββ environment.yml # Conda environment specification
|
| 259 |
-
βββ README.md # This file
|
| 260 |
-
```
|
| 261 |
-
|
| 262 |
-
## π References
|
| 263 |
-
|
| 264 |
-
- [HuggingFace Transformers](https://huggingface.co/docs/transformers/index)
|
| 265 |
-
- [OpenAI Whisper](https://github.com/openai/whisper)
|
| 266 |
-
- [Microsoft SpeechT5](https://huggingface.co/microsoft/speecht5_tts)
|
| 267 |
-
- [Gradio Documentation](https://gradio.app/docs/)
|
| 268 |
-
|
| 269 |
-
## π License
|
| 270 |
-
|
| 271 |
-
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 272 |
-
|
| 273 |
-
## π Acknowledgments
|
| 274 |
-
|
| 275 |
-
- This project utilises models from HuggingFace Hub, Microsoft, and OpenAI
|
| 276 |
-
- Inspired by research in content moderation and responsible AI
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Profanity Detection & Replacement System
|
| 3 |
+
emoji: π«
|
| 4 |
+
colorFrom: red
|
| 5 |
+
colorTo: blue
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.14.0
|
| 8 |
+
app_file: profanity_detector.py
|
| 9 |
+
pinned: false
|
| 10 |
+
---
|
| 11 |
|
| 12 |
+
# Profanity Detection & Replacement System
|
| 13 |
|
| 14 |
+
This app provides a comprehensive solution for detecting and cleaning profanity from both text and audio content. It uses state-of-the-art machine learning models to analyze content, identify inappropriate language, and generate clean alternatives.
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
## Features
|
| 17 |
|
| 18 |
+
- π Real-time profanity detection with adjustable sensitivity
|
| 19 |
+
- π Automatic text rephrasing to clean alternatives
|
| 20 |
+
- π€ Speech-to-text conversion with profanity filtering
|
| 21 |
+
- π£οΈ Text-to-speech generation for clean content
|
| 22 |
+
- π» User-friendly Gradio interface
|
| 23 |
+
- π Real-time streaming support for live audio processing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
## Models Used
|
| 26 |
|
| 27 |
+
- Profanity Detection: `parsawar/profanity_model_3.1`
|
| 28 |
+
- Text Detoxification: `s-nlp/t5-paranmt-detox`
|
| 29 |
+
- Speech Recognition: OpenAI Whisper (large)
|
| 30 |
+
- Text-to-Speech: Microsoft SpeechT5
|
| 31 |
|
| 32 |
+
## Requirements
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
+
- Python 3.10
|
| 35 |
+
- PyTorch with CUDA support
|
| 36 |
+
- Gradio
|
| 37 |
+
- Transformers
|
| 38 |
+
- OpenAI Whisper
|
| 39 |
+
- Other dependencies listed in `requirements.txt`
|
| 40 |
|
| 41 |
+
## Interface
|
| 42 |
|
| 43 |
+
The app provides three main interaction modes:
|
|
|
|
|
|
|
| 44 |
|
| 45 |
+
1. **Text Analysis**: Enter text to detect and clean profanity
|
| 46 |
+
2. **Audio Analysis**: Upload or record audio for profanity detection
|
| 47 |
+
3. **Real-time Streaming**: Process live audio with instant profanity filtering
|
| 48 |
|
| 49 |
+
## Technical Details
|
|
|
|
|
|
|
|
|
|
| 50 |
|
| 51 |
+
- GPU acceleration supported for faster processing
|
| 52 |
+
- Memory-optimized with FP16 precision where available
|
| 53 |
+
- Configurable profanity detection threshold
|
| 54 |
+
- Built-in error handling and logging
|
| 55 |
+
- Dark mode support
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Profanity Detection in Speech and Text
|
| 2 |
+
|
| 3 |
+
A robust multimodal system for detecting and rephrasing profanity in both speech and text, leveraging advanced NLP models to ensure accurate filtering while preserving conversational context.
|
| 4 |
+
|
| 5 |
+

|
| 6 |
+

|
| 7 |
+

|
| 8 |
+
|
| 9 |
+
## π Features
|
| 10 |
+
|
| 11 |
+
- **Multimodal Analysis**: Process both written text and spoken audio
|
| 12 |
+
- **Context-Aware Detection**: Goes beyond simple keyword matching
|
| 13 |
+
- **Automatic Content Refinement**: Intelligently rephrases content while preserving meaning
|
| 14 |
+
- **Audio Synthesis**: Converts rephrased content into high-quality spoken audio
|
| 15 |
+
- **Classification System**: Categorises content by toxicity levels
|
| 16 |
+
- **User-Friendly Interface**: Intuitive Gradio-based UI
|
| 17 |
+
- **Real-time Streaming**: Process audio in real-time as you speak
|
| 18 |
+
- **Adjustable Sensitivity**: Fine-tune profanity detection threshold
|
| 19 |
+
- **Visual Highlighting**: Instantly identify problematic words with visual highlighting
|
| 20 |
+
- **Toxicity Classification**: Automatically categorize content from "No Toxicity" to "Severe Toxicity"
|
| 21 |
+
- **Performance Optimization**: Half-precision support for improved GPU memory efficiency
|
| 22 |
+
|
| 23 |
+
## π§ Models Used
|
| 24 |
+
|
| 25 |
+
The system leverages four powerful models:
|
| 26 |
+
|
| 27 |
+
1. **Profanity Detection**: `parsawar/profanity_model_3.1` - A RoBERTa-based model trained for offensive language detection
|
| 28 |
+
2. **Content Refinement**: `s-nlp/t5-paranmt-detox` - A T5-based model for rephrasing offensive language
|
| 29 |
+
3. **Speech-to-Text**: OpenAI's `Whisper` (large) - For transcribing spoken audio
|
| 30 |
+
4. **Text-to-Speech**: Microsoft's `SpeechT5` - For converting rephrased text back to audio
|
| 31 |
+
|
| 32 |
+
## π§ Installation
|
| 33 |
+
|
| 34 |
+
### Prerequisites
|
| 35 |
+
|
| 36 |
+
- Python 3.10+
|
| 37 |
+
- CUDA-compatible GPU recommended (but CPU mode works too)
|
| 38 |
+
- FFmpeg for audio processing
|
| 39 |
+
|
| 40 |
+
### Option 1: Using Conda (Recommended for Local Development)
|
| 41 |
+
|
| 42 |
+
```bash
|
| 43 |
+
# Clone the repository
|
| 44 |
+
git clone https://github.com/yourusername/profanity-detection.git
|
| 45 |
+
cd profanity-detection
|
| 46 |
+
|
| 47 |
+
# Method A: Create environment from environment.yml (recommended)
|
| 48 |
+
conda env create -f environment.yml
|
| 49 |
+
conda activate llm_project
|
| 50 |
+
|
| 51 |
+
# Method B: Create a new conda environment manually
|
| 52 |
+
conda create -n profanity-detection python=3.10
|
| 53 |
+
conda activate profanity-detection
|
| 54 |
+
|
| 55 |
+
# Install PyTorch with CUDA support (adjust CUDA version if needed)
|
| 56 |
+
conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia
|
| 57 |
+
|
| 58 |
+
# Install FFmpeg for audio processing
|
| 59 |
+
conda install -c conda-forge ffmpeg
|
| 60 |
+
|
| 61 |
+
# Install Pillow properly to avoid DLL errors
|
| 62 |
+
conda install -c conda-forge pillow
|
| 63 |
+
|
| 64 |
+
# Install additional dependencies
|
| 65 |
+
pip install -r requirements.txt
|
| 66 |
+
|
| 67 |
+
# Set environment variable to avoid OpenMP conflicts (recommended)
|
| 68 |
+
conda env config vars set KMP_DUPLICATE_LIB_OK=TRUE
|
| 69 |
+
conda activate profanity-detection # Re-activate to apply the variable
|
| 70 |
+
```
|
| 71 |
+
|
| 72 |
+
### Option 2: Using Docker
|
| 73 |
+
|
| 74 |
+
```bash
|
| 75 |
+
# Clone the repository
|
| 76 |
+
git clone https://github.com/yourusername/profanity-detection.git
|
| 77 |
+
cd profanity-detection
|
| 78 |
+
|
| 79 |
+
# Build and run the Docker container
|
| 80 |
+
docker-compose build --no-cache
|
| 81 |
+
|
| 82 |
+
docker-compose up
|
| 83 |
+
```
|
| 84 |
+
|
| 85 |
+
## π Usage
|
| 86 |
+
|
| 87 |
+
### Running the Application
|
| 88 |
+
|
| 89 |
+
```bash
|
| 90 |
+
# Set environment variable to avoid OpenMP conflicts (if not set in conda config)
|
| 91 |
+
# For Windows:
|
| 92 |
+
set KMP_DUPLICATE_LIB_OK=TRUE
|
| 93 |
+
|
| 94 |
+
# For Linux/Mac:
|
| 95 |
+
export KMP_DUPLICATE_LIB_OK=TRUE
|
| 96 |
+
|
| 97 |
+
# Run the application
|
| 98 |
+
python profanity_detector.py
|
| 99 |
+
```
|
| 100 |
+
|
| 101 |
+
The Gradio interface will be accessible at http://127.0.0.1:7860 in your browser.
|
| 102 |
+
|
| 103 |
+
### Using the Interface
|
| 104 |
+
|
| 105 |
+
1. **Initialise Models**
|
| 106 |
+
- Click the "Initialize Models" button when you first open the interface
|
| 107 |
+
- Wait for all models to load (this may take a few minutes on first run)
|
| 108 |
+
|
| 109 |
+
2. **Text Analysis Tab**
|
| 110 |
+
- Enter text into the text box
|
| 111 |
+
- Adjust the "Profanity Detection Sensitivity" slider if needed
|
| 112 |
+
- Click "Analyze Text"
|
| 113 |
+
- View results including profanity score, toxicity classification, and rephrased content
|
| 114 |
+
- See highlighted profane words in the text
|
| 115 |
+
- Listen to the audio version of the rephrased content
|
| 116 |
+
|
| 117 |
+
3. **Audio Analysis Tab**
|
| 118 |
+
- Upload an audio file or record directly using your microphone
|
| 119 |
+
- Click "Analyze Audio"
|
| 120 |
+
- View transcription, profanity analysis, and rephrased content
|
| 121 |
+
- Listen to the cleaned audio version of the rephrased content
|
| 122 |
+
|
| 123 |
+
4. **Real-time Streaming Tab**
|
| 124 |
+
- Click "Start Real-time Processing"
|
| 125 |
+
- Speak into your microphone
|
| 126 |
+
- Watch as your speech is transcribed, analyzed, and rephrased in real-time
|
| 127 |
+
- Listen to the clean audio output
|
| 128 |
+
- Click "Stop Real-time Processing" when finished
|
| 129 |
+
|
| 130 |
+
## π§ Deployment Options
|
| 131 |
+
|
| 132 |
+
### Local Deployment with Conda
|
| 133 |
+
|
| 134 |
+
For the best development experience with fine-grained control:
|
| 135 |
+
|
| 136 |
+
```bash
|
| 137 |
+
# Create and configure environment
|
| 138 |
+
conda env create -f environment.yml
|
| 139 |
+
conda activate llm_project
|
| 140 |
+
|
| 141 |
+
# Run with sharing enabled (accessible from other devices)
|
| 142 |
+
python profanity_detector.py
|
| 143 |
+
```
|
| 144 |
+
|
| 145 |
+
### Docker Deployment (Production)
|
| 146 |
+
|
| 147 |
+
For containerised deployment with predictable environment:
|
| 148 |
+
|
| 149 |
+
#### Basic CPU Deployment
|
| 150 |
+
```bash
|
| 151 |
+
docker-compose up --build
|
| 152 |
+
```
|
| 153 |
+
|
| 154 |
+
#### GPU-Accelerated Deployment
|
| 155 |
+
```bash
|
| 156 |
+
# Automatic detection (recommended)
|
| 157 |
+
docker-compose up --build
|
| 158 |
+
|
| 159 |
+
# Or explicitly request GPU mode
|
| 160 |
+
docker-compose up --build profanity-detector-gpu
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
No need to edit any configuration files - the system will automatically detect and use your GPU if available.
|
| 164 |
+
|
| 165 |
+
#### Custom Port Configuration
|
| 166 |
+
To change the default port (7860):
|
| 167 |
+
1. Edit docker-compose.yml and change the port mapping (e.g., "8080:7860")
|
| 168 |
+
2. Run `docker-compose up --build`
|
| 169 |
+
|
| 170 |
+
## β οΈ Troubleshooting
|
| 171 |
+
|
| 172 |
+
### OpenMP Runtime Conflict
|
| 173 |
+
|
| 174 |
+
If you encounter this error:
|
| 175 |
+
```
|
| 176 |
+
OMP: Error #15: Initializing libiomp5md.dll, but found libiomp5md.dll already initialized.
|
| 177 |
+
```
|
| 178 |
+
|
| 179 |
+
**Solutions:**
|
| 180 |
+
|
| 181 |
+
1. **Temporary fix**: Set environment variable before running:
|
| 182 |
+
```bash
|
| 183 |
+
set KMP_DUPLICATE_LIB_OK=TRUE # Windows
|
| 184 |
+
export KMP_DUPLICATE_LIB_OK=TRUE # Linux/Mac
|
| 185 |
+
```
|
| 186 |
+
|
| 187 |
+
2. **Code-based fix**: Add to the beginning of your script:
|
| 188 |
+
```python
|
| 189 |
+
import os
|
| 190 |
+
os.environ['KMP_DUPLICATE_LIB_OK'] = 'TRUE'
|
| 191 |
+
```
|
| 192 |
+
|
| 193 |
+
3. **Permanent fix for Conda environment**:
|
| 194 |
+
```bash
|
| 195 |
+
conda env config vars set KMP_DUPLICATE_LIB_OK=TRUE -n profanity-detection
|
| 196 |
+
conda deactivate
|
| 197 |
+
conda activate profanity-detection
|
| 198 |
+
```
|
| 199 |
+
|
| 200 |
+
### GPU Memory Issues
|
| 201 |
+
|
| 202 |
+
If you encounter CUDA out of memory errors:
|
| 203 |
+
|
| 204 |
+
1. Use smaller models:
|
| 205 |
+
```python
|
| 206 |
+
# Change Whisper from "large" to "medium" or "small"
|
| 207 |
+
whisper_model = whisper.load_model("medium").to(device)
|
| 208 |
+
|
| 209 |
+
# Keep the TTS model on CPU to save GPU memory
|
| 210 |
+
tts_model = SpeechT5ForTextToSpeech.from_pretrained(TTS_MODEL) # CPU mode
|
| 211 |
+
```
|
| 212 |
+
|
| 213 |
+
2. Run some models on CPU instead of GPU:
|
| 214 |
+
```python
|
| 215 |
+
# Remove .to(device) to keep model on CPU
|
| 216 |
+
t5_model = AutoModelForSeq2SeqLM.from_pretrained(T5_MODEL) # CPU mode
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
3. Use Docker with specific GPU memory limits:
|
| 220 |
+
```yaml
|
| 221 |
+
# In docker-compose.yml
|
| 222 |
+
deploy:
|
| 223 |
+
resources:
|
| 224 |
+
reservations:
|
| 225 |
+
devices:
|
| 226 |
+
- driver: nvidia
|
| 227 |
+
count: 1
|
| 228 |
+
capabilities: [gpu]
|
| 229 |
+
options:
|
| 230 |
+
memory: 4G # Limit to 4GB of GPU memory
|
| 231 |
+
```
|
| 232 |
+
|
| 233 |
+
### Docker-Specific Issues
|
| 234 |
+
|
| 235 |
+
1. **Permission issues with mounted volumes**:
|
| 236 |
+
```bash
|
| 237 |
+
# Fix permissions (Linux/Mac)
|
| 238 |
+
sudo chown -R $USER:$USER .
|
| 239 |
+
```
|
| 240 |
+
|
| 241 |
+
2. **No GPU access in container**:
|
| 242 |
+
- Verify NVIDIA Container Toolkit installation
|
| 243 |
+
- Check GPU driver compatibility
|
| 244 |
+
- Run `nvidia-smi` on the host to confirm GPU availability
|
| 245 |
+
|
| 246 |
+
### First-Time Slowness
|
| 247 |
+
|
| 248 |
+
When first run, the application downloads all models, which may take time. Subsequent runs will be faster as models are cached locally. The text-to-speech model requires additional download time on first use.
|
| 249 |
+
|
| 250 |
+
## π Project Structure
|
| 251 |
+
|
| 252 |
+
```
|
| 253 |
+
profanity-detection/
|
| 254 |
+
βββ profanity_detector.py # Main application file
|
| 255 |
+
βββ Dockerfile # For containerised deployment
|
| 256 |
+
βββ docker-compose.yml # Container orchestration
|
| 257 |
+
βββ requirements.txt # Python dependencies
|
| 258 |
+
βββ environment.yml # Conda environment specification
|
| 259 |
+
βββ README.md # This file
|
| 260 |
+
```
|
| 261 |
+
|
| 262 |
+
## π References
|
| 263 |
+
|
| 264 |
+
- [HuggingFace Transformers](https://huggingface.co/docs/transformers/index)
|
| 265 |
+
- [OpenAI Whisper](https://github.com/openai/whisper)
|
| 266 |
+
- [Microsoft SpeechT5](https://huggingface.co/microsoft/speecht5_tts)
|
| 267 |
+
- [Gradio Documentation](https://gradio.app/docs/)
|
| 268 |
+
|
| 269 |
+
## π License
|
| 270 |
+
|
| 271 |
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
| 272 |
+
|
| 273 |
+
## π Acknowledgments
|
| 274 |
+
|
| 275 |
+
- This project utilises models from HuggingFace Hub, Microsoft, and OpenAI
|
| 276 |
+
- Inspired by research in content moderation and responsible AI
|