himel7 commited on
Commit
0f32d66
·
verified ·
1 Parent(s): 86d3754

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +75 -23
app.py CHANGED
@@ -1,21 +1,79 @@
1
  import gradio as gr
2
  from transformers import pipeline
 
 
 
3
 
4
  # Load models
5
  bias_detector = pipeline("text-classification", model="himel7/bias-detector")
6
  bias_type_classifier = pipeline("text-classification", model="maximuspowers/bias-type-classifier")
7
 
8
- def detect_bias_and_type(text):
9
- detection_result = bias_detector(text)[0]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  label = detection_result['label']
11
  score = detection_result['score']
12
 
13
  if label == "LABEL_1": # Biased
14
- type_result = bias_type_classifier(text)[0]
15
- return f"🧐 **Bias Detected!**\n- Bias Probability: {score:.2%}\n- Bias Type: {type_result['label']} ({type_result['score']:.2%})"
 
 
 
 
 
 
16
  else:
17
- return f"✅ **Unbiased** (Confidence: {score:.2%})"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
 
 
 
 
 
 
 
 
 
 
19
  badges_html = """
20
  <p align="center">
21
  <a href="https://huggingface.co/himel7/bias-detector">
@@ -34,27 +92,21 @@ badges_html = """
34
  """
35
 
36
  with gr.Blocks() as demo:
37
- # Render badges using HTML (not escaped)
38
  gr.HTML(badges_html)
 
39
 
40
- gr.Markdown("## Bias Analyzer")
41
- gr.Markdown("Enter a sentence to detect bias and its type.")
42
-
43
- text_input = gr.Textbox(lines=3, placeholder="Enter a sentence...")
44
- output = gr.Markdown()
45
-
46
- btn = gr.Button("Analyze")
47
- btn.click(detect_bias_and_type, inputs=text_input, outputs=output)
48
 
49
- # Examples
50
- gr.Examples(
51
- examples=[
52
- ["The brilliant leader saved the country from disaster."],
53
- ["The government announced new tax reforms."],
54
- ["The selfish billionaire hoarded his wealth."]
55
- ],
56
- inputs=text_input
57
- )
58
 
59
  if __name__ == "__main__":
60
  demo.launch()
 
1
  import gradio as gr
2
  from transformers import pipeline
3
+ import fitz # PyMuPDF
4
+ import re
5
+ import pandas as pd
6
 
7
  # Load models
8
  bias_detector = pipeline("text-classification", model="himel7/bias-detector")
9
  bias_type_classifier = pipeline("text-classification", model="maximuspowers/bias-type-classifier")
10
 
11
+ def extract_text_from_pdf(pdf_file):
12
+ """Extract text from a PDF file using PyMuPDF"""
13
+ text = ""
14
+ with fitz.open(pdf_file) as pdf:
15
+ for page in pdf:
16
+ text += page.get_text("text")
17
+ return text
18
+
19
+ def split_into_sentences(text):
20
+ """Split text into sentences (basic split by .!? with spaces)"""
21
+ sentences = re.split(r'(?<=[.!?])\s+', text.strip())
22
+ return [s for s in sentences if s]
23
+
24
+ def analyze_sentence(sentence):
25
+ """Run bias detection and (if biased) bias type classification"""
26
+ detection_result = bias_detector(sentence)[0]
27
  label = detection_result['label']
28
  score = detection_result['score']
29
 
30
  if label == "LABEL_1": # Biased
31
+ type_result = bias_type_classifier(sentence)[0]
32
+ return {
33
+ "sentence": sentence,
34
+ "bias": "Biased",
35
+ "bias_score": round(score, 2),
36
+ "bias_type": type_result['label'],
37
+ "bias_type_score": round(type_result['score'], 2)
38
+ }
39
  else:
40
+ return {
41
+ "sentence": sentence,
42
+ "bias": "Unbiased",
43
+ "bias_score": round(score, 2),
44
+ "bias_type": "-",
45
+ "bias_type_score": "-"
46
+ }
47
+
48
+ def analyze_pdf(pdf_file):
49
+ """Full pipeline: extract text, split sentences, analyze bias"""
50
+ text = extract_text_from_pdf(pdf_file)
51
+ sentences = split_into_sentences(text)
52
+
53
+ results = [analyze_sentence(s) for s in sentences]
54
+
55
+ # Stats
56
+ total = len(results)
57
+ biased = sum(1 for r in results if r["bias"] == "Biased")
58
+ unbiased = total - biased
59
+
60
+ stats_md = f"""
61
+ ### 📊 Bias Statistics
62
+ - **Total Sentences:** {total}
63
+ - **Biased Sentences:** {biased} ({(biased/total)*100:.1f}%)
64
+ - **Unbiased Sentences:** {unbiased} ({(unbiased/total)*100:.1f}%)
65
+ """
66
 
67
+ # Create a DataFrame for table display
68
+ df = pd.DataFrame(results)
69
+ return stats_md, df
70
+
71
+
72
+ def analyze_text(text):
73
+ """Single text input analysis"""
74
+ return analyze_sentence(text)
75
+
76
+ # Top HTML badges
77
  badges_html = """
78
  <p align="center">
79
  <a href="https://huggingface.co/himel7/bias-detector">
 
92
  """
93
 
94
  with gr.Blocks() as demo:
 
95
  gr.HTML(badges_html)
96
+ gr.Markdown("## Bias Detector + Bias Type Classifier")
97
 
98
+ with gr.Tab("Single Sentence"):
99
+ text_input = gr.Textbox(lines=3, placeholder="Enter a sentence...")
100
+ output = gr.JSON()
101
+ btn = gr.Button("Analyze")
102
+ btn.click(analyze_text, inputs=text_input, outputs=output)
 
 
 
103
 
104
+ with gr.Tab("Analyze PDF"):
105
+ pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
106
+ stats_output = gr.Markdown()
107
+ table_output = gr.Dataframe(headers=["Sentence", "Bias", "Bias Score", "Bias Type", "Bias Type Score"])
108
+ analyze_btn = gr.Button("Analyze PDF")
109
+ analyze_btn.click(analyze_pdf, inputs=pdf_input, outputs=[stats_output, table_output])
 
 
 
110
 
111
  if __name__ == "__main__":
112
  demo.launch()