class_docstr: ChatInterface is Gradio's high-level abstraction for creating chatbot UIs, and allows you to create
  a web-based demo around a chatbot model in a few lines of code. Only one parameter is required: fn, which
  takes a function that governs the response of the chatbot based on the user input and chat history. Additional
  parameters can be used to control the appearance and behavior of the demo.
  
  Example:
      import gradio as gr
  
      def echo(message, history):
          return message
  
      demo = gr.ChatInterface(fn=echo, examples=["hello", "hola", "merhaba"], title="Echo Bot")
      demo.launch()
  Demos: chatinterface_random_response, chatinterface_streaming_echo
  Guides: creating-a-chatbot-fast, sharing-your-app
docstr: Parameters:
      fn: the function to wrap the chat interface around. Should accept two parameters: a string input message and list of two-element lists of the form [[user_message, bot_message], ...] representing the chat history, and return a string response. See the Chatbot documentation for more information on the chat history format.
      chatbot: an instance of the gr.Chatbot component to use for the chat interface, if you would like to customize the chatbot properties. If not provided, a default gr.Chatbot component will be created.
      textbox: an instance of the gr.Textbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox component will be created.
      additional_inputs: an instance or list of instances of gradio components (or their string shortcuts) to use as additional inputs to the chatbot. If components are not already rendered in a surrounding Blocks, then the components will be displayed under the chatbot, in an accordion.
      additional_inputs_accordion_name: Deprecated. Will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead.
      additional_inputs_accordion: If a string is provided, this is the label of the `gr.Accordion` to use to contain additional inputs. A `gr.Accordion` object can be provided as well to configure other properties of the container holding the additional inputs. Defaults to a `gr.Accordion(label="Additional Inputs", open=False)`. This parameter is only used if `additional_inputs` is provided.
      examples: sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input.
      cache_examples: If True, caches examples in the server for fast runtime in examples. The default option in HuggingFace Spaces is True. The default option elsewhere is False.
      title: a title for the interface; if provided, appears above chatbot in large font. Also used as the tab title when opened in a browser window.
      description: a description for the interface; if provided, appears above the chatbot and beneath the title in regular font. Accepts Markdown and HTML content.
      theme: Theme to use, loaded from gradio.themes.
      css: custom css or path to custom css file to use with interface.
      analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
      submit_btn: Text to display on the submit button. If None, no button will be displayed. If a Button object, that button will be used.
      stop_btn: Text to display on the stop button, which replaces the submit_btn when the submit_btn or retry_btn is clicked and response is streaming. Clicking on the stop_btn will halt the chatbot response. If set to None, stop button functionality does not appear in the chatbot. If a Button object, that button will be used as the stop button.
      retry_btn: Text to display on the retry button. If None, no button will be displayed. If a Button object, that button will be used.
      undo_btn: Text to display on the delete last button. If None, no button will be displayed. If a Button object, that button will be used.
      clear_btn: Text to display on the clear button. If None, no button will be displayed. If a Button object, that button will be used.
      autofocus: If True, autofocuses to the textbox when the page loads.
      concurrency_limit: If set, this is the maximum number of chatbot submissions that can be running simultaneously. Can be set to None to mean no limit (any number of chatbot submissions can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `.queue()`, which is 1 by default).
