code: def type_spec_from_value(element, use_fallback=True):
      """Creates a type specification for the given value.
  
      Args:
        element: The element to create the type specification for.
        use_fallback: Whether to fall back to converting the element to a tensor
          in order to compute its `TypeSpec`.
  
      Returns:
        A nested structure of `TypeSpec`s that represents the type specification
        of `element`.
  
      Raises:
        TypeError: If a `TypeSpec` cannot be built for `element`, because its type
          is not supported.
      """
      spec = type_spec._type_spec_from_value(
          element
      )  # pylint: disable=protected-access
      if spec is not None:
          return spec
  
      if isinstance(element, collections_abc.Mapping):
          # We create a shallow copy in an attempt to preserve the key order.
          #
          # Note that we do not guarantee that the key order is preserved, which is
          # a limitation inherited from `copy()`. As a consequence, callers of
          # `type_spec_from_value` should not assume that the key order of a `dict`
          # in the returned nested structure matches the key order of the
          # corresponding `dict` in the input value.
          if isinstance(element, collections.defaultdict):
              ctor = lambda items: type(element)(
                  element.default_factory, items
              )
          else:
              ctor = type(element)
          return ctor(
              [(k, type_spec_from_value(v)) for k, v in element.items()]
          )
  
      if isinstance(element, tuple):
          if (
              hasattr(element, "_fields")
              and isinstance(element._fields, collections_abc.Sequence)
              and all(isinstance(f, str) for f in element._fields)
          ):
              if isinstance(element, wrapt.ObjectProxy):
                  element_type = type(element.__wrapped__)
              else:
                  element_type = type(element)
              # `element` is a namedtuple
              return element_type(
                  *[type_spec_from_value(v) for v in element]
              )
          # `element` is not a namedtuple
          return tuple([type_spec_from_value(v) for v in element])
  
      if hasattr(element.__class__, "__attrs_attrs__"):
          # `element` is an `attr.s` decorated class
          attrs = getattr(element.__class__, "__attrs_attrs__")
          return type(element)(
              *[
                  type_spec_from_value(getattr(element, a.name))
                  for a in attrs
              ]
          )
  
      if isinstance(element, CustomNestProtocol):
          # pylint: disable=protected-access
          metadata, children = element.__tf_flatten__()
          return element.__tf_unflatten__(
              metadata, type_spec_from_value(children)
          )
          # pylint: enable=protected-access
  
      if use_fallback:
          # As a fallback try converting the element to a tensor.
          try:
              tensor = ops.convert_to_tensor(element)
              spec = type_spec_from_value(tensor)
              if spec is not None:
                  return spec
          except (ValueError, TypeError) as e:
              logging.vlog(
                  3,
                  "Failed to convert %r to tensor: %s"
                  % (type(element).__name__, e),
              )
  
      raise TypeError(
          "Could not build a `TypeSpec` for {} with type {}".format(
              element, type(element).__name__
          )
      )
  
imports: import collections
  import functools
  import itertools
  import wrapt
  from tensorflow.python.data.util import nest
  from tensorflow.python.framework import composite_tensor
  from tensorflow.python.framework import none_tensor
  from tensorflow.python.framework import ops
  from tensorflow.python.framework import sparse_tensor
  from tensorflow.python.framework import tensor
  from tensorflow.python.framework import tensor_shape
  from tensorflow.python.framework import type_spec
  from tensorflow.python.ops import resource_variable_ops
  from tensorflow.python.ops import tensor_array_ops
  from tensorflow.python.ops.ragged import ragged_tensor
  from tensorflow.python.platform import tf_logging
  from tensorflow.python.types import internal
  from tensorflow.python.util import deprecation
  from tensorflow.python.util.compat import collections_abc
  from tensorflow.python.util.nest_util import CustomNestProtocol
  from tensorflow.python.util.tf_export import tf_export
label_desc: Use image node_id 17 for calling a global function with example usage: type_spec_from_value(element, use_fallback) and returns: spec, ctor, tuple, type, element, element_type, spec