Deep1994 commited on
Commit
cd16b80
·
1 Parent(s): ca02a1d

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -0
app.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import T5ForConditionalGeneration, T5Tokenizer
3
+ import torch
4
+
5
+ def set_seed(seed):
6
+ torch.manual_seed(seed)
7
+ if torch.cuda.is_available():
8
+ torch.cuda.manual_seed_all(seed)
9
+
10
+ tokenizer = T5Tokenizer.from_pretrained('Deep1994/t5-paraphrase-quora')
11
+
12
+ @st.cache(allow_output_mutation=True)
13
+ def load_model():
14
+ model = T5ForConditionalGeneration.from_pretrained('Deep1994/t5-paraphrase-quora')
15
+
16
+ return model
17
+
18
+ model = load_model()
19
+
20
+ st.sidebar.subheader('Select decoding strategy below.')
21
+ decoding_strategy = st.sidebar.selectbox("decoding_strategy", ['Top k/p sampling', 'Beam Search'])
22
+
23
+ st.title('Paraphrase a question in English.')
24
+
25
+ st.write('This is a fine-tuned t5 model that will paraphrase\
26
+ your English input text into another English output\
27
+ by leveraging a pre-trained [Text-To-Text Transfer Tranformers](https://arxiv.org/abs/1910.10683) model.')
28
+
29
+
30
+ st.subheader('Input Text')
31
+ text = st.text_area(' ', height=100)
32
+
33
+ if text != '':
34
+ set_seed(1234) # for reproducibility
35
+ prefix = 'paraphrase: '
36
+ encoding = tokenizer.encode_plus(prefix + text, padding=True, return_tensors="pt")
37
+ input_ids, attention_masks = encoding["input_ids"], encoding["attention_mask"]
38
+
39
+ if str(decoding_strategy) == 'Top k/p sampling':
40
+ beam_outputs = model.generate(
41
+ input_ids=input_ids, attention_mask=attention_masks,
42
+ do_sample=True,
43
+ max_length=20,
44
+ top_k=50,
45
+ top_p=0.95,
46
+ early_stopping=True,
47
+ num_return_sequences=5
48
+ )
49
+ elif str(decoding_strategy) == 'Beam Search':
50
+ beam_outputs = model.generate(
51
+ input_ids=input_ids,
52
+ attention_mask=attention_masks,
53
+ max_length=20,
54
+ num_beams=5,
55
+ no_repeat_ngram_size=2,
56
+ num_return_sequences=5,
57
+ early_stopping=True
58
+ )
59
+
60
+ final_outputs =[]
61
+ for beam_output in beam_outputs:
62
+ sent = tokenizer.decode(beam_output, skip_special_tokens=True, clean_up_tokenization_spaces=True)
63
+ # if sent.lower() != text.lower() and sent not in final_outputs:
64
+ # final_outputs.append(sent)
65
+ final_outputs.append(sent)
66
+
67
+ st.subheader('Paraphrased Text')
68
+ for i, final_output in enumerate(final_outputs):
69
+ st.write(final_output + '\n')