aryn25 commited on
Commit
edb4b31
·
verified ·
1 Parent(s): a3734e0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -18
app.py CHANGED
@@ -1,25 +1,63 @@
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
- # Load FLAN-T5 pipeline
5
- classifier = pipeline("text2text-generation", model="google/flan-t5-base", max_length=32)
 
 
 
 
6
 
7
- # Prompt template
8
- def classify_bias(headline):
9
- prompt = f"Classify the political bias of the following news headline into one of these three categories: Left, Center, or Right.\n\nHeadline: {headline}"
10
- output = classifier(prompt)[0]['generated_text'].strip()
11
- return output
 
 
12
 
13
- # Gradio UI
14
- with gr.Blocks() as demo:
15
- gr.Markdown("# 🧠 BiasLens – Political Bias Detector")
16
- gr.Markdown("Enter a news headline to detect whether it's Left, Center, or Right biased.")
17
-
18
- headline_input = gr.Textbox(label="📰 Enter News Headline", placeholder="e.g. Biden signs climate bill")
19
- result_output = gr.Textbox(label="🧭 Predicted Bias")
20
-
21
- classify_btn = gr.Button("Classify Bias")
22
-
23
- classify_btn.click(fn=classify_bias, inputs=[headline_input], outputs=[result_output])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
 
4
+ # Load zero-shot classifier for bias detection using Facebook's BART MNLI.
5
+ # This model will classify the input text into "biased" or "neutral".
6
+ classifier = pipeline(
7
+ "zero-shot-classification",
8
+ model="facebook/bart-large-mnli"
9
+ )
10
 
11
+ # Load FLAN-T5 for rewriting text.
12
+ # This model will generate a neutral version of biased text.
13
+ rewriter = pipeline(
14
+ "text2text-generation",
15
+ model="google/flan-t5-base",
16
+ max_length=128
17
+ )
18
 
19
+ def process_text(text):
20
+ # Define candidate labels for bias classification.
21
+ candidate_labels = ["biased", "neutral"]
22
+
23
+ # Run zero-shot classification.
24
+ classification = classifier(text, candidate_labels)
25
+ detected_bias = classification["labels"][0]
26
+ confidence = classification["scores"][0]
27
+
28
+ # If the text is detected as biased, generate a neutral rewrite.
29
+ if detected_bias == "biased":
30
+ prompt = (
31
+ f"Rewrite the following text to remove gender stereotypes and "
32
+ f"make it neutral:\n\n{text}"
33
+ )
34
+ rewritten_text = rewriter(prompt)[0]["generated_text"]
35
+ else:
36
+ rewritten_text = "The text is already neutral."
37
+
38
+ # Return results in a dictionary.
39
+ return {
40
+ "Detected Bias": detected_bias,
41
+ "Confidence": confidence,
42
+ "Rewritten Text": rewritten_text
43
+ }
44
 
45
+ # Build the Gradio UI.
46
+ with gr.Blocks() as demo:
47
+ gr.Markdown("# BiasNarrator")
48
+ gr.Markdown(
49
+ "Detect and neutralize gender stereotypes in narrative text. "
50
+ "Enter a story or sentence, and the model will check for biased language. "
51
+ "If bias is detected, it will generate a neutral version."
52
+ )
53
+
54
+ text_input = gr.Textbox(
55
+ label="Enter Story Text",
56
+ placeholder="Type a story or sentence here...",
57
+ lines=5
58
+ )
59
+ result_output = gr.JSON(label="Output")
60
+
61
+ text_input.submit(process_text, inputs=[text_input], outputs=[result_output])
62
+
63
  demo.launch()