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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -8
app.py CHANGED
@@ -232,21 +232,47 @@ def process_file_and_create_pdf(file):
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(
245
  fn=process_file_for_gradio,
246
- inputs=gr.File(label="Upload an Image (PNG, JPG) or a PDF", file_types=['.png', '.jpg', '.jpeg', '.pdf']),
 
 
 
247
  outputs=[
248
  gr.File(label="Download OCR Results PDF", interactive=False, visible=True),
249
- gr.Image(label="Uploaded Image Preview", interactive=False)
250
  ],
251
  title="OCR App with PaddleOCR and TrOCR",
252
  description="Upload an image or a multi-page PDF to get an output PDF with the recognized text from each page. The output PDF will be downloaded automatically.",
 
232
 
233
  # Gradio Interface
234
  @GPU
235
+ def process_file_for_gradio(image_file, pdf_file):
236
  """
237
+ Wrapper function for Gradio interface with separate inputs.
238
+ This function checks which input was provided and calls the main
239
+ processing logic accordingly.
240
  """
241
+ if image_file is not None:
242
+ # The gr.Image component returns a PIL Image object
243
+ # We need to save it to a temporary file for the main function
244
+ temp_dir = tempfile.mkdtemp()
245
+ image_path = os.path.join(temp_dir, "uploaded_image.png")
246
+ image_file.save(image_path)
247
+
248
+ # Create a mock file object to be compatible with the main function
249
+ class MockFile:
250
+ def __init__(self, name):
251
+ self.name = name
252
+
253
+ mock_file = MockFile(image_path)
254
+ output_path, input_image = process_file_and_create_pdf(mock_file)
255
+ shutil.rmtree(temp_dir)
256
+ return output_path, input_image
257
+
258
+ elif pdf_file is not None:
259
+ # The gr.File component passes a temporary file object directly
260
+ output_path, input_image = process_file_and_create_pdf(pdf_file)
261
+ return output_path, input_image
262
+
263
+ else:
264
+ return None, None
265
+
266
 
267
  demo = gr.Interface(
268
  fn=process_file_for_gradio,
269
+ inputs=[
270
+ gr.Image(label="Upload an Image", type="pil"),
271
+ gr.File(label="Upload a PDF", file_types=['.pdf'])
272
+ ],
273
  outputs=[
274
  gr.File(label="Download OCR Results PDF", interactive=False, visible=True),
275
+ gr.Image(label="Uploaded File Preview", interactive=False)
276
  ],
277
  title="OCR App with PaddleOCR and TrOCR",
278
  description="Upload an image or a multi-page PDF to get an output PDF with the recognized text from each page. The output PDF will be downloaded automatically.",