docstr_tok: ["Parameters",":","fn",":","the","function","to","wrap","the","chat","interface","around",".","Should","accept","two","parameters",":","a","string","input","message","and","list","of","two-element","lists","of","the","form","[","[","user_message",",","bot_message","]",",","...","]","representing","the","chat","history",",","and","return","a","string","response",".","See","the","Chatbot","documentation","for","more","information","on","the","chat","history","format",".","chatbot",":","an","instance","of","the","gr.Chatbot","component","to","use","for","the","chat","interface",",","if","you","would","like","to","customize","the","chatbot","properties",".","If","not","provided",",","a","default","gr.Chatbot","component","will","be","created",".","textbox",":","an","instance","of","the","gr.Textbox","component","to","use","for","the","chat","interface",",","if","you","would","like","to","customize","the","textbox","properties",".","If","not","provided",",","a","default","gr.Textbox","component","will","be","created",".","additional_inputs",":","an","instance","or","list","of","instances","of","gradio","components","(","or","their","string","shortcuts",")","to","use","as","additional","inputs","to","the","chatbot",".","If","components","are","not","already","rendered","in","a","surrounding","Blocks",",","then","the","components","will","be","displayed","under","the","chatbot",",","in","an","accordion",".","additional_inputs_accordion_name",":","Deprecated",".","Will","be","removed","in","a","future","version","of","Gradio",".","Use","the","`","additional_inputs_accordion","`","parameter","instead",".","additional_inputs_accordion",":","If","a","string","is","provided",",","this","is","the","label","of","the","`","gr.Accordion","`","to","use","to","contain","additional","inputs",".","A","`","gr.Accordion","`","object","can","be","provided","as","well","to","configure","other","properties","of","the","container","holding","the","additional","inputs",".","Defaults","to","a","`","gr.Accordion","(","label=","''","Additional","Inputs","''",",","open=False",")","`",".","This","parameter","is","only","used","if","`","additional_inputs","`","is","provided",".","examples",":","sample","inputs","for","the","function",";","if","provided",",","appear","below","the","chatbot","and","can","be","clicked","to","populate","the","chatbot","input",".","cache_examples",":","If","True",",","caches","examples","in","the","server","for","fast","runtime","in","examples",".","The","default","option","in","HuggingFace","Spaces","is","True",".","The","default","option","elsewhere","is","False",".","title",":","a","title","for","the","interface",";","if","provided",",","appears","above","chatbot","in","large","font",".","Also","used","as","the","tab","title","when","opened","in","a","browser","window",".","description",":","a","description","for","the","interface",";","if","provided",",","appears","above","the","chatbot","and","beneath","the","title","in","regular","font",".","Accepts","Markdown","and","HTML","content",".","theme",":","Theme","to","use",",","loaded","from","gradio.themes",".","css",":","custom","css","or","path","to","custom","css","file","to","use","with","interface",".","analytics_enabled",":","Whether","to","allow","basic","telemetry",".","If","None",",","will","use","GRADIO_ANALYTICS_ENABLED","environment","variable","if","defined",",","or","default","to","True",".","submit_btn",":","Text","to","display","on","the","submit","button",".","If","None",",","no","button","will","be","displayed",".","If","a","Button","object",",","that","button","will","be","used",".","stop_btn",":","Text","to","display","on","the","stop","button",",","which","replaces","the","submit_btn","when","the","submit_btn","or","retry_btn","is","clicked","and","response","is","streaming",".","Clicking","on","the","stop_btn","will","halt","the","chatbot","response",".","If","set","to","None",",","stop","button","functionality","does","not","appear","in","the","chatbot",".","If","a","Button","object",",","that","button","will","be","used","as","the","stop","button",".","retry_btn",":","Text","to","display","on","the","retry","button",".","If","None",",","no","button","will","be","displayed",".","If","a","Button","object",",","that","button","will","be","used",".","undo_btn",":","Text","to","display","on","the","delete","last","button",".","If","None",",","no","button","will","be","displayed",".","If","a","Button","object",",","that","button","will","be","used",".","clear_btn",":","Text","to","display","on","the","clear","button",".","If","None",",","no","button","will","be","displayed",".","If","a","Button","object",",","that","button","will","be","used",".","autofocus",":","If","True",",","autofocuses","to","the","textbox","when","the","page","loads",".","concurrency_limit",":","If","set",",","this","is","the","maximum","number","of","chatbot","submissions","that","can","be","running","simultaneously",".","Can","be","set","to","None","to","mean","no","limit","(","any","number","of","chatbot","submissions","can","be","running","simultaneously",")",".","Set","to","``","default","''","to","use","the","default","concurrency","limit","(","defined","by","the","`","default_concurrency_limit","`","parameter","in","`",".queue","(",")","`",",","which","is","1","by","default",")","."]
code: def __init__(
      self,
      fn: Callable,
      *,
      chatbot: Chatbot | None = None,
      textbox: Textbox | None = None,
      additional_inputs: str
      | Component
      | list[str | Component]
      | None = None,
      additional_inputs_accordion_name: str | None = None,
      additional_inputs_accordion: str | Accordion | None = None,
      examples: list[str] | None = None,
      cache_examples: bool | None = None,
      title: str | None = None,
      description: str | None = None,
      theme: Theme | str | None = None,
      css: str | None = None,
      analytics_enabled: bool | None = None,
      submit_btn: str | None | Button = "Submit",
      stop_btn: str | None | Button = "Stop",
      retry_btn: str | None | Button = "🔄  Retry",
      undo_btn: str | None | Button = "↩️ Undo",
      clear_btn: str | None | Button = "🗑️  Clear",
      autofocus: bool = True,
      concurrency_limit: int | None | Literal["default"] = "default",
  ):
      """
      Parameters:
          fn: the function to wrap the chat interface around. Should accept two parameters: a string input message and list of two-element lists of the form [[user_message, bot_message], ...] representing the chat history, and return a string response. See the Chatbot documentation for more information on the chat history format.
          chatbot: an instance of the gr.Chatbot component to use for the chat interface, if you would like to customize the chatbot properties. If not provided, a default gr.Chatbot component will be created.
          textbox: an instance of the gr.Textbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox component will be created.
          additional_inputs: an instance or list of instances of gradio components (or their string shortcuts) to use as additional inputs to the chatbot. If components are not already rendered in a surrounding Blocks, then the components will be displayed under the chatbot, in an accordion.
          additional_inputs_accordion_name: Deprecated. Will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead.
          additional_inputs_accordion: If a string is provided, this is the label of the `gr.Accordion` to use to contain additional inputs. A `gr.Accordion` object can be provided as well to configure other properties of the container holding the additional inputs. Defaults to a `gr.Accordion(label="Additional Inputs", open=False)`. This parameter is only used if `additional_inputs` is provided.
          examples: sample inputs for the function; if provided, appear below the chatbot and can be clicked to populate the chatbot input.
          cache_examples: If True, caches examples in the server for fast runtime in examples. The default option in HuggingFace Spaces is True. The default option elsewhere is False.
          title: a title for the interface; if provided, appears above chatbot in large font. Also used as the tab title when opened in a browser window.
          description: a description for the interface; if provided, appears above the chatbot and beneath the title in regular font. Accepts Markdown and HTML content.
          theme: Theme to use, loaded from gradio.themes.
          css: custom css or path to custom css file to use with interface.
          analytics_enabled: Whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
          submit_btn: Text to display on the submit button. If None, no button will be displayed. If a Button object, that button will be used.
          stop_btn: Text to display on the stop button, which replaces the submit_btn when the submit_btn or retry_btn is clicked and response is streaming. Clicking on the stop_btn will halt the chatbot response. If set to None, stop button functionality does not appear in the chatbot. If a Button object, that button will be used as the stop button.
          retry_btn: Text to display on the retry button. If None, no button will be displayed. If a Button object, that button will be used.
          undo_btn: Text to display on the delete last button. If None, no button will be displayed. If a Button object, that button will be used.
          clear_btn: Text to display on the clear button. If None, no button will be displayed. If a Button object, that button will be used.
          autofocus: If True, autofocuses to the textbox when the page loads.
          concurrency_limit: If set, this is the maximum number of chatbot submissions that can be running simultaneously. Can be set to None to mean no limit (any number of chatbot submissions can be running simultaneously). Set to "default" to use the default concurrency limit (defined by the `default_concurrency_limit` parameter in `.queue()`, which is 1 by default).
      """
      super().__init__(
          analytics_enabled=analytics_enabled,
          mode="chat_interface",
          css=css,
          title=title or "Gradio",
          theme=theme,
      )
      self.concurrency_limit = concurrency_limit
      self.fn = fn
      self.is_async = inspect.iscoroutinefunction(
          self.fn
      ) or inspect.isasyncgenfunction(self.fn)
      self.is_generator = inspect.isgeneratorfunction(
          self.fn
      ) or inspect.isasyncgenfunction(self.fn)
      self.examples = examples
      if self.space_id and cache_examples is None:
          self.cache_examples = True
      else:
          self.cache_examples = cache_examples or False
      self.buttons: list[Button | None] = []
  
      if additional_inputs:
          if not isinstance(additional_inputs, list):
              additional_inputs = [additional_inputs]
          self.additional_inputs = [
              get_component_instance(i)
              for i in additional_inputs  # type: ignore
          ]
      else:
          self.additional_inputs = []
      if additional_inputs_accordion_name is not None:
          print(
              "The `additional_inputs_accordion_name` parameter is deprecated and will be removed in a future version of Gradio. Use the `additional_inputs_accordion` parameter instead."
          )
          self.additional_inputs_accordion_params = {
              "label": additional_inputs_accordion_name
          }
      if additional_inputs_accordion is None:
          self.additional_inputs_accordion_params = {
              "label": "Additional Inputs",
              "open": False,
          }
      elif isinstance(additional_inputs_accordion, str):
          self.additional_inputs_accordion_params = {
              "label": additional_inputs_accordion
          }
      elif isinstance(additional_inputs_accordion, Accordion):
          self.additional_inputs_accordion_params = (
              additional_inputs_accordion.recover_kwargs(
                  additional_inputs_accordion.get_config()
              )
          )
      else:
          raise ValueError(
              f"The `additional_inputs_accordion` parameter must be a string or gr.Accordion, not {type(additional_inputs_accordion)}"
          )
  
      with self:
          if title:
              Markdown(
                  f"<h1 style='text-align: center; margin-bottom: 1rem'>{self.title}</h1>"
              )
          if description:
              Markdown(description)
  
          with Column(variant="panel"):
              if chatbot:
                  self.chatbot = chatbot.render()
              else:
                  self.chatbot = Chatbot(label="Chatbot")
  
              with Group():
                  with Row():
                      if textbox:
                          textbox.container = False
                          textbox.show_label = False
                          textbox_ = textbox.render()
                          assert isinstance(textbox_, Textbox)
                          self.textbox = textbox_
                      else:
                          self.textbox = Textbox(
                              container=False,
                              show_label=False,
                              label="Message",
                              placeholder="Type a message...",
                              scale=7,
                              autofocus=autofocus,
                          )
                      if submit_btn:
                          if isinstance(submit_btn, Button):
                              submit_btn.render()
                          elif isinstance(submit_btn, str):
                              submit_btn = Button(
                                  submit_btn,
                                  variant="primary",
                                  scale=1,
                                  min_width=150,
                              )
                          else:
                              raise ValueError(
                                  f"The submit_btn parameter must be a gr.Button, string, or None, not {type(submit_btn)}"
                              )
                      if stop_btn:
                          if isinstance(stop_btn, Button):
                              stop_btn.visible = False
                              stop_btn.render()
                          elif isinstance(stop_btn, str):
                              stop_btn = Button(
                                  stop_btn,
                                  variant="stop",
                                  visible=False,
                                  scale=1,
                                  min_width=150,
                              )
                          else:
                              raise ValueError(
                                  f"The stop_btn parameter must be a gr.Button, string, or None, not {type(stop_btn)}"
                              )
                      self.buttons.extend([submit_btn, stop_btn])  # type: ignore
  
              with Row():
                  for btn in [retry_btn, undo_btn, clear_btn]:
                      if btn:
                          if isinstance(btn, Button):
                              btn.render()
                          elif isinstance(btn, str):
                              btn = Button(btn, variant="secondary")
                          else:
                              raise ValueError(
                                  f"All the _btn parameters must be a gr.Button, string, or None, not {type(btn)}"
                              )
                      self.buttons.append(btn)  # type: ignore
  
                  self.fake_api_btn = Button("Fake API", visible=False)
                  self.fake_response_textbox = Textbox(
                      label="Response", visible=False
                  )
                  (
                      self.submit_btn,
                      self.stop_btn,
                      self.retry_btn,
                      self.undo_btn,
                      self.clear_btn,
                  ) = self.buttons
  
          if examples:
              if self.is_generator:
                  examples_fn = self._examples_stream_fn
              else:
                  examples_fn = self._examples_fn
  
              self.examples_handler = Examples(
                  examples=examples,
                  inputs=[self.textbox] + self.additional_inputs,
                  outputs=self.chatbot,
                  fn=examples_fn,
              )
  
          any_unrendered_inputs = any(
              not inp.is_rendered for inp in self.additional_inputs
          )
          if self.additional_inputs and any_unrendered_inputs:
              with Accordion(**self.additional_inputs_accordion_params):  # type: ignore
                  for input_component in self.additional_inputs:
                      if not input_component.is_rendered:
                          input_component.render()
  
          # The example caching must happen after the input components have rendered
          if cache_examples:
              client_utils.synchronize_async(
                  self.examples_handler.cache
              )
  
          self.saved_input = State()
          self.chatbot_state = (
              State(self.chatbot.value)
              if self.chatbot.value
              else State([])
          )
  
          self._setup_events()
          self._setup_api()
  
imports: from __future__ import annotations
  import inspect
  from typing import AsyncGenerator, Callable, Literal, Union, cast
  import anyio
  from gradio_client import utils
  from gradio_client.documentation import document, set_documentation_group
  from gradio.blocks import Blocks
  from gradio.components import Button, Chatbot, Component, Markdown, State, Textbox, get_component_instance
  from gradio.events import Dependency, on
  from gradio.helpers import create_examples
  from gradio.helpers import special_args
  from gradio.layouts import Accordion, Column, Group, Row
  from gradio.routes import Request
  from gradio.themes import ThemeClass
  from gradio.utils import SyncToAsyncIterator, async_iteration
label_desc: Use image node_id 1 to create a new ChatInterface object from inherited base classes: Blocks with example: obj = ChatInterface(fn)