code: def setup_ui(ui, gallery):
      def save_preview(index, images, filename):
          # this function is here for backwards compatibility and likely will be removed soon
  
          if len(images) == 0:
              print(
                  "There is no image in gallery to save as a preview."
              )
              return [
                  page.create_html(ui.tabname)
                  for page in ui.stored_extra_pages
              ]
  
          index = int(index)
          index = 0 if index < 0 else index
          index = len(images) - 1 if index >= len(images) else index
  
          img_info = images[index if index >= 0 else 0]
          image = image_from_url_text(img_info)
          geninfo, items = read_info_from_image(image)
  
          is_allowed = False
          for extra_page in ui.stored_extra_pages:
              if any(
                  path_is_parent(x, filename)
                  for x in extra_page.allowed_directories_for_previews()
              ):
                  is_allowed = True
                  break
  
          assert is_allowed, f"writing to {filename} is not allowed"
  
          save_image_with_geninfo(image, geninfo, filename)
  
          return [
              page.create_html(ui.tabname)
              for page in ui.stored_extra_pages
          ]
  
      ui.button_save_preview.click(
          fn=save_preview,
          _js="function(x, y, z){return [selected_gallery_index(), y, z]}",
          inputs=[
              ui.preview_target_filename,
              gallery,
              ui.preview_target_filename,
          ],
          outputs=[*ui.pages],
      )
  
      for editor in ui.user_metadata_editors:
          editor.setup_ui(gallery)
  
imports: import functools
  import os.path
  import urllib.parse
  from pathlib import Path
  from modules import shared, ui_extra_networks_user_metadata, errors, extra_networks
  from modules.images import read_info_from_image, save_image_with_geninfo
  import gradio
  import json
  import html
  from fastapi.exceptions import HTTPException
  from modules.generation_parameters_copypaste import image_from_url_text
  from modules.ui_components import ToolButton
label_desc: Use image node_id 14 for calling a global function with example usage: setup_ui(ui, gallery) and returns: unknown, unknown