Update app.py
Browse files
app.py
CHANGED
|
@@ -1,44 +1,47 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
-
|
|
|
|
|
|
|
|
|
|
| 3 |
|
| 4 |
-
#
|
| 5 |
-
|
| 6 |
-
"
|
| 7 |
-
|
| 8 |
-
)
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
# Return the results.
|
| 20 |
return {
|
| 21 |
-
"
|
| 22 |
-
"Confidence":
|
| 23 |
}
|
| 24 |
|
| 25 |
-
#
|
| 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 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import BertTokenizer, BertForSequenceClassification
|
| 4 |
+
import zipfile
|
| 5 |
+
import os
|
| 6 |
|
| 7 |
+
# β
Unzip the fine-tuned model if it's not already extracted
|
| 8 |
+
if not os.path.exists("fine_tuned_model"):
|
| 9 |
+
with zipfile.ZipFile("fine_tuned_model.zip", 'r') as zip_ref:
|
| 10 |
+
zip_ref.extractall("fine_tuned_model")
|
|
|
|
| 11 |
|
| 12 |
+
# β
Load your fine-tuned model and tokenizer
|
| 13 |
+
model_path = "./fine_tuned_model"
|
| 14 |
+
tokenizer = BertTokenizer.from_pretrained(model_path)
|
| 15 |
+
model = BertForSequenceClassification.from_pretrained(model_path)
|
| 16 |
+
model.eval()
|
| 17 |
+
|
| 18 |
+
# β
Define label mapping (adjust based on your labels)
|
| 19 |
+
label_map = {0: "Original-like", 1: "Swapped-like"}
|
| 20 |
+
|
| 21 |
+
# β
Inference function
|
| 22 |
+
def detect_bias(text):
|
| 23 |
+
inputs = tokenizer(text, return_tensors="pt", truncation=True, padding=True)
|
| 24 |
+
with torch.no_grad():
|
| 25 |
+
outputs = model(**inputs)
|
| 26 |
+
logits = outputs.logits
|
| 27 |
+
probs = torch.softmax(logits, dim=1).squeeze()
|
| 28 |
+
pred_label = torch.argmax(probs).item()
|
| 29 |
+
confidence = round(probs[pred_label].item(), 2)
|
| 30 |
|
|
|
|
| 31 |
return {
|
| 32 |
+
"Predicted Class": label_map[pred_label],
|
| 33 |
+
"Confidence": confidence
|
| 34 |
}
|
| 35 |
|
| 36 |
+
# β
Gradio UI
|
| 37 |
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# Bias Bin β Fine-Tuned BERT Version")
|
| 39 |
+
gr.Markdown("This interface uses a fine-tuned BERT model to classify gender bias in narrative text.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
+
text_input = gr.Textbox(label="Enter Narrative Text", lines=4, placeholder="Type here...")
|
| 42 |
+
submit_btn = gr.Button("Detect Bias")
|
| 43 |
+
output = gr.JSON(label="Output")
|
| 44 |
+
|
| 45 |
+
submit_btn.click(fn=detect_bias, inputs=text_input, outputs=output)
|
| 46 |
+
|
| 47 |
+
demo.launch()
|