Commit
Β·
61f26f1
1
Parent(s):
5f82e6a
Refactor create_gradio_interface: remove unused processing state and clean up return values
Browse files
app.py
CHANGED
@@ -293,15 +293,6 @@ pdf_cache = {
|
|
293 |
"is_parsed": False,
|
294 |
"results": []
|
295 |
}
|
296 |
-
|
297 |
-
# Processing state
|
298 |
-
processing_results = {
|
299 |
-
'original_image': None,
|
300 |
-
'processed_image': None,
|
301 |
-
'layout_result': None,
|
302 |
-
'markdown_content': None,
|
303 |
-
'raw_output': None,
|
304 |
-
}
|
305 |
@spaces.GPU()
|
306 |
def inference(image: Image.Image, prompt: str, max_new_tokens: int = 24000) -> str:
|
307 |
"""Run inference on an image with the given prompt"""
|
@@ -579,12 +570,6 @@ def create_gradio_interface():
|
|
579 |
font-weight: bold;
|
580 |
}
|
581 |
|
582 |
-
.status-loading {
|
583 |
-
background: #fff3cd;
|
584 |
-
color: #856404;
|
585 |
-
border: 1px solid #ffeaa7;
|
586 |
-
}
|
587 |
-
|
588 |
.status-ready {
|
589 |
background: #d1edff;
|
590 |
color: #0c5460;
|
@@ -625,7 +610,6 @@ def create_gradio_interface():
|
|
625 |
with gr.Row():
|
626 |
# Left column - Input and controls
|
627 |
with gr.Column(scale=1):
|
628 |
-
gr.Markdown("### π Input")
|
629 |
|
630 |
# File input
|
631 |
file_input = gr.File(
|
@@ -644,12 +628,10 @@ def create_gradio_interface():
|
|
644 |
|
645 |
# Page navigation for PDFs
|
646 |
with gr.Row():
|
647 |
-
prev_page_btn = gr.Button("β Previous", size="
|
648 |
page_info = gr.HTML('<div class="page-info">No file loaded</div>')
|
649 |
-
next_page_btn = gr.Button("Next βΆ", size="
|
650 |
|
651 |
-
gr.Markdown("### βοΈ Settings")
|
652 |
-
|
653 |
# Advanced settings
|
654 |
with gr.Accordion("Advanced Settings", open=False):
|
655 |
max_new_tokens = gr.Slider(
|
@@ -686,7 +668,6 @@ def create_gradio_interface():
|
|
686 |
|
687 |
# Right column - Results
|
688 |
with gr.Column(scale=2):
|
689 |
-
gr.Markdown("### π Results")
|
690 |
|
691 |
# Results tabs
|
692 |
with gr.Tabs():
|
@@ -712,47 +693,21 @@ def create_gradio_interface():
|
|
712 |
)
|
713 |
|
714 |
# Event handlers
|
715 |
-
def load_model_on_startup():
|
716 |
-
"""Load model when the interface starts"""
|
717 |
-
try:
|
718 |
-
# Model is already loaded at script level
|
719 |
-
return '<div class="model-status status-ready">β
Model loaded successfully!</div>'
|
720 |
-
except Exception as e:
|
721 |
-
return f'<div class="model-status status-error">β Error: {str(e)}</div>'
|
722 |
-
|
723 |
def process_document(file_path, max_tokens, min_pix, max_pix):
|
724 |
"""Process the uploaded document"""
|
725 |
global pdf_cache
|
726 |
|
727 |
try:
|
728 |
if not file_path:
|
729 |
-
return
|
730 |
-
None,
|
731 |
-
"Please upload a file first.",
|
732 |
-
"No file uploaded",
|
733 |
-
None,
|
734 |
-
'<div class="model-status status-error">β No file uploaded</div>'
|
735 |
-
)
|
736 |
|
737 |
if model is None:
|
738 |
-
return
|
739 |
-
None,
|
740 |
-
"Model not loaded. Please refresh the page and try again.",
|
741 |
-
"Model not loaded",
|
742 |
-
None,
|
743 |
-
'<div class="model-status status-error">β Model not loaded</div>'
|
744 |
-
)
|
745 |
|
746 |
# Load and preview file
|
747 |
image, page_info = load_file_for_preview(file_path)
|
748 |
if image is None:
|
749 |
-
return
|
750 |
-
None,
|
751 |
-
page_info,
|
752 |
-
"Failed to load file",
|
753 |
-
None,
|
754 |
-
'<div class="model-status status-error">β Failed to load file</div>'
|
755 |
-
)
|
756 |
|
757 |
# Process the image(s)
|
758 |
if pdf_cache["file_type"] == "pdf":
|
@@ -786,9 +741,7 @@ def create_gradio_interface():
|
|
786 |
return (
|
787 |
first_result['processed_image'],
|
788 |
markdown_update,
|
789 |
-
first_result['
|
790 |
-
first_result['layout_result'],
|
791 |
-
'<div class="model-status status-ready">β
Processing completed!</div>'
|
792 |
)
|
793 |
else:
|
794 |
# Process single image
|
@@ -811,22 +764,14 @@ def create_gradio_interface():
|
|
811 |
return (
|
812 |
result['processed_image'],
|
813 |
markdown_update,
|
814 |
-
result['
|
815 |
-
result['layout_result'],
|
816 |
-
'<div class="model-status status-ready">β
Processing completed!</div>'
|
817 |
)
|
818 |
|
819 |
except Exception as e:
|
820 |
error_msg = f"Error processing document: {str(e)}"
|
821 |
print(error_msg)
|
822 |
traceback.print_exc()
|
823 |
-
return
|
824 |
-
None,
|
825 |
-
error_msg,
|
826 |
-
error_msg,
|
827 |
-
None,
|
828 |
-
f'<div class="model-status status-error">β {error_msg}</div>'
|
829 |
-
)
|
830 |
|
831 |
def handle_file_upload(file_path):
|
832 |
"""Handle file upload and show preview"""
|
@@ -843,7 +788,7 @@ def create_gradio_interface():
|
|
843 |
|
844 |
def clear_all():
|
845 |
"""Clear all data and reset interface"""
|
846 |
-
global pdf_cache
|
847 |
|
848 |
pdf_cache = {
|
849 |
"images": [],
|
@@ -853,13 +798,6 @@ def create_gradio_interface():
|
|
853 |
"is_parsed": False,
|
854 |
"results": []
|
855 |
}
|
856 |
-
processing_results = {
|
857 |
-
'original_image': None,
|
858 |
-
'processed_image': None,
|
859 |
-
'layout_result': None,
|
860 |
-
'markdown_content': None,
|
861 |
-
'raw_output': None,
|
862 |
-
}
|
863 |
|
864 |
return (
|
865 |
None, # file_input
|
|
|
293 |
"is_parsed": False,
|
294 |
"results": []
|
295 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
296 |
@spaces.GPU()
|
297 |
def inference(image: Image.Image, prompt: str, max_new_tokens: int = 24000) -> str:
|
298 |
"""Run inference on an image with the given prompt"""
|
|
|
570 |
font-weight: bold;
|
571 |
}
|
572 |
|
|
|
|
|
|
|
|
|
|
|
|
|
573 |
.status-ready {
|
574 |
background: #d1edff;
|
575 |
color: #0c5460;
|
|
|
610 |
with gr.Row():
|
611 |
# Left column - Input and controls
|
612 |
with gr.Column(scale=1):
|
|
|
613 |
|
614 |
# File input
|
615 |
file_input = gr.File(
|
|
|
628 |
|
629 |
# Page navigation for PDFs
|
630 |
with gr.Row():
|
631 |
+
prev_page_btn = gr.Button("β Previous", size="md")
|
632 |
page_info = gr.HTML('<div class="page-info">No file loaded</div>')
|
633 |
+
next_page_btn = gr.Button("Next βΆ", size="md")
|
634 |
|
|
|
|
|
635 |
# Advanced settings
|
636 |
with gr.Accordion("Advanced Settings", open=False):
|
637 |
max_new_tokens = gr.Slider(
|
|
|
668 |
|
669 |
# Right column - Results
|
670 |
with gr.Column(scale=2):
|
|
|
671 |
|
672 |
# Results tabs
|
673 |
with gr.Tabs():
|
|
|
693 |
)
|
694 |
|
695 |
# Event handlers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
696 |
def process_document(file_path, max_tokens, min_pix, max_pix):
|
697 |
"""Process the uploaded document"""
|
698 |
global pdf_cache
|
699 |
|
700 |
try:
|
701 |
if not file_path:
|
702 |
+
return None, "Please upload a file first.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
703 |
|
704 |
if model is None:
|
705 |
+
return None, "Model not loaded. Please refresh the page and try again.", None
|
|
|
|
|
|
|
|
|
|
|
|
|
706 |
|
707 |
# Load and preview file
|
708 |
image, page_info = load_file_for_preview(file_path)
|
709 |
if image is None:
|
710 |
+
return None, page_info, None
|
|
|
|
|
|
|
|
|
|
|
|
|
711 |
|
712 |
# Process the image(s)
|
713 |
if pdf_cache["file_type"] == "pdf":
|
|
|
741 |
return (
|
742 |
first_result['processed_image'],
|
743 |
markdown_update,
|
744 |
+
first_result['layout_result']
|
|
|
|
|
745 |
)
|
746 |
else:
|
747 |
# Process single image
|
|
|
764 |
return (
|
765 |
result['processed_image'],
|
766 |
markdown_update,
|
767 |
+
result['layout_result']
|
|
|
|
|
768 |
)
|
769 |
|
770 |
except Exception as e:
|
771 |
error_msg = f"Error processing document: {str(e)}"
|
772 |
print(error_msg)
|
773 |
traceback.print_exc()
|
774 |
+
return None, error_msg, None
|
|
|
|
|
|
|
|
|
|
|
|
|
775 |
|
776 |
def handle_file_upload(file_path):
|
777 |
"""Handle file upload and show preview"""
|
|
|
788 |
|
789 |
def clear_all():
|
790 |
"""Clear all data and reset interface"""
|
791 |
+
global pdf_cache
|
792 |
|
793 |
pdf_cache = {
|
794 |
"images": [],
|
|
|
798 |
"is_parsed": False,
|
799 |
"results": []
|
800 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
801 |
|
802 |
return (
|
803 |
None, # file_input
|