Rauhan commited on
Commit
9bad00c
·
1 Parent(s): c1d34e6

update: main.py

Browse files
Files changed (1) hide show
  1. main.py +26 -16
main.py CHANGED
@@ -3,9 +3,9 @@ from datetime import datetime
3
  import streamlit as st
4
  from io import BytesIO
5
  from fpdf import FPDF
6
- import time
7
- import requests
8
  import tempfile
 
 
9
 
10
  # Configure the page
11
  st.set_page_config(
@@ -49,6 +49,27 @@ def get_dejavu_font_path():
49
  tempFontFile.close()
50
  return tempFontFile.name
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  # Sidebar
53
  with st.sidebar:
54
  st.markdown("### Upload PDF")
@@ -91,7 +112,7 @@ if uploadedFile:
91
  # Generate summary
92
  statusBox.info("Generating summary...")
93
  summaryStartTime = time.time()
94
- summary = pipeline.run(pdfBytes)
95
  totalTime = time.time() - startTime
96
 
97
  if summary:
@@ -104,23 +125,12 @@ if uploadedFile:
104
  if st.button("Download Summary as PDF"):
105
  try:
106
  font_path = get_dejavu_font_path()
107
-
108
- pdf = FPDF()
109
- pdf.add_page()
110
- pdf.add_font("DejaVu", "", font_path, uni=True)
111
- pdf.set_font("DejaVu", '', 16)
112
- pdf.cell(200, 10, txt="PDF Summary", ln=True, align='C')
113
- pdf.ln(10)
114
- pdf.set_font("DejaVu", '', 12)
115
- pdf.multi_cell(0, 10, summary)
116
-
117
- pdfOutput = BytesIO()
118
- pdf.output(pdfOutput)
119
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
120
 
121
  st.download_button(
122
  label="Click to Download",
123
- data=pdfOutput.getvalue(),
124
  file_name=f"summary_{timestamp}.pdf",
125
  mime="application/pdf"
126
  )
 
3
  import streamlit as st
4
  from io import BytesIO
5
  from fpdf import FPDF
 
 
6
  import tempfile
7
+ import requests
8
+ import time
9
 
10
  # Configure the page
11
  st.set_page_config(
 
49
  tempFontFile.close()
50
  return tempFontFile.name
51
 
52
+ # Cache the summary generation
53
+ @st.cache_data(show_spinner=False)
54
+ def generate_summary(pipeline, pdf_bytes):
55
+ return pipeline.run(pdf_bytes)
56
+
57
+ # Cache the PDF generation
58
+ @st.cache_data(show_spinner=False)
59
+ def generate_pdf_bytes(summary, font_path):
60
+ pdf = FPDF()
61
+ pdf.add_page()
62
+ pdf.add_font("DejaVu", "", font_path, uni=True)
63
+ pdf.set_font("DejaVu", '', 16)
64
+ pdf.cell(200, 10, txt="PDF Summary", ln=True, align='C')
65
+ pdf.ln(10)
66
+ pdf.set_font("DejaVu", '', 12)
67
+ pdf.multi_cell(0, 10, summary)
68
+
69
+ pdf_output = BytesIO()
70
+ pdf.output(pdf_output)
71
+ return pdf_output.getvalue()
72
+
73
  # Sidebar
74
  with st.sidebar:
75
  st.markdown("### Upload PDF")
 
112
  # Generate summary
113
  statusBox.info("Generating summary...")
114
  summaryStartTime = time.time()
115
+ summary = generate_summary(pipeline, pdfBytes)
116
  totalTime = time.time() - startTime
117
 
118
  if summary:
 
125
  if st.button("Download Summary as PDF"):
126
  try:
127
  font_path = get_dejavu_font_path()
128
+ pdf_bytes = generate_pdf_bytes(summary, font_path)
 
 
 
 
 
 
 
 
 
 
 
129
  timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
130
 
131
  st.download_button(
132
  label="Click to Download",
133
+ data=pdf_bytes,
134
  file_name=f"summary_{timestamp}.pdf",
135
  mime="application/pdf"
136
  )