MohamedRashad commited on
Commit
c166286
Β·
1 Parent(s): 61f26f1

Refactor turn_page function: update return values to include processed image and layout JSON, and improve page info handling

Browse files
Files changed (1) hide show
  1. app.py +41 -52
app.py CHANGED
@@ -476,45 +476,47 @@ def load_file_for_preview(file_path: str) -> Tuple[Optional[Image.Image], str]:
476
  return None, f"Error loading file: {str(e)}"
477
 
478
 
479
- def turn_page(direction: str) -> Tuple[Optional[Image.Image], str, str]:
480
- """Navigate through PDF pages"""
481
  global pdf_cache
482
-
483
  if not pdf_cache["images"]:
484
- return None, "No file loaded", "No results yet"
485
-
486
  if direction == "prev":
487
  pdf_cache["current_page"] = max(0, pdf_cache["current_page"] - 1)
488
  elif direction == "next":
489
  pdf_cache["current_page"] = min(
490
- pdf_cache["total_pages"] - 1,
491
  pdf_cache["current_page"] + 1
492
  )
493
-
494
  index = pdf_cache["current_page"]
495
- current_image = pdf_cache["images"][index]
496
- page_info = f"Page {index + 1} / {pdf_cache['total_pages']}"
497
-
 
 
 
 
 
498
  # Get results for current page if available
499
- current_result = ""
500
- if (pdf_cache["is_parsed"] and
501
- index < len(pdf_cache["results"]) and
502
  pdf_cache["results"][index]):
 
503
  result = pdf_cache["results"][index]
504
- if result.get('markdown_content'):
505
- current_result = result['markdown_content']
506
- else:
507
- current_result = result.get('raw_output', 'No content available')
508
- else:
509
- current_result = "Page not processed yet"
510
-
511
- # Check if the result contains mostly Arabic text and return appropriate update
512
- if is_arabic_text(current_result):
513
- result_update = gr.update(value=current_result, rtl=True)
514
  else:
515
- result_update = current_result
516
-
517
- return current_image, page_info, result_update
518
 
519
 
520
  def create_gradio_interface():
@@ -534,7 +536,6 @@ def create_gradio_interface():
534
  }
535
 
