Piyush Singh
commited on
Commit
·
19116b5
1
Parent(s):
5c173e9
Add application file
Browse files- app.py +50 -0
- modeling_multitask_bias.py +25 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import DebertaV2Tokenizer
|
| 4 |
+
from modeling_multitask_bias import MultiTaskBiasModel
|
| 5 |
+
|
| 6 |
+
REPO = "piyush333/deberta-v3-multitask-bias-detector-mach-1"
|
| 7 |
+
CKPT = "model_dpo_epoch_5.pt"
|
| 8 |
+
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 9 |
+
|
| 10 |
+
label_maps = {
|
| 11 |
+
"political": {0: "left", 1: "neutral", 2: "right"},
|
| 12 |
+
"gender": {0: "misogynist", 1: "neutral", 2: "misandrist"},
|
| 13 |
+
"immigration": {0: "anti", 1: "neutral", 2: "pro"}
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
def load_model():
|
| 17 |
+
tok = DebertaV2Tokenizer.from_pretrained("microsoft/deberta-v3-base")
|
| 18 |
+
mdl = MultiTaskBiasModel()
|
| 19 |
+
state = torch.hub.load_state_dict_from_url(
|
| 20 |
+
f"https://huggingface.co/{REPO}/resolve/main/{CKPT}",
|
| 21 |
+
map_location="cpu"
|
| 22 |
+
)
|
| 23 |
+
mdl.load_state_dict(state)
|
| 24 |
+
mdl.to(DEVICE).eval()
|
| 25 |
+
return tok, mdl
|
| 26 |
+
|
| 27 |
+
tokenizer, model = load_model()
|
| 28 |
+
|
| 29 |
+
def predict(text, task):
|
| 30 |
+
enc = tokenizer(text, return_tensors="pt", truncation=True, padding=True).to(DEVICE)
|
| 31 |
+
with torch.no_grad():
|
| 32 |
+
logits = model(enc["input_ids"], enc["attention_mask"], [task])
|
| 33 |
+
probs = torch.softmax(logits, dim=1)[0].tolist()
|
| 34 |
+
classes = [label_maps[task][i] for i in range(3)]
|
| 35 |
+
pred = classes[int(torch.tensor(probs).argmax())]
|
| 36 |
+
return pred, {cls: round(float(p), 4) for cls, p in zip(classes, probs)}
|
| 37 |
+
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=predict,
|
| 40 |
+
inputs=[
|
| 41 |
+
gr.Textbox(label="Text", lines=4, placeholder="Enter text..."),
|
| 42 |
+
gr.Dropdown(["political", "gender", "immigration"], label="Task")
|
| 43 |
+
],
|
| 44 |
+
outputs=[gr.Textbox(label="Prediction"), gr.JSON(label="Probabilities")],
|
| 45 |
+
title="DeBERTaV3 Multi-Task Bias Detector (Mach-1)",
|
| 46 |
+
description="Detects stance bias in political, gender, and immigration domains"
|
| 47 |
+
)
|
| 48 |
+
|
| 49 |
+
if __name__ == "__main__":
|
| 50 |
+
demo.launch()
|
modeling_multitask_bias.py
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import torch.nn as nn
|
| 3 |
+
from transformers import DebertaV2Model
|
| 4 |
+
|
| 5 |
+
class MultiTaskBiasModel(nn.Module):
|
| 6 |
+
def __init__(self, model_name="microsoft/deberta-v3-base"):
|
| 7 |
+
super().__init__()
|
| 8 |
+
self.bert = DebertaV2Model.from_pretrained(model_name)
|
| 9 |
+
hidden = self.bert.config.hidden_size
|
| 10 |
+
self.heads = nn.ModuleDict({
|
| 11 |
+
task: nn.Sequential(
|
| 12 |
+
nn.Linear(hidden, hidden),
|
| 13 |
+
nn.ReLU(),
|
| 14 |
+
nn.Dropout(0.2),
|
| 15 |
+
nn.Linear(hidden, 3)
|
| 16 |
+
)
|
| 17 |
+
for task in ["political", "gender", "immigration"]
|
| 18 |
+
})
|
| 19 |
+
|
| 20 |
+
def forward(self, input_ids, attention_mask, tasks):
|
| 21 |
+
outputs = self.bert(input_ids=input_ids, attention_mask=attention_mask).last_hidden_state[:, 0]
|
| 22 |
+
logits = []
|
| 23 |
+
for i in range(len(tasks)):
|
| 24 |
+
logits.append(self.heads[tasks[i]](outputs[i].unsqueeze(0)))
|
| 25 |
+
return torch.cat(logits, dim=0)
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
transformers
|
| 3 |
+
gradio
|