code: def dict_to_example(instance: Dict[str, Any]) -> example_pb2.Example:
      """Converts dict to tf example."""
      feature = {}
      for key, value in instance.items():
          # TODO(jyzhao): support more types.
          # Convert to Python Scalars
          if isinstance(value, np.ndarray):
              pyval = value.tolist()
          else:
              try:
                  pyval = value.item()
              except AttributeError:
                  pyval = value
  
          # Convert bytes to str
          if isinstance(pyval, bytes):
              pyval = pyval.decode(_DEFAULT_ENCODING)
          if not pyval:
              feature[key] = feature_pb2.Feature()
          elif isinstance(pyval, (int, str, float)):
              feature[key] = pyval_to_feature([pyval])
          elif isinstance(pyval, list):
              feature[key] = pyval_to_feature(pyval)
          else:
              raise RuntimeError(
                  f"Value type {type(value[0])} is not supported."
              )
  
      return example_pb2.Example(
          features=feature_pb2.Features(feature=feature)
      )
  
imports: import datetime
  import os
  import re
  from typing import Any, Dict, Iterable, List, Optional, Tuple, Union
  from absl import logging
  import numpy
  from tfx.dsl.io import fileio
  from tfx.proto import example_gen_pb2
  from tfx.proto import range_config_pb2
  from tfx.utils import io_utils
  from google.protobuf import json_format
  from tensorflow.core.example import example_pb2
  from tensorflow.core.example import feature_pb2
label_desc: Use image node_id 2 for calling a global function with example usage: dict_to_example(instance) and returns: example_pb2