paragon-analytics commited on
Commit
ea558ed
1 Parent(s): 868eab7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -8
app.py CHANGED
@@ -22,13 +22,14 @@ import spacy_streamlit
22
  nlp = spacy.load('en_core_web_sm')
23
  import torch
24
  import tensorflow as tf
25
- from transformers import RobertaTokenizer, RobertaModel
26
- from transformers import AutoModelForSequenceClassification
27
- from transformers import TFAutoModelForSequenceClassification
28
- from transformers import AutoTokenizer
29
  tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/bert_resil")
30
  model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/bert_resil")
31
 
 
 
 
32
  kw_extractor = yake.KeywordExtractor()
33
  custom_kw_extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.2, top=10, features=None)
34
 
@@ -111,13 +112,31 @@ def process_final_text(text):
111
  score.append(a)
112
 
113
  word_attributions = [(letter[i], score[i]) for i in range(0, len(letter))]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
 
115
- return {"Resilience": float(scores.numpy()[1]), "Non-Resilience": float(scores.numpy()[0])},keywords,NER,word_attributions
116
 
117
  def main(prob1):
118
  text = str(prob1)
119
  obj = process_final_text(text)
120
- return obj[0],obj[1],obj[2],obj[3]
121
 
122
  title = "Welcome to **ResText** 🪐"
123
  description1 = """
@@ -143,14 +162,15 @@ with gr.Blocks(title=title) as demo:
143
  combine_adjacent=False).style(color_map={"++": "darkgreen","+": "green",
144
  "--": "darkred",
145
  "-": "red", "NA":"white"})
 
146
 
147
  submit_btn.click(
148
  main,
149
  [prob1],
150
- [label,impplot,NER,intp], api_name="ResText"
151
  )
152
 
153
  gr.Markdown("### Click on any of the examples below to see to what extent they contain resilience messaging:")
154
- gr.Examples([["Please stay at home and avoid unnecessary trips."],["Please stay at home and avoid unnecessary trips. We will survive this."],["We will survive this."],["Watch today’s news briefing with the latest updates on COVID-19 in Connecticut."],["So let's keep doing what we know works. Let's stay strong, and let's beat this virus. I know we can, and I know we can come out stronger on the other side."],["It is really wonderful how much resilience there is in human nature. Let any obstructing cause, no matter what, be removed in any way, even by death, and we fly back to first principles of hope and enjoyment."],["Resilience is accepting your new reality, even if it’s less good than the one you had before. You can fight it, you can do nothing but scream about what you’ve lost, or you can accept that and try to put together something that’s good."],["You survived all of the days you thought you couldn't, never underestimate your resilience."],["Like tiny seeds with potent power to push through tough ground and become mighty trees, we hold innate reserves of unimaginable strength. We are resilient."]], [prob1], [label,impplot,NER,intp], main, cache_examples=True)
155
 
156
  demo.launch()
 
22
  nlp = spacy.load('en_core_web_sm')
23
  import torch
24
  import tensorflow as tf
25
+ from transformers import RobertaTokenizer, RobertaModel, AutoModelForSequenceClassification, TFAutoModelForSequenceClassification
26
+ from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
 
 
27
  tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/bert_resil")
28
  model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/bert_resil")
29
 
30
+ para_tokenizer = AutoTokenizer.from_pretrained("paragon-analytics/t5_para")
31
+ para_model = AutoModelForSequenceClassification.from_pretrained("paragon-analytics/t5_para")
32
+
33
  kw_extractor = yake.KeywordExtractor()
34
  custom_kw_extractor = yake.KeywordExtractor(lan="en", n=2, dedupLim=0.2, top=10, features=None)
35
 
 
112
  score.append(a)
113
 
114
  word_attributions = [(letter[i], score[i]) for i in range(0, len(letter))]
115
+
116
+ # Paraphraser:
117
+ inp_text = "paraphrase: " + X_test + " </s>"
118
+
119
+ encoding = para_tokenizer.encode_plus(inp_text,pad_to_max_length=True, return_tensors="pt")
120
+ input_ids, attention_masks = encoding["input_ids"].to("cuda"), encoding["attention_mask"].to("cuda")
121
+
122
+ outputs = para_model.generate(
123
+ input_ids=input_ids, attention_mask=attention_masks,
124
+ max_length=256,
125
+ do_sample=True,
126
+ top_k=120,
127
+ top_p=0.95,
128
+ early_stopping=True,
129
+ num_return_sequences=5
130
+ )
131
+
132
+ para_list = [tokenizer.decode(output, skip_special_tokens=True,clean_up_tokenization_spaces=True) for output in outputs]
133
 
134
+ return {"Resilience": float(scores.numpy()[1]), "Non-Resilience": float(scores.numpy()[0])},keywords,NER,word_attributions,para_list
135
 
136
  def main(prob1):
137
  text = str(prob1)
138
  obj = process_final_text(text)
139
+ return obj[0],obj[1],obj[2],obj[3],obj[4]
140
 
141
  title = "Welcome to **ResText** 🪐"
142
  description1 = """
 
162
  combine_adjacent=False).style(color_map={"++": "darkgreen","+": "green",
163
  "--": "darkred",
164
  "-": "red", "NA":"white"})
165
+ paraph = gr.Textbox(label = "Paraphrased Sentences:")
166
 
167
  submit_btn.click(
168
  main,
169
  [prob1],
170
+ [label,impplot,NER,intp,paraph], api_name="ResText"
171
  )
172
 
173
  gr.Markdown("### Click on any of the examples below to see to what extent they contain resilience messaging:")
174
+ gr.Examples([["Please stay at home and avoid unnecessary trips."],["Please stay at home and avoid unnecessary trips. We will survive this."],["We will survive this."],["Watch today’s news briefing with the latest updates on COVID-19 in Connecticut."],["So let's keep doing what we know works. Let's stay strong, and let's beat this virus. I know we can, and I know we can come out stronger on the other side."],["It is really wonderful how much resilience there is in human nature. Let any obstructing cause, no matter what, be removed in any way, even by death, and we fly back to first principles of hope and enjoyment."],["Resilience is accepting your new reality, even if it’s less good than the one you had before. You can fight it, you can do nothing but scream about what you’ve lost, or you can accept that and try to put together something that’s good."],["You survived all of the days you thought you couldn't, never underestimate your resilience."],["Like tiny seeds with potent power to push through tough ground and become mighty trees, we hold innate reserves of unimaginable strength. We are resilient."]], [prob1], [label,impplot,NER,intp,paraph], main, cache_examples=True)
175
 
176
  demo.launch()