Update app.py
Browse files
app.py
CHANGED
|
@@ -28,7 +28,7 @@ label_map = {
|
|
| 28 |
def detect_bias(text):
|
| 29 |
# convert user input into tensors using the tokenizer
|
| 30 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 31 |
-
|
| 32 |
# disable gradient tracking — we’re only doing prediction, not training
|
| 33 |
with torch.no_grad():
|
| 34 |
outputs = model(**inputs) # pass inputs through the model
|
|
@@ -37,22 +37,25 @@ def detect_bias(text):
|
|
| 37 |
pred_label = torch.argmax(probs).item() # get the predicted label (0 or 1)
|
| 38 |
confidence = round(probs[pred_label].item(), 2) # grab the confidence score of that prediction
|
| 39 |
|
| 40 |
-
#
|
| 41 |
if confidence > 0.75 and pred_label == 1:
|
| 42 |
final_label = "Biased"
|
| 43 |
explanation = (
|
| 44 |
"⚠️ This text is likely biased. The model is highly confident that it reflects gender stereotypes or role bias."
|
| 45 |
)
|
| 46 |
-
elif 0.56
|
| 47 |
final_label = "Possibly Biased"
|
| 48 |
explanation = (
|
| 49 |
"🤔 This text might contain some gender bias, but the model is not entirely sure. Review it carefully."
|
| 50 |
)
|
| 51 |
-
|
| 52 |
final_label = "Unbiased"
|
| 53 |
explanation = (
|
| 54 |
"✅ This text appears neutral with no strong signs of gender bias based on the model's prediction."
|
| 55 |
)
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
# send the results back to the UI
|
| 58 |
return {
|
|
@@ -64,7 +67,7 @@ def detect_bias(text):
|
|
| 64 |
# build the Gradio web interface
|
| 65 |
with gr.Blocks() as demo:
|
| 66 |
# title and description at the top
|
| 67 |
-
gr.Markdown("Bias Bin – Fine-Tuned BERT Version by Aryan, Gowtham & Manoj")
|
| 68 |
gr.Markdown("This tool detects **gender bias** in narrative text using a BERT model fine-tuned on custom counterfactual data.")
|
| 69 |
|
| 70 |
# text input box for user
|
|
@@ -87,4 +90,4 @@ with gr.Blocks() as demo:
|
|
| 87 |
gr.Markdown("⚠️ **Disclaimer:** This model is trained on a small, augmented dataset and may not always be accurate. Interpret results carefully and consider human review where needed.")
|
| 88 |
|
| 89 |
# launch the app (runs on HF Spaces)
|
| 90 |
-
demo.launch()
|
|
|
|
| 28 |
def detect_bias(text):
|
| 29 |
# convert user input into tensors using the tokenizer
|
| 30 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 31 |
+
|
| 32 |
# disable gradient tracking — we’re only doing prediction, not training
|
| 33 |
with torch.no_grad():
|
| 34 |
outputs = model(**inputs) # pass inputs through the model
|
|
|
|
| 37 |
pred_label = torch.argmax(probs).item() # get the predicted label (0 or 1)
|
| 38 |
confidence = round(probs[pred_label].item(), 2) # grab the confidence score of that prediction
|
| 39 |
|
| 40 |
+
# classify the result based on thresholds
|
| 41 |
if confidence > 0.75 and pred_label == 1:
|
| 42 |
final_label = "Biased"
|
| 43 |
explanation = (
|
| 44 |
"⚠️ This text is likely biased. The model is highly confident that it reflects gender stereotypes or role bias."
|
| 45 |
)
|
| 46 |
+
elif 0.56 < confidence <= 0.75 and pred_label == 1:
|
| 47 |
final_label = "Possibly Biased"
|
| 48 |
explanation = (
|
| 49 |
"🤔 This text might contain some gender bias, but the model is not entirely sure. Review it carefully."
|
| 50 |
)
|
| 51 |
+
elif confidence <= 0.56:
|
| 52 |
final_label = "Unbiased"
|
| 53 |
explanation = (
|
| 54 |
"✅ This text appears neutral with no strong signs of gender bias based on the model's prediction."
|
| 55 |
)
|
| 56 |
+
else:
|
| 57 |
+
final_label = label_map[pred_label]
|
| 58 |
+
explanation = "Prediction complete."
|
| 59 |
|
| 60 |
# send the results back to the UI
|
| 61 |
return {
|
|
|
|
| 67 |
# build the Gradio web interface
|
| 68 |
with gr.Blocks() as demo:
|
| 69 |
# title and description at the top
|
| 70 |
+
gr.Markdown("## Bias Bin – Fine-Tuned BERT Version by Aryan, Gowtham & Manoj")
|
| 71 |
gr.Markdown("This tool detects **gender bias** in narrative text using a BERT model fine-tuned on custom counterfactual data.")
|
| 72 |
|
| 73 |
# text input box for user
|
|
|
|
| 90 |
gr.Markdown("⚠️ **Disclaimer:** This model is trained on a small, augmented dataset and may not always be accurate. Interpret results carefully and consider human review where needed.")
|
| 91 |
|
| 92 |
# launch the app (runs on HF Spaces)
|
| 93 |
+
demo.launch()
|