Paraphrasing / app.py
datasciencedojo's picture
Update app.py
23f7624
raw
history blame
2.92 kB
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."], ["Niagara Falls is viewed by thousands of tourists every year."]]
css = """
footer {display:none !important}
.output-markdown{display:none !important}
.gr-button-primary {
z-index: 14;
height: 43px;
width: 130px;
left: 0px;
top: 0px;
padding: 0px;
cursor: pointer !important;
background: none rgb(17, 20, 45) !important;
border: none !important;
text-align: center !important;
font-family: Poppins !important;
font-size: 14px !important;
font-weight: 500 !important;
color: rgb(255, 255, 255) !important;
line-height: 1 !important;
border-radius: 12px !important;
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
box-shadow: none !important;
}
.gr-button-primary:hover{
z-index: 14;
height: 43px;
width: 130px;
left: 0px;
top: 0px;
padding: 0px;
cursor: pointer !important;
background: none rgb(37, 56, 133) !important;
border: none !important;
text-align: center !important;
font-family: Poppins !important;
font-size: 14px !important;
font-weight: 500 !important;
color: rgb(255, 255, 255) !important;
line-height: 1 !important;
border-radius: 12px !important;
transition: box-shadow 200ms ease 0s, background 200ms ease 0s !important;
box-shadow: rgb(0 0 0 / 23%) 0px 1px 7px 0px !important;
}
.hover\:bg-orange-50:hover {
--tw-bg-opacity: 1 !important;
background-color: rgb(229,225,255) !important;
}
"""
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")], title="Paraphrasing | Datascience Dojo", examples=examples, css=css)
demo.launch( debug = True )