Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
import torch
|
4 |
+
import torch.nn.functional as F
|
5 |
+
|
6 |
+
from transformers import XGLMTokenizer, XGLMForCausalLM
|
7 |
+
|
8 |
+
tokenizer = XGLMTokenizer.from_pretrained("facebook/xglm-564M")
|
9 |
+
model = XGLMForCausalLM.from_pretrained("facebook/xglm-564M")
|
10 |
+
|
11 |
+
data_samples = {
|
12 |
+
'en': [
|
13 |
+
{
|
14 |
+
"premise": "I wanted to conserve energy.",
|
15 |
+
"choice1": "I swept the floor in the unoccupied room.",
|
16 |
+
"choice2": "I shut off the light in the unoccupied room.",
|
17 |
+
"question": "effect",
|
18 |
+
"label": "1"
|
19 |
+
}
|
20 |
+
],
|
21 |
+
'zh': [
|
22 |
+
{
|
23 |
+
"premise": "我想节约能源。",
|
24 |
+
"choice1": "我在空着的房间里扫了地板。",
|
25 |
+
"choice2": "我把空房间里的灯关了。",
|
26 |
+
"question": "effect",
|
27 |
+
"label": "1"
|
28 |
+
}
|
29 |
+
]
|
30 |
+
}
|
31 |
+
|
32 |
+
def get_logprobs(prompt):
|
33 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
34 |
+
input_ids, output_ids = inputs["input_ids"], inputs["input_ids"][:, 1:]
|
35 |
+
outputs = model(**inputs, labels=input_ids)
|
36 |
+
logits = outputs.logits
|
37 |
+
logprobs = torch.gather(F.log_softmax(logits, dim=2), 2, output_ids.unsqueeze(2))
|
38 |
+
return logprobs
|
39 |
+
|
40 |
+
|
41 |
+
# Zero-shot evaluation for the Choice of Plausible Alternatives (COPA) task.
|
42 |
+
# A return value of 0 indicates that the first alternative is more plausible,
|
43 |
+
# while 1 indicates that the second alternative is more plausible.
|
44 |
+
def COPA_eval(premise, choice1, choice2):
|
45 |
+
lprob1 = get_logprobs(premise + "\n" + choice1).sum()
|
46 |
+
lprob2 = get_logprobs(premise + "\n" + choice2).sum()
|
47 |
+
#return 0 if lprob1 > lprob2 else 1
|
48 |
+
return choice1 if lprob1 > lprob2 else choice2
|
49 |
+
|
50 |
+
|
51 |
+
|
52 |
+
iface = gr.Interface(
|
53 |
+
fn=COPA_eval,
|
54 |
+
inputs=["text", "text", "text"],
|
55 |
+
outputs=["text"],
|
56 |
+
)
|
57 |
+
iface.launch()
|