File size: 1,250 Bytes
c3ccf82 add7ad3 c3ccf82 add7ad3 c3ccf82 add7ad3 c3ccf82 edb4b31 add7ad3 53dac87 add7ad3 edb4b31 add7ad3 |
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 43 44 |
import gradio as gr
from transformers import pipeline
# Load the zero-shot classifier for bias detection using Facebook's BART MNLI.
classifier = pipeline(
"zero-shot-classification",
model="facebook/bart-large-mnli"
)
def process_text(text):
# Define candidate labels for bias classification.
candidate_labels = ["biased", "neutral"]
# Run zero-shot classification.
classification = classifier(text, candidate_labels)
detected_bias = classification["labels"][0]
confidence = classification["scores"][0]
# Return the results.
return {
"Detected Bias": detected_bias,
"Confidence": round(confidence, 2),
}
# Build the Gradio UI.
with gr.Blocks() as demo:
gr.Markdown("# Bias Bin")
gr.Markdown(
"Detect gender stereotypes in narrative text. "
"Enter a story or sentence below and click the **Submit** button."
)
text_input = gr.Textbox(
label="Enter Story Text",
placeholder="Type a story or sentence here...",
lines=5
)
submit_btn = gr.Button("Submit")
result_output = gr.JSON(label="Output")
submit_btn.click(fn=process_text, inputs=[text_input], outputs=[result_output])
demo.launch() |