Spaces:
Sleeping
Sleeping
Fixed MNLI issues
Browse files
app.py
CHANGED
@@ -11,50 +11,75 @@ qa_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-small")
|
|
11 |
qa_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small", device_map="auto")
|
12 |
|
13 |
def predict(context, intent, multi_class):
|
14 |
-
input_text = "
|
15 |
input_ids = qa_tokenizer(input_text, return_tensors="pt").input_ids.to(device)
|
16 |
-
opposite_output = qa_tokenizer.decode(qa_model.generate(input_ids, max_length=2)[0])
|
17 |
-
input_text = "
|
18 |
input_ids = qa_tokenizer(input_text, return_tensors="pt").input_ids.to(device)
|
19 |
-
object_output = qa_tokenizer.decode(qa_model.generate(input_ids, max_length=2)[0])
|
20 |
-
batch = ['
|
|
|
21 |
outputs = []
|
|
|
22 |
for i, hypothesis in enumerate(batch):
|
23 |
-
input_ids = te_tokenizer.encode(context, hypothesis, return_tensors='pt').to(device)
|
|
|
24 |
# -> [contradiction, neutral, entailment]
|
25 |
logits = te_model(input_ids)[0][0]
|
26 |
|
27 |
-
if (i
|
28 |
# -> [contradiction, entailment]
|
29 |
probs = logits[[0,2]].softmax(dim=0)
|
30 |
else:
|
31 |
probs = logits.softmax(dim=0)
|
32 |
outputs.append(probs)
|
33 |
|
34 |
-
#
|
35 |
-
|
36 |
-
|
|
|
|
|
|
|
37 |
outputs[0] = outputs[0].flip(dims=[0])
|
38 |
-
pn_tensor = (outputs[0] + outputs[1])/2
|
39 |
-
pn_tensor[1] = pn_tensor[1] * outputs[2][0]
|
40 |
-
pn_tensor[2] = pn_tensor[2] * outputs[2][1]
|
41 |
-
pn_tensor[0] = pn_tensor[0] * outputs[2][1]
|
42 |
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
|
|
45 |
if (multi_class):
|
46 |
-
|
|
|
47 |
else:
|
48 |
-
|
49 |
-
|
50 |
-
return {"agree":
|
51 |
|
52 |
gradio_app = gr.Interface(
|
53 |
predict,
|
54 |
inputs=[gr.Text(label="Sentence"), gr.Text(label="Class"), gr.Checkbox(label="Allow multiple true classes")],
|
55 |
outputs=[gr.Label(num_top_classes=3)],
|
56 |
title="Intent Analysis",
|
57 |
-
description="This model predicts whether or not the **
|
58 |
)
|
59 |
|
60 |
-
gradio_app.launch()
|
|
|
11 |
qa_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-small", device_map="auto")
|
12 |
|
13 |
def predict(context, intent, multi_class):
|
14 |
+
input_text = "What is the opposite of " + intent + "?"
|
15 |
input_ids = qa_tokenizer(input_text, return_tensors="pt").input_ids.to(device)
|
16 |
+
opposite_output = qa_tokenizer.decode(qa_model.generate(input_ids, max_length=2)[0], skip_special_tokens=True)
|
17 |
+
input_text = "What object is the following describing: " + context
|
18 |
input_ids = qa_tokenizer(input_text, return_tensors="pt").input_ids.to(device)
|
19 |
+
object_output = qa_tokenizer.decode(qa_model.generate(input_ids, max_length=2)[0], skip_special_tokens=True)
|
20 |
+
batch = ['The ' + object_output + ' is ' + intent, 'The ' + object_output + ' is ' + opposite_output, 'The ' + object_output + ' is not ' + intent, 'The ' + object_output + ' is not ' + opposite_output]
|
21 |
+
|
22 |
outputs = []
|
23 |
+
print(intent, opposite_output, object_output)
|
24 |
for i, hypothesis in enumerate(batch):
|
25 |
+
# input_ids = te_tokenizer.encode(context, hypothesis, return_tensors='pt').to(device)
|
26 |
+
input_ids = te_model(context, hypothesis).to(device)
|
27 |
# -> [contradiction, neutral, entailment]
|
28 |
logits = te_model(input_ids)[0][0]
|
29 |
|
30 |
+
if (i >= 2):
|
31 |
# -> [contradiction, entailment]
|
32 |
probs = logits[[0,2]].softmax(dim=0)
|
33 |
else:
|
34 |
probs = logits.softmax(dim=0)
|
35 |
outputs.append(probs)
|
36 |
|
37 |
+
# calculate the stochastic vector for it being neither the positive or negative class
|
38 |
+
perfect_prob = [0, 0]
|
39 |
+
perfect_prob[0] = (outputs[2][0] + outputs[3][1])/2
|
40 |
+
perfect_prob[1] = 1-perfect_prob[2][0]
|
41 |
+
|
42 |
+
# -> [entailment, neutral, contradiction] for positive
|
43 |
outputs[0] = outputs[0].flip(dims=[0])
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
# combine the negative and positive class by summing by the opposite of the negative class
|
46 |
+
aggregated = (outputs[0] + outputs[1])/2
|
47 |
+
|
48 |
+
# multiplying vectors
|
49 |
+
aggregated[1] = aggregated[1] * perfect_prob[0]
|
50 |
+
|
51 |
+
# if it is neither the positive or negative class, then it is more likely the neutral class, so adjust accordingly
|
52 |
+
if (perfect_prob[0] > perfect_prob[1]):
|
53 |
+
aggregated[2] = aggregated[2] * perfect_prob[1]
|
54 |
+
aggregated[0] = aggregated[0] * perfect_prob[1]
|
55 |
+
else:
|
56 |
+
# if it is more likely the positive class, increase its probability by a scale of the probability of it not being perfect
|
57 |
+
if (aggregated[0] > aggregated[2]):
|
58 |
+
aggregated[2] = aggregated[2] * perfect_prob[0]
|
59 |
+
aggregated[0] = aggregated[0] * perfect_prob[1]
|
60 |
+
# if it is more likely the negative class, increase its probability by a scale of the probability of it not being perfect
|
61 |
+
else:
|
62 |
+
aggregated[2] = aggregated[2] * perfect_prob[1]
|
63 |
+
aggregated[0] = aggregated[0] * perfect_prob[0]
|
64 |
+
|
65 |
+
# to exagerate differences
|
66 |
+
aggregated = aggregated.exp() - 1
|
67 |
|
68 |
+
# multiple true classes
|
69 |
if (multi_class):
|
70 |
+
aggregated = torch.sigmoid(aggregated)
|
71 |
+
# only one true class
|
72 |
else:
|
73 |
+
aggregated = aggregated.softmax(dim=0)
|
74 |
+
aggregated = aggregated.tolist()
|
75 |
+
return {"agree": aggregated[0], "neutral": aggregated[1], "disagree": aggregated[2]}
|
76 |
|
77 |
gradio_app = gr.Interface(
|
78 |
predict,
|
79 |
inputs=[gr.Text(label="Sentence"), gr.Text(label="Class"), gr.Checkbox(label="Allow multiple true classes")],
|
80 |
outputs=[gr.Label(num_top_classes=3)],
|
81 |
title="Intent Analysis",
|
82 |
+
description="This model predicts whether or not the **_class_** describes the **_object described in the sentence._**"
|
83 |
)
|
84 |
|
85 |
+
gradio_app.launch(share=True)
|