code: def build_demo(embed_mode):
      textbox = gr.Textbox(
          show_label=False,
          placeholder="Enter text and press ENTER",
          container=False,
      )
      with gr.Blocks(
          title="LLMGA", theme=gr.themes.Default(), css=block_css
      ) as demo:
          state = gr.State()
  
          if not embed_mode:
              gr.Markdown(title_markdown)
  
          with gr.Row():
              with gr.Column(scale=3):
                  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,
                      )
  
                  imagebox = gr.Image(type="pil")
                  image_process_mode = gr.Radio(
                      ["Crop", "Resize", "Pad", "Default"],
                      value="Resize",
                      label="Preprocess for non-square image",
                      visible=False,
                  )
  
                  cur_dir = os.path.dirname(os.path.abspath(__file__))
                  gr.Examples(
                      examples=[
                          [
                              f"{cur_dir}/examples/jiateng.png",
                              "Generate a similar image.",
                          ],
                          [
                              f"{cur_dir}/examples/snow_scene.jpeg",
                              "Generate a similar image.",
                          ],
                          [
                              f"{cur_dir}/examples/aes.png",
                              "Generate a similar image.",
                          ],
                      ],
                      inputs=[imagebox, textbox],
                  )
                  gr.Examples(
                      examples=[
                          [
                              "Please help me design a spaceship and show me its blueprint."
                          ],
                          [
                              "I need a Chinese style painting for decoration. Please help me design it."
                          ],
                      ],
                      inputs=[textbox],
                  )
  
                  with gr.Accordion(
                      "MLLM's Parameters", open=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=1024,
                          step=64,
                          interactive=True,
                          label="Max output tokens",
                      )
  
                  with gr.Accordion(
                      "SD's Parameters", open=False
                  ) as parameter_row:
                      num_inference_steps = gr.Slider(
                          minimum=10,
                          maximum=80,
                          value=30,
                          step=2,
                          interactive=True,
                          label="num_inference_steps",
                      )
  
              with gr.Column(scale=8):
                  chatbot = gr.Chatbot(
                      elem_id="chatbot",
                      label="LLMGA Chatbot",
                      height=550,
                  )
                  with gr.Row():
                      with gr.Column(scale=8):
                          textbox.render()
                      with gr.Column(scale=1, min_width=50):
                          submit_btn = gr.Button(
                              value="Send", variant="primary"
                          )
                  with gr.Row(elem_id="buttons") as button_row:
                      regenerate_btn = gr.Button(
                          value="🔄 Text Regenerate", interactive=False
                      )
                      clear_btn = gr.Button(
                          value="🗑️  Clear", interactive=False
                      )
  
              with gr.Column(scale=4):
                  gen_imagebox = gr.Image(
                      type="pil", label="Image Output", height=400
                  )
                  with gr.Row(elem_id="buttons") as button_row:
                      gen_regenerate_btn = gr.Button(
                          value="🔄 Image Regenerate", interactive=False
                      )
  
          url_params = gr.JSON(visible=False)
  
          # Register listeners
          btn_list = [regenerate_btn, clear_btn, gen_regenerate_btn]
  
          regenerate_btn.click(
              regenerate,
              [state, image_process_mode],
              [state, chatbot, textbox, imagebox] + btn_list,
              queue=False,
          ).then(
              http_bot,
              [
                  state,
                  model_selector,
                  temperature,
                  top_p,
                  max_output_tokens,
              ],
              [state, chatbot] + btn_list,
          )
  
          clear_btn.click(
              clear_history,
              None,
              [state, chatbot, textbox, imagebox, gen_imagebox]
              + btn_list,
              queue=False,
          )
  
          textbox.submit(
              add_text,
              [state, textbox, imagebox, image_process_mode],
              [state, chatbot, textbox, imagebox] + btn_list,
              queue=False,
          ).then(
              http_bot,
              [
                  state,
                  model_selector,
                  temperature,
                  top_p,
                  max_output_tokens,
              ],
              [state, chatbot] + btn_list,
          ).then(
              generation_bot,
              [state, num_inference_steps],
              [gen_imagebox, gen_regenerate_btn],
          )
  
          submit_btn.click(
              add_text,
              [state, textbox, imagebox, image_process_mode],
              [state, chatbot, textbox, imagebox] + btn_list,
              queue=False,
          ).then(
              http_bot,
              [
                  state,
                  model_selector,
                  temperature,
                  top_p,
                  max_output_tokens,
              ],
              [state, chatbot] + btn_list,
          ).then(
              generation_bot,
              [state, num_inference_steps],
              [gen_imagebox, gen_regenerate_btn],
          )
  
          gen_regenerate_btn.click(
              generation_bot,
              [state, num_inference_steps],
              [gen_imagebox, gen_regenerate_btn],
          )
  
          if args.model_list_mode == "once":
              demo.load(
                  load_demo,
                  [url_params],
                  [state, model_selector],
                  _js=get_window_url_params,
                  queue=False,
              )
          elif args.model_list_mode == "reload":
              demo.load(
                  load_demo_refresh_model_list,
                  None,
                  [state, model_selector],
                  queue=False,
              )
          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 llmga.llava.conversation import default_conversation, conv_templates, SeparatorStyle
  from llmga.llava.constants import LOGDIR
  from llmga.llava.utils import build_logger, server_error_msg, violates_moderation, moderation_msg
  from llmga.llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
  from llmga.llava.conversation import conv_templates, SeparatorStyle
  from llmga.llava.model.builder import load_pretrained_model
  from llmga.llava.utils import disable_torch_init
  from llmga.llava.mm_utils import tokenizer_image_token, get_model_name_from_path, KeywordsStoppingCriteria
  import hashlib
  import torch
  from llmga.diffusers.pipeline_stable_diffusion_xl_lpw import StableDiffusionXLPipeline
  import copy
label_desc: Use image node_id 8 for calling a global function with example usage: build_demo(embed_mode) and returns: demo