File size: 1,456 Bytes
c1146a2
 
8c9a81a
 
c1146a2
8c9a81a
 
 
 
c1146a2
8c9a81a
 
 
 
 
 
 
c1146a2
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import gradio as gr
import torch
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
from sentence_splitter import SentenceSplitter, split_text_into_sentences

model_name = 'tuner007/pegasus_paraphrase'
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
tokenizer = PegasusTokenizer.from_pretrained(model_name)
model = PegasusForConditionalGeneration.from_pretrained(model_name).to(torch_device)

def paraphrase_text(input_text, num_return_sequences = 3):
    batch = tokenizer.prepare_seq2seq_batch([input_text], truncation=True, padding='longest', max_length=60,
                                            return_tensors="pt").to(torch_device)
    translated = model.generate(**batch, max_length=60, num_beams=10, num_return_sequences=num_return_sequences,
                                temperature=1.5)
    paraphrased_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
    return paraphrased_text[0], paraphrased_text[1], paraphrased_text[2]

examples = [["Uploading a video to YouTube can help exposure for your business.", "45"], ["Niagara Falls is viewed by thousands of tourists every year.", "30"]]

demo = gr.Interface(fn=paraphrase_text, inputs=gr.Textbox(lines=3, placeholder="Enter sample text here", label="Original text"), outputs=[gr.Textbox(label="Paraphrasing 1"), gr.Textbox(label="Paraphrasing 2"), gr.Textbox(label="Paraphrasing 3")], examples=examples)
demo.launch( debug = True )