TheCleverIdiot's picture
Update app.py
eb6e19b verified
from __future__ import absolute_import, division, print_function, unicode_literals
import spacy
import gradio as gr
import os
from fastai.text.all import *
from transformers import *
# from blurr.data.all import *
# from blurr.modeling.all import *
from spacy_readability import Readability
# from save_data import save_data_and_sendmail
readablility_nlp = spacy.load('en_core_web_sm')
read = Readability()
cwd = os.getcwd()
readablility_nlp.add_pipe(read, last=True)
bart_ext_model_path = os.path.join(cwd, 'bart_extractive_model')
bart_extractive_model = BartForConditionalGeneration.from_pretrained(bart_ext_model_path)
bart_extractive_tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
t5_model_path = os.path.join(cwd, 't5_model')
t5_model = AutoModelWithLMHead.from_pretrained(t5_model_path)
t5_tokenizer = AutoTokenizer.from_pretrained("mrm8488/t5-base-finetuned-summarize-news")
def generate_text_summarization(sum_type,article):
if article.strip():
print("text input :",article)
if sum_type == 'BART Extractive Text Summarization':
inputs = bart_extractive_tokenizer([article], max_length=1024, return_tensors='pt')
summary_ids = bart_extractive_model.generate(inputs['input_ids'], num_beams=4, min_length=60, max_length=300, early_stopping=True)
summary = [bart_extractive_tokenizer.decode(g, skip_special_tokens=True, clean_up_tokenization_spaces=False) for g in summary_ids]
print(type(summary))
print(summary)
summary= summary[0]
doc = readablility_nlp(summary)
summary_score = round(doc._.flesch_kincaid_reading_ease,2)
summarized_data = {
"summary" : summary,
"score" : summary_score
}
if sum_type == 'T5 Abstractive Text Summarization':
inputs = t5_tokenizer.encode(article, return_tensors="pt", max_length=2048)
summary_ids = t5_model.generate(inputs,
num_beams=2,
no_repeat_ngram_size=2,
min_length=100,
max_length=300,
early_stopping=True)
summary = t5_tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print(type(summary))
print(summary)
doc = readablility_nlp(summary)
summary_score = round(doc._.flesch_kincaid_reading_ease,2)
summarized_data = {
"summary" : summary,
"score" : summary_score
}
# save_data_and_sendmail(article, sum_type, summary)
return summary
else:
raise gr.Error("Please enter text in inputbox!!!!")
input_text=gr.Textbox(lines=5, label="Paragraph")
input_radio= gr.Radio(['BART Extractive Text Summarization','T5 Abstractive Text Summarization'],label='Select summarization',value='BART Extractive Text Summarization')
output_text=gr.Textbox(lines=7, label="Summarize text")
demo = gr.Interface(
generate_text_summarization,
[input_radio,input_text],
output_text,
title="Text Summarization",
css=".gradio-container {background-color: lightgray}",
article="""<p style='text-align: center;'>Developed by: <a href="https://aritraghosh.xyz" target="_blank">Aritra Ghosh and Subhojit Ghosh IP 6th Sem Innovative Project</a></p>"""
)
demo.launch()