File size: 38,821 Bytes
8d96f17 4d661b4 434d541 4d661b4 d126c17 4d661b4 d126c17 4d661b4 d126c17 4d661b4 d126c17 4d661b4 c6b9f2b 4d661b4 c6b9f2b 314c4a7 c6b9f2b 40b8823 c6b9f2b 12f4426 c6b9f2b 12f4426 314c4a7 c6b9f2b 314c4a7 c6b9f2b 4d661b4 4230d1c 4d661b4 d06d1bc e12f847 4d661b4 e12f847 40b8823 c6b9f2b 4d661b4 c6b9f2b 4d661b4 c6b9f2b 12f4426 c6b9f2b 314c4a7 c6b9f2b 314c4a7 c6b9f2b 4d661b4 c6b9f2b d52b60e c6b9f2b b00791d 12f4426 b00791d c6b9f2b 4d661b4 2665493 4d661b4 2665493 4d661b4 2665493 4d661b4 12f4426 2665493 4d661b4 2665493 12f4426 2665493 12f4426 2665493 12f4426 2665493 12f4426 4d661b4 12f4426 b00791d 2665493 4d661b4 12f4426 2665493 4d661b4 2665493 4d661b4 2665493 4d661b4 2665493 12f4426 4d661b4 2665493 4d661b4 2665493 b00791d 2665493 c6b9f2b 4a1e588 2665493 e12f847 2665493 e12f847 2665493 e12f847 2665493 e12f847 2665493 e12f847 2665493 e12f847 2665493 e12f847 2665493 e12f847 2665493 4a1e588 b00791d 4d661b4 b00791d 4d661b4 314c4a7 4d661b4 314c4a7 4a1e588 4d661b4 8d96f17 4d661b4 e1d2112 d126c17 e1d2112 c6b9f2b 4d661b4 4230d1c 8d96f17 c6b9f2b 314c4a7 4d661b4 e1d2112 b00791d d52b60e b00791d 12f4426 e1d2112 8d96f17 c6b9f2b 314c4a7 d52b60e c6b9f2b 314c4a7 e1d2112 8d96f17 c6b9f2b 40b8823 4c1e812 8d96f17 c6b9f2b 8d96f17 c6b9f2b 12f4426 c6b9f2b 8d96f17 c6b9f2b 8d96f17 c6b9f2b 8d96f17 c6b9f2b 12f4426 c6b9f2b 40b8823 8d96f17 4d661b4 4a1e588 4d661b4 8d96f17 d126c17 e1d2112 8d96f17 c6b9f2b 8d96f17 c6b9f2b 4d661b4 d126c17 8d96f17 12f4426 4d661b4 8d96f17 12f4426 c6b9f2b 4d661b4 c6b9f2b b00791d 8d96f17 b00791d c6b9f2b 4d661b4 314c4a7 e1d2112 314c4a7 4d661b4 e1d2112 314c4a7 9a971c3 314c4a7 d126c17 4d661b4 80c0a3d 314c4a7 d126c17 4d661b4 4a1e588 314c4a7 12f4426 40b8823 4d661b4 e1d2112 c6b9f2b 314c4a7 8d96f17 314c4a7 b00791d 314c4a7 12f4426 314c4a7 b00791d 40b8823 12f4426 8d96f17 d52b60e e1d2112 4d661b4 40b8823 4d661b4 40b8823 e1d2112 40b8823 b00791d 314c4a7 4d661b4 e1d2112 d126c17 e1d2112 4d661b4 314c4a7 4d661b4 314c4a7 4d661b4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 |
# import base64
# from PIL import Image
# import re
# import fitz # PyMuPDF
# import numpy as np
# import cv2
# import torch
# import torch.serialization
# import os
# import time
# from typing import Optional, Tuple, List, Dict, Any
# from ultralytics import YOLO
# import logging
# import gradio as gr
# import shutil
# import tempfile
# import io
# # ============================================================================
# # --- Global Patches and Setup ---
# # ============================================================================
# # Patch torch.load to prevent weights_only error with older models
# _original_torch_load = torch.load
# def patched_torch_load(*args, **kwargs):
# kwargs["weights_only"] = False
# return _original_torch_load(*args, **kwargs)
# torch.load = patched_torch_load
# logging.basicConfig(level=logging.WARNING)
# # ============================================================================
# # --- CONFIGURATION AND CONSTANTS ---
# # ============================================================================
# WEIGHTS_PATH = 'best.pt'
# SCALE_FACTOR = 2.0
# # OUTPUT_DIR = "yolo_extracted_regions"
# # OUTPUT_DIR = os.path.join(tempfile.gettempdir(), "yolo_extracted_regions")
# from transformers import TrOCRProcessor
# from optimum.onnxruntime import ORTModelForVision2Seq
# MODEL_NAME = 'breezedeus/pix2text-mfr-1.5'
# processor = TrOCRProcessor.from_pretrained(MODEL_NAME)
# ort_model = ORTModelForVision2Seq.from_pretrained(MODEL_NAME, use_cache=False)
# # Detection parameters
# CONF_THRESHOLD = 0.2
# TARGET_CLASSES = ['figure', 'equation']
# IOU_MERGE_THRESHOLD = 0.4
# IOA_SUPPRESSION_THRESHOLD = 0.7
# # Global counters (Reset per run)
# GLOBAL_FIGURE_COUNT = 0
# GLOBAL_EQUATION_COUNT = 0
# # ============================================================================
# # --- BOX COMBINATION LOGIC (Retained for detection accuracy) ---
# # ============================================================================
# def calculate_iou(box1, box2):
# x1_a, y1_a, x2_a, y2_a = box1
# x1_b, y1_b, x2_b, y2_b = box2
# x_left = max(x1_a, x1_b)
# y_top = max(y1_a, y1_b)
# x_right = min(x2_a, x2_b)
# y_bottom = min(y2_a, y2_b)
# intersection_area = max(0, x_right - x_left) * max(0, y_bottom - y_top)
# box_a_area = (x2_a - x1_a) * (y2_a - y1_a)
# box_b_area = (x2_b - x1_b) * (y2_b - y1_b)
# union_area = float(box_a_area + box_b_area - intersection_area)
# return intersection_area / union_area if union_area > 0 else 0
# def filter_nested_boxes(detections, ioa_threshold=0.80):
# if not detections: return []
# for d in detections:
# x1, y1, x2, y2 = d['coords']
# d['area'] = (x2 - x1) * (y2 - y1)
# detections.sort(key=lambda x: x['area'], reverse=True)
# keep_indices = []
# is_suppressed = [False] * len(detections)
# for i in range(len(detections)):
# if is_suppressed[i]: continue
# keep_indices.append(i)
# box_a = detections[i]['coords']
# for j in range(i + 1, len(detections)):
# if is_suppressed[j]: continue
# box_b = detections[j]['coords']
# x_left = max(box_a[0], box_b[0])
# y_top = max(box_a[1], box_b[1])
# x_right = min(box_a[2], box_b[2])
# y_bottom = min(box_a[3], box_b[3])
# intersection = max(0, x_right - x_left) * max(0, y_bottom - y_top)
# area_b = detections[j]['area']
# if area_b > 0 and intersection / area_b > ioa_threshold:
# is_suppressed[j] = True
# return [detections[i] for i in keep_indices]
# def merge_overlapping_boxes(detections, iou_threshold):
# if not detections: return []
# detections.sort(key=lambda d: d['conf'], reverse=True)
# merged_detections = []
# is_merged = [False] * len(detections)
# for i in range(len(detections)):
# if is_merged[i]: continue
# current_box = detections[i]['coords']
# current_class = detections[i]['class']
# merged_x1, merged_y1, merged_x2, merged_y2 = current_box
# for j in range(i + 1, len(detections)):
# if is_merged[j] or detections[j]['class'] != current_class: continue
# other_box = detections[j]['coords']
# iou = calculate_iou(current_box, other_box)
# if iou > iou_threshold:
# merged_x1 = min(merged_x1, other_box[0])
# merged_y1 = min(merged_y1, other_box[1])
# merged_x2 = max(merged_x2, other_box[2])
# merged_y2 = max(merged_y2, other_box[3])
# is_merged[j] = True
# merged_detections.append({
# 'coords': (merged_x1, merged_y1, merged_x2, merged_y2),
# 'y1': merged_y1, 'class': current_class, 'conf': detections[i]['conf']
# })
# return merged_detections
# # ============================================================================
# # --- UTILITY FUNCTIONS ---
# # ============================================================================
# def pixmap_to_numpy(pix: fitz.Pixmap) -> np.ndarray:
# """Converts a PyMuPDF Pixmap to a NumPy array for OpenCV/YOLO."""
# img = np.frombuffer(pix.samples, dtype=np.uint8).reshape(
# (pix.h, pix.w, pix.n)
# )
# if pix.n == 4:
# img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
# elif pix.n == 1:
# img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
# return img
# def run_yolo_detection_and_count(
# image: np.ndarray, model: YOLO, page_num: int
# ) -> Tuple[int, int, List[Dict[str, str]]]:
# global GLOBAL_FIGURE_COUNT, GLOBAL_EQUATION_COUNT
# yolo_detections = []
# page_equations = 0
# page_figures = 0
# detected_items = []
# try:
# results = model.predict(image, conf=CONF_THRESHOLD, verbose=False)
# if results and results[0].boxes:
# for box in results[0].boxes.data.tolist():
# x1, y1, x2, y2, conf, cls_id = box
# cls_name = model.names[int(cls_id)]
# if cls_name in TARGET_CLASSES:
# yolo_detections.append({
# 'coords': (x1, y1, x2, y2),
# 'class': cls_name,
# 'conf': conf
# })
# except Exception as e:
# logging.error(f"YOLO inference failed on page {page_num}: {e}")
# return 0, 0, []
# merged_detections = merge_overlapping_boxes(yolo_detections, IOU_MERGE_THRESHOLD)
# final_detections = filter_nested_boxes(merged_detections, IOA_SUPPRESSION_THRESHOLD)
# for det in final_detections:
# bbox = det["coords"]
# if det["class"] == "equation":
# GLOBAL_EQUATION_COUNT += 1
# page_equations += 1
# b64 = crop_and_convert_to_base64(image, bbox)
# detected_items.append({
# "type": "equation",
# "id": f"EQUATION{GLOBAL_EQUATION_COUNT}",
# "base64": b64
# })
# elif det["class"] == "figure":
# GLOBAL_FIGURE_COUNT += 1
# page_figures += 1
# b64 = crop_and_convert_to_base64(image, bbox)
# detected_items.append({
# "type": "figure",
# "id": f"FIGURE{GLOBAL_FIGURE_COUNT}",
# "base64": b64
# })
# logging.warning(f" -> Page {page_num}: EQs={page_equations}, Figs={page_figures}")
# return page_equations, page_figures, detected_items
# def get_latex_from_base64(base64_string: str) -> str:
# if ort_model is None or processor is None:
# return "[MODEL_ERROR: Model not initialized]"
# try:
# image_data = base64.b64decode(base64_string)
# image = Image.open(io.BytesIO(image_data)).convert('RGB')
# pixel_values = processor(images=image, return_tensors="pt").pixel_values
# generated_ids = ort_model.generate(pixel_values)
# raw_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
# if not raw_text:
# return "[OCR_WARNING: No formula found]"
# latex = raw_text[0]
# latex = re.sub(r'[\r\n]+', '', latex)
# return latex
# except Exception as e:
# return f"[TR_OCR_ERROR: {e}]"
# def extract_images_from_page_in_memory(page) -> Dict[str, str]:
# """
# Extract images from a page and return:
# { "EQUATION1": base64_string, "FIGURE1": base64_string }
# """
# image_map = {}
# image_list = page.get_images(full=True)
# for idx, img in enumerate(image_list, start=1):
# xref = img[0]
# base = page.parent.extract_image(xref)
# image_bytes = base["image"]
# base64_img = base64.b64encode(image_bytes).decode("utf-8")
# # Convention: first image = FIGURE1, second image = EQUATION1 etc
# # You can tune this if needed
# image_map[f"FIGURE{idx}"] = base64_img
# return image_map
# def embed_images_as_base64_in_memory(structured_data, detected_items):
# tag_regex = re.compile(r'(figure|equation)(\d+)', re.IGNORECASE)
# item_lookup = {d["id"]: d for d in detected_items}
# final_data = []
# for item in structured_data:
# text_fields = [
# item.get('question', ''),
# item.get('passage', ''),
# item.get('new_passage', '')
# ]
# if 'options' in item:
# text_fields.extend(item['options'].values())
# used_tags = set()
# for text in text_fields:
# for m in tag_regex.finditer(text or ""):
# used_tags.add(m.group(0).upper())
# for tag in used_tags:
# base_key = tag.lower().replace(" ", "")
# if tag not in item_lookup:
# item[base_key] = "[MISSING_IMAGE]"
# continue
# entry = item_lookup[tag]
# if entry["type"] == "equation":
# item[base_key] = get_latex_from_base64(entry["base64"])
# else:
# item[base_key] = entry["base64"]
# final_data.append(item)
# return final_data
# def crop_and_convert_to_base64(image: np.ndarray, bbox: Tuple[float, float, float, float]) -> str:
# x1, y1, x2, y2 = map(int, bbox)
# h, w, _ = image.shape
# x1 = max(0, x1)
# y1 = max(0, y1)
# x2 = min(w, x2)
# y2 = min(h, y2)
# crop = image[y1:y2, x1:x2]
# _, buffer = cv2.imencode(".png", crop)
# return base64.b64encode(buffer).decode("utf-8")
# # ============================================================================
# # --- MAIN DOCUMENT PROCESSING FUNCTION (Fixed for JSON serialization) ---
# # ============================================================================
# # NOTE: The return signature now uses Dict[str, int] for the equation counts
# def run_single_pdf_preprocessing(pdf_path: str) -> Tuple[int, int, int, str, float, Dict[str, int], List[str]]:
# """
# Runs the pipeline, returns counts, report, total time, page counts dict (str keys), and empty list.
# """
# global GLOBAL_FIGURE_COUNT, GLOBAL_EQUATION_COUNT
# start_time = time.time()
# log_messages = []
# all_saved_images = []
# all_base64_images: List[str] = []
# # Dictionary to store {page_number (int): equation_count (int)}
# equation_counts_per_page: Dict[int, int] = {}
# # Reset globals
# GLOBAL_FIGURE_COUNT = 0
# GLOBAL_EQUATION_COUNT = 0
# # if os.path.exists(OUTPUT_DIR):
# # shutil.rmtree(OUTPUT_DIR)
# # os.makedirs(OUTPUT_DIR, exist_ok=True)
# # 1. Validation and Model Loading
# t0 = time.time()
# if not os.path.exists(pdf_path):
# report = f"β FATAL ERROR: Input PDF not found at {pdf_path}."
# return 0, 0, 0, report, time.time() - start_time, {}, []
# try:
# model = YOLO(WEIGHTS_PATH)
# logging.warning(f"β
Loaded YOLO model from: {WEIGHTS_PATH}")
# except Exception as e:
# report = f"β ERROR loading YOLO model: {e}\n(Ensure 'best.pt' is available and valid.)"
# return 0, 0, 0, report, time.time() - start_time, {}, []
# t1 = time.time()
# log_messages.append(f"Model Loading Time: {t1-t0:.4f}s")
# # 2. PDF Loading
# t2 = time.time()
# try:
# doc = fitz.open(pdf_path)
# total_pages = doc.page_count
# logging.warning(f"β
Opened PDF with {doc.page_count} pages")
# except Exception as e:
# report = f"β ERROR loading PDF file: {e}"
# return 0, 0, 0, report, time.time() - start_time, {}, []
# t3 = time.time()
# log_messages.append(f"PDF Initialization Time: {t3-t2:.4f}s")
# mat = fitz.Matrix(SCALE_FACTOR, SCALE_FACTOR)
# # 3. Page Processing and Detection Loop
# t4 = time.time()
# for page_num_0_based in range(doc.page_count):
# page_start_time = time.time()
# fitz_page = doc.load_page(page_num_0_based)
# page_num = page_num_0_based + 1
# # Render page to image for YOLO
# try:
# pix_start = time.time()
# pix = fitz_page.get_pixmap(matrix=mat)
# original_img = pixmap_to_numpy(pix)
# pix_time = time.time() - pix_start
# except Exception as e:
# logging.error(f"Error converting page {page_num} to image: {e}. Skipping.")
# continue
# # Core Detection
# detect_start = time.time()
# # page_equations, _ = run_yolo_detection_and_count(original_img, model, page_num)
# page_equations, _, page_images = run_yolo_detection_and_count(original_img, model, page_num)
# all_saved_images.extend(page_images)
# detect_time = time.time() - detect_start
# # Store the count in the dictionary (INT keys)
# equation_counts_per_page[page_num] = page_equations
# page_total_time = time.time() - page_start_time
# log_messages.append(f"Page {page_num} Time: Total={page_total_time:.4f}s (Render={pix_time:.4f}s, Detect={detect_time:.4f}s)")
# doc.close()
# t5 = time.time()
# detection_loop_time = t5 - t4
# log_messages.append(f"Total Detection Loop Time ({total_pages} pages): {detection_loop_time:.4f}s")
# # FIX APPLIED HERE: Convert integer keys to string keys for JSON serialization
# equation_counts_per_page_str_keys: Dict[str, int] = {
# str(k): v for k, v in equation_counts_per_page.items()
# }
# # 4. Final Report Generation
# total_execution_time = t5 - start_time
# report = (
# f"β
**YOLO Counting Complete!**\n\n"
# f"**1) Total Pages Detected in PDF:** **{total_pages}**\n"
# f"**2) Total Equations Detected:** **{GLOBAL_EQUATION_COUNT}**\n"
# f"**3) Total Figures Detected:** **{GLOBAL_FIGURE_COUNT}**\n"
# f"---\n"
# f"**4) Total Execution Time:** **{total_execution_time:.4f}s**\n"
# f"### Detailed Step Timing\n"
# f"```\n"
# + "\n".join(log_messages) +
# f"\n```"
# )
# # Return the dictionary with string keys
# # return total_pages, GLOBAL_EQUATION_COUNT, GLOBAL_FIGURE_COUNT, report, total_execution_time, equation_counts_per_page_str_keys, []
# return total_pages, GLOBAL_EQUATION_COUNT, GLOBAL_FIGURE_COUNT, report, total_execution_time, equation_counts_per_page_str_keys, all_saved_images
# # ============================================================================
# # --- GRADIO INTERFACE FUNCTION (Updated) ---
# # ============================================================================
# def gradio_process_pdf(pdf_file) -> Tuple[str, str, str, str, Dict[str, int], List[str]]:
# """
# Gradio wrapper function to handle file upload and return results.
# """
# if pdf_file is None:
# # Return an empty dict with string keys
# return "N/A", "N/A", "N/A", "Please upload a PDF file.", {}, []
# pdf_path = pdf_file.name
# try:
# # Unpack the new return value: equation_counts_per_page (with string keys)
# # num_pages, num_equations, num_figures, report, total_time, equation_counts_per_page, _ = run_single_pdf_preprocessing(
# # pdf_path
# # )
# # num_pages, num_equations, num_figures, report, total_time, equation_counts_per_page, images = run_single_pdf_preprocessing(pdf_path)
# num_pages, num_equations, num_figures, report, total_time, equation_counts_per_page, images = run_single_pdf_preprocessing(pdf_path)
# # Return results (6 items now)
# # return str(num_pages), str(num_equations), str(num_figures), report, equation_counts_per_page, []
# return str(num_pages), str(num_equations), str(num_figures), report, equation_counts_per_page, images
# except Exception as e:
# error_msg = f"An unexpected error occurred: {e}"
# logging.error(error_msg, exc_info=True)
# # Return an empty dict on error
# return "Error", "Error", "Error", error_msg, {}, []
# # ============================================================================
# # --- GRADIO INTERFACE DEFINITION (Updated) ---
# # ============================================================================
# if __name__ == "__main__":
# if not os.path.exists(WEIGHTS_PATH):
# logging.error(f"β FATAL ERROR: YOLO weight file '{WEIGHTS_PATH}' not found. Cannot run live inference.")
# input_file = gr.File(label="Upload PDF Document", type="filepath", file_types=[".pdf"])
# # Outputs
# output_pages = gr.Textbox(label="Total Pages in PDF", interactive=False)
# output_equations = gr.Textbox(label="Total Equations Detected", interactive=False)
# output_figures = gr.Textbox(label="Total Figures Detected", interactive=False)
# output_report = gr.Markdown(label="Processing Summary and Timing")
# # NEW OUTPUT: JSON component for structured data
# output_page_counts = gr.JSON(label="Equation Count Per Page (Dictionary)")
# # Gradio Gallery is retained but will receive an empty list []
# output_gallery = gr.Gallery(
# label="Detected Equations (Disabled for Speed)",
# columns=5,
# height="auto",
# object_fit="contain",
# allow_preview=False
# )
# interface = gr.Interface(
# fn=gradio_process_pdf,
# inputs=input_file,
# # Outputs list remains the same, but the JSON component now receives string keys.
# outputs=[
# output_pages,
# output_equations,
# output_figures,
# output_report,
# output_page_counts,
# output_gallery
# ],
# title="π YOLO Counting with Per-Page Data & Timing",
# description=(
# "Upload a PDF to run YOLO detection. The results include total counts, a breakdown of "
# "equation counts per page (in JSON format), and detailed timing."
# ),
# )
# print("\nStarting Gradio application...")
# # interface.launch(inbrowser=True)
# interface.launch(
# inbrowser=True,
# # allowed_paths=[OUTPUT_DIR]
# )
import base64
from PIL import Image
import re
import fitz # PyMuPDF
import numpy as np
import cv2
import torch
import torch.serialization
import os
import time
from typing import Optional, Tuple, List, Dict, Any, Union
from ultralytics import YOLO
import logging
import gradio as gr
import shutil
import tempfile
import io
# ============================================================================
# --- Global Patches and Setup ---
# ============================================================================
# Patch torch.load to prevent weights_only error with older models
_original_torch_load = torch.load
def patched_torch_load(*args, **kwargs):
kwargs["weights_only"] = False
return _original_torch_load(*args, **kwargs)
torch.load = patched_torch_load
logging.basicConfig(level=logging.WARNING)
# ============================================================================
# --- CONFIGURATION AND CONSTANTS ---
# ============================================================================
WEIGHTS_PATH = 'best.pt'
SCALE_FACTOR = 2.0
# --- OCR Model Initialization (Retained but not used in the main loop for counting) ---
from transformers import TrOCRProcessor
from optimum.onnxruntime import ORTModelForVision2Seq
MODEL_NAME = 'breezedeus/pix2text-mfr-1.5'
# Note: These models are kept global but unused in the main flow,
# as the user did not explicitly ask to remove the heavy OCR dependency yet.
try:
processor = TrOCRProcessor.from_pretrained(MODEL_NAME)
ort_model = ORTModelForVision2Seq.from_pretrained(MODEL_NAME, use_cache=False)
except Exception as e:
logging.warning(f"OCR model loading failed (expected if dependencies are missing): {e}")
processor = None
ort_model = None
# Detection parameters
CONF_THRESHOLD = 0.2
TARGET_CLASSES = ['figure', 'equation']
IOU_MERGE_THRESHOLD = 0.4
IOA_SUPPRESSION_THRESHOLD = 0.7
# --- REMOVED GLOBAL COUNTERS ---
# GLOBAL_FIGURE_COUNT = 0
# GLOBAL_EQUATION_COUNT = 0
# ============================================================================
# --- BOX COMBINATION LOGIC (Retained) ---
# ============================================================================
def calculate_iou(box1, box2):
x1_a, y1_a, x2_a, y2_a = box1
x1_b, y1_b, x2_b, y2_b = box2
x_left = max(x1_a, x1_b)
y_top = max(y1_a, y1_b)
x_right = min(x2_a, x2_b)
y_bottom = min(y2_a, y2_b)
intersection_area = max(0, x_right - x_left) * max(0, y_bottom - y_top)
box_a_area = (x2_a - x1_a) * (y2_a - y1_a)
box_b_area = (x2_b - x1_b) * (y2_b - y1_b)
union_area = float(box_a_area + box_b_area - intersection_area)
return intersection_area / union_area if union_area > 0 else 0
def filter_nested_boxes(detections, ioa_threshold=0.80):
if not detections: return []
for d in detections:
x1, y1, x2, y2 = d['coords']
d['area'] = (x2 - x1) * (y2 - y1)
detections.sort(key=lambda x: x['area'], reverse=True)
keep_indices = []
is_suppressed = [False] * len(detections)
for i in range(len(detections)):
if is_suppressed[i]: continue
keep_indices.append(i)
box_a = detections[i]['coords']
for j in range(i + 1, len(detections)):
if is_suppressed[j]: continue
box_b = detections[j]['coords']
x_left = max(box_a[0], box_b[0])
y_top = max(box_a[1], box_b[1])
x_right = min(box_a[2], box_b[2])
y_bottom = min(box_a[3], box_b[3])
intersection = max(0, x_right - x_left) * max(0, y_bottom - y_top)
area_b = detections[j]['area']
if area_b > 0 and intersection / area_b > ioa_threshold:
is_suppressed[j] = True
return [detections[i] for i in keep_indices]
def merge_overlapping_boxes(detections, iou_threshold):
if not detections: return []
detections.sort(key=lambda d: d['conf'], reverse=True)
merged_detections = []
is_merged = [False] * len(detections)
for i in range(len(detections)):
if is_merged[i]: continue
current_box = detections[i]['coords']
current_class = detections[i]['class']
merged_x1, merged_y1, merged_x2, merged_y2 = current_box
for j in range(i + 1, len(detections)):
if is_merged[j] or detections[j]['class'] != current_class: continue
other_box = detections[j]['coords']
iou = calculate_iou(current_box, other_box)
if iou > iou_threshold:
merged_x1 = min(merged_x1, other_box[0])
merged_y1 = min(merged_y1, other_box[1])
merged_x2 = max(merged_x2, other_box[2])
merged_y2 = max(other_box[3], other_box[3])
is_merged[j] = True
merged_detections.append({
'coords': (merged_x1, merged_y1, merged_x2, merged_y2),
'y1': merged_y1, 'class': current_class, 'conf': detections[i]['conf']
})
return merged_detections
# ============================================================================
# --- UTILITY FUNCTIONS ---
# ============================================================================
def pixmap_to_numpy(pix: fitz.Pixmap) -> np.ndarray:
"""Converts a PyMuPDF Pixmap to a NumPy array for OpenCV/YOLO."""
img = np.frombuffer(pix.samples, dtype=np.uint8).reshape(
(pix.h, pix.w, pix.n)
)
if pix.n == 4:
img = cv2.cvtColor(img, cv2.COLOR_RGBA2RGB)
elif pix.n == 1:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
return img
def crop_and_convert_to_base64(image: np.ndarray, bbox: Tuple[float, float, float, float]) -> str:
x1, y1, x2, y2 = map(int, bbox)
h, w, _ = image.shape
x1 = max(0, x1)
y1 = max(0, y1)
x2 = min(w, x2)
y2 = min(h, y2)
crop = image[y1:y2, x1:x2]
_, buffer = cv2.imencode(".png", crop)
return base64.b64encode(buffer).decode("utf-8")
# --- NEW: Function to format base64 for Gradio Gallery ---
def base64_to_gradio_gallery_tuple(base64_str: str, label: str) -> Tuple[str, str]:
"""Converts raw base64 to a data URI tuple for Gradio Gallery."""
# Format: ('data:image/png;base64,...', 'label')
return (f"data:image/png;base64,{base64_str}", label)
# --- UPDATED: run_yolo_detection_and_count to use passed counters ---
def run_yolo_detection_and_count(
image: np.ndarray, model: YOLO, page_num: int,
current_eq_count: int, current_fig_count: int
) -> Tuple[int, int, List[Dict[str, str]], int, int]:
"""
Performs YOLO detection and returns page counts, detected items,
and the updated global counters.
"""
# Use the passed counters as starting points for this page
eq_counter = current_eq_count
fig_counter = current_fig_count
page_equations = 0
page_figures = 0
detected_items = []
yolo_detections = []
try:
results = model.predict(image, conf=CONF_THRESHOLD, verbose=False)
if results and results[0].boxes:
for box in results[0].boxes.data.tolist():
x1, y1, x2, y2, conf, cls_id = box
cls_name = model.names[int(cls_id)]
if cls_name in TARGET_CLASSES:
yolo_detections.append({
'coords': (x1, y1, x2, y2),
'class': cls_name,
'conf': conf
})
except Exception as e:
logging.error(f"YOLO inference failed on page {page_num}: {e}")
return 0, 0, [], eq_counter, fig_counter
merged_detections = merge_overlapping_boxes(yolo_detections, IOU_MERGE_THRESHOLD)
final_detections = filter_nested_boxes(merged_detections, IOA_SUPPRESSION_THRESHOLD)
for det in final_detections:
bbox = det["coords"]
if det["class"] == "equation":
eq_counter += 1
page_equations += 1
b64 = crop_and_convert_to_base64(image, bbox)
detected_items.append({
"type": "equation",
"id": f"EQUATION{eq_counter}",
"base64": b64
})
elif det["class"] == "figure":
fig_counter += 1
page_figures += 1
b64 = crop_and_convert_to_base64(image, bbox)
detected_items.append({
"type": "figure",
"id": f"FIGURE{fig_counter}",
"base64": b64
})
logging.warning(f" -> Page {page_num}: EQs={page_equations}, Figs={page_figures}")
# Return page counts, detected items, and the UPDATED total counters
return page_equations, page_figures, detected_items, eq_counter, fig_counter
# --- Other unused functions (get_latex_from_base64, etc.) are kept but not modified as
# the focus is on the concurrency and Gradio Gallery fix. ---
def get_latex_from_base64(base64_string: str) -> str:
if ort_model is None or processor is None:
return "[MODEL_ERROR: Model not initialized]"
try:
image_data = base64.b64decode(base64_string)
image = Image.open(io.BytesIO(image_data)).convert('RGB')
pixel_values = processor(images=image, return_tensors="pt").pixel_values
generated_ids = ort_model.generate(pixel_values)
raw_text = processor.batch_decode(generated_ids, skip_special_tokens=True)
if not raw_text:
return "[OCR_WARNING: No formula found]"
latex = raw_text[0]
latex = re.sub(r'[\r\n]+', '', latex)
return latex
except Exception as e:
return f"[TR_OCR_ERROR: {e}]"
def embed_images_as_base64_in_memory(structured_data, detected_items):
tag_regex = re.compile(r'(figure|equation)(\d+)', re.IGNORECASE)
item_lookup = {d["id"]: d for d in detected_items}
final_data = []
for item in structured_data:
text_fields = [
item.get('question', ''),
item.get('passage', ''),
item.get('new_passage', '')
]
if 'options' in item:
text_fields.extend(item['options'].values())
used_tags = set()
for text in text_fields:
for m in tag_regex.finditer(text or ""):
used_tags.add(m.group(0).upper())
for tag in used_tags:
base_key = tag.lower().replace(" ", "")
if tag not in item_lookup:
item[base_key] = "[MISSING_IMAGE]"
continue
entry = item_lookup[tag]
if entry["type"] == "equation":
item[base_key] = get_latex_from_base64(entry["base64"])
else:
item[base_key] = entry["base64"]
final_data.append(item)
return final_data
# ============================================================================
# --- MAIN DOCUMENT PROCESSING FUNCTION (Fixed for concurrency) ---
# ============================================================================
# --- UPDATED return type for clarity ---
def run_single_pdf_preprocessing(
pdf_path: str
) -> Tuple[int, int, int, str, float, Dict[str, int], List[Tuple[str, str]]]:
"""
Runs the pipeline, returns counts, report, total time, page counts dict (str keys),
and a list of (image_data_uri, label) for the Gradio gallery.
"""
# --- INITIALIZE LOCAL COUNTERS ---
start_time = time.time()
log_messages = []
# This list now holds (data_uri, label) tuples for Gradio
all_gradio_gallery_items: List[Tuple[str, str]] = []
# Dictionary to store {page_number (int): equation_count (int)}
equation_counts_per_page: Dict[int, int] = {}
# --- USE LOCAL COUNTERS FOR THREAD SAFETY ---
total_figure_count = 0
total_equation_count = 0
# 1. Validation and Model Loading
t0 = time.time()
if not os.path.exists(pdf_path):
report = f"β FATAL ERROR: Input PDF not found at {pdf_path}."
# Return empty list of tuples for gallery on error
return 0, 0, 0, report, time.time() - start_time, {}, []
try:
model = YOLO(WEIGHTS_PATH)
logging.warning(f"β
Loaded YOLO model from: {WEIGHTS_PATH}")
except Exception as e:
report = f"β ERROR loading YOLO model: {e}\n(Ensure 'best.pt' is available and valid.)"
return 0, 0, 0, report, time.time() - start_time, {}, []
t1 = time.time()
log_messages.append(f"Model Loading Time: {t1-t0:.4f}s")
# 2. PDF Loading
t2 = time.time()
try:
doc = fitz.open(pdf_path)
total_pages = doc.page_count
logging.warning(f"β
Opened PDF with {doc.page_count} pages")
except Exception as e:
report = f"β ERROR loading PDF file: {e}"
return 0, 0, 0, report, time.time() - start_time, {}, []
t3 = time.time()
log_messages.append(f"PDF Initialization Time: {t3-t2:.4f}s")
mat = fitz.Matrix(SCALE_FACTOR, SCALE_FACTOR)
# 3. Page Processing and Detection Loop
t4 = time.time()
for page_num_0_based in range(doc.page_count):
page_start_time = time.time()
fitz_page = doc.load_page(page_num_0_based)
page_num = page_num_0_based + 1
# Render page to image for YOLO
try:
pix_start = time.time()
pix = fitz_page.get_pixmap(matrix=mat)
original_img = pixmap_to_numpy(pix)
pix_time = time.time() - pix_start
except Exception as e:
logging.error(f"Error converting page {page_num} to image: {e}. Skipping.")
continue
# Core Detection
detect_start = time.time()
# --- PASSING AND RECEIVING THE COUNTERS HERE (Concurrency Fix) ---
(
page_equations,
page_figures,
page_images_dicts,
total_equation_count,
total_figure_count
) = run_yolo_detection_and_count(
original_img,
model,
page_num,
total_equation_count,
total_figure_count
)
# --- FORMATTING FOR GRADIO GALLERY (Gradio Format Fix) ---
for item in page_images_dicts:
gradio_tuple = base64_to_gradio_gallery_tuple(item["base64"], item["id"])
all_gradio_gallery_items.append(gradio_tuple)
detect_time = time.time() - detect_start
# Store the count in the dictionary (INT keys)
equation_counts_per_page[page_num] = page_equations
page_total_time = time.time() - page_start_time
log_messages.append(f"Page {page_num} Time: Total={page_total_time:.4f}s (Render={pix_time:.4f}s, Detect={detect_time:.4f}s)")
doc.close()
t5 = time.time()
detection_loop_time = t5 - t4
log_messages.append(f"Total Detection Loop Time ({total_pages} pages): {detection_loop_time:.4f}s")
# Convert integer keys to string keys for JSON serialization
equation_counts_per_page_str_keys: Dict[str, int] = {
str(k): v for k, v in equation_counts_per_page.items()
}
# 4. Final Report Generation
total_execution_time = t5 - start_time
report = (
f"β
**YOLO Counting Complete!**\n\n"
f"**1) Total Pages Detected in PDF:** **{total_pages}**\n"
f"**2) Total Equations Detected:** **{total_equation_count}**\n" # Uses local final count
f"**3) Total Figures Detected:** **{total_figure_count}**\n" # Uses local final count
f"---\n"
f"**4) Total Execution Time:** **{total_execution_time:.4f}s**\n"
f"### Detailed Step Timing\n"
f"```\n"
+ "\n".join(log_messages) +
f"\n```"
)
# Return the dictionary with string keys and the properly formatted gallery items
return total_pages, total_equation_count, total_figure_count, report, total_execution_time, equation_counts_per_page_str_keys, all_gradio_gallery_items
# ============================================================================
# --- GRADIO INTERFACE FUNCTION (Updated) ---
# ============================================================================
# --- UPDATED return type for clarity ---
def gradio_process_pdf(pdf_file) -> Tuple[str, str, str, str, Dict[str, int], List[Tuple[str, str]]]:
"""
Gradio wrapper function to handle file upload and return results.
"""
if pdf_file is None:
# Return empty list of tuples for gallery on error
return "N/A", "N/A", "N/A", "Please upload a PDF file.", {}, []
pdf_path = pdf_file.name
try:
# Unpack the new return value: equation_counts_per_page (with string keys)
(
num_pages,
num_equations,
num_figures,
report,
total_time,
equation_counts_per_page,
gallery_items # Now correctly formatted list of tuples
) = run_single_pdf_preprocessing(pdf_path)
# Return results (6 items now)
return str(num_pages), str(num_equations), str(num_figures), report, equation_counts_per_page, gallery_items
except Exception as e:
error_msg = f"An unexpected error occurred: {e}"
logging.error(error_msg, exc_info=True)
# Return empty list of tuples for gallery on error
return "Error", "Error", "Error", error_msg, {}, []
# ============================================================================
# --- GRADIO INTERFACE DEFINITION (Updated) ---
# ============================================================================
if __name__ == "__main__":
if not os.path.exists(WEIGHTS_PATH):
logging.error(f"β FATAL ERROR: YOLO weight file '{WEIGHTS_PATH}' not found. Cannot run live inference.")
input_file = gr.File(label="Upload PDF Document", type="filepath", file_types=[".pdf"])
# Outputs
output_pages = gr.Textbox(label="Total Pages in PDF", interactive=False)
output_equations = gr.Textbox(label="Total Equations Detected", interactive=False)
output_figures = gr.Textbox(label="Total Figures Detected", interactive=False)
output_report = gr.Markdown(label="Processing Summary and Timing")
# NEW OUTPUT: JSON component for structured data
output_page_counts = gr.JSON(label="Equation Count Per Page (Dictionary)")
# Gradio Gallery is retained and now receives the correctly formatted list of tuples
output_gallery = gr.Gallery(
label="Detected Items (Gallery Format Fix Applied)",
columns=5,
height="auto",
object_fit="contain",
allow_preview=False
)
interface = gr.Interface(
fn=gradio_process_pdf,
inputs=input_file,
# Outputs list remains the same, but the gallery now works
outputs=[
output_pages,
output_equations,
output_figures,
output_report,
output_page_counts,
output_gallery
],
title="π YOLO Counting with Per-Page Data & Timing (Concurrency Fix)",
description=(
"Upload a PDF to run YOLO detection. The concurrency bug and Gradio Gallery display error have been fixed."
),
)
print("\nStarting Gradio application...")
interface.launch(inbrowser=True)
|