code: def from_value(
      value: Any, context: trace.TracingContext = None
  ) -> trace.TraceType:
      """Returns a TraceType corresponding to the value based on the context.
  
      Args:
        value: The value to generate a TraceType for.
        context: The TracingContext to be shared during protocol calls.
  
      Returns:
        A TraceType object representing the given value.
      """
  
      if context is None:
          context = InternalTracingContext()
  
      if context.is_legacy_signature and isinstance(
          value, trace.TraceType
      ):
          return value
      elif isinstance(value, trace.SupportsTracingProtocol):
          generated_type = value.__tf_tracing_type__(context)
          if not isinstance(generated_type, trace.TraceType):
              raise TypeError(
                  "Expected an instance of TraceType for Tracing Protocol call to "
                  + str(value)
                  + " but got "
                  + str(generated_type)
              )
          return generated_type
  
      # TODO(b/183107079): Allow these once they're handled properly.
      if isinstance(value, weakref.ref):
          raise TypeError(
              f"weakref input {value} not supported for tf.function."
          )
  
      if hasattr(value, "__wrapped__"):
          return from_value(value.__wrapped__, context)
  
      if isinstance(value, list):
          return default_types.List(
              *(from_value(c, context) for c in value)
          )
  
      if isinstance(value, tuple):
          if util.is_namedtuple(value):
              named_tuple_type = type(value)
              return default_types.NamedTuple.from_type_and_attributes(
                  named_tuple_type,
                  tuple(from_value(c, context) for c in value),
              )
          else:
              return default_types.Tuple(
                  *(from_value(c, context) for c in value)
              )
  
      if isinstance(value, collections.abc.Mapping):
          mapping_type = type(value)
          return default_types.Dict(
              {k: from_value(value[k], context) for k in value},
              mapping_type,
          )
  
      if util.is_attrs(value):
          return default_types.Attrs.from_type_and_attributes(
              type(value),
              tuple(
                  from_value(getattr(value, a.name), context)
                  for a in value.__attrs_attrs__
              ),
          )
  
      if util.is_np_ndarray(value):
          ndarray = value.__array__()
          return default_types.TENSOR(ndarray.shape, ndarray.dtype)
  
      if isinstance(value, custom_nest_protocol.CustomNestProtocol):
          metadata, components = value.__tf_flatten__()
          return custom_nest_trace_type.CustomNestTraceType(
              type(value),
              metadata,
              tuple(from_value(c, context) for c in components),
          )
  
      try:
          ref = weakref.ref(value)
          if ref is None:
              raise TypeError(
                  f"Deleted objects are not valid tf.function arguments, Got {value!r}"
              )
          else:
              return default_types.Weakref(ref)
      except TypeError:
          try:
              return default_types.Literal(value)
          except:
              raise TypeError(  # pylint: disable=raise-missing-from
                  f"Could not generate a generic TraceType for {value!r}."
                  f"Please verify that it is immutable/hashable. Otheriwse, consider "
                  f"implementing the Tracing Protocol for it."
              )
  
imports: import collections.abc
  from typing import Any, Dict, Hashable, Optional
  import weakref
  from tensorflow.core.function.trace_type import custom_nest_trace_type
  from tensorflow.core.function.trace_type import default_types
  from tensorflow.core.function.trace_type import util
  from tensorflow.python.types import trace
  from tensorflow.python.util import custom_nest_protocol
label_desc: Use image node_id 1 for calling a global function with example usage: from_value(value, context) and returns: value, from_value, default_types, default_types, default_types, default_types, custom_nest_trace_type, generated_type, default_types, default_types, default_types, default_types