code: def instantiate_model_and_trainer(
      cfg: DictConfig, model_name: str, do_training: bool
  ) -> Tuple[pl.Trainer, ThutmoseTaggerModel]:
      """Function for instantiating a model and a trainer
      Args:
          cfg: The config used to instantiate the model and the trainer.
          model_name: A str indicates the model direction, currently only 'itn'.
          do_training: A boolean flag indicates whether the model will be trained or evaluated.
  
      Returns:
          trainer: A PyTorch Lightning trainer
          model: A ThutmoseTaggerModel
      """
  
      if model_name not in MODEL_NAMES:
          raise ValueError(f"{model_name} is unknown model type")
  
      # Get configs for the corresponding models
      trainer_cfg = cfg.get("trainer")
      model_cfg = cfg.get("model")
      pretrained_cfg = cfg.get("pretrained_model", None)
      trainer = pl.Trainer(**trainer_cfg)
      if not pretrained_cfg:
          logging.info(f"Initializing {model_name} model")
          if model_name == ITN_MODEL:
              model = ThutmoseTaggerModel(model_cfg, trainer=trainer)
          else:
              raise ValueError(f"{model_name} is unknown model type")
      elif os.path.exists(pretrained_cfg):
          logging.info(
              f"Restoring pretrained {model_name} model from {pretrained_cfg}"
          )
          model = ThutmoseTaggerModel.restore_from(pretrained_cfg)
      else:
          logging.info(f"Loading pretrained model {pretrained_cfg}")
          if model_name == ITN_MODEL:
              if (
                  pretrained_cfg
                  not in ThutmoseTaggerModel.get_available_model_names()
              ):
                  raise (
                      ValueError(
                          f"{pretrained_cfg} not in the list of available Tagger models."
                          f"Select from {ThutmoseTaggerModel.list_available_models()}"
                      )
                  )
              model = ThutmoseTaggerModel.from_pretrained(
                  pretrained_cfg
              )
          else:
              raise ValueError(f"{model_name} is unknown model type")
  
      # Setup train and validation data
      if do_training:
          model.setup_training_data(train_data_config=cfg.data.train_ds)
          model.setup_validation_data(
              val_data_config=cfg.data.validation_ds
          )
  
      logging.info(f"Model {model_name} -- Device {model.device}")
      return trainer, model
  
imports: import os
  from typing import Tuple
  import pytorch_lightning
  from omegaconf import DictConfig
  from nemo.collections.nlp.models import ThutmoseTaggerModel
  from nemo.utils import logging
label_desc: Use image node_id 1 for calling a global function with example usage: instantiate_model_and_trainer(cfg, model_name, do_training) and returns: trainer,  model