code: def model_from_fgraph(fgraph: FunctionGraph) -> Model:
      """Convert FunctionGraph to PyMC model.
  
      This requires nodes to be properly tagged with `ModelVar` dummy Ops.
  
      See: fgraph_from_model
      """
  
      def first_non_model_var(var):
          if var.owner and isinstance(var.owner.op, ModelVar):
              new_var = var.owner.inputs[0]
              return first_non_model_var(new_var)
          else:
              return var
  
      model = Model()
      if model.parent is not None:
          raise RuntimeError(
              "model_to_fgraph cannot be called inside a PyMC model context"
          )
      model._coords = getattr(fgraph, "_coords", {})
      model._dim_lengths = getattr(fgraph, "_dim_lengths", {})
  
      # Replace dummy `ModelVar` Ops by the underlying variables,
      fgraph = fgraph.clone()
      model_dummy_vars = [
          model_node.outputs[0]
          for model_node in fgraph.toposort()
          if isinstance(model_node.op, ModelVar)
      ]
      model_dummy_vars_to_vars = {
          # Deterministics could refer to other model variables directly,
          # We make sure to replace them by the first non-model variable
          dummy_var: first_non_model_var(dummy_var.owner.inputs[0])
          for dummy_var in model_dummy_vars
      }
      toposort_replace(fgraph, tuple(model_dummy_vars_to_vars.items()))
  
      # Populate new PyMC model mappings
      for model_var in model_dummy_vars:
          if isinstance(model_var.owner.op, ModelFreeRV):
              var, value, *dims = model_var.owner.inputs
              transform = model_var.owner.op.transform
              model.free_RVs.append(var)
              # PyMC does not allow setting transform when we pass a value_var. Why?
              model.create_value_var(
                  var, transform=None, value_var=value
              )
              model.rvs_to_transforms[var] = transform
              model.set_initval(var, initval=None)
          elif isinstance(model_var.owner.op, ModelObservedRV):
              var, value, *dims = model_var.owner.inputs
              model.observed_RVs.append(var)
              model.create_value_var(
                  var, transform=None, value_var=value
              )
          elif isinstance(model_var.owner.op, ModelPotential):
              var, *dims = model_var.owner.inputs
              model.potentials.append(var)
          elif isinstance(model_var.owner.op, ModelDeterministic):
              var, *dims = model_var.owner.inputs
              # If a Deterministic is a direct view on an RV, copy it
              if var in model.basic_RVs:
                  var = var.copy()
              model.deterministics.append(var)
          elif isinstance(model_var.owner.op, ModelNamed):
              var, *dims = model_var.owner.inputs
          else:
              raise TypeError(
                  f"Unexpected ModelVar type {type(model_var)}"
              )
  
          var.name = model_var.name
          dims = [dim.data for dim in dims] if dims else None
          model.add_named_variable(var, dims=dims)
  
      return model
  
imports: from copy import copy
  from typing import Dict, Optional, Tuple
  import pytensor
  from pytensor import Variable, shared
  from pytensor.compile import SharedVariable
  from pytensor.graph import Apply, FunctionGraph, Op, node_rewriter
  from pytensor.graph.rewriting.basic import out2in
  from pytensor.scalar import Identity
  from pytensor.tensor.elemwise import Elemwise
  from pytensor.tensor.sharedvar import ScalarSharedVariable
  from pymc.logprob.transforms import Transform
  from pymc.model.core import Model
  from pymc.pytensorf import StringType, find_rng_nodes, toposort_replace
label_desc: Use image node_id 4 for calling a global function with example usage: model_from_fgraph(fgraph) and returns: model, first_non_model_var, var