code: def replace_equations(
      doc,
      blocks: List[Page],
      block_types: List[List[BlockType]],
      texify_model,
      batch_size=settings.TEXIFY_BATCH_SIZE,
  ):
      unsuccessful_ocr = 0
      successful_ocr = 0
  
      # Find potential equation regions, and length of text in each region
      reformat_regions = []
      reformat_region_lens = []
      for pnum, page in enumerate(blocks):
          regions, region_lens = find_page_equation_regions(
              pnum, page, block_types
          )
          reformat_regions.append(regions)
          reformat_region_lens.append(region_lens)
  
      eq_count = sum([len(x) for x in reformat_regions])
  
      # Get images for each region
      flat_reformat_region_lens = [
          item for sublist in reformat_region_lens for item in sublist
      ]
      images = []
      merged_boxes = []
      for page_idx, reformat_regions_page in enumerate(
          reformat_regions
      ):
          page_obj = doc[page_idx]
          for reformat_region in reformat_regions_page:
              bboxes, merged_box = get_bboxes_for_region(
                  blocks[page_idx], reformat_region
              )
              png_image = get_masked_image(page_obj, merged_box, bboxes)
              images.append(png_image)
              merged_boxes.append(merged_box)
  
      # Make batched predictions
      predictions = get_latex_batched(
          images, flat_reformat_region_lens, texify_model, batch_size
      )
  
      # Replace blocks with predictions
      page_start = 0
      converted_spans = []
      for page_idx, reformat_regions_page in enumerate(
          reformat_regions
      ):
          page_predictions = predictions[
              page_start : page_start + len(reformat_regions_page)
          ]
          page_boxes = merged_boxes[
              page_start : page_start + len(reformat_regions_page)
          ]
          (
              new_page_blocks,
              success_count,
              fail_count,
              converted_span,
          ) = replace_blocks_with_latex(
              blocks[page_idx],
              page_boxes,
              reformat_regions_page,
              page_predictions,
              page_idx,
          )
          converted_spans.extend(converted_span)
          blocks[page_idx].blocks = new_page_blocks
          page_start += len(reformat_regions_page)
          successful_ocr += success_count
          unsuccessful_ocr += fail_count
  
      # If debug mode is on, dump out conversions for comparison
      dump_equation_debug_data(doc, images, converted_spans)
  
      return blocks, {
          "successful_ocr": successful_ocr,
          "unsuccessful_ocr": unsuccessful_ocr,
          "equations": eq_count,
      }
  
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 9 for calling a global function with example usage: replace_equations(doc, blocks, block_types, texify_model, batch_size) and returns: blocks,  dict