Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,44 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load the
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
model
|
|
|
|
| 8 |
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
| 22 |
|
| 23 |
# Build the Gradio UI.
|
| 24 |
with gr.Blocks() as demo:
|
| 25 |
-
gr.Markdown("# Bias Bin
|
| 26 |
-
gr.Markdown(
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 28 |
submit_btn = gr.Button("Submit")
|
| 29 |
-
result_output = gr.
|
| 30 |
|
| 31 |
-
submit_btn.click(fn=
|
| 32 |
-
|
| 33 |
-
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load the zero-shot classifier for bias detection using Facebook's BART MNLI.
|
| 5 |
+
classifier = pipeline(
|
| 6 |
+
"zero-shot-classification",
|
| 7 |
+
model="facebook/bart-large-mnli"
|
| 8 |
+
)
|
| 9 |
|
| 10 |
+
def process_text(text):
|
| 11 |
+
# Define candidate labels for bias classification.
|
| 12 |
+
candidate_labels = ["biased", "neutral"]
|
| 13 |
+
|
| 14 |
+
# Run zero-shot classification.
|
| 15 |
+
classification = classifier(text, candidate_labels)
|
| 16 |
+
detected_bias = classification["labels"][0]
|
| 17 |
+
confidence = classification["scores"][0]
|
| 18 |
+
|
| 19 |
+
# Return the results.
|
| 20 |
+
return {
|
| 21 |
+
"Detected Bias": detected_bias,
|
| 22 |
+
"Confidence": round(confidence, 2),
|
| 23 |
+
}
|
| 24 |
|
| 25 |
# Build the Gradio UI.
|
| 26 |
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# Bias Bin")
|
| 28 |
+
gr.Markdown(
|
| 29 |
+
"Detect gender stereotypes in narrative text. "
|
| 30 |
+
"Enter a story or sentence below and click the **Submit** button."
|
| 31 |
+
)
|
| 32 |
+
|
| 33 |
+
text_input = gr.Textbox(
|
| 34 |
+
label="Enter Story Text",
|
| 35 |
+
placeholder="Type a story or sentence here...",
|
| 36 |
+
lines=5
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
submit_btn = gr.Button("Submit")
|
| 40 |
+
result_output = gr.JSON(label="Output")
|
| 41 |
|
| 42 |
+
submit_btn.click(fn=process_text, inputs=[text_input], outputs=[result_output])
|
| 43 |
+
|
| 44 |
+
demo.launch()
|