code: def cached_stan_model(source):
      """Compiles and caches a Stan model.
  
      Args:
        source: Stan model source code.
  
      Returns:
        model: Compiled Stan model.
      """
      filename = (
          FLAGS.stan_model_filename_template.format(
              hashlib.md5(source.encode("ascii")).hexdigest()
          )
          + ".stan"
      )
      # We rely on CmdStanPy's internal caching, based on mtimes of the source and
      # binary files. CmdStanPy will not recompile the model if the binary is newer
      # than the source file.
      if not os.path.exists(filename):
          with open(filename, "w") as f:
              f.write(source)
      model = cmdstanpy.CmdStanModel(stan_file=filename)
      model.compile()
      return model
  
file_path: probability/spinoffs/inference_gym/inference_gym/tools/stan/util.py
imports: import contextlib
  import glob
  import hashlib
  import os
  import re
  import shutil
  import tempfile
  from absl import flags
  import cmdstanpy
  import numpy
label_desc: Use image node_id 1 for calling a global function with example usage: cached_stan_model(source) and returns: model