code: def run(args: PredictArgs):
      # check if loading a json file
      # TODO: support running from JSON
      # if len(args.files) == 1 and args.files[0].endswith("json"):
      #     import json
      #     with open(args.files[0], 'r') as f:
      #        json_args = json.load(f)
      #        for key, value in json_args.items():
      #            setattr(args, key, value)
  
      # checks
      if args.extended_prediction_data_format not in ["pred", "json"]:
          raise Exception(
              "Only 'pred' and 'json' are allowed extended prediction data formats"
          )
  
      # add json as extension, resolve wildcard, expand user, ... and remove .json again
      args.checkpoint = [
          (cp if cp.endswith(".json") else cp + ".json")
          for cp in args.checkpoint
      ]
      args.checkpoint = glob_all(args.checkpoint)
      args.checkpoint = [cp[:-5] for cp in args.checkpoint]
  
      # create ctc decoder
      prepare_ctc_decoder_params(args.ctc_decoder)
  
      # predict for all models
      from calamari_ocr.ocr.predict.predictor import MultiPredictor
  
      predictor = MultiPredictor.from_paths(
          checkpoints=args.checkpoint,
          voter_params=args.voter,
          predictor_params=args.predictor,
      )
      do_prediction = predictor.predict(args.data)
      pipeline: CalamariPipeline = (
          predictor.data.get_or_create_pipeline(
              predictor.params.pipeline, args.data
          )
      )
      reader = pipeline.reader()
      if len(reader) == 0:
          raise Exception(
              "Empty dataset provided. Check your command line arguments or if the provided files are empty."
          )
  
      avg_sentence_confidence = 0
      n_predictions = 0
  
      reader.prepare_store()
  
      # output the voted results to the appropriate files
      for s in do_prediction:
          _, (result, prediction), meta = s.inputs, s.outputs, s.meta
          sample = reader.sample_by_id(meta["id"])
          n_predictions += 1
          sentence = prediction.sentence
  
          avg_sentence_confidence += prediction.avg_char_probability
          if args.verbose:
              lr = "\u202A\u202B"
              logger.info(
                  "{}: '{}{}{}'".format(
                      meta["id"],
                      lr[get_base_level(sentence)],
                      sentence,
                      "\u202C",
                  )
              )
  
          output_dir = (
              args.output_dir
              if args.output_dir
              else os.path.dirname(prediction.line_path)
          )
  
          reader.store_text_prediction(
              prediction, meta["id"], output_dir=output_dir
          )
  
          if args.extended_prediction_data:
              ps = Predictions()
              ps.line_path = (
                  sample["image_path"]
                  if "image_path" in sample
                  else sample["id"]
              )
              ps.predictions.extend(
                  [prediction] + [r.prediction for r in result]
              )
              output_dir = (
                  output_dir
                  if output_dir
                  else os.path.dirname(ps.line_path)
              )
              if not os.path.exists(output_dir):
                  os.mkdir(output_dir)
  
              if args.extended_prediction_data_format == "pred":
                  data = zlib.compress(
                      ps.to_json(indent=2, ensure_ascii=False).encode(
                          "utf-8"
                      )
                  )
              elif args.extended_prediction_data_format == "json":
                  # remove logits
                  for p in ps.predictions:
                      p.logits = None
  
                  data = ps.to_json(indent=2)
              else:
                  raise Exception("Unknown prediction format.")
  
              reader.store_extended_prediction(
                  data,
                  sample,
                  output_dir=output_dir,
                  extension=args.extended_prediction_data_format,
              )
  
      avg_sentence_confidence = (
          avg_sentence_confidence / n_predictions
          if n_predictions
          else 0
      )
      logger.info(
          "Average sentence confidence: {:.2%}".format(
              avg_sentence_confidence
          )
      )
  
      reader.store()
      logger.info("All prediction files written")
  
file_path: calamari/calamari_ocr/scripts/predict.py
imports: import os
  import zlib
  from dataclasses import dataclass, field
  from typing import TYPE_CHECKING, List, Optional
  import tfaip.util.logging
  from bidi.algorithm import get_base_level
  from paiargparse import PAIArgumentParser, pai_meta, pai_dataclass
  from calamari_ocr import __version__
  from calamari_ocr.ocr.dataset.datareader.base import CalamariDataGeneratorParams
  from calamari_ocr.ocr.dataset.datareader.file import FileDataParams
  from calamari_ocr.ocr.dataset.params import DATA_GENERATOR_CHOICES
  from calamari_ocr.ocr.model.ctcdecoder.ctc_decoder import CTCDecoderParams, CTCDecoderType
  from calamari_ocr.ocr.predict.params import Predictions, PredictorParams
  from calamari_ocr.ocr.voting import VoterParams
  from calamari_ocr.utils.glob import glob_all
label_desc: Use image node_id 2 for calling a global function with example usage: run(args) without return types