tensorkelechi commited on
Commit
b2b8748
·
verified ·
1 Parent(s): 4c3560c

Update app.py #0

Browse files
Files changed (1) hide show
  1. app.py +43 -22
app.py CHANGED
@@ -1,28 +1,32 @@
1
- import streamlit as str
 
2
  from PyPDF2 import PdfReader
3
 
4
  from transformers import pipeline
5
 
6
  summarizer = pipeline(task="summarization")
7
 
8
- str.set_page_config(
 
9
  page_title='Text Summarizer'
10
  )
11
 
12
- str.title('Text Summarization')
 
 
13
 
14
  def summarize_text(text):
15
  summary = summarizer(text)
16
  summary = summary[0]['summary_text']
17
  return summary
18
 
19
- input = str.text_area('Enter long text')
20
 
21
  output = summarize_text(input)
22
 
23
 
24
- if str.button('Summarize text'):
25
- str.markdown(f'''
26
  <div style="background-color: black; color: white; font-weight: bold; padding: 1rem; border-radius: 10px;">
27
  <h4>Results</h4>
28
  <p>
@@ -30,46 +34,63 @@ if str.button('Summarize text'):
30
  </p>
31
  </div>
32
  ''', unsafe_allow_html=True)
33
- str.success('Done')
34
 
35
 
36
  #####
37
 
38
  # PDF summary section
39
- str.subheader('PDF summary')
 
40
 
41
  try:
42
- uploaded_pdf = str.file_uploader('Choose a pdf file', type=['pdf'])
 
43
 
44
  if uploaded_pdf is not None:
45
- str.success('Succesfully uploaded')
46
 
 
47
  def extract_text(pdf_file):
48
  pdf_content = PdfReader(pdf_file)
49
- pages = pdf_content.pages
50
  # page_count = len(pages)
51
- page_text = pages[17].extract_text()
52
-
53
- return page_text
54
 
 
 
 
 
 
 
 
 
55
 
56
- pdf_input = extract_text(uploaded_pdf)
 
 
57
 
58
- pdf_output = summarize_text(pdf_input)
59
 
60
- except:
61
- str.error('Please select a valid file')
62
 
 
 
 
 
63
 
64
 
65
 
66
- if str.button('Summarize pdf page'):
67
- str.markdown(f'''
68
  <div style="background-color: black; color: white; font-weight: bold; padding: 1rem; border-radius: 10px;">
69
- <h4>Results</h4>
70
  <p>
71
  {pdf_output}
72
  </p>
73
  </div>
74
  ''', unsafe_allow_html=True)
75
- str.success('PDF page summarized :)', icon="✅")
 
 
 
1
+ import streamlit as st
2
+ import pdfkit
3
  from PyPDF2 import PdfReader
4
 
5
  from transformers import pipeline
6
 
7
  summarizer = pipeline(task="summarization")
8
 
9
+ # Basic text summary
10
+ st.set_page_config(
11
  page_title='Text Summarizer'
12
  )
13
 
14
+ st.title('Text Summarization')
15
+
16
+ # Text summary function
17
 
18
  def summarize_text(text):
19
  summary = summarizer(text)
20
  summary = summary[0]['summary_text']
21
  return summary
22
 
23
+ input = st.text_area('Enter long text')
24
 
25
  output = summarize_text(input)
26
 
27
 
28
+ if st.button('Summarize text'):
29
+ st.markdown(f'''
30
  <div style="background-color: black; color: white; font-weight: bold; padding: 1rem; border-radius: 10px;">
31
  <h4>Results</h4>
32
  <p>
 
34
  </p>
35
  </div>
36
  ''', unsafe_allow_html=True)
37
+ st.success('Done')
38
 
39
 
40
  #####
41
 
42
  # PDF summary section
43
+
44
+ st.subheader('PDF summary')
45
 
46
  try:
47
+ # Upload file
48
+ uploaded_pdf = st.file_uploader('Choose a pdf file', type=['pdf'])
49
 
50
  if uploaded_pdf is not None:
51
+ st.success('Succesfully uploaded')
52
 
53
+ # Extract PDF content
54
  def extract_text(pdf_file):
55
  pdf_content = PdfReader(pdf_file)
56
+ pages =pdf_content.pages
57
  # page_count = len(pages)
58
+
59
+ page_text_stack = []
 
60
 
61
+ for page in pages:
62
+ page_text = page.extract_text()
63
+ page_text_stack.append(page_text)
64
+
65
+ pages_stack = []
66
+
67
+ for text_stack in page_text_stack:
68
+ pages_stack.append(text_stack)
69
 
70
+ return page_text
71
+
72
+
73
 
 
74
 
75
+ except: # Handle blank file error
76
+ st.error('Please select a valid file')
77
 
78
+ # Prepare output
79
+ pdf_input = extract_text(uploaded_pdf)
80
+ pdf_output = summarize_text(pdf_input)
81
+ summary_pdf = pdfkit.from_sting(pdf_input, 'Summary.pdf')
82
 
83
 
84
 
85
+ if st.button('Summarize pdf page'):
86
+ st.markdown(f'''
87
  <div style="background-color: black; color: white; font-weight: bold; padding: 1rem; border-radius: 10px;">
88
+ <h4>Download the summary here </h4>
89
  <p>
90
  {pdf_output}
91
  </p>
92
  </div>
93
  ''', unsafe_allow_html=True)
94
+ st.write('Download summary pdf here')
95
+ download_button = st.download_button(summary_pdf, label='Download summary')
96
+ st.success('PDF page summarized :)', icon="✅")