code: def space_layout(
      texts, boxes, threshold_show_spaces=8, threshold_char_width=0.02
  ):
      line_boxes = []
      line_texts = []
      max_line_char_num = 0
      line_width = 0
      # print(f"len_boxes: {len(boxes)}")
      boxes = np.array(boxes)
      texts = np.array(texts)
      while len(boxes) > 0:
          box = boxes[0]
          mid = (boxes[:, 3] + boxes[:, 1]) / 2
          inline_boxes = np.logical_and(mid > box[1], mid < box[3])
          sorted_xs = np.argsort(boxes[inline_boxes][:, 0], axis=0)
          line_box = boxes[inline_boxes][sorted_xs]
          line_text = texts[inline_boxes][sorted_xs]
          boxes = boxes[~inline_boxes]
          texts = texts[~inline_boxes]
  
          line_boxes.append(line_box.tolist())
          line_texts.append(line_text.tolist())
          if len(" ".join(line_texts[-1])) > max_line_char_num:
              max_line_char_num = len(" ".join(line_texts[-1]))
              line_width = np.array(line_boxes[-1])
              line_width = (
                  line_width[:, 2].max() - line_width[:, 0].min()
              )
  
      char_width = (
          (line_width / max_line_char_num)
          if max_line_char_num > 0
          else 0
      )
      if threshold_char_width == 0.0:
          if char_width == 0:
              char_width = 1
      else:
          if char_width <= 0.02:
              char_width = 0.02
  
      space_line_texts = []
      for i, line_box in enumerate(line_boxes):
          space_line_text = ""
          for j, box in enumerate(line_box):
              left_char_num = int(box[0] / char_width)
              left_char_num = max(
                  (left_char_num - len(space_line_text)), 1
              )
  
              # verbose layout
              # space_line_text += " " * left_char_num
  
              # minified layout
              if left_char_num > threshold_show_spaces:
                  space_line_text += f" <{left_char_num}> "
              else:
                  space_line_text += " "
  
              space_line_text += line_texts[i][j]
          space_line_texts.append(space_line_text + "\n")
  
      return "".join(space_line_texts)
  
imports: from typing import List, Union, Any, Tuple, Optional
  import requests
  import torch
  from langchain.docstore.document import Document
  from langchain.document_loaders import ImageCaptionLoader
  import numpy
  from utils import get_device, clear_torch_cache, NullContext
  from doctr.utils.common_types import AbstractFile
label_desc: Use image node_id 4 for calling a global function with example usage: space_layout(texts, boxes, threshold_show_spaces, threshold_char_width) and returns: str