aryn25 commited on
Commit
ddb8efa
Β·
verified Β·
1 Parent(s): 00fd0ce

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -35
app.py CHANGED
@@ -1,44 +1,47 @@
1
  import gradio as gr
2
- from transformers import pipeline
 
 
 
3
 
4
- # Load the zero-shot classifier for bias detection using Facebook's BART MNLI.
5
- classifier = pipeline(
6
- "zero-shot-classification",
7
- model="facebook/bart-large-mnli"
8
- )
9
 
10
- def process_text(text):
11
- # Define candidate labels for bias classification.
12
- candidate_labels = ["biased", "neutral"]
13
-
14
- # Run zero-shot classification.
15
- classification = classifier(text, candidate_labels)
16
- detected_bias = classification["labels"][0]
17
- confidence = classification["scores"][0]
 
 
 
 
 
 
 
 
 
 
18
 
19
- # Return the results.
20
  return {
21
- "Detected Bias": detected_bias,
22
- "Confidence": round(confidence, 2),
23
  }
24
 
25
- # Build the Gradio UI.
26
  with gr.Blocks() as demo:
27
- gr.Markdown("# Bias Bin")
28
- gr.Markdown(
29
- "Detect gender stereotypes in narrative text. "
30
- "Enter a story or sentence below and click the **Submit** button."
31
- )
32
-
33
- text_input = gr.Textbox(
34
- label="Enter Story Text",
35
- placeholder="Type a story or sentence here...",
36
- lines=5
37
- )
38
 
39
- submit_btn = gr.Button("Submit")
40
- result_output = gr.JSON(label="Output")
41
-
42
- submit_btn.click(fn=process_text, inputs=[text_input], outputs=[result_output])
43
-
44
- demo.launch()
 
 
1
  import gradio as gr
2
+ import torch
3
+ from transformers import BertTokenizer, BertForSequenceClassification
4
+ import zipfile
5
+ import os
6
 
7
+ # βœ… Unzip the fine-tuned model if it's not 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 your fine-tuned 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 label mapping (adjust based on your labels)
19
+ label_map = {0: "Original-like", 1: "Swapped-like"}
20
+
21
+ # βœ… Inference 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
  return {
32
+ "Predicted Class": label_map[pred_label],
33
+ "Confidence": confidence
34
  }
35
 
36
+ # βœ… Gradio UI
37
  with gr.Blocks() as demo:
38
+ gr.Markdown("# Bias Bin – Fine-Tuned BERT Version")
39
+ gr.Markdown("This interface uses a fine-tuned BERT model to classify gender bias in narrative text.")
 
 
 
 
 
 
 
 
 
40
 
41
+ text_input = gr.Textbox(label="Enter Narrative Text", lines=4, placeholder="Type here...")
42
+ submit_btn = gr.Button("Detect Bias")
43
+ output = gr.JSON(label="Output")
44
+
45
+ submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
46
+
47
+ demo.launch()