youj2005 commited on
Commit
36b3b29
1 Parent(s): 2a8fa62

Made improvements and changed base mnli model

Browse files
Files changed (2) hide show
  1. app.py +20 -35
  2. gradio_cached_examples/18/log.csv +4 -1
app.py CHANGED
@@ -1,12 +1,11 @@
1
  import gradio as gr
2
- from transformers import BartForSequenceClassification, BartTokenizer
3
  from transformers import T5Tokenizer, T5ForConditionalGeneration
4
  import torch
5
 
6
  device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
7
-
8
- te_tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-mnli')
9
- te_model = BartForSequenceClassification.from_pretrained('facebook/bart-large-mnli', device_map="auto")
10
  qa_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large")
11
  qa_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large", device_map="auto")
12
 
@@ -17,65 +16,51 @@ def predict(context, intent, multi_class):
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
  for i, hypothesis in enumerate(batch):
24
  input_ids = te_tokenizer.encode(context, hypothesis, return_tensors='pt').to(device)
25
 
26
  # -> [contradiction, neutral, entailment]
27
  logits = te_model(input_ids)[0][0]
28
-
 
29
  if (i >= 2):
30
  # -> [contradiction, entailment]
31
  probs = logits[[0,2]].softmax(dim=0)
32
  else:
33
- probs = logits.softmax(dim=0)
34
  outputs.append(probs)
35
-
36
  # calculate the stochastic vector for it being neither the positive or negative class
37
- perfect_prob = [0, 0]
38
- perfect_prob[1] = max(float(outputs[2][0]), float(outputs[3][0]))
39
- perfect_prob[0] = 1-perfect_prob[1]
40
  # -> [entailment, contradiction] for perfect
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
- # this way 0 maps to 0
67
- aggregated = aggregated.exp()-1
68
 
69
  # multiple true classes
70
  if (multi_class):
71
  aggregated = torch.sigmoid(aggregated)
 
72
  # only one true class
73
  else:
74
  aggregated = aggregated.softmax(dim=0)
 
75
  aggregated = aggregated.tolist()
76
- return {"agree": aggregated[0], "neutral": aggregated[1], "disagree": aggregated[2]}, {"agree": outputs[0][0], "neutral": outputs[0][1], "disagree": outputs[0][2]}
77
-
78
- examples = [["The pants fit great, even the waist will fit me fine once I'm back to my normal weight, but the bottom is what's large. You can roll up the bottom part of the legs, or the top at the waist band for hanging out at the house, but if you have one nearby, simply have them re-hemmed.", "long"]]
79
 
