|
import gradio as gr |
|
import torch |
|
from transformers import BertTokenizer, BertForSequenceClassification |
|
import zipfile |
|
import os |
|
|
|
|
|
if not os.path.exists("fine_tuned_model"): |
|
with zipfile.ZipFile("fine_tuned_model.zip", 'r') as zip_ref: |
|
zip_ref.extractall("fine_tuned_model") |
|
|
|
|
|
model_path = "./fine_tuned_model" |
|
tokenizer = BertTokenizer.from_pretrained(model_path) |
|
model = BertForSequenceClassification.from_pretrained(model_path) |
|
model.eval() |
|
|
|
|
|
def detect_bias(text): |
|
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True) |
|
|
|
with torch.no_grad(): |
|
outputs = model(**inputs) |
|
logits = outputs.logits |
|
probs = torch.softmax(logits, dim=1).squeeze() |
|
pred_label = torch.argmax(probs).item() |
|
confidence = round(probs[pred_label].item(), 2) |
|
|
|
|
|
pred_label = 1 - pred_label |
|
|
|
|
|
if pred_label == 1: |
|
if confidence > 0.75: |
|
final_label = "Biased" |
|
explanation = ( |
|
"β οΈ This text is likely biased. The model is highly confident that it reflects gender stereotypes or role bias." |
|
) |
|
elif 0.5 <= confidence <= 0.75: |
|
final_label = "Possibly Biased" |
|
explanation = ( |
|
"π€ This text might contain some gender bias, but the model is not entirely sure. Review it carefully." |
|
) |
|
else: |
|
final_label = "Uncertain" |
|
explanation = ( |
|
"π The model predicted 'biased' but with low confidence. The result may not be reliable." |
|
) |
|
|
|
elif pred_label == 0: |
|
if confidence > 0.75: |
|
final_label = "Unbiased" |
|
explanation = ( |
|
"β
This text appears neutral with no strong signs of gender bias based on the model's understanding." |
|
) |
|
elif 0.5 <= confidence <= 0.75: |
|
final_label = "Possibly Unbiased" |
|
explanation = ( |
|
"π€ This text seems unbiased, but the model isn't highly confident. It may still be worth reviewing." |
|
) |
|
else: |
|
final_label = "Uncertain" |
|
explanation = ( |
|
"π The model predicted 'unbiased' but with low confidence. The result is unclear." |
|
) |
|
|
|
return { |
|
"Bias Classification": final_label, |
|
"Confidence Score": confidence, |
|
"Explanation": explanation |
|
} |
|
|
|
|
|
with gr.Blocks() as demo: |
|
gr.Markdown("## Bias Bin β Fine-Tuned BERT Version by Aryan, Gowtham & Manoj") |
|
gr.Markdown("Detect gender bias in text using a BERT model fine-tuned with counterfactual data.") |
|
|
|
|
|
text_input = gr.Textbox( |
|
label="Enter Narrative Text", |
|
lines=4, |
|
placeholder="E.g., 'The woman stayed at home while the man went to work.'" |
|
) |
|
|
|
|
|
submit_btn = gr.Button("Detect Bias") |
|
|
|
|
|
output = gr.JSON(label="Prediction Output") |
|
|
|
|
|
submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output) |
|
|
|
|
|
gr.Markdown("β οΈ **Disclaimer:** This model is trained on a small, synthetic dataset and may not always be accurate. Results should be interpreted cautiously and reviewed by a human.") |
|
|
|
|
|
demo.launch() |