Spaces:
Runtime error
Runtime error
#importing the necessary library | |
import re | |
import nltk | |
import torch | |
from nltk.tokenize import sent_tokenize | |
nltk.download('punkt') | |
import gradio as gr | |
from gradio.mix import Parallel | |
from transformers import pipeline | |
import numpy as np | |
# Defining a function to read in the text file | |
def read_in_text(url): | |
with open(URL, "r") as file: | |
article = file.read() | |
return article | |
#initailizing the model pipeline | |
from transformers import BartTokenizer, BartForConditionalGeneration | |
model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn") | |
tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-cnn") | |
#Defining a function to get the summary of the article | |
def final_summary(file): | |
#reading in the text and tokenizing it into sentence | |
text = read_in_text(file.name) | |
chunks = sent_tokenize(text) | |
output = [] | |
#looping through the sentences in a batch of 10 and summarizing them | |
for i in range(0,len(chunks), 10): | |
sentence = ' '.join(chunks[i:i+10]) | |
inputs = tokenizer(sentence, max_length=1024, return_tensors="pt") | |
summary_ids = model.generate(inputs["input_ids"]) | |
summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] | |
output.append(summary) | |
#joining all the summary output together | |
summary = " ".join(output) | |
lines1 = sent_tokenize(summary) | |
for i in range(len(lines1)): | |
lines1[i] = "* " + lines1[i].strip().replace(" .", ".") | |
summ_bullet1 = "\n".join(lines1) | |
return summ_bullet1 | |
#creating an interface for the headline generator using gradio | |
demo = gr.Interface(final_summary, inputs=[gr.inputs.File(label="Drop your .txt file here", optional=False)], | |
title = "ARTICLE SUMMARIZER", | |
outputs=[gr.outputs.Textbox(label="Summary")], | |
theme= "darkhuggingface") | |
#launching the app | |
if __name__ == "__main__": | |
demo.launch(debug=True) |