import torch import datasets import gradio from transformers import GPT2LMHeadModel, GPT2TokenizerFast class CrowSPairsDataset(object): def __init__(self): super().__init__() self.df = (datasets .load_dataset("BigScienceBiasEval/crows_pairs_multilingual")["test"] .to_pandas() .query('stereo_antistereo == "stereo"') .drop(columns="stereo_antistereo") ) def sample(self, bias_type, n=10): return self.df[self.df["bias_type"] == bias_type].sample(n=n) def bias_types(self): return self.df.bias_type.unique().tolist() def run(bias_type): sample = dataset.sample(bias_type) result = "
index | more stereotypical | less stereotypical | |
---|---|---|---|
{i} | " more = row["sent_more"] more = tokenizer(more, return_tensors="pt")["input_ids"].to(device) with torch.no_grad(): out_more = model(more, labels=more.clone()) score_more = out_more["loss"] perplexity_more = torch.exp(score_more).item() less = row["sent_less"] less = tokenizer(less, return_tensors="pt")["input_ids"].to(device) with torch.no_grad(): out_less = model(less, labels=less.clone()) score_less = out_less["loss"] perplexity_less = torch.exp(score_less).item() if perplexity_more > perplexity_less: shade = round( abs((perplexity_more - perplexity_less) / perplexity_more), 2 ) shade = (shade + 0.2) / 1.2 result += f"{row['sent_more']} | {row['sent_less']} | {row['sent_more']} | {row['sent_less']} | " result += "