File size: 2,380 Bytes
1d88294
 
 
 
 
 
 
bc5c316
 
1d88294
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ac01d7
 
 
1713868
c8c32f6
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
58
59
60
61
62
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()