Aakash Bakhle commited on
Commit
0decf73
1 Parent(s): aa315c8

initial commit

Browse files
Files changed (2) hide show
  1. app.py +52 -0
  2. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import PegasusForConditionalGeneration, PegasusTokenizer
3
+
4
+ st.title("Paraphrase sentences")
5
+
6
+ model_name = "tuner007/pegasus_paraphrase"
7
+ torch_device = "cpu"
8
+ tokenizer = PegasusTokenizer.from_pretrained(model_name)
9
+
10
+
11
+ @st.cache(allow_output_mutation=True)
12
+ def load_model():
13
+ model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)
14
+ return model
15
+
16
+
17
+ def get_response(
18
+ input_text, num_return_sequences, num_beams, max_length=60, temperature=1.5
19
+ ):
20
+ model = load_model()
21
+ batch = tokenizer(
22
+ [input_text],
23
+ truncation=True,
24
+ padding="longest",
25
+ max_length=max_length,
26
+ return_tensors="pt",
27
+ ).to(torch_device)
28
+ translated = model.generate(
29
+ **batch,
30
+ max_length=max_length,
31
+ num_beams=num_beams,
32
+ num_return_sequences=num_return_sequences,
33
+ temperature=temperature
34
+ )
35
+ tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
36
+ return tgt_text
37
+
38
+
39
+ num_beams = 10
40
+ num_return_sequences = st.slider("Number of paraphrases", 1, 10, 5, 1)
41
+ context = st.text_area(label="Enter a sentence to paraphrase", max_chars=384)
42
+
43
+ with st.expander("Advanced"):
44
+ temperature = st.slider("Temperature", 0.1, 5.0, 1.5, 0.1)
45
+ max_length = st.slider("Max length", 10, 100, 60, 10)
46
+ if context:
47
+ response = get_response(
48
+ context, num_return_sequences, num_beams, max_length, temperature
49
+ )
50
+
51
+ for paraphrase in response:
52
+ st.write(paraphrase)
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ transformers==4.12.5