code: def make_input_fn_from_generator(gen):
      """Use py_func to yield elements from the given generator."""
      first_ex = six.next(gen)
      flattened = contrib.framework().nest.flatten(first_ex)
      types = [t.dtype for t in flattened]
      shapes = [[None] * len(t.shape) for t in flattened]
      first_ex_list = [first_ex]
  
      def py_func():
          if first_ex_list:
              example = first_ex_list.pop()
          else:
              example = six.next(gen)
          return contrib.framework().nest.flatten(example)
  
      def input_fn():
          flat_example = tf.py_func(py_func, [], types)
          _ = [
              t.set_shape(shape)
              for t, shape in zip(flat_example, shapes)
          ]
          example = contrib.framework().nest.pack_sequence_as(
              first_ex, flat_example
          )
          return example
  
      return input_fn
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import collections
  import operator
  import os
  import re
  import string
  import time
  import numpy
  import six
  from six.moves import input
  from tensor2tensor.data_generators import problem
  from tensor2tensor.data_generators import text_encoder
  from tensor2tensor.data_generators import text_problems
  from tensor2tensor.utils import contrib
  from tensor2tensor.utils import hparam
  from tensor2tensor.utils import mlperf_log
  from tensor2tensor.utils import registry
  import tensorflow.compat.v1
  from tensorflow.compat.v1 import estimator
label_desc: Use image node_id 8 for calling a global function with example usage: make_input_fn_from_generator(gen) and returns: input_fn, contrib, example