File size: 2,106 Bytes
f4e5c3c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#importing the necessary library
import re
import nltk
import torch
from nltk.tokenize import sent_tokenize
nltk.download('punkt')
from IPython.display import HTML, display
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), 20):
    sentence = ' '.join(chunks[i:i+20])
    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)