Update app.py
Browse files
app.py
CHANGED
|
@@ -301,8 +301,6 @@
|
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
import fitz # PyMuPDF
|
| 307 |
import numpy as np
|
| 308 |
import cv2
|
|
@@ -328,7 +326,6 @@ def patched_torch_load(*args, **kwargs):
|
|
| 328 |
return _original_torch_load(*args, **kwargs)
|
| 329 |
torch.load = patched_torch_load
|
| 330 |
|
| 331 |
-
# Suppress warnings during normal operation
|
| 332 |
logging.basicConfig(level=logging.WARNING)
|
| 333 |
|
| 334 |
# ============================================================================
|
|
@@ -496,12 +493,12 @@ def run_yolo_detection_and_count(
|
|
| 496 |
|
| 497 |
|
| 498 |
# ============================================================================
|
| 499 |
-
# --- MAIN DOCUMENT PROCESSING FUNCTION ---
|
| 500 |
# ============================================================================
|
| 501 |
|
| 502 |
-
def run_single_pdf_preprocessing(pdf_path: str) -> Tuple[int, int, int, str, List[
|
| 503 |
"""
|
| 504 |
-
Runs the pipeline, returns counts, report, and a list of cropped equation images
|
| 505 |
"""
|
| 506 |
global GLOBAL_FIGURE_COUNT, GLOBAL_EQUATION_COUNT
|
| 507 |
|
|
@@ -531,7 +528,7 @@ def run_single_pdf_preprocessing(pdf_path: str) -> Tuple[int, int, int, str, Lis
|
|
| 531 |
return 0, 0, 0, report, []
|
| 532 |
|
| 533 |
mat = fitz.Matrix(SCALE_FACTOR, SCALE_FACTOR)
|
| 534 |
-
all_equation_images = [] # Stores
|
| 535 |
|
| 536 |
for page_num_0_based in range(doc.page_count):
|
| 537 |
fitz_page = doc.load_page(page_num_0_based)
|
|
@@ -554,20 +551,24 @@ def run_single_pdf_preprocessing(pdf_path: str) -> Tuple[int, int, int, str, Lis
|
|
| 554 |
bbox = eq['bbox_pdf']
|
| 555 |
|
| 556 |
try:
|
| 557 |
-
# FIX
|
| 558 |
rect = fitz.Rect(bbox)
|
| 559 |
clip_rect = rect + (0, 0, 5, 5) # Add small padding
|
| 560 |
|
| 561 |
# Get the pixmap for the cropped area (high-res render)
|
| 562 |
eq_pix = fitz_page.get_pixmap(matrix=fitz.Matrix(3.0, 3.0), clip=clip_rect)
|
| 563 |
|
| 564 |
-
#
|
| 565 |
img_bytes = eq_pix.tobytes("png")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 566 |
|
| 567 |
-
all_equation_images.append(
|
| 568 |
|
| 569 |
except Exception as e:
|
| 570 |
-
# This error means the image generation failed for a specific box.
|
| 571 |
logging.error(f"Error cropping equation on page {page_num} with bbox {bbox}: {e}")
|
| 572 |
|
| 573 |
doc.close()
|
|
@@ -580,33 +581,40 @@ def run_single_pdf_preprocessing(pdf_path: str) -> Tuple[int, int, int, str, Lis
|
|
| 580 |
f"**3) Total Figures Detected:** **{GLOBAL_FIGURE_COUNT}**"
|
| 581 |
)
|
| 582 |
|
|
|
|
| 583 |
return total_pages, GLOBAL_EQUATION_COUNT, GLOBAL_FIGURE_COUNT, report, all_equation_images
|
| 584 |
|
| 585 |
|
| 586 |
# ============================================================================
|
| 587 |
-
# --- GRADIO INTERFACE FUNCTION ---
|
| 588 |
# ============================================================================
|
| 589 |
|
| 590 |
-
def gradio_process_pdf(pdf_file) -> Tuple[str, str, str, str, List[
|
| 591 |
"""
|
| 592 |
-
Gradio wrapper function to handle file upload and return
|
| 593 |
"""
|
| 594 |
if pdf_file is None:
|
| 595 |
return "N/A", "N/A", "N/A", "Please upload a PDF file.", []
|
| 596 |
|
| 597 |
pdf_path = pdf_file.name
|
|
|
|
| 598 |
|
| 599 |
try:
|
| 600 |
-
# Run the core logic
|
| 601 |
-
num_pages, num_equations, num_figures, report, equation_images = run_single_pdf_preprocessing(
|
|
|
|
|
|
|
| 602 |
|
| 603 |
-
# Return results
|
| 604 |
return str(num_pages), str(num_equations), str(num_figures), report, equation_images
|
| 605 |
|
| 606 |
except Exception as e:
|
| 607 |
error_msg = f"An unexpected error occurred: {e}"
|
| 608 |
logging.error(error_msg, exc_info=True)
|
| 609 |
return "Error", "Error", "Error", error_msg, []
|
|
|
|
|
|
|
|
|
|
| 610 |
|
| 611 |
|
| 612 |
# ============================================================================
|
|
@@ -626,7 +634,7 @@ if __name__ == "__main__":
|
|
| 626 |
output_figures = gr.Textbox(label="Total Figures Detected", interactive=False)
|
| 627 |
output_report = gr.Markdown(label="Processing Summary")
|
| 628 |
|
| 629 |
-
# Gradio Gallery
|
| 630 |
output_gallery = gr.Gallery(
|
| 631 |
label="Detected Equations for Debugging",
|
| 632 |
columns=5,
|
|
|
|
| 301 |
|
| 302 |
|
| 303 |
|
|
|
|
|
|
|
| 304 |
import fitz # PyMuPDF
|
| 305 |
import numpy as np
|
| 306 |
import cv2
|
|
|
|
| 326 |
return _original_torch_load(*args, **kwargs)
|
| 327 |
torch.load = patched_torch_load
|
| 328 |
|
|
|
|
| 329 |
logging.basicConfig(level=logging.WARNING)
|
| 330 |
|
| 331 |
# ============================================================================
|
|
|
|
| 493 |
|
| 494 |
|
| 495 |
# ============================================================================
|
| 496 |
+
# --- MAIN DOCUMENT PROCESSING FUNCTION (Fixed for Gradio Gallery) ---
|
| 497 |
# ============================================================================
|
| 498 |
|
| 499 |
+
def run_single_pdf_preprocessing(pdf_path: str, temp_output_dir: str) -> Tuple[int, int, int, str, List[str]]:
|
| 500 |
"""
|
| 501 |
+
Runs the pipeline, returns counts, report, and a list of paths to cropped equation images.
|
| 502 |
"""
|
| 503 |
global GLOBAL_FIGURE_COUNT, GLOBAL_EQUATION_COUNT
|
| 504 |
|
|
|
|
| 528 |
return 0, 0, 0, report, []
|
| 529 |
|
| 530 |
mat = fitz.Matrix(SCALE_FACTOR, SCALE_FACTOR)
|
| 531 |
+
all_equation_images = [] # Stores file paths (strings) for Gradio gallery
|
| 532 |
|
| 533 |
for page_num_0_based in range(doc.page_count):
|
| 534 |
fitz_page = doc.load_page(page_num_0_based)
|
|
|
|
| 551 |
bbox = eq['bbox_pdf']
|
| 552 |
|
| 553 |
try:
|
| 554 |
+
# FIX 1 (PyMuPDF Rect): Only instantiate Rect, no .prerotate()
|
| 555 |
rect = fitz.Rect(bbox)
|
| 556 |
clip_rect = rect + (0, 0, 5, 5) # Add small padding
|
| 557 |
|
| 558 |
# Get the pixmap for the cropped area (high-res render)
|
| 559 |
eq_pix = fitz_page.get_pixmap(matrix=fitz.Matrix(3.0, 3.0), clip=clip_rect)
|
| 560 |
|
| 561 |
+
# FIX 2 (Gradio Gallery): Save to a temporary file path
|
| 562 |
img_bytes = eq_pix.tobytes("png")
|
| 563 |
+
filename = f"eq_{GLOBAL_EQUATION_COUNT}_p{page_num}.png"
|
| 564 |
+
output_path = os.path.join(temp_output_dir, filename)
|
| 565 |
+
|
| 566 |
+
with open(output_path, 'wb') as f:
|
| 567 |
+
f.write(img_bytes)
|
| 568 |
|
| 569 |
+
all_equation_images.append(output_path)
|
| 570 |
|
| 571 |
except Exception as e:
|
|
|
|
| 572 |
logging.error(f"Error cropping equation on page {page_num} with bbox {bbox}: {e}")
|
| 573 |
|
| 574 |
doc.close()
|
|
|
|
| 581 |
f"**3) Total Figures Detected:** **{GLOBAL_FIGURE_COUNT}**"
|
| 582 |
)
|
| 583 |
|
| 584 |
+
# Return the list of file paths (strings)
|
| 585 |
return total_pages, GLOBAL_EQUATION_COUNT, GLOBAL_FIGURE_COUNT, report, all_equation_images
|
| 586 |
|
| 587 |
|
| 588 |
# ============================================================================
|
| 589 |
+
# --- GRADIO INTERFACE FUNCTION (Fixed for temp dir management) ---
|
| 590 |
# ============================================================================
|
| 591 |
|
| 592 |
+
def gradio_process_pdf(pdf_file) -> Tuple[str, str, str, str, List[str]]:
|
| 593 |
"""
|
| 594 |
+
Gradio wrapper function to handle file upload, manage temporary directory, and return file paths.
|
| 595 |
"""
|
| 596 |
if pdf_file is None:
|
| 597 |
return "N/A", "N/A", "N/A", "Please upload a PDF file.", []
|
| 598 |
|
| 599 |
pdf_path = pdf_file.name
|
| 600 |
+
temp_output_dir = tempfile.mkdtemp() # Create temp directory
|
| 601 |
|
| 602 |
try:
|
| 603 |
+
# Run the core logic, passing the temp directory
|
| 604 |
+
num_pages, num_equations, num_figures, report, equation_images = run_single_pdf_preprocessing(
|
| 605 |
+
pdf_path, temp_output_dir
|
| 606 |
+
)
|
| 607 |
|
| 608 |
+
# Return results and the list of image file paths
|
| 609 |
return str(num_pages), str(num_equations), str(num_figures), report, equation_images
|
| 610 |
|
| 611 |
except Exception as e:
|
| 612 |
error_msg = f"An unexpected error occurred: {e}"
|
| 613 |
logging.error(error_msg, exc_info=True)
|
| 614 |
return "Error", "Error", "Error", error_msg, []
|
| 615 |
+
finally:
|
| 616 |
+
# CRUCIAL: Clean up the temporary directory containing the image files
|
| 617 |
+
shutil.rmtree(temp_output_dir, ignore_errors=True)
|
| 618 |
|
| 619 |
|
| 620 |
# ============================================================================
|
|
|
|
| 634 |
output_figures = gr.Textbox(label="Total Figures Detected", interactive=False)
|
| 635 |
output_report = gr.Markdown(label="Processing Summary")
|
| 636 |
|
| 637 |
+
# Gradio Gallery expects a list of file paths (strings)
|
| 638 |
output_gallery = gr.Gallery(
|
| 639 |
label="Detected Equations for Debugging",
|
| 640 |
columns=5,
|