Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import pandas as pd
|
| 2 |
+
import numpy as np
|
| 3 |
+
import re
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import nltk
|
| 6 |
+
from nltk.tokenize import sent_tokenize
|
| 7 |
+
nltk.download('punkt')
|
| 8 |
+
|
| 9 |
+
def read_in_text(url):
|
| 10 |
+
with open(url, 'r') as file:
|
| 11 |
+
article = file.read()
|
| 12 |
+
return article
|
| 13 |
+
|
| 14 |
+
def clean_text(url):
|
| 15 |
+
text = url
|
| 16 |
+
#converting the text to all lower case
|
| 17 |
+
text = text.lower()
|
| 18 |
+
|
| 19 |
+
#removing the dates, time and name of author
|
| 20 |
+
text = re.sub('(by[\s\w,|]+ - \d\d\/\d\d\/\d\d\s\d+:\d+\s\w{2}\s\w{2})|(by[\s\w|,]+\d\d,\s\d{4})', "", text)
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
| 25 |
+
from transformers import PegasusForConditionalGeneration, PegasusTokenizer
|
| 26 |
+
|
| 27 |
+
import torch
|
| 28 |
+
device = 'cuda' if torch.cuda.is_available() else 'cpu'
|
| 29 |
+
print ("device ",device)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
tokenizer_1 = AutoTokenizer.from_pretrained("jaimin/T5-Large")
|
| 34 |
+
model_1 = AutoModelForSeq2SeqLM.from_pretrained("jaimin/T5-Large")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
tokenizer_2 = PegasusTokenizer.from_pretrained('jaimin/pegasus')
|
| 40 |
+
model_2 = PegasusForConditionalGeneration.from_pretrained('jaimin/pegasus').to(device)
|
| 41 |
+
|
| 42 |
+
# Diverse Beam search
|
| 43 |
+
def my_paraphrase(sentence, model, tokenizer):
|
| 44 |
+
|
| 45 |
+
text = "paraphrase: "+sentence + " </s>"
|
| 46 |
+
encoding = tokenizer.encode_plus(text, padding=True, return_tensors="pt", truncation=True)
|
| 47 |
+
input_ids,attention_mask = encoding["input_ids"].to(device), encoding["attention_mask"].to(device)
|
| 48 |
+
|
| 49 |
+
model.eval()
|
| 50 |
+
diverse_beam_outputs = model.generate(
|
| 51 |
+
input_ids=input_ids,attention_mask=attention_mask,
|
| 52 |
+
max_length = 512,
|
| 53 |
+
early_stopping=True,
|
| 54 |
+
num_beams=5,
|
| 55 |
+
num_beam_groups = 5,
|
| 56 |
+
num_return_sequences=5,
|
| 57 |
+
diversity_penalty = 0.70
|
| 58 |
+
)
|
| 59 |
+
sent = tokenizer.decode(diverse_beam_outputs[0], skip_special_tokens=True,clean_up_tokenization_spaces=True)
|
| 60 |
+
return sent
|
| 61 |
+
|
| 62 |
+
def return_output(file, models):
|
| 63 |
+
|
| 64 |
+
docs = file
|
| 65 |
+
sentence = clean_text(docs)
|
| 66 |
+
|
| 67 |
+
if models == 'T5':
|
| 68 |
+
model = model_1
|
| 69 |
+
tokenizer = tokenizer_1
|
| 70 |
+
|
| 71 |
+
elif models == 'Pegasus':
|
| 72 |
+
model = model_2
|
| 73 |
+
tokenizer = tokenizer_2
|
| 74 |
+
|
| 75 |
+
output = " ".join([my_paraphrase(sent, model, tokenizer) for sent in sent_tokenize(sentence)])
|
| 76 |
+
new_output = output.replace('paraphrasedoutput:', "")
|
| 77 |
+
new_output = new_output.replace('.<n>', '.\n')
|
| 78 |
+
return new_output
|
| 79 |
+
|
| 80 |
+
demo = gr.Interface(return_output, inputs=[gr.inputs.Textbox(label="Text", optional=False),
|
| 81 |
+
gr.inputs.Dropdown(['Pegasus', 'T5'], type="value", default=None, label="Models", optional=False),],
|
| 82 |
+
outputs=[gr.outputs.Textbox(label="Summary")])
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
demo.launch(debug=True)
|