@@ -886,6 +886,15 @@ def _close_images(images_list):
886886 pass
887887
888888
889+ def _emit_progress (progress_callback , ** payload ):
890+ if progress_callback is None :
891+ return
892+ try :
893+ progress_callback (** payload )
894+ except Exception :
895+ logger .exception ("Progress callback failed" )
896+
897+
889898def doc_analyze (
890899 pdf_bytes ,
891900 image_writer : DataWriter | None ,
@@ -901,6 +910,7 @@ def doc_analyze(
901910):
902911 effort = _validate_parse_effort (effort )
903912 effective_image_analysis = _resolve_effective_image_analysis (effort , image_analysis )
913+ progress_callback = kwargs .pop ("progress_callback" , None )
904914 client_side_output_generation = bool (
905915 kwargs .pop ("client_side_output_generation" , False )
906916 )
@@ -932,6 +942,14 @@ def doc_analyze(
932942 f'Hybrid processing-window run. page_count={ page_count } , '
933943 f'window_size={ configured_window_size } , total_windows={ total_windows } '
934944 )
945+ _emit_progress (
946+ progress_callback ,
947+ page_total = page_count ,
948+ page_current = 0 ,
949+ window_index = 0 ,
950+ window_total = total_windows ,
951+ text = f"0/{ page_count } pages" ,
952+ )
935953
936954 batch_ratio = get_batch_ratio (device ) if not _ocr_enable else 1
937955
@@ -956,6 +974,16 @@ def doc_analyze(
956974 f'pages { window_start + 1 } -{ window_end + 1 } /{ page_count } '
957975 f'({ len (images_pil_list )} pages)'
958976 )
977+ _emit_progress (
978+ progress_callback ,
979+ page_total = page_count ,
980+ page_current = window_start ,
981+ window_index = window_index + 1 ,
982+ window_total = total_windows ,
983+ window_start = window_start + 1 ,
984+ window_end = window_end + 1 ,
985+ text = f"pages { window_start + 1 } -{ window_end + 1 } /{ page_count } " ,
986+ )
959987 images_layout_res , hybrid_pipeline_model = _predict_layout_for_window (
960988 images_pil_list ,
961989 inline_formula_enable ,
@@ -1058,6 +1086,16 @@ def doc_analyze(
10581086 _ocr_enable = _ocr_enable ,
10591087 progress_bar = progress_bar ,
10601088 )
1089+ _emit_progress (
1090+ progress_callback ,
1091+ page_total = page_count ,
1092+ page_current = window_end + 1 ,
1093+ window_index = window_index + 1 ,
1094+ window_total = total_windows ,
1095+ window_start = window_start + 1 ,
1096+ window_end = window_end + 1 ,
1097+ text = f"pages { window_start + 1 } -{ window_end + 1 } /{ page_count } " ,
1098+ )
10611099 last_append_end_time = time .time ()
10621100 finally :
10631101 _close_images (images_list )
@@ -1109,6 +1147,7 @@ async def aio_doc_analyze(
11091147):
11101148 effort = _validate_parse_effort (effort )
11111149 effective_image_analysis = _resolve_effective_image_analysis (effort , image_analysis )
1150+ progress_callback = kwargs .pop ("progress_callback" , None )
11121151 client_side_output_generation = bool (
11131152 kwargs .pop ("client_side_output_generation" , False )
11141153 )
@@ -1140,6 +1179,14 @@ async def aio_doc_analyze(
11401179 f'Hybrid processing-window run. page_count={ page_count } , '
11411180 f'window_size={ configured_window_size } , total_windows={ total_windows } '
11421181 )
1182+ _emit_progress (
1183+ progress_callback ,
1184+ page_total = page_count ,
1185+ page_current = 0 ,
1186+ window_index = 0 ,
1187+ window_total = total_windows ,
1188+ text = f"0/{ page_count } pages" ,
1189+ )
11431190
11441191 batch_ratio = get_batch_ratio (device ) if not _ocr_enable else 1
11451192
@@ -1163,6 +1210,16 @@ async def aio_doc_analyze(
11631210 f'pages { window_start + 1 } -{ window_end + 1 } /{ page_count } '
11641211 f'({ len (images_pil_list )} pages)'
11651212 )
1213+ _emit_progress (
1214+ progress_callback ,
1215+ page_total = page_count ,
1216+ page_current = window_start ,
1217+ window_index = window_index + 1 ,
1218+ window_total = total_windows ,
1219+ window_start = window_start + 1 ,
1220+ window_end = window_end + 1 ,
1221+ text = f"pages { window_start + 1 } -{ window_end + 1 } /{ page_count } " ,
1222+ )
11661223 images_layout_res , hybrid_pipeline_model = await asyncio .to_thread (
11671224 _predict_layout_for_window ,
11681225 images_pil_list ,
@@ -1272,6 +1329,16 @@ async def aio_doc_analyze(
12721329 _ocr_enable = _ocr_enable ,
12731330 progress_bar = progress_bar ,
12741331 )
1332+ _emit_progress (
1333+ progress_callback ,
1334+ page_total = page_count ,
1335+ page_current = window_end + 1 ,
1336+ window_index = window_index + 1 ,
1337+ window_total = total_windows ,
1338+ window_start = window_start + 1 ,
1339+ window_end = window_end + 1 ,
1340+ text = f"pages { window_start + 1 } -{ window_end + 1 } /{ page_count } " ,
1341+ )
12751342 last_append_end_time = time .time ()
12761343 finally :
12771344 _close_images (images_list )
0 commit comments