File size: 1,948 Bytes
633b145
4b19bc2
 
 
633b145
4b19bc2
 
 
 
633b145
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e96495d
 
 
 
 
 
 
 
 
 
54541ae
633b145
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 get_response(input_text, num_return_sequences):
    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)
    tgt_text = tokenizer.batch_decode(translated, skip_special_tokens=True)
    return tgt_text


def get_response_from_text(
        context="I am a student at the University of Washington. I am taking a course called Data Science."):
    splitter = SentenceSplitter(language='en')
    sentence_list = splitter.split(context)

    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


def greet(context):
    paragraphs = context.split("\n")
    paraphrased_text = []
    for paragraph in paragraphs:
        try:
            paraphrased_paragraph = get_response_from_text(paragraph)
            paraphrased_text.append(paraphrased_paragraph)
        except:
            pass

    response = ' '.join(paraphrased_text)
    return str(response)


iface = gr.Interface(fn=greet, inputs="text", outputs="text", title="Developed by Farhan Siddiqui")
iface.launch()