File size: 1,480 Bytes
6da6acb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import gradio as gr
from transformers import pipeline

# Load both models
bias_detector = pipeline("text-classification", model="himel7/bias-detector")
bias_type_classifier = pipeline("text-classification", model="maximuspowers/bias-type-classifier")

def detect_bias_and_type(text):
    detection_result = bias_detector(text)[0]
    label = detection_result['label']
    score = detection_result['score']

    if label == "LABEL_1":  # Biased
        type_result = bias_type_classifier(text)[0]
        bias_type = type_result['label']
        type_score = type_result['score']
        return (f"🧐 **Bias Detected!**\n"
                f"- **Bias Probability:** {score:.2%}\n"
                f"- **Bias Type:** {bias_type} (Confidence: {type_score:.2%})")
    else:
        return f"✅ **Unbiased** (Confidence: {score:.2%})"

# Gradio UI
iface = gr.Interface(
    fn=detect_bias_and_type,
    inputs=gr.Textbox(lines=3, placeholder="Enter a sentence..."),
    outputs="markdown",
    title="Bias Detector + Bias Type Classifier",
    description=(
        "This tool detects whether a text is biased and classifies the type of bias.\n"
        "Models: `himel7/bias-detector` and `maximuspowers/bias-type-classifier`"
    ),
    examples=[
        ["The brilliant leader saved the country from disaster."],
        ["The government announced new tax reforms."],
        ["The selfish billionaire hoarded his wealth."]
    ]
)

if __name__ == "__main__":
    iface.launch()