File size: 1,542 Bytes
91407c5 f27083d 91407c5 b6d1788 91407c5 f27083d 91407c5 81ca650 91407c5 6a6e451 91407c5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import torch
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
model_name = 'prithivida/parrot_paraphraser_on_T5'
torch_device = 'cuda' if torch.cuda.is_available() else 'cpu'
tokenizer = AutoTokenizer.from_pretrained("prithivida/parrot_paraphraser_on_T5")
model = AutoModelForSeq2SeqLM.from_pretrained("prithivida/parrot_paraphraser_on_T5")
def get_response(input_text,num_return_sequences):
batch = tokenizer.prepare_seq2seq_batch([input_text],truncation=True,padding='longest',max_length=100, return_tensors="pt").to(torch_device)
translated = model.generate(**batch,max_length=100,num_beams=10, num_return_sequences=num_return_sequences, temperature=0.9)
tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
return tgt_text
from sentence_splitter import SentenceSplitter, split_text_into_sentences
splitter = SentenceSplitter(language='en')
def paraphraze(text):
sentence_list = splitter.split(text)
paraphrase = []
for i in sentence_list:
a = get_response(i,1)
paraphrase.append(a)
paraphrase2 = [' '.join(x) for x in paraphrase]
paraphrase3 = [' '.join(x for x in paraphrase2) ]
paraphrased_text = str(paraphrase3).strip('[]').strip("'")
return paraphrased_text
import gradio as gr
def summarize(text):
paraphrased_text = paraphraze(text)
return paraphrased_text
gr.Interface(fn=summarize, inputs=gr.inputs.Textbox(lines=7, placeholder="Enter text here"), outputs=[gr.outputs.Textbox(label="Paraphrased Text")],examples=[["My Geckhos."
]]).launch(inline=False) |