Update app.py
Browse files
app.py
CHANGED
|
@@ -1,25 +1,63 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
-
# Load
|
| 5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
-
#
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
|
| 4 |
+
# Load zero-shot classifier for bias detection using Facebook's BART MNLI.
|
| 5 |
+
# This model will classify the input text into "biased" or "neutral".
|
| 6 |
+
classifier = pipeline(
|
| 7 |
+
"zero-shot-classification",
|
| 8 |
+
model="facebook/bart-large-mnli"
|
| 9 |
+
)
|
| 10 |
|
| 11 |
+
# Load FLAN-T5 for rewriting text.
|
| 12 |
+
# This model will generate a neutral version of biased text.
|
| 13 |
+
rewriter = pipeline(
|
| 14 |
+
"text2text-generation",
|
| 15 |
+
model="google/flan-t5-base",
|
| 16 |
+
max_length=128
|
| 17 |
+
)
|
| 18 |
|
| 19 |
+
def process_text(text):
|
| 20 |
+
# Define candidate labels for bias classification.
|
| 21 |
+
candidate_labels = ["biased", "neutral"]
|
| 22 |
+
|
| 23 |
+
# Run zero-shot classification.
|
| 24 |
+
classification = classifier(text, candidate_labels)
|
| 25 |
+
detected_bias = classification["labels"][0]
|
| 26 |
+
confidence = classification["scores"][0]
|
| 27 |
+
|
| 28 |
+
# If the text is detected as biased, generate a neutral rewrite.
|
| 29 |
+
if detected_bias == "biased":
|
| 30 |
+
prompt = (
|
| 31 |
+
f"Rewrite the following text to remove gender stereotypes and "
|
| 32 |
+
f"make it neutral:\n\n{text}"
|
| 33 |
+
)
|
| 34 |
+
rewritten_text = rewriter(prompt)[0]["generated_text"]
|
| 35 |
+
else:
|
| 36 |
+
rewritten_text = "The text is already neutral."
|
| 37 |
+
|
| 38 |
+
# Return results in a dictionary.
|
| 39 |
+
return {
|
| 40 |
+
"Detected Bias": detected_bias,
|
| 41 |
+
"Confidence": confidence,
|
| 42 |
+
"Rewritten Text": rewritten_text
|
| 43 |
+
}
|
| 44 |
|
| 45 |
+
# Build the Gradio UI.
|
| 46 |
+
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# BiasNarrator")
|
| 48 |
+
gr.Markdown(
|
| 49 |
+
"Detect and neutralize gender stereotypes in narrative text. "
|
| 50 |
+
"Enter a story or sentence, and the model will check for biased language. "
|
| 51 |
+
"If bias is detected, it will generate a neutral version."
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
text_input = gr.Textbox(
|
| 55 |
+
label="Enter Story Text",
|
| 56 |
+
placeholder="Type a story or sentence here...",
|
| 57 |
+
lines=5
|
| 58 |
+
)
|
| 59 |
+
result_output = gr.JSON(label="Output")
|
| 60 |
+
|
| 61 |
+
text_input.submit(process_text, inputs=[text_input], outputs=[result_output])
|
| 62 |
+
|
| 63 |
demo.launch()
|