80
  gradio_app = gr.Interface(
81
  predict,
@@ -83,8 +68,8 @@ gradio_app = gr.Interface(
83
  inputs=[gr.Text(label="Statement"), gr.Text(label="Class"), gr.Checkbox(label="Allow multiple true classes")],
84
  outputs=[gr.Label(num_top_classes=3, label="With Postprocessing"), gr.Label(num_top_classes=3, label="Without Postprocessing")],
85
  title="Intent Analysis",
86
- description="This model predicts whether or not the **_class_** describes the **_object described in the sentence_**. <br /> The two outputs shows what TE would predict with and without the postprocessing. An example edge case for normal TE is shown below. <br /> **_It is recommended that you clone the repository to speed up processing time_**.",
87
  cache_examples=True
88
  )
89
 
90
- gradio_app.launch(share=True)
 
1
  import gradio as gr
2
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification
3
  from transformers import T5Tokenizer, T5ForConditionalGeneration
4
  import torch
5
 
6
  device = torch.device("mps" if torch.backends.mps.is_available() else "cpu")
7
+ te_tokenizer = AutoTokenizer.from_pretrained('MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli')
8
+ te_model = AutoModelForSequenceClassification.from_pretrained('MoritzLaurer/DeBERTa-v3-base-mnli-fever-anli').to(device)
 
9
  qa_tokenizer = T5Tokenizer.from_pretrained("google/flan-t5-large")
10
  qa_model = T5ForConditionalGeneration.from_pretrained("google/flan-t5-large", device_map="auto")
11
 
 
16
  input_text = "What object is the following describing: " + context
17
  input_ids = qa_tokenizer(input_text, return_tensors="pt").input_ids.to(device)
18
  object_output = qa_tokenizer.decode(qa_model.generate(input_ids, max_length=2)[0], skip_special_tokens=True)
19
+ batch = ['The ' + object_output + ' is ' + intent, 'The ' + object_output + ' is ' + opposite_output, 'The ' + object_output + ' is neither ' + intent + ' nor ' + opposite_output]
 
20
  outputs = []
21
+ normal = 0
22
+
23
  for i, hypothesis in enumerate(batch):
24
  input_ids = te_tokenizer.encode(context, hypothesis, return_tensors='pt').to(device)
25
 
26
  # -> [contradiction, neutral, entailment]
27
  logits = te_model(input_ids)[0][0]
28
+ if (i == 0):
29
+ normal = logits
30
  if (i >= 2):
31
  # -> [contradiction, entailment]
32
  probs = logits[[0,2]].softmax(dim=0)
33
  else:
34
+ probs = torch.exp(logits)
35
  outputs.append(probs)
 
36
  # calculate the stochastic vector for it being neither the positive or negative class
37
+ perfect_prob = outputs[2]
 
 
38
  # -> [entailment, contradiction] for perfect
39
 
40
  # -> [entailment, neutral, contradiction] for positive
41
+ outputs[1] = outputs[1].flip(dims=[0])
42
 
43
  # combine the negative and positive class by summing by the opposite of the negative class
44
  aggregated = (outputs[0] + outputs[1])/2
45
 
46
  # multiplying vectors
47
  aggregated[1] = aggregated[1] * perfect_prob[0]
48
+ aggregated[0] = aggregated[0] * perfect_prob[1]
49
+ aggregated[2] = aggregated[2] * perfect_prob[1]
50
 
51
+ aggregated = torch.sqrt(aggregated)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
 
53
  # multiple true classes
54
  if (multi_class):
55
  aggregated = torch.sigmoid(aggregated)
56
+ normal = torch.sigmoid(normal)
57
  # only one true class
58
  else:
59
  aggregated = aggregated.softmax(dim=0)
60
+ normal = normal.softmax(dim=0)
61
  aggregated = aggregated.tolist()
62
+ return {"agree": aggregated[0], "neutral": aggregated[1], "disagree": aggregated[2]}, {"agree": normal[0], "neutral": normal[1], "disagree": normal[2]}
63
+ examples = [["These are my absolute favorite cargos in my closet. I’m 5’7 and they’re actually long enough for me. I’m 165lbs and ordered an M & it fits nice and loose just how I wanted it. The adjustable waist band is awesome!", "long"], ["I feel strongly about politics in the US", "long"], ["The pants are long", "long"], ["The pants are slightly long", "long"]]
 
64
 
65
  gradio_app = gr.Interface(
66
  predict,
 
68
  inputs=[gr.Text(label="Statement"), gr.Text(label="Class"), gr.Checkbox(label="Allow multiple true classes")],
69
  outputs=[gr.Label(num_top_classes=3, label="With Postprocessing"), gr.Label(num_top_classes=3, label="Without Postprocessing")],
70
  title="Intent Analysis",
71
+ description="This model predicts whether or not the **_class_** describes the **_object described in the sentence_**. <br /> The two outputs shows what TE would predict with and without the postprocessing. An example edge case for normal TE is shown below. <br /> **_It is recommended that you clone the repository to speed up processing time_**. <br /> Additionally, note the difference between the strength of the probability when going between the last two examples, the former representing a strong opinion and the latter a weaker opinion",
72
  cache_examples=True
73
  )
74
 
75
+ gradio_app.launch()
gradio_cached_examples/18/log.csv CHANGED
@@ -1,2 +1,5 @@
1
  With Postprocessing,Without Postprocessing,flag,username,timestamp
2
- "{""label"":""agree"",""confidences"":[{""label"":""agree"",""confidence"":0.37631523609161377},{""label"":""neutral"",""confidence"":0.3404143750667572},{""label"":""disagree"",""confidence"":0.28327038884162903}]}","{""label"":""neutral"",""confidences"":[{""label"":""neutral"",""confidence"":0.8370960354804993},{""label"":""disagree"",""confidence"":0.12820996344089508},{""label"":""agree"",""confidence"":0.03469394892454147}]}",,,2024-03-10 20:51:53.608441
 
 
 
 
1
  With Postprocessing,Without Postprocessing,flag,username,timestamp
2
+ "{""label"":""neutral"",""confidences"":[{""label"":""neutral"",""confidence"":0.3989219665527344},{""label"":""agree"",""confidence"":0.3555052578449249},{""label"":""disagree"",""confidence"":0.24557280540466309}]}","{""label"":""agree"",""confidences"":[{""label"":""agree"",""confidence"":0.9896149635314941},{""label"":""neutral"",""confidence"":0.008607939817011356},{""label"":""disagree"",""confidence"":0.0017770910635590553}]}",,,2024-03-14 17:33:32.082554
3
+ "{""label"":""neutral"",""confidences"":[{""label"":""neutral"",""confidence"":0.9888092875480652},{""label"":""agree"",""confidence"":0.0059028444811701775},{""label"":""disagree"",""confidence"":0.005287905689328909}]}","{""label"":""neutral"",""confidences"":[{""label"":""neutral"",""confidence"":0.9971976280212402},{""label"":""agree"",""confidence"":0.0019434979185461998},{""label"":""disagree"",""confidence"":0.0008588095079176128}]}",,,2024-03-14 17:33:43.894521
4
+ "{""label"":""agree"",""confidences"":[{""label"":""agree"",""confidence"":0.999862790107727},{""label"":""disagree"",""confidence"":0.00007605748396599665},{""label"":""neutral"",""confidence"":0.00006114941061241552}]}","{""label"":""agree"",""confidences"":[{""label"":""agree"",""confidence"":0.9909017086029053},{""label"":""neutral"",""confidence"":0.008000608533620834},{""label"":""disagree"",""confidence"":0.0010977860074490309}]}",,,2024-03-14 17:33:56.295829
5
+ "{""label"":""agree"",""confidences"":[{""label"":""agree"",""confidence"":0.5945301651954651},{""label"":""neutral"",""confidence"":0.26899591088294983},{""label"":""disagree"",""confidence"":0.1364738941192627}]}","{""label"":""agree"",""confidences"":[{""label"":""agree"",""confidence"":0.8074565529823303},{""label"":""neutral"",""confidence"":0.1722831279039383},{""label"":""disagree"",""confidence"":0.02026035077869892}]}",,,2024-03-14 17:34:11.315778