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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -84
app.py CHANGED
@@ -1,93 +1,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 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
- # classify the result based on thresholds
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.56 < 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
- elif confidence <= 0.56:
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
- else:
57
- final_label = label_map[pred_label]
58
- explanation = "Prediction complete."
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
 
60
- # send the results back to the UI
61
  return {
62
  "Bias Classification": final_label,
63
  "Confidence Score": confidence,
64
  "Explanation": explanation
65
- }
66
-
67
- # build the Gradio web interface
68
- with gr.Blocks() as demo:
69
- # title and description at the top
70
- gr.Markdown("## Bias Bin – Fine-Tuned BERT Version by Aryan, Gowtham & Manoj")
71
- gr.Markdown("This tool detects **gender bias** in narrative text using a BERT model fine-tuned on custom counterfactual data.")
72
-
73
- # text input box for user
74
- text_input = gr.Textbox(
75
- label="Enter Narrative Text",
76
- lines=4,
77
- placeholder="E.g., 'The woman stayed at home while the man went to work.'"
78
- )
79
-
80
- # button to trigger prediction
81
- submit_btn = gr.Button("Detect Bias")
82
-
83
- # box to show the output (bias label + confidence + explanation)
84
- output = gr.JSON(label="Prediction Output")
85
-
86
- # link the button to the function
87
- submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
88
-
89
- # show a simple disclaimer at the bottom for transparency
90
- 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.")
91
-
92
- # launch the app (runs on HF Spaces)
93
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 = (
16
+ "⚠️ This text is likely biased. The model is highly confident that it reflects gender stereotypes or role bias."
17
+ )
18
+ elif 0.5 <= confidence <= 0.75:
19
+ final_label = "Possibly Biased"
20
+ explanation = (
21
+ "🤔 This text might contain some gender bias, but the model is not entirely sure. Review it carefully."
22
+ )
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 = (
33
+ "✅ This text appears neutral with no strong signs of gender bias based on the model's understanding."
34
+ )
35
+ elif 0.5 <= confidence <= 0.75:
36
+ final_label = "Possibly Unbiased"
37
+ explanation = (
38
+ "🤔 This text seems unbiased, but the model isn't highly confident. It may still be worth reviewing."
39
+ )
40
+ else:
41
+ final_label = "Uncertain"
42
+ explanation = (
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
+ }