code: def restructure_output(
      output: Tuple[Union[torch.Tensor, tf.Tensor]],
      structure: OrderedDict,
      output_type: Any = None,
  ):
      """Restructure the flatter output using the structure dictionary given as
      input.
      """
      output_dict = {}
      idx = 0
      for key, value in structure.items():
          if value is None:
              output_dict[key] = output[idx]
              idx += 1
          else:
              tensor_shape = output[idx].shape[1:]
              stack_fn = (
                  torch.stack
                  if isinstance(output[idx], torch.Tensor)
                  else tf.stack
              )
              reshape_fn = (
                  torch.reshape
                  if isinstance(output[idx], torch.Tensor)
                  else tf.reshape
              )
  
              output_dict[key] = list(
                  reshape_fn(
                      stack_fn(
                          output[
                              idx : int(np.prod(value)) + idx
                          ]  # noqa E203
                      ),
                      (*value, *tensor_shape),
                  )
              )
              idx += np.prod(value)
      if output_type is not None:
          return output_type(**output_dict)
      return output_dict
  
file_path: nebuly/optimization/nebullvm/nebullvm/tools/huggingface.py
imports: from collections import OrderedDict
  from typing import Union, Iterable, List, Dict, Tuple, Type, Any
  import numpy
  from nebullvm.core.models import Device, DeviceType
  from nebullvm.optional_modules.tensorflow import tensorflow
  from nebullvm.optional_modules.torch import torch, Module
label_desc: Use image node_id 5 for calling a global function with example usage: restructure_output(output, structure, output_type) and returns: output_dict, output_type