Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,90 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
-
import zipfile
|
| 5 |
-
import os
|
| 6 |
|
| 7 |
-
#
|
| 8 |
if not os.path.exists("fine_tuned_model"):
|
|
|
|
| 9 |
with zipfile.ZipFile("fine_tuned_model.zip", 'r') as zip_ref:
|
| 10 |
zip_ref.extractall("fine_tuned_model")
|
| 11 |
|
| 12 |
-
#
|
| 13 |
model_path = "./fine_tuned_model"
|
| 14 |
-
tokenizer = BertTokenizer.from_pretrained(model_path)
|
| 15 |
-
model = BertForSequenceClassification.from_pretrained(model_path)
|
| 16 |
-
model.eval()
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
def detect_bias(text):
|
|
|
|
| 23 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
|
|
|
|
|
|
| 24 |
with torch.no_grad():
|
| 25 |
-
outputs = model(**inputs)
|
| 26 |
-
logits = outputs.logits
|
| 27 |
-
probs = torch.softmax(logits, dim=1).squeeze()
|
| 28 |
-
pred_label = torch.argmax(probs).item()
|
| 29 |
-
confidence = round(probs[pred_label].item(), 2)
|
| 30 |
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
|
|
|
| 38 |
return {
|
| 39 |
-
"Bias Classification":
|
| 40 |
"Confidence Score": confidence,
|
| 41 |
"Explanation": explanation
|
| 42 |
}
|
| 43 |
|
| 44 |
-
#Gradio
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
)
|
| 50 |
|
|
|
|
| 51 |
text_input = gr.Textbox(
|
| 52 |
label="Enter Narrative Text",
|
| 53 |
-
|
| 54 |
-
|
| 55 |
)
|
| 56 |
|
|
|
|
| 57 |
submit_btn = gr.Button("Detect Bias")
|
|
|
|
|
|
|
| 58 |
output = gr.JSON(label="Prediction Output")
|
| 59 |
|
|
|
|
| 60 |
submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
-
gr.Markdown(
|
| 64 |
-
"___\n"
|
| 65 |
-
"<span style='color: gray; font-style: italic;'>⚠️ Disclaimer: This model is trained on a small, synthetic dataset. "
|
| 66 |
-
"Its predictions may not always be accurate or generalizable. Use with caution and consider human review when necessary.</span>",
|
| 67 |
-
unsafe_allow_html=True
|
| 68 |
-
)
|
| 69 |
|
|
|
|
| 70 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr # gradio makes it super easy to build a web UI
|
| 2 |
+
import torch # torch is used to run the model and handle tensors
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification # for loading our fine-tuned BERT model and tokenizer
|
| 4 |
+
import zipfile # we use this to unzip the uploaded model
|
| 5 |
+
import os # lets us check if the model folder already exists
|
| 6 |
|
| 7 |
+
# check if model folder is already extracted
|
| 8 |
if not os.path.exists("fine_tuned_model"):
|
| 9 |
+
# if not, unzip it
|
| 10 |
with zipfile.ZipFile("fine_tuned_model.zip", 'r') as zip_ref:
|
| 11 |
zip_ref.extractall("fine_tuned_model")
|
| 12 |
|
| 13 |
+
# path to our model directory
|
| 14 |
model_path = "./fine_tuned_model"
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# load the tokenizer and model from the directory
|
| 17 |
+
tokenizer = BertTokenizer.from_pretrained(model_path) # tokenizer breaks text into model-friendly tokens
|
| 18 |
+
model = BertForSequenceClassification.from_pretrained(model_path) # load the actual fine-tuned BERT model
|
| 19 |
+
model.eval() # set it to eval mode so it doesn’t try to learn during predictions
|
| 20 |
|
| 21 |
+
# define labels in case we want to refer back to them
|
| 22 |
+
label_map = {
|
| 23 |
+
0: "Unbiased",
|
| 24 |
+
1: "Biased"
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
# the main function that runs when user submits text
|
| 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
|
| 35 |
+
logits = outputs.logits # raw prediction scores
|
| 36 |
+
probs = torch.softmax(logits, dim=1).squeeze() # turn scores into probabilities
|
| 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 |
+
# now we decide the final label based on confidence score and predicted class
|
| 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.50 <= 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 |
+
else:
|
| 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 {
|
| 59 |
+
"Bias Classification": final_label,
|
| 60 |
"Confidence Score": confidence,
|
| 61 |
"Explanation": explanation
|
| 62 |
}
|
| 63 |
|
| 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
|
| 71 |
text_input = gr.Textbox(
|
| 72 |
label="Enter Narrative Text",
|
| 73 |
+
lines=4,
|
| 74 |
+
placeholder="E.g., 'The woman stayed at home while the man went to work.'"
|
| 75 |
)
|
| 76 |
|
| 77 |
+
# button to trigger prediction
|
| 78 |
submit_btn = gr.Button("Detect Bias")
|
| 79 |
+
|
| 80 |
+
# box to show the output (bias label + confidence + explanation)
|
| 81 |
output = gr.JSON(label="Prediction Output")
|
| 82 |
|
| 83 |
+
# link the button to the function
|
| 84 |
submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
|
| 85 |
|
| 86 |
+
# show a simple disclaimer at the bottom for transparency
|
| 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()
|