code: def replace_blocks_with_latex(
      page_blocks: Page,
      merged_boxes,
      reformat_regions,
      predictions,
      pnum,
  ):
      new_blocks = []
      converted_spans = []
      current_region = 0
      idx = 0
      success_count = 0
      fail_count = 0
      while idx < len(page_blocks.blocks):
          block = page_blocks.blocks[idx]
          if (
              current_region >= len(reformat_regions)
              or idx < reformat_regions[current_region][0]
          ):
              new_blocks.append(block)
              idx += 1
              continue
  
          orig_block_text = " ".join(
              [
                  page_blocks.blocks[i].prelim_text
                  for i in reformat_regions[current_region]
              ]
          )
          latex_text = predictions[current_region]
          conditions = [
              len(latex_text) > 0,
              get_total_texify_tokens(latex_text)
              < settings.TEXIFY_MODEL_MAX,  # Make sure we didn't run to the overall token max
              len(latex_text) > len(orig_block_text) * 0.8,
              len(latex_text.strip()) > 0,
          ]
  
          idx = reformat_regions[current_region][-1] + 1
          if not all(conditions):
              fail_count += 1
              converted_spans.append(None)
              for i in reformat_regions[current_region]:
                  new_blocks.append(page_blocks.blocks[i])
          else:
              success_count += 1
              block_line = Line(
                  spans=[
                      Span(
                          text=latex_text,
                          bbox=merged_boxes[current_region],
                          span_id=f"{pnum}_{idx}_fixeq",
                          font="Latex",
                          color=0,
                          block_type="Formula",
                      )
                  ],
                  bbox=merged_boxes[current_region],
              )
              converted_spans.append(deepcopy(block_line.spans[0]))
              new_blocks.append(
                  Block(
                      lines=[block_line],
                      bbox=merged_boxes[current_region],
                      pnum=pnum,
                  )
              )
          current_region += 1
      return new_blocks, success_count, fail_count, converted_spans
  
imports: import io
  from copy import deepcopy
  from functools import partial
  from typing import List
  import torch
  from texify.inference import batch_inference
  from texify.model.model import load_model
  from texify.model.processor import load_processor
  import re
  from PIL import Image, ImageDraw
  from marker.bbox import should_merge_blocks, merge_boxes
  from marker.debug.data import dump_equation_debug_data
  from marker.settings import settings
  from marker.schema import Page, Span, Line, Block, BlockType
  import os
label_desc: Use image node_id 8 for calling a global function with example usage: replace_blocks_with_latex(page_blocks, merged_boxes, reformat_regions, predictions, pnum) and returns: new_blocks,  success_count,  fail_count,  converted_spans