Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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
|
| 9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
label = detection_result['label']
|
| 11 |
score = detection_result['score']
|
| 12 |
|
| 13 |
if label == "LABEL_1": # Biased
|
| 14 |
-
type_result = bias_type_classifier(
|
| 15 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
else:
|
| 17 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
btn = gr.Button("Analyze")
|
| 47 |
-
btn.click(detect_bias_and_type, inputs=text_input, outputs=output)
|
| 48 |
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 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()
|