errantanomie commited on
Commit
69f3e4a
·
verified ·
1 Parent(s): 652de8b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +135 -5
app.py CHANGED
@@ -1,8 +1,83 @@
1
- # Add this to your imports
 
 
 
 
 
 
 
 
 
2
  from streamlit_sortables import sort_items
 
 
 
3
  import uuid
4
 
5
- # Function to get a thumbnail, using your original function
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  def get_pdf_thumbnail(uploaded_file, page_num):
7
  """Generates a thumbnail image of a PDF page."""
8
  uploaded_file.seek(0)
@@ -13,6 +88,53 @@ def get_pdf_thumbnail(uploaded_file, page_num):
13
  img.thumbnail((100, 140))
14
  return img
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
  # Sidebar Navigation
17
  st.sidebar.title("Tool Selector")
18
  selection = st.sidebar.radio("Choose a tool:", ["PDF Combiner", "PDF Transcriber", "PDF Rotator", "PDF Document Separator"])
@@ -43,15 +165,24 @@ if selection == "PDF Document Separator":
43
  st.session_state.available_pages.append({"page_index":page_num, "rotation":0, "uuid":str(uuid.uuid4()), "image":image})
44
 
45
 
 
 
 
 
 
 
46
 
47
  left_col, right_col = st.columns(2)
48
 
49
-
50
  with left_col:
51
  st.header("Document Overview")
52
  st.write("Drag pages to the right to start creating the new document.")
53
  for page in st.session_state.available_pages:
54
- st.image(page["image"], caption=f"Page {page['page_index'] + 1}", use_container_width=True)
 
 
 
 
55
 
56
  with right_col:
57
  st.header("Target Document Builder")
@@ -74,7 +205,6 @@ if selection == "PDF Document Separator":
74
  if rotation != page['rotation']:
75
  page['rotation'] = rotation
76
 
77
-
78
  # The rest of your original app.py code should go here
79
  # PDF Combiner with Preview and Reordering
80
  elif selection == "PDF Combiner":
 
1
+ import os
2
+ import streamlit as st # Moved this to the top
3
+ from PyPDF2 import PdfMerger
4
+ from google.cloud import vision
5
+ from google.oauth2 import service_account
6
+ import fitz
7
+ from PIL import Image
8
+ from io import BytesIO
9
+ import tempfile
10
+ import json
11
  from streamlit_sortables import sort_items
12
+ import subprocess
13
+ from tqdm import tqdm
14
+ from datetime import datetime
15
  import uuid
16
 
17
+ #os.environ["PATH"] += os.pathsep + "/usr/bin" + os.pathsep + "/usr/local/bin" #Removed redundant path setting
18
+ print(f"Current PATH: {os.environ['PATH']}")
19
+
20
+
21
+ # Check if /usr/bin/pdfinfo exists
22
+ if os.path.exists("/usr/bin/pdfinfo"):
23
+ print("pdfinfo exists at /usr/bin/pdfinfo")
24
+ # Check the file permissions
25
+ permissions = os.stat("/usr/bin/pdfinfo").st_mode
26
+ print(f"File permissions: {oct(permissions)}")
27
+ else:
28
+ print("pdfinfo does not exist at /usr/bin/pdfinfo")
29
+
30
+
31
+ # Load Google Cloud Vision credentials from secret
32
+ credentials_json = os.getenv("GOOGLE_CREDENTIALS_JSON")
33
+ if credentials_json:
34
+ credentials_dict = json.loads(credentials_json)
35
+ credentials = service_account.Credentials.from_service_account_info(credentials_dict)
36
+ client = vision.ImageAnnotatorClient(credentials=credentials)
37
+ else:
38
+ client = None
39
+
40
+ # Function to extract text using Google Cloud Vision
41
+ def extract_text_with_google_vision(image_bytes):
42
+ """Extracts text using Google Cloud Vision."""
43
+ image = vision.Image(content=image_bytes)
44
+ response = client.document_text_detection(image=image)
45
+ if response.error.message:
46
+ raise Exception(f"Google Cloud Vision API Error: {response.error.message}")
47
+ return response.full_text_annotation.text if response.full_text_annotation else ""
48
+
49
+ # Function to process PDF for transcription
50
+ def process_pdf(file):
51
+ """Converts PDF pages to images and extracts text."""
52
+ text = ""
53
+ with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
54
+ temp_pdf.write(file.read())
55
+ temp_pdf_path = temp_pdf.name
56
+
57
+ try:
58
+ doc = fitz.open(temp_pdf_path)
59
+ for i in tqdm(range(len(doc)), desc="Processing pages"):
60
+ page = doc.load_page(i)
61
+ pix = page.get_pixmap()
62
+ image = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
63
+ image_bytes = BytesIO()
64
+ image.save(image_bytes, format="PNG")
65
+ image_bytes.seek(0)
66
+ try:
67
+ page_text = extract_text_with_google_vision(image_bytes.getvalue())
68
+ text += f"--- Page {i + 1} ---\n{page_text}\n\n"
69
+ except Exception as e:
70
+ st.error(f"Error on page {i + 1}: {e}")
71
+ finally:
72
+ os.remove(temp_pdf_path)
73
+
74
+ with tempfile.NamedTemporaryFile(suffix=".txt", delete=False) as temp_txt:
75
+ temp_txt.write(text.encode("utf-8"))
76
+ temp_txt_path = temp_txt.name
77
+ return temp_txt_path
78
+
79
+
80
+ # Function to generate thumbnail from PDF
81
  def get_pdf_thumbnail(uploaded_file, page_num):
