Spaces:
Sleeping
Sleeping
Remove offensive speech model
Browse files
app.py
CHANGED
|
@@ -6,31 +6,23 @@ from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
|
| 6 |
hate_model = AutoModelForSequenceClassification.from_pretrained("KoalaAI/HateSpeechDetector")
|
| 7 |
hate_tokenizer = AutoTokenizer.from_pretrained("KoalaAI/HateSpeechDetector")
|
| 8 |
|
| 9 |
-
offensive_model = AutoModelForSequenceClassification.from_pretrained("KoalaAI/OffensiveSpeechDetector")
|
| 10 |
-
offensive_tokenizer = AutoTokenizer.from_pretrained("KoalaAI/OffensiveSpeechDetector")
|
| 11 |
-
|
| 12 |
# Define a function that takes an input text and returns the scores from the models
|
| 13 |
def get_scores(text):
|
| 14 |
# Tokenize and encode the input text
|
| 15 |
hate_input = hate_tokenizer(text, return_tensors="pt")
|
| 16 |
-
offensive_input = offensive_tokenizer(text, return_tensors="pt")
|
| 17 |
|
| 18 |
# Get the logits from the models
|
| 19 |
hate_logits = hate_model(**hate_input).logits
|
| 20 |
-
offensive_logits = offensive_model(**offensive_input).logits
|
| 21 |
|
| 22 |
# Apply softmax to get probabilities
|
| 23 |
hate_probs = hate_logits.softmax(dim=1)
|
| 24 |
-
offensive_probs = offensive_logits.softmax(dim=1)
|
| 25 |
|
| 26 |
# Get the labels from the models
|
| 27 |
hate_labels = hate_model.config.id2label
|
| 28 |
-
offensive_labels = offensive_model.config.id2label
|
| 29 |
|
| 30 |
# Format the output as a dictionary of scores
|
| 31 |
output = {}
|
| 32 |
output["Hate speech"] = {hate_labels[i]: round(p.item(), 4) for i, p in enumerate(hate_probs[0])}
|
| 33 |
-
output["Offensive speech"] = {offensive_labels[i]: round(p.item(), 4) for i, p in enumerate(offensive_probs[0])}
|
| 34 |
|
| 35 |
return output
|
| 36 |
|
|
|
|
| 6 |
hate_model = AutoModelForSequenceClassification.from_pretrained("KoalaAI/HateSpeechDetector")
|
| 7 |
hate_tokenizer = AutoTokenizer.from_pretrained("KoalaAI/HateSpeechDetector")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
| 9 |
# Define a function that takes an input text and returns the scores from the models
|
| 10 |
def get_scores(text):
|
| 11 |
# Tokenize and encode the input text
|
| 12 |
hate_input = hate_tokenizer(text, return_tensors="pt")
|
|
|
|
| 13 |
|
| 14 |
# Get the logits from the models
|
| 15 |
hate_logits = hate_model(**hate_input).logits
|
|
|
|
| 16 |
|
| 17 |
# Apply softmax to get probabilities
|
| 18 |
hate_probs = hate_logits.softmax(dim=1)
|
|
|
|
| 19 |
|
| 20 |
# Get the labels from the models
|
| 21 |
hate_labels = hate_model.config.id2label
|
|
|
|
| 22 |
|
| 23 |
# Format the output as a dictionary of scores
|
| 24 |
output = {}
|
| 25 |
output["Hate speech"] = {hate_labels[i]: round(p.item(), 4) for i, p in enumerate(hate_probs[0])}
|
|
|
|
| 26 |
|
| 27 |
return output
|
| 28 |
|