Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# Load both 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 |
+
bias_type = type_result['label']
|
| 16 |
+
type_score = type_result['score']
|
| 17 |
+
return (f"🧐 **Bias Detected!**\n"
|
| 18 |
+
f"- **Bias Probability:** {score:.2%}\n"
|
| 19 |
+
f"- **Bias Type:** {bias_type} (Confidence: {type_score:.2%})")
|
| 20 |
+
else:
|
| 21 |
+
return f"✅ **Unbiased** (Confidence: {score:.2%})"
|
| 22 |
+
|
| 23 |
+
# Gradio UI
|
| 24 |
+
iface = gr.Interface(
|
| 25 |
+
fn=detect_bias_and_type,
|
| 26 |
+
inputs=gr.Textbox(lines=3, placeholder="Enter a sentence..."),
|
| 27 |
+
outputs="markdown",
|
| 28 |
+
title="Bias Detector + Bias Type Classifier",
|
| 29 |
+
description=(
|
| 30 |
+
"This tool detects whether a text is biased and classifies the type of bias.\n"
|
| 31 |
+
"Models: `himel7/bias-detector` and `maximuspowers/bias-type-classifier`"
|
| 32 |
+
),
|
| 33 |
+
examples=[
|
| 34 |
+
["The brilliant leader saved the country from disaster."],
|
| 35 |
+
["The government announced new tax reforms."],
|
| 36 |
+
["The selfish billionaire hoarded his wealth."]
|
| 37 |
+
]
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
if __name__ == "__main__":
|
| 41 |
+
iface.launch()
|