Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,36 @@
|
|
| 1 |
-
import gradio as gr #
|
| 2 |
-
import torch #
|
| 3 |
-
from transformers import BertTokenizer, BertForSequenceClassification #
|
| 4 |
-
import zipfile #
|
| 5 |
-
import os #
|
| 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 |
-
#
|
| 14 |
model_path = "./fine_tuned_model"
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
-
#
|
| 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 just for reference (not used directly in decision now)
|
| 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)
|
| 35 |
-
logits = outputs.logits
|
| 36 |
-
probs = torch.softmax(logits, dim=1).squeeze()
|
| 37 |
-
pred_label = torch.argmax(probs).item()
|
| 38 |
-
confidence = round(probs[pred_label].item(), 2)
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
if pred_label == 1: #
|
| 42 |
if confidence > 0.75:
|
| 43 |
final_label = "Biased"
|
| 44 |
explanation = (
|
|
@@ -55,7 +47,7 @@ def detect_bias(text):
|
|
| 55 |
"😐 The model predicted 'biased' but with low confidence. The result may not be reliable."
|
| 56 |
)
|
| 57 |
|
| 58 |
-
elif pred_label == 0: #
|
| 59 |
if confidence > 0.75:
|
| 60 |
final_label = "Unbiased"
|
| 61 |
explanation = (
|
|
@@ -72,37 +64,35 @@ def detect_bias(text):
|
|
| 72 |
"😐 The model predicted 'unbiased' but with low confidence. The result is unclear."
|
| 73 |
)
|
| 74 |
|
| 75 |
-
# send the results back to the UI
|
| 76 |
return {
|
| 77 |
"Bias Classification": final_label,
|
| 78 |
"Confidence Score": confidence,
|
| 79 |
"Explanation": explanation
|
| 80 |
}
|
| 81 |
|
| 82 |
-
# build the Gradio
|
| 83 |
with gr.Blocks() as demo:
|
| 84 |
-
# title and description at the top
|
| 85 |
gr.Markdown("## Bias Bin – Fine-Tuned BERT Version by Aryan, Gowtham & Manoj")
|
| 86 |
-
gr.Markdown("
|
| 87 |
|
| 88 |
-
#
|
| 89 |
text_input = gr.Textbox(
|
| 90 |
label="Enter Narrative Text",
|
| 91 |
lines=4,
|
| 92 |
placeholder="E.g., 'The woman stayed at home while the man went to work.'"
|
| 93 |
)
|
| 94 |
|
| 95 |
-
# button to
|
| 96 |
submit_btn = gr.Button("Detect Bias")
|
| 97 |
|
| 98 |
-
#
|
| 99 |
output = gr.JSON(label="Prediction Output")
|
| 100 |
|
| 101 |
-
#
|
| 102 |
submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
-
gr.Markdown("⚠️ **Disclaimer:** This model is trained on a small,
|
| 106 |
|
| 107 |
-
#
|
| 108 |
demo.launch()
|
|
|
|
| 1 |
+
import gradio as gr # used to build the web interface
|
| 2 |
+
import torch # used to run the model and handle predictions
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification # to load our trained model and tokenizer
|
| 4 |
+
import zipfile # for extracting the uploaded model
|
| 5 |
+
import os # to check if folder already exists
|
| 6 |
|
| 7 |
+
# check if the fine-tuned model folder is already extracted
|
| 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 |
+
# load tokenizer and model
|
| 13 |
model_path = "./fine_tuned_model"
|
| 14 |
+
tokenizer = BertTokenizer.from_pretrained(model_path)
|
| 15 |
+
model = BertForSequenceClassification.from_pretrained(model_path)
|
| 16 |
+
model.eval() # set model to evaluation mode (important for inference)
|
| 17 |
|
| 18 |
+
# this function will be triggered when user submits a sentence
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
def detect_bias(text):
|
|
|
|
| 20 |
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 21 |
|
|
|
|
| 22 |
with torch.no_grad():
|
| 23 |
+
outputs = model(**inputs)
|
| 24 |
+
logits = outputs.logits
|
| 25 |
+
probs = torch.softmax(logits, dim=1).squeeze()
|
| 26 |
+
pred_label = torch.argmax(probs).item()
|
| 27 |
+
confidence = round(probs[pred_label].item(), 2)
|
| 28 |
+
|
| 29 |
+
# flip label logic because model predictions seem inverted
|
| 30 |
+
pred_label = 1 - pred_label # flip 0<->1
|
| 31 |
|
| 32 |
+
# prediction and explanation logic based on flipped label and confidence
|
| 33 |
+
if pred_label == 1: # now 1 = biased
|
| 34 |
if confidence > 0.75:
|
| 35 |
final_label = "Biased"
|
| 36 |
explanation = (
|
|
|
|
| 47 |
"😐 The model predicted 'biased' but with low confidence. The result may not be reliable."
|
| 48 |
)
|
| 49 |
|
| 50 |
+
elif pred_label == 0: # now 0 = unbiased
|
| 51 |
if confidence > 0.75:
|
| 52 |
final_label = "Unbiased"
|
| 53 |
explanation = (
|
|
|
|
| 64 |
"😐 The model predicted 'unbiased' but with low confidence. The result is unclear."
|
| 65 |
)
|
| 66 |
|
|
|
|
| 67 |
return {
|
| 68 |
"Bias Classification": final_label,
|
| 69 |
"Confidence Score": confidence,
|
| 70 |
"Explanation": explanation
|
| 71 |
}
|
| 72 |
|
| 73 |
+
# build the Gradio UI
|
| 74 |
with gr.Blocks() as demo:
|
|
|
|
| 75 |
gr.Markdown("## Bias Bin – Fine-Tuned BERT Version by Aryan, Gowtham & Manoj")
|
| 76 |
+
gr.Markdown("Detect gender bias in text using a BERT model fine-tuned with counterfactual data.")
|
| 77 |
|
| 78 |
+
# input box for users
|
| 79 |
text_input = gr.Textbox(
|
| 80 |
label="Enter Narrative Text",
|
| 81 |
lines=4,
|
| 82 |
placeholder="E.g., 'The woman stayed at home while the man went to work.'"
|
| 83 |
)
|
| 84 |
|
| 85 |
+
# button to submit
|
| 86 |
submit_btn = gr.Button("Detect Bias")
|
| 87 |
|
| 88 |
+
# output area
|
| 89 |
output = gr.JSON(label="Prediction Output")
|
| 90 |
|
| 91 |
+
# connect button to function
|
| 92 |
submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
|
| 93 |
|
| 94 |
+
# disclaimer at the bottom
|
| 95 |
+
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.")
|
| 96 |
|
| 97 |
+
# run the app
|
| 98 |
demo.launch()
|