samyak152002 commited on
Commit
a949995
·
verified ·
1 Parent(s): 99adcd5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -6
app.py CHANGED
@@ -4,18 +4,55 @@ import streamlit as st
4
  import base64
5
  from annotations import analyze_pdf
6
 
7
- def display_pdf(pdf_bytes):
8
- """Displays the PDF in the browser using an embed tag."""
9
  if pdf_bytes and len(pdf_bytes) > 0:
10
- # Encode the PDF to base64
11
  base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
12
  pdf_display = f"""
13
- <embed src="data:application/pdf;base64,{base64_pdf}" width="100%" height="800px" type="application/pdf">
14
  """
15
  st.components.v1.html(pdf_display, height=800, width=700, scrolling=True)
16
  else:
17
  st.info("No annotated PDF to display.")
18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  def main():
20
  st.set_page_config(
21
  page_title="PDF Analyzer",
@@ -32,6 +69,8 @@ def main():
32
 
33
  if uploaded_file is not None:
34
  with st.spinner("Analyzing PDF..."):
 
 
35
  language_results, annotated_pdf = analyze_pdf(uploaded_file)
36
 
37
  if "error" in language_results:
@@ -46,9 +85,25 @@ def main():
46
  else:
47
  st.write("Annotated PDF is empty.")
48
 
49
- # Display the annotated PDF
 
 
 
 
 
 
50
  st.subheader("📄 Annotated PDF")
51
- display_pdf(annotated_pdf)
 
 
 
 
 
 
 
 
 
 
52
 
53
  # Sidebar for error details
54
  st.sidebar.header("📝 Error Details")
 
4
  import base64
5
  from annotations import analyze_pdf
6
 
7
+ def display_pdf_iframe(pdf_bytes):
8
+ """Displays the PDF using an iframe tag."""
9
  if pdf_bytes and len(pdf_bytes) > 0:
 
10
  base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
11
  pdf_display = f"""
12
+ <iframe src="data:application/pdf;base64,{base64_pdf}" width="100%" height="800px" type="application/pdf"></iframe>
13
  """
14
  st.components.v1.html(pdf_display, height=800, width=700, scrolling=True)
15
  else:
16
  st.info("No annotated PDF to display.")
17
 
18
+ def display_pdf_object(pdf_bytes):
19
+ """Displays the PDF using an object tag."""
20
+ if pdf_bytes and len(pdf_bytes) > 0:
21
+ base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
22
+ pdf_display = f"""
23
+ <object data="data:application/pdf;base64,{base64_pdf}" type="application/pdf" width="100%" height="800px">
24
+ <p>Your browser does not support PDFs.
25
+ <a href="data:application/pdf;base64,{base64_pdf}">Download the PDF</a>.</p>
26
+ </object>
27
+ """
28
+ st.components.v1.html(pdf_display, height=800, width=700, scrolling=True)
29
+ else:
30
+ st.info("No annotated PDF to display.")
31
+
32
+ def display_pdf_pdfjs(pdf_bytes):
33
+ """Displays the PDF using PDF.js embedded in an iframe."""
34
+ if pdf_bytes and len(pdf_bytes) > 0:
35
+ base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
36
+ pdfjs_viewer_url = f"https://mozilla.github.io/pdf.js/web/viewer.html?file=data:application/pdf;base64,{base64_pdf}"
37
+ pdf_display = f"""
38
+ <iframe src="{pdfjs_viewer_url}" width="100%" height="800px"></iframe>
39
+ """
40
+ st.components.v1.html(pdf_display, height=800, width=700, scrolling=True)
41
+ else:
42
+ st.info("No annotated PDF to display.")
43
+
44
+ def display_pdf_new_tab(pdf_bytes):
45
+ """Provides a link to view the PDF in a new browser tab."""
46
+ if pdf_bytes and len(pdf_bytes) > 0:
47
+ base64_pdf = base64.b64encode(pdf_bytes).decode('utf-8')
48
+ href = f"data:application/pdf;base64,{base64_pdf}"
49
+ pdf_display = f"""
50
+ <a href="{href}" target="_blank">🔍 View Annotated PDF</a>
51
+ """
52
+ st.markdown(pdf_display, unsafe_allow_html=True)
53
+ else:
54
+ st.info("No annotated PDF to display.")
55
+
56
  def main():
57
  st.set_page_config(
58
  page_title="PDF Analyzer",
 
69
 
70
  if uploaded_file is not None:
71
  with st.spinner("Analyzing PDF..."):
72
+ # Reset file pointer before reading
73
+ uploaded_file.seek(0)
74
  language_results, annotated_pdf = analyze_pdf(uploaded_file)
75
 
76
  if "error" in language_results:
 
85
  else:
86
  st.write("Annotated PDF is empty.")
87
 
88
+ # Select embedding method
89
+ embedding_option = st.selectbox(
90
+ "Select PDF Display Method",
91
+ options=["Iframe", "Object Tag", "PDF.js", "Open in New Tab"],
92
+ index=0
93
+ )
94
+
95
  st.subheader("📄 Annotated PDF")
96
+
97
+ if embedding_option == "Iframe":
98
+ display_pdf_iframe(annotated_pdf)
99
+ elif embedding_option == "Object Tag":
100
+ display_pdf_object(annotated_pdf)
101
+ elif embedding_option == "PDF.js":
102
+ display_pdf_pdfjs(annotated_pdf)
103
+ elif embedding_option == "Open in New Tab":
104
+ display_pdf_new_tab(annotated_pdf)
105
+ else:
106
+ st.info("Select a display method to view the PDF.")
107
 
108
  # Sidebar for error details
109
  st.sidebar.header("📝 Error Details")