imperiusrex commited on
Commit
affec76
·
verified ·
1 Parent(s): 1ae4d91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -5
app.py CHANGED
@@ -128,6 +128,7 @@ def process_image_page(img):
128
  def process_file_and_create_pdf(file):
129
  """
130
  Main function to process a file (image or PDF) and return a path to a new PDF.
 
131
  The @GPU decorator ensures this function is run on the GPU.
132
  """
133
  if file is None:
@@ -135,7 +136,6 @@ def process_file_and_create_pdf(file):
135
 
136
  temp_output_dir = tempfile.mkdtemp()
137
  output_pdf_path = os.path.join(temp_output_dir, "ocr_results.pdf")
138
-
139
  input_image_for_display = None
140
 
141
  try:
@@ -203,7 +203,6 @@ def process_file_and_create_pdf(file):
203
 
204
  temp_img_path = os.path.join(temp_output_dir, "original_image.png")
205
  original_image.save(temp_img_path)
206
- # Corrected the typo: `T` changed to `True`
207
  c.drawImage(temp_img_path, 50, height - 300, width=200, preserveAspectRatio=True)
208
  os.remove(temp_img_path)
209
 
@@ -218,20 +217,28 @@ def process_file_and_create_pdf(file):
218
  y = height - 50
219
  c.save()
220
 
 
221
  return output_pdf_path, input_image_for_display
222
 
223
  except Exception as e:
224
  print(f"An error occurred: {e}")
 
 
 
 
225
  if os.path.exists(temp_output_dir):
 
226
  shutil.rmtree(temp_output_dir)
227
- return None, None
228
 
229
  # Gradio Interface
230
  @GPU
231
  def process_file_for_gradio(file):
 
 
 
 
 
232
  output_path, input_image = process_file_and_create_pdf(file)
233
- if output_path is None:
234
- return None, None
235
  return output_path, input_image
236
 
237
  demo = gr.Interface(
 
128
  def process_file_and_create_pdf(file):
129
  """
130
  Main function to process a file (image or PDF) and return a path to a new PDF.
131
+ This function will ensure the temporary output directory is cleaned up safely.
132
  The @GPU decorator ensures this function is run on the GPU.
133
  """
134
  if file is None:
 
136
 
137
  temp_output_dir = tempfile.mkdtemp()
138
  output_pdf_path = os.path.join(temp_output_dir, "ocr_results.pdf")
 
139
  input_image_for_display = None
140
 
141
  try:
 
203
 
204
  temp_img_path = os.path.join(temp_output_dir, "original_image.png")
205
  original_image.save(temp_img_path)
 
206
  c.drawImage(temp_img_path, 50, height - 300, width=200, preserveAspectRatio=True)
207
  os.remove(temp_img_path)
208
 
 
217
  y = height - 50
218
  c.save()
219
 
220
+ print(f"Generated PDF path: {output_pdf_path}")
221
  return output_pdf_path, input_image_for_display
222
 
223
  except Exception as e:
224
  print(f"An error occurred: {e}")
225
+ # Return None, None on error
226
+ return None, None
227
+ finally:
228
+ # Ensure temporary directory is cleaned up after the function returns
229
  if os.path.exists(temp_output_dir):
230
+ print(f"Cleaning up temporary directory: {temp_output_dir}")
231
  shutil.rmtree(temp_output_dir)
 
232
 
233
  # Gradio Interface
234
  @GPU
235
  def process_file_for_gradio(file):
236
+ """
237
+ Wrapper function for Gradio interface.
238
+ This function calls the main processing logic and returns the outputs
239
+ in the format required by the gr.Interface.
240
+ """
241
  output_path, input_image = process_file_and_create_pdf(file)
 
 
242
  return output_path, input_image
243
 
244
  demo = gr.Interface(