Spaces:
Runtime error
Runtime error
File size: 1,763 Bytes
1d88294 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import gradio as gr
import torch
import torch.nn.functional as F
from transformers import XGLMTokenizer, XGLMForCausalLM
tokenizer = XGLMTokenizer.from_pretrained("facebook/xglm-564M")
model = XGLMForCausalLM.from_pretrained("facebook/xglm-564M")
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"],
)
iface.launch() |