82
  """Generates a thumbnail image of a PDF page."""
83
  uploaded_file.seek(0)
 
88
  img.thumbnail((100, 140))
89
  return img
90
 
91
+ # Function to merge PDFs
92
+ def merge_pdfs(reordered_files):
93
+ """Merges multiple PDFs into one."""
94
+ merger = PdfMerger()
95
+ for file in reordered_files:
96
+ file.seek(0)
97
+ merger.append(file)
98
+ output_filename = "combined_document.pdf"
99
+ with open(output_filename, "wb") as output_file:
100
+ merger.write(output_file)
101
+ return output_filename
102
+
103
+ def download_file(output_file, file_name, mime_type):
104
+ with open(output_file, "rb") as f:
105
+ st.download_button(
106
+ label="Download File",
107
+ data=f,
108
+ file_name=file_name,
109
+ mime=mime_type,
110
+ )
111
+
112
+ def generate_unique_filename(original_filename, suffix, file_type):
113
+ """Generates a unique filename based on date, time, and original filename."""
114
+ now = datetime.now()
115
+ timestamp = now.strftime("%Y%m%d_%H%M%S")
116
+ name, ext = os.path.splitext(original_filename)
117
+ return f"{name}_{timestamp}{suffix}{file_type}"
118
+
119
+ def rotate_pdf(pdf_file, rotation_angle):
120
+ """Rotates all pages of a PDF by a specified angle."""
121
+ with tempfile.NamedTemporaryFile(suffix=".pdf", delete=False) as temp_pdf:
122
+ temp_pdf.write(pdf_file.read())
123
+ temp_pdf_path = temp_pdf.name
124
+
125
+ doc = fitz.open(temp_pdf_path)
126
+ for page in doc:
127
+ page.set_rotation(rotation_angle)
128
+ os.remove(temp_pdf_path)
129
+ return doc
130
+
131
+ def display_pdf_preview(doc, page_num=0):
132
+ """Displays a preview of a PDF page."""
133
+ page = doc.load_page(page_num)
134
+ pix = page.get_pixmap()
135
+ img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
136
+ st.image(img, use_container_width=True)
137
+
138
  # Sidebar Navigation
139
  st.sidebar.title("Tool Selector")
140
  selection = st.sidebar.radio("Choose a tool:", ["PDF Combiner", "PDF Transcriber", "PDF Rotator", "PDF Document Separator"])
 
165
  st.session_state.available_pages.append({"page_index":page_num, "rotation":0, "uuid":str(uuid.uuid4()), "image":image})
166
 
167
 
168
+ def move_page_to_target(page_uuid):
169
+ """Moves a page from available_pages to target_pages."""
170
+ page_to_move = next((page for page in st.session_state.available_pages if page["uuid"] == page_uuid), None)
171
+ if page_to_move:
172
+ st.session_state.target_pages.append(page_to_move)
173
+ st.session_state.available_pages = [page for page in st.session_state.available_pages if page["uuid"] != page_uuid]
174
 
175
  left_col, right_col = st.columns(2)
176
 
 
177
  with left_col:
178
  st.header("Document Overview")
179
  st.write("Drag pages to the right to start creating the new document.")
180
  for page in st.session_state.available_pages:
181
+ # Add a button for each page to move to the right column
182
+ if st.button(f"Add Page {page['page_index'] + 1} to Document", key=f"button_{page['uuid']}"):
183
+ move_page_to_target(page["uuid"])
184
+ st.image(page["image"], caption=f"Page {page['page_index'] + 1}", use_container_width=True)
185
+
186
 
187
  with right_col:
188
  st.header("Target Document Builder")
 
205
  if rotation != page['rotation']:
206
  page['rotation'] = rotation
207
 
 
208
  # The rest of your original app.py code should go here
209
  # PDF Combiner with Preview and Reordering
210
  elif selection == "PDF Combiner":