aryn25 commited on
Commit
3a92873
·
verified ·
1 Parent(s): 8dd90db

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -11
app.py CHANGED
@@ -1,15 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  def detect_bias(text):
 
2
  inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
3
 
 
4
  with torch.no_grad():
5
- outputs = model(**inputs)
6
- logits = outputs.logits
7
- probs = torch.softmax(logits, dim=1).squeeze()
8
- pred_label = torch.argmax(probs).item()
9
- confidence = round(probs[pred_label].item(), 2)
10
-
11
- # logic checks both label AND confidence
12
- if pred_label == 1: # predicted as biased
13
  if confidence > 0.75:
14
  final_label = "Biased"
15
  explanation = (
@@ -23,10 +52,10 @@ def detect_bias(text):
23
  else:
24
  final_label = "Uncertain"
25
  explanation = (
26
- "😐 The model predicted 'biased' but with very low confidence. The result may not be reliable."
27
  )
28
 
29
- elif pred_label == 0: # predicted as unbiased
30
  if confidence > 0.75:
31
  final_label = "Unbiased"
32
  explanation = (
@@ -43,8 +72,37 @@ def detect_bias(text):
43
  "😐 The model predicted 'unbiased' but with low confidence. The result is unclear."
44
  )
45
 
 
46
  return {
47
  "Bias Classification": final_label,
48
  "Confidence Score": confidence,
49
  "Explanation": explanation
50
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 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) # 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
+ # proper logic: evaluate both label and confidence
41
+ if pred_label == 1: # model predicts "biased"
42
  if confidence > 0.75:
43
  final_label = "Biased"
44
  explanation = (
 
52
  else:
53
  final_label = "Uncertain"
54
  explanation = (
55
+ "😐 The model predicted 'biased' but with low confidence. The result may not be reliable."
56
  )
57
 
58
+ elif pred_label == 0: # model predicts "unbiased"
59
  if confidence > 0.75:
60
  final_label = "Unbiased"
61
  explanation = (
 
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 web interface
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("This tool detects **gender bias** in narrative text using a BERT model fine-tuned on custom counterfactual data.")
87
+
88
+ # text input box for user
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 trigger prediction
96
+ submit_btn = gr.Button("Detect Bias")
97
+
98
+ # box to show the output (bias label + confidence + explanation)
99
+ output = gr.JSON(label="Prediction Output")
100
+
101
+ # link the button to the function
102
+ submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
103
+
104
+ # show a simple disclaimer at the bottom for transparency
105
+ 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.")
106
+
107
+ # launch the app (runs on HF Spaces)
108
+ demo.launch()