valhalla's picture
Update app.py
bc5c316
Raw
History Blame Contribute Delete
2.38 kB
import gradio as gr
import torch
import torch.nn.functional as F
from transformers import XGLMTokenizer, XGLMForCausalLM
tokenizer = XGLMTokenizer.from_pretrained("facebook/xglm-2.9B")
model = XGLMForCausalLM.from_pretrained("facebook/xglm-2.9B")
data_samples = {
'en': [
{
"premise": "I wanted to conserve energy.",
"choice1": "I swept the floor in the unoccupied room.",
"choice2": "I shut off the light in the unoccupied room.",
"question": "effect",
"label": "1"
}
],
'zh': [
{
"premise": "ๆˆ‘ๆƒณ่Š‚็บฆ่ƒฝๆบใ€‚",
"choice1": "ๆˆ‘ๅœจ็ฉบ็€็š„ๆˆฟ้—ด้‡Œๆ‰ซไบ†ๅœฐๆฟใ€‚",
"choice2": "ๆˆ‘ๆŠŠ็ฉบๆˆฟ้—ด้‡Œ็š„็ฏๅ…ณไบ†ใ€‚",
"question": "effect",
"label": "1"
}
]
}
def get_logprobs(prompt):
inputs = tokenizer(prompt, return_tensors="pt")
input_ids, output_ids = inputs["input_ids"], inputs["input_ids"][:, 1:]
outputs = model(**inputs, labels=input_ids)
logits = outputs.logits
logprobs = torch.gather(F.log_softmax(logits, dim=2), 2, output_ids.unsqueeze(2))
return logprobs
# Zero-shot evaluation for the Choice of Plausible Alternatives (COPA) task.
# A return value of 0 indicates that the first alternative is more plausible,
# while 1 indicates that the second alternative is more plausible.
def COPA_eval(premise, choice1, choice2):
lprob1 = get_logprobs(premise + "\n" + choice1).sum()
lprob2 = get_logprobs(premise + "\n" + choice2).sum()
#return 0 if lprob1 > lprob2 else 1
return choice1 if lprob1 > lprob2 else choice2
iface = gr.Interface(
fn=COPA_eval,
inputs=["text", "text", "text"],
outputs=["text"],
theme="huggingface",
title="XGLM-Few-shot Learning with Multilingual Language Models",
description="A simple interface for zero-shot evaluation for the Choice of Plausible Alternatives (COPA) task using XGLM.",
examples=[["I wanted to conserve energy.", "I swept the floor in the unoccupied room.", "I shut off the light in the unoccupied room.",], ["ๆˆ‘ๆƒณ่Š‚็บฆ่ƒฝๆบใ€‚", "ๆˆ‘ๅœจ็ฉบ็€็š„ๆˆฟ้—ด้‡Œๆ‰ซไบ†ๅœฐๆฟใ€‚", "ๆˆ‘ๆŠŠ็ฉบๆˆฟ้—ด้‡Œ็š„็ฏๅ…ณไบ†ใ€‚",]],
article="<p style='text-align: center'><a href='https://arxiv.org/abs/2112.10668'>Few-shot Learning with Multilingual Language Models</a>"
)
iface.launch()