Update README.md
Browse files
README.md
CHANGED
|
@@ -12,4 +12,47 @@ datasets:
|
|
| 12 |
- segyges/OpenWebText2
|
| 13 |
language:
|
| 14 |
- en
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
- segyges/OpenWebText2
|
| 13 |
language:
|
| 14 |
- en
|
| 15 |
+
library_name: transformers
|
| 16 |
+
---
|
| 17 |
+
from transformers import pipeline
|
| 18 |
+
|
| 19 |
+
class ContentFilter:
|
| 20 |
+
def __init__(self):
|
| 21 |
+
# Load pre-trained model for text classification
|
| 22 |
+
self.classifier = pipeline("text-classification", model="cardiffnlp/twitter-roberta-base-offensive")
|
| 23 |
+
|
| 24 |
+
def filter_content(self, text, threshold=0.75):
|
| 25 |
+
# Classify the input text
|
| 26 |
+
result = self.classifier(text)[0]
|
| 27 |
+
|
| 28 |
+
# Determine if the content is offensive based on the classification
|
| 29 |
+
is_offensive = result['label'] == 'LABEL_1' and result['score'] > threshold
|
| 30 |
+
|
| 31 |
+
return {
|
| 32 |
+
"text": text,
|
| 33 |
+
"is_offensive": is_offensive,
|
| 34 |
+
"confidence": result['score']
|
| 35 |
+
}
|
| 36 |
+
|
| 37 |
+
# Create an instance of the ContentFilter
|
| 38 |
+
content_filter = ContentFilter()
|
| 39 |
+
|
| 40 |
+
# Function to test the filter
|
| 41 |
+
def test_filter():
|
| 42 |
+
texts = [
|
| 43 |
+
"Have a nice day!",
|
| 44 |
+
"You are an idiot!",
|
| 45 |
+
"The weather is lovely.",
|
| 46 |
+
"I hate you so much."
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
for text in texts:
|
| 50 |
+
result = content_filter.filter_content(text)
|
| 51 |
+
print(f"Text: {result['text']}")
|
| 52 |
+
print(f"Is offensive: {result['is_offensive']}")
|
| 53 |
+
print(f"Confidence: {result['confidence']:.4f}")
|
| 54 |
+
print()
|
| 55 |
+
|
| 56 |
+
# Run the test
|
| 57 |
+
if __name__ == "__main__":
|
| 58 |
+
test_filter()
|