nikhedward's picture
Update app.py
8f4f65a verified
import gradio as gr
import transformers
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
title = "Text Summarizer πŸ“"
text_1 = """
Miss Brill is the story of an old woman told brilliantly and realistically, balancing thoughts and emotions that sustain her late solitary life amidst all the bustle of modern life. Miss Brill is a regular visitor on Sundays to the Jardins Publiques (the Public Gardens) of a small French suburb where she sits and watches all sorts of people come and go. She listens to the band playing, loves to watch people and guess what keeps them going, and enjoys contemplating the world as a great stage upon which actors perform. She finds herself to be another actor among the so many she sees, or at least herself as 'part of the performance after all.' One Sunday Miss Brill puts on her fur and goes to the Public Gardens as usual. The evening ends with her sudden realization that she is old and lonely, a realization brought to her by a conversation she overhears between a boy and a girl, presumably lovers, who comment on her unwelcome presence in their vicinity. Miss Brill is sad and depressed as she returns home, not stopping by as usual to buy her Sunday delicacy, a slice of honey-cake. She retires to her dark room, puts the fur back into the box and imagines that she has heard something cry.
"""
text_2 = """
Senior British royals, including Prince William and his wife, Duchess Kate, went to church on Easter Sunday without the queen. Queen Elizabeth II, who has been experiencing mobility problems, did not attend the service at St. George's Chapel on the grounds of Windsor Castle, a fixture in the royals' calendar. William and Kate, known as the Duke and Duchess of Cambridge, were accompanied by two of their three children: Prince George, 8, and Princess Charlotte, 6. Also in attendance were the queen's youngest son, Prince Edward, with his wife Sophie and their children, and Princess Eugenie, the daughter of Prince Andrew. Last week, she had a visit from her grandson Prince Harry and his wife Meghan, a spokesperson for the couple confirmed to USA TODAY – the first time the couple has visited the U.K. together since they stepped down as working royals in 2020 and moved to California.
"""
text_3 = """
In the article β€œBats,” by Debbie Dean, we learn that in contrast to some mistaken beliefs, bats have sight, are mammals, and are not especially likely to carry rabies. Bats are relatively misunderstood and unappreciated. Bats have some interesting physical features. They have similar bone structure and skeletons to that of humans, so they are not winged rodents. They are color blind, so they use echolocation if there is not sufficient light. Otherwise, their sight is enough. Species of bats total about a thousand. The species come in a variety of sizes and have unique diets. Most eat insects, but some eat plant products and small animals. However, vampire bats drink blood, which can be harmful to livestock. Farmers have accidentally killed many helpful bats while trying to rid themselves of vampire bats. Bats can actually be helpful to humans. They destroy unwanted bugs, spread fruit seeds, and pollinate plants. However, the survival of bats is not known because many are killed by human disruptions and predators. The bat population has dropped steadily and may continue to drop. Hopefully, we will realize that although bats look different than our favorite animals, we
can learn to accept and admire their uniqueness
"""
sample_texts = [[text_1], [text_2], [text_3]]
desc = """
<p style='text-align: center; color: #FF7F50'>This is an abstractive text summarizer app using fine-tuned bart-large-cnn model. The abstractive approach involves rephrasing the complete document while capturing the complete meaning of the document. This type of summarization provides more human-like summary.
<p style='text-align: center; color: #FF7F50'> Note: For faster inference input smaller texts. Current model supports context size of 1024 words, and anything longer is truncated</'p>
<p style='text-align: center; color: #FF7F50'>Sample text inputs are provided at the bottom!</p>
"""
model_name = "nikhedward/bart-large-cnn-finetuned-multi-news"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSeq2SeqLM.from_pretrained(model_name)
def auto_summarize(inp):
inp = inp.replace('\n','')
inp = tokenizer.encode(inp, return_tensors='pt', max_length=1024, truncation=True)
summary_ids = model.generate(inp, num_beams=4, max_length=150, early_stopping=True, do_sample=True, top_k=50, top_p=0.95)
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
return summary
interface = gr.Interface(fn=auto_summarize, inputs=gr.inputs.Textbox(lines=10, label="Input Text"), description = desc, theme = "dark-peach",
examples = sample_texts, title = title, outputs="text", css=".footer{display:none !important}")
interface.launch()