NSFW Content Filtering with LLMs: A Model Comparison
Analyzing text prompts using Large Language Models (LLMs) can prevent the generation of unwanted NSFW content at the prompt stage. This saves resources compared to post-filtering images. Testing on a dataset of 1000 prompts (400 NSFW) demonstrated the effectiveness of the approach using models Phi-3-mini-4k-instruct-q4, Qwen2.5-1.5b-instruct-q4_k_s, and Gemma-2-2b-it-Q4_K_S.
Evaluation criteria: accuracy (percentage of blocked NSFW prompts), error rate (percentage of false positives on SFW content), and analysis time in milliseconds. Tests were conducted without tensor cores.
Basic Filtering Approaches
A blacklist of words works quickly (under 200 ms) but is easily bypassed and requires language support. Post-analysis of images (e.g., NsfwSpy) achieves 95% accuracy but wastes resources on generation.
LLM analysis of prompts is optimal: checking text is cheaper, and models are available without fine-tuning. A system prompt defines the classification task as NSFW/SFW.
Model Comparison Without Grammar
Testing was performed with a standard prompt and with few-shot examples.
| Model | Prompt | Accuracy (%) | Error Rate (%) | Time (ms) |
|--------|--------|--------------|------------|------------|
| Phi-3 | Standard | 55 | 1.3 | 359 |
| Phi-3 | With examples | 93.5 | 42.5 | 523 |
| Qwen2.5 | Standard | 53.2 | 1 | 85 |
| Qwen2.5 | With examples | 80 | 4 | 280 |
| Gemma-2 | Standard | 66.7 | <1 | 130 |
| Gemma-2 | With examples | <0.1 | <0.1 | 238 |
Phi-3 shows increased accuracy with examples but a high error rate. Qwen2.5 is fast and balances metrics with few-shot. Gemma-2 leads in standard mode, but few-shot fails due to answer parsing issues.
Implementing Grammar for Stabilization
Grammar restricts the model's output to "yes" (NSFW) / "no" (SFW) options. This solves parsing problems and provides predictable answers.
Updated results:
| Model | Prompt | Accuracy (%) | Error Rate (%) | Time (ms) |
|--------|--------|--------------|------------|------------|
| Phi-3 | Standard | 51 | 1 | 270 |
| Phi-3 | With examples | 93.7 | 43.1 | 553 |
| Qwen2.5 | Standard | 55 | <1 | 156 |
| Qwen2.5 | With examples | 80.5 | 3.8 | 378 |
| Gemma-2 | Standard | 66 | <1 | 231 |
| Gemma-2 | With examples | 40.2 | <0.1 | 321 |
Time increased by ~100 ms. Gemma-2 with examples achieved 0% error on 1000 tests, but accuracy dropped. The standard prompt for Gemma-2 maintains balance: 66% accuracy, minimal error.
- Model sizes: Qwen2.5 (0.9 GB), Gemma-2 (1.5 GB), Phi-3 (2+ GB).
- Recommendations: Gemma-2 + Grammar without few-shot for strict systems; Qwen2.5 for speed.
Scaling the Filter
One model can handle multiple topics (NSFW + other restrictions). Test on a representative dataset, optimize the prompt iteratively. In production, use tensor cores to reduce latency by 10x.
Key takeaways:
- Preliminary prompt analysis reduces load by 95%+ compared to post-filtering.
- Gemma-2 is optimal for accuracy/error rate without few-shot (66% / <1%).
- Grammar guarantees parsing at the cost of speed.
- Test set: 1000 prompts (40% NSFW) for reliable evaluation.
- Few-shot improves Phi-3/Qwen but risks errors.
— Editorial Team
No comments yet.