Spaces:
Sleeping
Sleeping
test
Browse files
app.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import AutoTokenizer, AutoModelForSequenceClassification
|
| 3 |
+
import torch
|
| 4 |
+
|
| 5 |
+
# Load the model and tokenizer
|
| 6 |
+
tokenizer = AutoTokenizer.from_pretrained("BenjaminOcampo/peace_hatebert")
|
| 7 |
+
model = AutoModelForSequenceClassification.from_pretrained("BenjaminOcampo/peace_hatebert")
|
| 8 |
+
|
| 9 |
+
# Define more nuanced labels for the model output
|
| 10 |
+
nuanced_labels = {
|
| 11 |
+
0: "Non-Hate Speech",
|
| 12 |
+
1: "Explicit Hate",
|
| 13 |
+
2: "Implicit Hate",
|
| 14 |
+
3: "White Grievance"
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# Microaggressions detection rules
|
| 18 |
+
microaggressions = {
|
| 19 |
+
"You're so articulate": "This phrase can imply surprise that the individual can speak well, often used in a way that suggests it is unexpected for someone of their background.",
|
| 20 |
+
"Where are you really from": "This question implies that the individual does not belong or is not truly part of the community.",
|
| 21 |
+
"I don't see color": "This statement can negate the experiences and identities of people of different races.",
|
| 22 |
+
"You're a credit to your race": "This phrase implies that most people of the individual’s race are not successful or commendable."
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
# A sample set of explanations and suggestions
|
| 26 |
+
bias_suggestions = {
|
| 27 |
+
"Explicit Hate": {
|
| 28 |
+
"suggestion": "Consider using language that promotes inclusivity and respect.",
|
| 29 |
+
"explanation": "The text contains explicit hate speech, which is overtly harmful and discriminatory. It is important to foster communication that is inclusive and respectful of all individuals."
|
| 30 |
+
},
|
| 31 |
+
"Implicit Hate": {
|
| 32 |
+
"suggestion": "Try rephrasing to avoid subtle bias and ensure clarity.",
|
| 33 |
+
"explanation": "The text contains implicit hate speech, which can perpetuate stereotypes and bias in a less overt manner. Aim for language that is clear and free from insinuations."
|
| 34 |
+
},
|
| 35 |
+
"White Grievance": {
|
| 36 |
+
"suggestion": "Reconsider any generalized claims about racial groups.",
|
| 37 |
+
"explanation": "The text appears to express grievances linked to racial identity, which can contribute to divisive narratives. Strive for dialogue that acknowledges diversity and avoids stereotyping."
|
| 38 |
+
},
|
| 39 |
+
"Non-Hate Speech": {
|
| 40 |
+
"suggestion": "No problematic content detected.",
|
| 41 |
+
"explanation": "The text does not appear to contain hate speech or bias. It seems respectful and neutral."
|
| 42 |
+
},
|
| 43 |
+
"Microaggression": {
|
| 44 |
+
"suggestion": "Be mindful of how certain phrases can be interpreted by others.",
|
| 45 |
+
"explanation": "The text includes phrases that may be considered microaggressions, which can subtly perpetuate stereotypes or biases."
|
| 46 |
+
}
|
| 47 |
+
}
|
| 48 |
+
|
| 49 |
+
def analyze_text(text):
|
| 50 |
+
# Tokenize input text
|
| 51 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 52 |
+
|
| 53 |
+
# Get model predictions
|
| 54 |
+
with torch.no_grad():
|
| 55 |
+
outputs = model(**inputs)
|
| 56 |
+
|
| 57 |
+
# Get prediction probabilities
|
| 58 |
+
probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
|
| 59 |
+
predicted_class = torch.argmax(probs, dim=-1).item()
|
| 60 |
+
|
| 61 |
+
# Map the predicted class to the nuanced label
|
| 62 |
+
label = nuanced_labels.get(predicted_class, "Unknown")
|
| 63 |
+
|
| 64 |
+
# Check for microaggressions using the predefined rules
|
| 65 |
+
for phrase, explanation in microaggressions.items():
|
| 66 |
+
if phrase.lower() in text.lower():
|
| 67 |
+
label = "Microaggression"
|
| 68 |
+
suggestion = bias_suggestions[label]["suggestion"]
|
| 69 |
+
explanation = bias_suggestions[label]["explanation"]
|
| 70 |
+
return label, suggestion, explanation
|
| 71 |
+
|
| 72 |
+
# Fetch suggestion and explanation based on label
|
| 73 |
+
suggestion = bias_suggestions[label]["suggestion"]
|
| 74 |
+
explanation = bias_suggestions[label]["explanation"]
|
| 75 |
+
|
| 76 |
+
return label, suggestion, explanation
|
| 77 |
+
|
| 78 |
+
# Create the Gradio interface
|
| 79 |
+
interface = gr.Interface(
|
| 80 |
+
fn=analyze_text,
|
| 81 |
+
inputs=gr.inputs.Textbox(lines=5, placeholder="Enter text to analyze..."),
|
| 82 |
+
outputs=[
|
| 83 |
+
gr.outputs.Textbox(label="Classification"),
|
| 84 |
+
gr.outputs.Textbox(label="Suggestion"),
|
| 85 |
+
gr.outputs.Textbox(label="Explanation")
|
| 86 |
+
],
|
| 87 |
+
title="Nuanced Bias and Microaggression Detection",
|
| 88 |
+
description="Analyze text for nuanced bias categories such as implicit hate, explicit hate, or white grievance, and detect microaggressions to provide suggestions for improvement."
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
# Launch the interface
|
| 92 |
+
interface.launch()
|