536
  .process-button {
537
- background: linear-gradient(45deg, #667eea 0%, #764ba2 100%) !important;
538
  border: none !important;
539
  color: white !important;
540
  font-weight: bold !important;
@@ -546,7 +547,6 @@ def create_gradio_interface():
546
  }
547
 
548
  .info-box {
549
- background: #f8f9fa;
550
  border: 1px solid #dee2e6;
551
  border-radius: 8px;
552
  padding: 15px;
@@ -556,7 +556,6 @@ def create_gradio_interface():
556
  .page-info {
557
  text-align: center;
558
  padding: 8px 16px;
559
- background: #e9ecef;
560
  border-radius: 20px;
561
  font-weight: bold;
562
  margin: 10px 0;
@@ -575,12 +574,6 @@ def create_gradio_interface():
575
  color: #0c5460;
576
  border: 1px solid #b8daff;
577
  }
578
-
579
- .status-error {
580
- background: #f8d7da;
581
- color: #721c24;
582
- border: 1px solid #f5c6cb;
583
- }
584
  """
585
 
586
  with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Dots.OCR Demo") as demo:
@@ -789,25 +782,19 @@ def create_gradio_interface():
789
  def clear_all():
790
  """Clear all data and reset interface"""
791
  global pdf_cache
792
-
793
  pdf_cache = {
794
- "images": [],
795
- "current_page": 0,
796
- "total_pages": 0,
797
- "file_type": None,
798
- "is_parsed": False,
799
- "results": []
800
  }
801
-
802
  return (
803
  None, # file_input
804
  None, # image_preview
805
- "No file loaded", # page_info
806
  None, # processed_image
807
  "Click 'Process Document' to see extracted content...", # markdown_output
808
- "", # raw_output
809
  None, # json_output
810
- '<div class="model-status status-ready">βœ… Interface cleared</div>' # model_status
811
  )
812
 
813
  # Wire up event handlers
@@ -817,14 +804,15 @@ def create_gradio_interface():
817
  outputs=[image_preview, page_info]
818
  )
819
 
 
820
  prev_page_btn.click(
821
- lambda: handle_page_turn("prev"),
822
- outputs=[image_preview, page_info, markdown_output]
823
  )
824
-
825
  next_page_btn.click(
826
- lambda: handle_page_turn("next"),
827
- outputs=[image_preview, page_info, markdown_output]
828
  )
829
 
830
  process_btn.click(
@@ -833,10 +821,11 @@ def create_gradio_interface():
833
  outputs=[processed_image, markdown_output, json_output]
834
  )
835
 
 
836
  clear_btn.click(
837
  clear_all,
838
  outputs=[
839
- file_input, image_preview, page_info, processed_image,
840
  markdown_output, json_output
841
  ]
842
  )
 
476
  return None, f"Error loading file: {str(e)}"
477
 
478
 
479
+ def turn_page(direction: str) -> Tuple[Optional[Image.Image], str, Any, Optional[Image.Image], Optional[Dict]]:
480
+ """Navigate through PDF pages and update all relevant outputs."""
481
  global pdf_cache
482
+
483
  if not pdf_cache["images"]:
484
+ return None, '<div class="page-info">No file loaded</div>', "No results yet", None, None
485
+
486
  if direction == "prev":
487
  pdf_cache["current_page"] = max(0, pdf_cache["current_page"] - 1)
488
  elif direction == "next":
489
  pdf_cache["current_page"] = min(
490
+ pdf_cache["total_pages"] - 1,
491
  pdf_cache["current_page"] + 1
492
  )
493
+
494
  index = pdf_cache["current_page"]
495
+ current_image_preview = pdf_cache["images"][index]
496
+ page_info_html = f'<div class="page-info">Page {index + 1} / {pdf_cache["total_pages"]}</div>'
497
+
498
+ # Initialize default result values
499
+ markdown_content = "Page not processed yet"
500
+ processed_img = None
501
+ layout_json = None
502
+
503
  # Get results for current page if available
504
+ if (pdf_cache["is_parsed"] and
505
+ index < len(pdf_cache["results"]) and
 
506
  pdf_cache["results"][index]):
507
+
508
  result = pdf_cache["results"][index]
509
+ markdown_content = result.get('markdown_content') or result.get('raw_output', 'No content available')
510
+ processed_img = result.get('processed_image', None) # Get the processed image
511
+ layout_json = result.get('layout_result', None) # Get the layout JSON
512
+
513
+ # Check for Arabic text to set RTL property
514
+ if is_arabic_text(markdown_content):
515
+ markdown_update = gr.update(value=markdown_content, rtl=True)
 
 
 
516
  else:
517
+ markdown_update = markdown_content
518
+
519
+ return current_image_preview, page_info_html, markdown_update, processed_img, layout_json
520
 
521
 
522
  def create_gradio_interface():
 
536
  }
537
 
538
  .process-button {
 
539
  border: none !important;
540
  color: white !important;
541
  font-weight: bold !important;
 
547
  }
548
 
549
  .info-box {
 
550
  border: 1px solid #dee2e6;
551
  border-radius: 8px;
552
  padding: 15px;
 
556
  .page-info {
557
  text-align: center;
558
  padding: 8px 16px;
 
559
  border-radius: 20px;
560
  font-weight: bold;
561
  margin: 10px 0;
 
574
  color: #0c5460;
575
  border: 1px solid #b8daff;
576
  }
 
 
 
 
 
 
577
  """
578
 
579
  with gr.Blocks(theme=gr.themes.Soft(), css=css, title="Dots.OCR Demo") as demo:
 
782
  def clear_all():
783
  """Clear all data and reset interface"""
784
  global pdf_cache
785
+
786
  pdf_cache = {
787
+ "images": [], "current_page": 0, "total_pages": 0,
788
+ "file_type": None, "is_parsed": False, "results": []
 
 
 
 
789
  }
790
+
791
  return (
792
  None, # file_input
793
  None, # image_preview
794
+ '<div class="page-info">No file loaded</div>', # page_info
795
  None, # processed_image
796
  "Click 'Process Document' to see extracted content...", # markdown_output
 
797
  None, # json_output
 
798
  )
799
 
800
  # Wire up event handlers
 
804
  outputs=[image_preview, page_info]
805
  )
806
 
807
+ # The outputs list is now updated to include all components that need to change
808
  prev_page_btn.click(
809
+ lambda: turn_page("prev"),
810
+ outputs=[image_preview, page_info, markdown_output, processed_image, json_output]
811
  )
812
+
813
  next_page_btn.click(
814
+ lambda: turn_page("next"),
815
+ outputs=[image_preview, page_info, markdown_output, processed_image, json_output]
816
  )
817
 
818
  process_btn.click(
 
821
  outputs=[processed_image, markdown_output, json_output]
822
  )
823
 
824
+ # The outputs list for the clear button is now correct
825
  clear_btn.click(
826
  clear_all,
827
  outputs=[
828
+ file_input, image_preview, page_info, processed_image,
829
  markdown_output, json_output
830
  ]
831
  )