Spaces:
Sleeping
Sleeping
Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -1,12 +1,132 @@
|
|
| 1 |
-
---
|
| 2 |
-
title: Content Classifier
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom: blue
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk: gradio
|
| 7 |
-
sdk_version:
|
| 8 |
-
app_file:
|
| 9 |
-
pinned: false
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Content Classifier
|
| 3 |
+
emoji: 🔍
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: green
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app_hf.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# Content Classifier
|
| 14 |
+
|
| 15 |
+
This Space provides a content classification service using an ONNX model. It categorizes text as either "safe" or "unsafe" content.
|
| 16 |
+
|
| 17 |
+
## Features
|
| 18 |
+
|
| 19 |
+
- **Single Text Classification**: Classify individual pieces of text
|
| 20 |
+
- **Batch Processing**: Process multiple texts at once
|
| 21 |
+
- **API Access**: Use as a web service via HTTP requests
|
| 22 |
+
- **Real-time Interface**: Interactive Gradio web interface
|
| 23 |
+
|
| 24 |
+
## Usage
|
| 25 |
+
|
| 26 |
+
### Web Interface
|
| 27 |
+
Simply enter text in the interface and click "Classify" to get predictions.
|
| 28 |
+
|
| 29 |
+
### API Usage
|
| 30 |
+
|
| 31 |
+
#### Single Text Classification
|
| 32 |
+
```bash
|
| 33 |
+
curl -X POST https://your-space-name.hf.space/predict \
|
| 34 |
+
-H "Content-Type: application/json" \
|
| 35 |
+
-d '{"text": "Your content to classify"}'
|
| 36 |
+
```
|
| 37 |
+
|
| 38 |
+
#### Batch Processing
|
| 39 |
+
```bash
|
| 40 |
+
curl -X POST https://your-space-name.hf.space/predict \
|
| 41 |
+
-H "Content-Type: application/json" \
|
| 42 |
+
-d '{"text": ["Text 1", "Text 2", "Text 3"]}'
|
| 43 |
+
```
|
| 44 |
+
|
| 45 |
+
### Response Format
|
| 46 |
+
```json
|
| 47 |
+
{
|
| 48 |
+
"is_threat": false,
|
| 49 |
+
"final_confidence": 0.85,
|
| 50 |
+
"threat_prediction": "safe",
|
| 51 |
+
"onnx_prediction": {
|
| 52 |
+
"safe": 0.85,
|
| 53 |
+
"unsafe": 0.15
|
| 54 |
+
},
|
| 55 |
+
"models_used": ["onnx"]
|
| 56 |
+
}
|
| 57 |
+
```
|
| 58 |
+
|
| 59 |
+
## Model Information
|
| 60 |
+
|
| 61 |
+
The classifier uses an ONNX model (`contextClassifier.onnx`) for efficient inference. The model processes text and outputs probability scores for "safe" and "unsafe" classifications.
|
| 62 |
+
|
| 63 |
+
## Local Development
|
| 64 |
+
|
| 65 |
+
1. Clone this repository
|
| 66 |
+
2. Install dependencies: `pip install -r requirements.txt`
|
| 67 |
+
3. Run the application: `python app_hf.py`
|
| 68 |
+
4. Access the interface at `http://localhost:7860`
|
| 69 |
+
|
| 70 |
+
## Basic Python Usage
|
| 71 |
+
|
| 72 |
+
```python
|
| 73 |
+
from inference import ContentClassifierInference
|
| 74 |
+
|
| 75 |
+
# Initialize classifier
|
| 76 |
+
classifier = ContentClassifierInference()
|
| 77 |
+
|
| 78 |
+
# Classify single text
|
| 79 |
+
result = classifier.predict("Your text here")
|
| 80 |
+
print(f"Threat: {result['is_threat']}, Confidence: {result['final_confidence']}")
|
| 81 |
+
|
| 82 |
+
# Classify multiple texts
|
| 83 |
+
texts = ["Text 1", "Text 2"]
|
| 84 |
+
results = classifier.predict_batch(texts)
|
| 85 |
+
```
|
| 86 |
+
|
| 87 |
+
### Response Format
|
| 88 |
+
|
| 89 |
+
The model returns predictions in the following format:
|
| 90 |
+
|
| 91 |
+
```json
|
| 92 |
+
{
|
| 93 |
+
"is_threat": false,
|
| 94 |
+
"final_confidence": 0.75,
|
| 95 |
+
"threat_prediction": "safe",
|
| 96 |
+
"sentiment_analysis": null,
|
| 97 |
+
"onnx_prediction": {
|
| 98 |
+
"safe": 0.75,
|
| 99 |
+
"unsafe": 0.25
|
| 100 |
+
},
|
| 101 |
+
"models_used": ["onnx"],
|
| 102 |
+
"raw_predictions": {
|
| 103 |
+
"onnx": {
|
| 104 |
+
"safe": 0.75,
|
| 105 |
+
"unsafe": 0.25
|
| 106 |
+
},
|
| 107 |
+
"sentiment": null
|
| 108 |
+
}
|
| 109 |
+
}
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
### Configuration
|
| 113 |
+
|
| 114 |
+
Modify `config.json` to adjust:
|
| 115 |
+
- `labels`: Class labels for your model
|
| 116 |
+
- `max_length`: Maximum input sequence length
|
| 117 |
+
- `threshold`: Classification confidence threshold
|
| 118 |
+
|
| 119 |
+
## Testing
|
| 120 |
+
|
| 121 |
+
Run the test script:
|
| 122 |
+
```bash
|
| 123 |
+
python test_inference.py
|
| 124 |
+
```
|
| 125 |
+
|
| 126 |
+
## Model Requirements
|
| 127 |
+
|
| 128 |
+
- Input: Text string
|
| 129 |
+
- Output: Classification probabilities
|
| 130 |
+
- Format: ONNX model file
|
| 131 |
+
|
| 132 |
+
Note: You may need to adjust the `preprocess` method in `inference.py` based on your specific model's input requirements (tokenization, encoding, etc.).
|