Spaces:
Runtime error
Runtime error
Commit ·
f4e5c3c
1
Parent(s): f7a0f35
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#importing the necessary library
|
| 2 |
+
import re
|
| 3 |
+
import nltk
|
| 4 |
+
import torch
|
| 5 |
+
from nltk.tokenize import sent_tokenize
|
| 6 |
+
nltk.download('punkt')
|
| 7 |
+
from IPython.display import HTML, display
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from gradio.mix import Parallel
|
| 10 |
+
from transformers import pipeline
|
| 11 |
+
import numpy as np
|
| 12 |
+
|
| 13 |
+
# Defining a function to read in the text file
|
| 14 |
+
def read_in_text(url):
|
| 15 |
+
with open(url, 'r') as file:
|
| 16 |
+
article = file.read()
|
| 17 |
+
return article
|
| 18 |
+
|
| 19 |
+
#initailizing the model pipeline
|
| 20 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 21 |
+
|
| 22 |
+
model = BartForConditionalGeneration.from_pretrained("facebook/bart-large-cnn")
|
| 23 |
+
tokenizer = BartTokenizer.from_pretrained("facebook/bart-large-cnn")
|
| 24 |
+
|
| 25 |
+
#Defining a function to get the summary of the article
|
| 26 |
+
def final_summary(file):
|
| 27 |
+
#reading in the text and tokenizing it into sentence
|
| 28 |
+
text = read_in_text(file.name)
|
| 29 |
+
chunks = sent_tokenize(text)
|
| 30 |
+
output = []
|
| 31 |
+
|
| 32 |
+
#looping through the sentences in a batch of 10 and summarizing them
|
| 33 |
+
for i in range(0,len(chunks), 20):
|
| 34 |
+
sentence = ' '.join(chunks[i:i+20])
|
| 35 |
+
inputs = tokenizer(sentence, max_length=1024, return_tensors="pt")
|
| 36 |
+
summary_ids = model.generate(inputs["input_ids"])
|
| 37 |
+
summary = tokenizer.batch_decode(summary_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
|
| 38 |
+
output.append(summary)
|
| 39 |
+
|
| 40 |
+
#joining all the summary output together
|
| 41 |
+
summary = ' '.join(output)
|
| 42 |
+
lines1 = sent_tokenize(summary)
|
| 43 |
+
for i in range(len(lines1)):
|
| 44 |
+
lines1[i] = "* " + lines1[i].strip().replace(' .', '.')
|
| 45 |
+
|
| 46 |
+
summ_bullet1 = "\n".join(lines1)
|
| 47 |
+
return summ_bullet1
|
| 48 |
+
|
| 49 |
+
#creating an interface for the headline generator using gradio
|
| 50 |
+
demo = gr.Interface(final_summary, inputs=[gr.inputs.File(label="Drop your .txt file here", optional=False)],
|
| 51 |
+
title = "ARTICLE SUMMARIZER",
|
| 52 |
+
outputs=[gr.outputs.Textbox(label="Summary")],
|
| 53 |
+
theme= "darkhuggingface")
|
| 54 |
+
|
| 55 |
+
#launching the app
|
| 56 |
+
if __name__ == "__main__":
|
| 57 |
+
demo.launch(debug=True)
|