code: def build_demo(embed_mode):
      textbox = gr.Textbox(
          show_label=False,
          placeholder="Enter text and press ENTER",
          visible=False,
          container=False,
      )
      with gr.Blocks(
          title="FERRET", theme=gr.themes.Base(), css=css
      ) as demo:
          state = gr.State()
  
          if not embed_mode:
              gr.Markdown(title_markdown)
              gr.Markdown(Instructions)
  
          with gr.Row():
              with gr.Column(scale=4):
                  with gr.Row(elem_id="model_selector_row"):
                      model_selector = gr.Dropdown(
                          choices=models,
                          value=models[0] if len(models) > 0 else "",
                          interactive=True,
                          show_label=False,
                          container=False,
                      )
  
                  original_image = gr.Image(type="pil", visible=False)
                  image_process_mode = gr.Radio(
                      ["Raw+Processor", "Crop", "Resize", "Pad"],
                      value="Raw+Processor",
                      label="Preprocess for non-square image",
                      visible=False,
                  )
  
                  # Added for any-format input.
                  sketch_pad = ImageMask(
                      label="Image & Sketch",
                      type="pil",
                      elem_id="img2text",
                  )
                  refer_input_mode = gr.Radio(
                      ["Point", "Box", "Sketch"],
                      value="Point",
                      label="Referring Input Type",
                  )
                  refer_input_state = gr.State(
                      {
                          "region_placeholder_tokens": [],
                          "region_coordinates": [],
                          "region_masks": [],
                          "region_masks_in_prompts": [],
                          "masks": [],
                      }
                  )
                  refer_text_show = gr.HighlightedText(
                      value=[], label="Referring Input Cache"
                  )
  
                  imagebox_refer = gr.Image(
                      type="pil", label="Parsed Referring Input"
                  )
                  imagebox_output = gr.Image(
                      type="pil", label="Output Vis"
                  )
  
                  cur_dir = os.path.dirname(os.path.abspath(__file__))
                  gr.Examples(
                      examples=[
                          # [f"{cur_dir}/examples/harry-potter-hogwarts.jpg", "What is in [region0]? And what do people use it for?"],
                          # [f"{cur_dir}/examples/ingredients.jpg", "What objects are in [region0] and [region1]?"],
                          # [f"{cur_dir}/examples/extreme_ironing.jpg", "What is unusual about this image? And tell me the coordinates of mentioned objects."],
                          [
                              f"{cur_dir}/examples/ferret.jpg",
                              "What's the relationship between object [region0] and object [region1]?",
                          ],
                          [
                              f"{cur_dir}/examples/waterview.jpg",
                              "What are the things I should be cautious about when I visit here? Tell me the coordinates in response.",
                          ],
                          [
                              f"{cur_dir}/examples/flickr_9472793441.jpg",
                              "Describe the image in details.",
                          ],
                          # [f"{cur_dir}/examples/coco_000000281759.jpg", "What are the locations of the woman wearing a blue dress, the woman in flowery top, the girl in purple dress, the girl wearing green shirt?"],
                          [
                              f"{cur_dir}/examples/room_planning.jpg",
                              "How to improve the design of the given room?",
                          ],
                          [
                              f"{cur_dir}/examples/make_sandwitch.jpg",
                              "How can I make a sandwich with available ingredients?",
                          ],
                          [
                              f"{cur_dir}/examples/bathroom.jpg",
                              "What is unusual about this image?",
                          ],
                          [
                              f"{cur_dir}/examples/kitchen.png",
                              "Is the object a man or a chicken? Explain the reason.",
                          ],
                      ],
                      inputs=[sketch_pad, textbox],
                  )
  
                  with gr.Accordion(
                      "Parameters", open=False, visible=False
                  ) as parameter_row:
                      temperature = gr.Slider(
                          minimum=0.0,
                          maximum=1.0,
                          value=0.2,
                          step=0.1,
                          interactive=True,
                          label="Temperature",
                      )
                      top_p = gr.Slider(
                          minimum=0.0,
                          maximum=1.0,
                          value=0.7,
                          step=0.1,
                          interactive=True,
                          label="Top P",
                      )
                      max_output_tokens = gr.Slider(
                          minimum=0,
                          maximum=1024,
                          value=512,
                          step=64,
                          interactive=True,
                          label="Max output tokens",
                      )
  
              with gr.Column(scale=5):
                  chatbot = gr.Chatbot(
                      elem_id="chatbot", label="FERRET", visible=False
                  ).style(height=750)
                  with gr.Row():
                      with gr.Column(scale=8):
                          textbox.render()
                      with gr.Column(scale=1, min_width=60):
                          submit_btn = gr.Button(
                              value="Submit", visible=False
                          )
                  with gr.Row(visible=False) as button_row:
                      upvote_btn = gr.Button(
                          value="👍  Upvote", interactive=False
                      )
                      downvote_btn = gr.Button(
                          value="👎  Downvote", interactive=False
                      )
                      # flag_btn = gr.Button(value="⚠️  Flag", interactive=False)
                      # stop_btn = gr.Button(value="⏹️  Stop Generation", interactive=False)
                      regenerate_btn = gr.Button(
                          value="🔄  Regenerate", interactive=False
                      )
                      clear_btn = gr.Button(
                          value="🗑️  Clear history", interactive=False
                      )
                      location_btn = gr.Button(
                          value="🪄 Show location", interactive=False
                      )
  
          if not embed_mode:
              gr.Markdown(tos_markdown)
              gr.Markdown(learn_more_markdown)
          url_params = gr.JSON(visible=False)
  
          # Register listeners
          btn_list = [
              upvote_btn,
              downvote_btn,
              location_btn,
              regenerate_btn,
              clear_btn,
          ]
          upvote_btn.click(
              upvote_last_response,
              [state, model_selector],
              [textbox, upvote_btn, downvote_btn, location_btn],
          )
          downvote_btn.click(
              downvote_last_response,
              [state, model_selector],
              [textbox, upvote_btn, downvote_btn, location_btn],
          )
          # flag_btn.click(flag_last_response,
          #     [state, model_selector], [textbox, upvote_btn, downvote_btn, flag_btn])
          regenerate_btn.click(
              regenerate,
              [state, image_process_mode],
              [state, chatbot, textbox] + btn_list,
          ).then(
              http_bot,
              [
                  state,
                  model_selector,
                  temperature,
                  top_p,
                  max_output_tokens,
                  refer_input_state,
              ],
              [state, chatbot] + btn_list,
          )
          clear_btn.click(
              clear_history,
              None,
              [state, chatbot, textbox, imagebox_output, original_image]
              + btn_list
              + [
                  sketch_pad,
                  refer_input_state,
                  refer_text_show,
                  imagebox_refer,
              ],
          )
          location_btn.click(
              show_location,
              [sketch_pad, chatbot],
              [imagebox_output, chatbot, location_btn],
          )
  
          textbox.submit(
              add_text,
              [
                  state,
                  textbox,
                  image_process_mode,
                  original_image,
                  sketch_pad,
              ],
              [state, chatbot, textbox, original_image] + btn_list,
          ).then(
              http_bot,
              [
                  state,
                  model_selector,
                  temperature,
                  top_p,
                  max_output_tokens,
                  refer_input_state,
              ],
              [state, chatbot] + btn_list,
          )
  
          submit_btn.click(
              add_text,
              [
                  state,
                  textbox,
                  image_process_mode,
                  original_image,
                  sketch_pad,
              ],
              [state, chatbot, textbox, original_image] + btn_list,
          ).then(
              http_bot,
              [
                  state,
                  model_selector,
                  temperature,
                  top_p,
                  max_output_tokens,
                  refer_input_state,
              ],
              [state, chatbot] + btn_list,
          )
  
          sketch_pad.edit(
              draw,
              inputs=[
                  refer_input_mode,
                  sketch_pad,
                  refer_input_state,
                  refer_text_show,
                  imagebox_refer,
              ],
              outputs=[
                  refer_input_state,
                  refer_text_show,
                  imagebox_refer,
              ],
              queue=True,
          )
  
          if args.model_list_mode == "once":
              demo.load(
                  load_demo,
                  [url_params],
                  [
                      state,
                      model_selector,
                      chatbot,
                      textbox,
                      submit_btn,
                      button_row,
                      parameter_row,
                  ],
                  _js=get_window_url_params,
              )
          elif args.model_list_mode == "reload":
              demo.load(
                  load_demo_refresh_model_list,
                  None,
                  [
                      state,
                      model_selector,
                      chatbot,
                      textbox,
                      submit_btn,
                      button_row,
                      parameter_row,
                  ],
              )
          else:
              raise ValueError(
                  f"Unknown model list mode: {args.model_list_mode}"
              )
  
      return demo
  
imports: import argparse
  import datetime
  import json
  import os
  import time
  import gradio
  import requests
  from ferret.conversation import default_conversation, conv_templates, SeparatorStyle
  from ferret.constants import LOGDIR
  from ferret.utils import build_logger, server_error_msg, violates_moderation, moderation_msg
  import hashlib
  import re
  from copy import deepcopy
  from PIL import ImageDraw, ImageFont
  from gradio import processing_utils
  import numpy
  import torch
  import torch.nn.functional
  from scipy.ndimage import binary_dilation, binary_erosion
  import pdb
  from ferret.serve.gradio_css import code_highlight_css
label_desc: Use image node_id 21 for calling a global function with example usage: build_demo(embed_mode) and returns: demo