aryn25 commited on
Commit
1b7cbf8
·
verified ·
1 Parent(s): c5474bb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -39
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
- #Unzip model if needed
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 model and tokenizer
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
- #Define output labels
19
- label_map = {0: "Unbiased", 1: "Biased"}
 
 
20
 
21
- #Bias classification function
 
 
 
 
 
 
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
- explanation = (
32
- "⚠️ This text may contain stereotypical gender associations or role biases. "
33
- "Consider rephrasing to ensure neutrality and inclusiveness."
34
- if pred_label == 1
35
- else " This text appears neutral with no obvious gender bias based on the model's understanding."
36
- )
 
 
 
 
 
 
 
 
 
 
37
 
 
38
  return {
39
- "Bias Classification": label_map[pred_label],
40
  "Confidence Score": confidence,
41
  "Explanation": explanation
42
  }
43
 
44
- #Gradio Interface
45
  with gr.Blocks() as demo:
46
- gr.Markdown(
47
- "# Bias Bin – Fine-Tuned BERT Version by Aryan, Gowtham & Manoj\n"
48
- "This tool detects **gender bias** in narrative text using a BERT model fine-tuned on custom counterfactual data."
49
- )
50
 
 
51
  text_input = gr.Textbox(
52
  label="Enter Narrative Text",
53
- placeholder="E.g., 'She is a great leader and he takes care of the house.'",
54
- lines=4
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
- #Disclaimer
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()