aryn25 commited on
Commit
add7ad3
·
verified ·
1 Parent(s): 5fbd385

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -25
app.py CHANGED
@@ -1,33 +1,44 @@
1
  import gradio as gr
2
- from transformers import pipeline, BertTokenizer, BertForSequenceClassification
3
 
4
- # Load the fine-tuned model and tokenizer from the saved directory.
5
- model_path = "fine_tuned_model"
6
- tokenizer = BertTokenizer.from_pretrained(model_path)
7
- model = BertForSequenceClassification.from_pretrained(model_path)
 
8
 
9
- # Create an inference pipeline using the fine-tuned model.
10
- # For demonstration, we use the zero-shot pipeline with BART MNLI,
11
- # but here you can choose to use your fine-tuned model if it is adapted for inference.
12
- # In this example, we'll demonstrate with the fine-tuned BERT model for classification.
13
- def classify_text(text):
14
- # Tokenize input text
15
- inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True, max_length=128)
16
- outputs = model(**inputs)
17
- # Get the predicted label (0: original, 1: swapped)
18
- predicted_label = torch.argmax(outputs.logits, dim=1).item()
19
- # Map the label to a meaningful output
20
- label_map = {0: "Original (potentially biased)", 1: "Gender-swapped (counterfactual)"}
21
- return label_map[predicted_label]
 
22
 
23
  # Build the Gradio UI.
24
  with gr.Blocks() as demo:
25
- gr.Markdown("# Bias Bin Inference")
26
- gr.Markdown("Enter a narrative text below to classify it based on bias (original vs. gender-swapped).")
27
- text_input = gr.Textbox(label="Enter Narrative Text", placeholder="Type text here...", lines=5)
 
 
 
 
 
 
 
 
 
28
  submit_btn = gr.Button("Submit")
29
- result_output = gr.Textbox(label="Predicted Label")
30
 
31
- submit_btn.click(fn=classify_text, inputs=[text_input], outputs=[result_output])
32
-
33
- demo.launch()
 
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()