code: def _build_visualization(
      dsk_composition: TaskComposition,
      templates: Jinja2Templates,
      *,
      no_optimization: bool = False,
  ):
      def endpoint_visualization(request: Request):
          nonlocal dsk_composition, templates, no_optimization
          with BytesIO() as f:
              visualize(
                  dsk_composition,
                  fhandle=f,
                  no_optimization=no_optimization,
              )
              f.seek(0)
              raw = f.read()
          encoded = base64.b64encode(raw).decode("ascii")
          return templates.TemplateResponse(
              "dag.html", {"request": request, "encoded_image": encoded}
          )
  
      return endpoint_visualization
  
file_path: lightning-flash/src/flash/core/serve/interfaces/http.py
imports: import base64
  import uuid
  from io import BytesIO
  from pathlib import Path
  from typing import TYPE_CHECKING, Any, Callable, Dict, Optional
  from flash.core.serve.dag.task import get
  from flash.core.serve.dag.visualize import visualize
  from flash.core.serve.execution import ComponentJSON, MergedJSON, TaskComposition, build_composition, component_dag_content, merged_dag_content
  from flash.core.serve.interfaces.models import Alive, EndpointProtocol
  from flash.core.utilities.imports import _CYTOOLZ_AVAILABLE, _FASTAPI_AVAILABLE
label_desc: Use image node_id 4 for calling a global function with example usage: _build_visualization(dsk_composition, templates) and returns: endpoint_visualization, templates