Spaces:
Runtime error
Runtime error
imseldrith
commited on
Commit
·
7066d5f
1
Parent(s):
e20e636
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
|
4 |
+
from sentence_splitter import SentenceSplitter, split_text_into_sentences
|
5 |
+
|
6 |
+
model_name = 'tuner007/pegasus_paraphrase'
|
7 |
+
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
8 |
+
tokenizer = PegasusTokenizer.from_pretrained(model_name)
|
9 |
+
model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)
|
10 |
+
|
11 |
+
|
12 |
+
def get_response(input_text, num_return_sequences):
|
13 |
+
batch = tokenizer.prepare_seq2seq_batch([input_text], truncation=True, padding='longest', max_length=10000,
|
14 |
+
return_tensors="pt").to(torch_device)
|
15 |
+
translated = model.generate(**batch, num_beams=10, num_return_sequences=num_return_sequences,
|
16 |
+
temperature=1.5)
|
17 |
+
tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
|
18 |
+
return tgt_text
|
19 |
+
|
20 |
+
|
21 |
+
def get_response_from_text(
|
22 |
+
context="I am a student at the University of Washington. I am taking a course called Data Science."):
|
23 |
+
splitter = SentenceSplitter(language='en')
|
24 |
+
sentence_list = splitter.split(context)
|
25 |
+
|
26 |
+
paraphrase = []
|
27 |
+
|
28 |
+
for i in sentence_list:
|
29 |
+
a = get_response(i, 1)
|
30 |
+
paraphrase.append(a)
|
31 |
+
paraphrase2 = [' '.join(x) for x in paraphrase]
|
32 |
+
paraphrase3 = [' '.join(x for x in paraphrase2)]
|
33 |
+
paraphrased_text = str(paraphrase3).strip('[]').strip("'")
|
34 |
+
return paraphrased_text
|
35 |
+
|
36 |
+
|
37 |
+
def greet(context):
|
38 |
+
return get_response_from_text(context)
|
39 |
+
|
40 |
+
|
41 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
42 |
+
iface.launch()
|