code: def plot_attn_map(tokens, attn, disabled_tokens=[]):
      # visual parameters
      width = 9
      word_height = 1
      pad = 0.1
  
      """ Expecting attn.shape == (len(tokens), len(tokens)) """
      n_tokens = len(tokens)
      assert attn.shape == (
          n_tokens,
          n_tokens,
      ), "attention shape must correspond to num of tokens"
      # disable special tokens
      if disabled_tokens:
          # recalculate attention scores without disabled_tokens
          disabled_ids = [
              tokens.index(tok)
              for tok in tokens
              if tok in disabled_tokens
          ]
          # zero all attentions of disbaled_tokens
          for disable_tok_id in disabled_ids:
              attn[disable_tok_id] = 0  # zero row  (attention from)
              attn[:, disable_tok_id] = 0  # zero column (attention to)
  
      # plot
      fig, ax1 = plt.subplots(1, 1, figsize=(6.5, n_tokens / 4.5))
      fig.tight_layout()
      plt.subplots_adjust(left=0.3, bottom=0.0, right=0.7, top=1.0)
  
      ax1.axis("off")
      yoffset = 1
      xoffset = 0
      for position, word in enumerate(tokens):
          ax1.text(
              xoffset + 0,
              yoffset - position * word_height,
              word,
              ha="right",
              va="center",
          )
          ax1.text(
              xoffset + width,
              yoffset - position * word_height,
              word,
              ha="left",
              va="center",
          )
      for i in range(0, n_tokens):
          for j in range(0, n_tokens):
              ax1.plot(
                  [xoffset + pad, xoffset + width - pad],
                  [
                      yoffset - word_height * i,
                      yoffset - word_height * j,
                  ],
                  color="blue",
                  linewidth=2,
                  alpha=float(attn[i, j]),
              )
  
      img_width, img_height = (
          fig.get_size_inches() * fig.dpi
      )  # size in pixels
      out_url = fig_to_uri(fig)
  
      # Plotly hack to set image as background image of plot
      fig = go.Figure()
      scale_factor = 1.0
  
      fig.add_trace(
          go.Scatter(
              x=[0, img_width * scale_factor],
              y=[0, img_height * scale_factor],
              mode="markers",
              marker_opacity=0,
          )
      )
  
      # Configure axes
      fig.update_xaxes(
          visible=False, range=[0, img_width * scale_factor]
      )
  
      fig.update_yaxes(
          visible=False,
          range=[0, img_height * scale_factor],
          # the scaleanchor attribute ensures that the aspect ratio stays constant
          scaleanchor="x",
      )
  
      # Add image
      fig.add_layout_image(
          dict(
              x=0,
              sizex=img_width * scale_factor,
              y=img_height * scale_factor,
              sizey=img_height * scale_factor,
              xref="x",
              yref="y",
              opacity=1.0,
              layer="below",
              sizing="stretch",
              source=out_url,
          )
      )
  
      # Configure other layout
      fig.update_layout(
          width=img_width * scale_factor,
          height=img_height * scale_factor,
          margin={"l": 0, "r": 0, "t": 0, "b": 0},
      )
  
      return fig
  
file_path: nlp-architect/solutions/InterpreT/application/plotFunc.py
imports: import base64
  import copy
  from io import BytesIO
  import numpy
  import matplotlib.pyplot
  import plotly.graph_objects
label_desc: Use image node_id 4 for calling a global function with example usage: plot_attn_map(tokens, attn, disabled_tokens) and returns: fig