abdulmatinomotoso commited on
Commit
97a6a00
1 Parent(s): bcc7a2e

Create app.py

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