|
|
|
from doctest import OutputChecker |
|
import sys |
|
import torch |
|
import re |
|
import os |
|
import gradio as gr |
|
import requests |
|
import torch |
|
from transformers import GPT2Tokenizer, GPT2LMHeadModel |
|
from torch.nn.functional import softmax |
|
import numpy as np |
|
|
|
|
|
|
|
|
|
|
|
|
|
from sentence_transformers import SentenceTransformer, util |
|
|
|
model_sts = SentenceTransformer('stsb-distilbert-base') |
|
|
|
|
|
|
|
|
|
|
|
from transformers import GPT2Tokenizer, GPT2LMHeadModel |
|
import numpy as np |
|
import re |
|
|
|
|
|
|
|
def get_sim(x): |
|
x = str(x)[1:-1] |
|
x = str(x)[1:-1] |
|
return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
tokenizer = GPT2Tokenizer.from_pretrained('gpt2') |
|
model = GPT2LMHeadModel.from_pretrained('gpt2') |
|
|
|
|
|
|
|
def sentence_prob_mean(text): |
|
|
|
input_ids = tokenizer.encode(text, return_tensors='pt') |
|
|
|
|
|
with torch.no_grad(): |
|
outputs = model(input_ids, labels=input_ids) |
|
logits = outputs.logits |
|
|
|
|
|
shift_logits = logits[..., :-1, :].contiguous() |
|
shift_labels = input_ids[..., 1:].contiguous() |
|
|
|
|
|
probs = softmax(shift_logits, dim=-1) |
|
|
|
|
|
gathered_probs = torch.gather(probs, 2, shift_labels.unsqueeze(-1)).squeeze(-1) |
|
|
|
|
|
mean_prob = torch.mean(gathered_probs).item() |
|
|
|
return mean_prob |
|
|
|
|
|
def cos_sim(a, b): |
|
return np.inner(a, b) / (np.linalg.norm(a) * (np.linalg.norm(b))) |
|
|
|
|
|
|
|
|
|
def Visual_re_ranker(caption_man, caption_woman, visual_context_label, context_prob): |
|
caption_man = caption_man |
|
caption_woman = caption_woman |
|
visual_context_label = visual_context_label |
|
context_prob = context_prob |
|
caption_emb_man = model_sts.encode(caption_man, convert_to_tensor=True) |
|
caption_emb_woman = model_sts.encode(caption_woman, convert_to_tensor=True) |
|
context_label_emb = model_sts.encode(visual_context_label, convert_to_tensor=True) |
|
|
|
sim_m = cosine_scores = util.pytorch_cos_sim(caption_emb_man, context_label_emb) |
|
sim_m = sim_m.cpu().numpy() |
|
sim_m = get_sim(sim_m) |
|
|
|
sim_w = cosine_scores = util.pytorch_cos_sim(caption_emb_woman, context_label_emb) |
|
sim_w = sim_w.cpu().numpy() |
|
sim_w = get_sim(sim_w) |
|
|
|
|
|
LM_man = sentence_prob_mean(caption_man) |
|
LM_woman = sentence_prob_mean(caption_woman) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
score_man = pow(float(LM_man),pow((1-float(sim_m))/(1+ float(sim_m)),1-float(context_prob))) |
|
score_woman = pow(float(LM_woman),pow((1-float(sim_w))/(1+ float(sim_w)),1-float(context_prob))) |
|
|
|
|
|
|
|
return {"Man": float(score_man)/1, "Woman": float(score_woman)/1} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo = gr.Interface( |
|
fn=Visual_re_ranker, |
|
description="Demo for Women Wearing Lipstick: Measuring the Bias Between Object and Its Related Gender (distilbert)", |
|
inputs=[gr.Textbox(value="a man riding a motorcycle on a road") , gr.Textbox(value="a woman riding a motorcycle on a road"), gr.Textbox(value="motor scooter"), gr.Textbox(value="0.2183")], |
|
|
|
|
|
|
|
outputs="label", |
|
) |
|
demo.launch() |
|
|
|
|