MyNameIsSimon commited on
Commit
e20cb99
·
1 Parent(s): 9b493b0

audio interface

Browse files
Files changed (2) hide show
  1. app.py +22 -5
  2. custom_chat_interface.py +888 -0
app.py CHANGED
@@ -1,4 +1,8 @@
1
  import gradio as gr
 
 
 
 
2
 
3
  from llama_cpp import Llama
4
  from llama_cpp.llama_chat_format import MoondreamChatHandler
@@ -58,16 +62,29 @@ class MyModel:
58
  yield response
59
 
60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
61
  """
62
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
63
  """
64
  my_model = MyModel()
65
- model_choices = [
66
- "lab2-as/lora_model_gguf, Q4",
67
- "lab2-as/lora_model_no_quant_gguf, Q4"
68
- ]
69
- demo = gr.ChatInterface(
70
  my_model.respond,
 
71
  additional_inputs=[
72
  gr.Dropdown(
73
  choices=model_choices,
 
1
  import gradio as gr
2
+ import numpy as np
3
+
4
+ from transformers import pipeline
5
+ from custom_chat_interface import CustomChatInterface
6
 
7
  from llama_cpp import Llama
8
  from llama_cpp.llama_chat_format import MoondreamChatHandler
 
62
  yield response
63
 
64
 
65
+ transcriber = pipeline("automatic-speech-recognition", model="openai/whisper-base.en")
66
+ def transcribe(audio):
67
+ sr, y = audio
68
+
69
+ # Convert to mono if stereo
70
+ if y.ndim > 1:
71
+ y = y.mean(axis=1)
72
+
73
+ y = y.astype(np.float32)
74
+ y /= np.max(np.abs(y))
75
+
76
+ text = transcriber({"sampling_rate": sr, "raw": y})["text"]
77
+ return text
78
+
79
+
80
  """
81
  For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
82
  """
83
  my_model = MyModel()
84
+ model_choices = ["lab2-as/lora_model_gguf, Q4", "lab2-as/lora_model_no_quant_gguf, Q4"]
85
+ demo = CustomChatInterface(
 
 
 
86
  my_model.respond,
87
+ transcriber=transcribe,
88
  additional_inputs=[
89
  gr.Dropdown(
90
  choices=model_choices,
custom_chat_interface.py ADDED
@@ -0,0 +1,888 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ This file defines a useful high-level abstraction to build Gradio chatbots: ChatInterface.
3
+ """
4
+
5
+ from __future__ import annotations
6
+
7
+ import builtins
8
+ import functools
9
+ import inspect
10
+ import warnings
11
+ from collections.abc import AsyncGenerator, Callable, Generator, Sequence
12
+ from pathlib import Path
13
+ from typing import Literal, Union, cast
14
+
15
+ import anyio
16
+ from gradio_client.documentation import document
17
+
18
+ from gradio import Interface, Audio
19
+ from gradio import utils
20
+ from gradio.blocks import Blocks
21
+ from gradio.components import (
22
+ Button,
23
+ Chatbot,
24
+ Component,
25
+ Markdown,
26
+ MultimodalTextbox,
27
+ State,
28
+ Textbox,
29
+ get_component_instance,
30
+ )
31
+ from gradio.components.chatbot import (
32
+ ExampleMessage,
33
+ FileDataDict,
34
+ Message,
35
+ MessageDict,
36
+ TupleFormat,
37
+ )
38
+ from gradio.components.multimodal_textbox import MultimodalPostprocess, MultimodalValue
39
+ from gradio.context import get_blocks_context
40
+ from gradio.events import Dependency, SelectData
41
+ from gradio.helpers import create_examples as Examples # noqa: N812
42
+ from gradio.helpers import special_args, update
43
+ from gradio.layouts import Accordion, Column, Group, Row
44
+ from gradio.routes import Request
45
+ from gradio.themes import ThemeClass as Theme
46
+
47
+
48
+ @document()
49
+ class CustomChatInterface(Blocks):
50
+ """
51
+ ChatInterface is Gradio's high-level abstraction for creating chatbot UIs, and allows you to create
52
+ a web-based demo around a chatbot model in a few lines of code. Only one parameter is required: fn, which
53
+ takes a function that governs the response of the chatbot based on the user input and chat history. Additional
54
+ parameters can be used to control the appearance and behavior of the demo.
55
+
56
+ Example:
57
+ import gradio as gr
58
+
59
+ def echo(message, history):
60
+ return message
61
+
62
+ demo = gr.ChatInterface(fn=echo, type="messages", examples=[{"text": "hello", "text": "hola", "text": "merhaba"}], title="Echo Bot")
63
+ demo.launch()
64
+ Demos: chatinterface_multimodal, chatinterface_random_response, chatinterface_streaming_echo
65
+ Guides: creating-a-chatbot-fast, sharing-your-app
66
+ """
67
+
68
+ def __init__(
69
+ self,
70
+ fn: Callable,
71
+ *,
72
+ multimodal: bool = False,
73
+ type: Literal["messages", "tuples"] | None = None,
74
+ chatbot: Chatbot | None = None,
75
+ textbox: Textbox | MultimodalTextbox | None = None,
76
+ additional_inputs: str | Component | list[str | Component] | None = None,
77
+ additional_inputs_accordion: str | Accordion | None = None,
78
+ additional_outputs: Component | list[Component] | None = None,
79
+ examples: list[str] | list[MultimodalValue] | list[list] | None = None,
80
+ example_labels: list[str] | None = None,
81
+ example_icons: list[str] | None = None,
82
+ cache_examples: bool | None = None,
83
+ cache_mode: Literal["eager", "lazy"] | None = None,
84
+ title: str | None = None,
85
+ description: str | None = None,
86
+ theme: Theme | str | None = None,
87
+ css: str | None = None,
88
+ css_paths: str | Path | Sequence[str | Path] | None = None,
89
+ js: str | None = None,
90
+ head: str | None = None,
91
+ head_paths: str | Path | Sequence[str | Path] | None = None,
92
+ analytics_enabled: bool | None = None,
93
+ autofocus: bool = True,
94
+ autoscroll: bool = True,
95
+ submit_btn: str | bool | None = True,
96
+ stop_btn: str | bool | None = True,
97
+ concurrency_limit: int | None | Literal["default"] = "default",
98
+ delete_cache: tuple[int, int] | None = None,
99
+ show_progress: Literal["full", "minimal", "hidden"] = "minimal",
100
+ fill_height: bool = True,
101
+ fill_width: bool = False,
102
+ api_name: str | Literal[False] = "chat",
103
+ transcriber= None,
104
+ ):
105
+ """
106
+ Parameters:
107
+ fn: the function to wrap the chat interface around. In the default case (assuming `type` is set to "messages"), the function should accept two parameters: a `str` input message and `list` of openai-style dictionary {"role": "user" | "assistant", "content": `str` | {"path": `str`} | `gr.Component`} representing the chat history, and return/yield a `str` (if a simple message) or `dict` (for a complete openai-style message) response.
108
+ multimodal: if True, the chat interface will use a `gr.MultimodalTextbox` component for the input, which allows for the uploading of multimedia files. If False, the chat interface will use a gr.Textbox component for the input. If this is True, the first argument of `fn` should accept not a `str` message but a `dict` message with keys "text" and "files"
109
+ type: The format of the messages passed into the chat history parameter of `fn`. If "messages", passes the history as a list of dictionaries with openai-style "role" and "content" keys. The "content" key's value should be one of the following - (1) strings in valid Markdown (2) a dictionary with a "path" key and value corresponding to the file to display or (3) an instance of a Gradio component: at the moment gr.Image, gr.Plot, gr.Video, gr.Gallery, gr.Audio, and gr.HTML are supported. The "role" key should be one of 'user' or 'assistant'. Any other roles will not be displayed in the output. If this parameter is 'tuples' (deprecated), passes the chat history as a `list[list[str | None | tuple]]`, i.e. a list of lists. The inner list should have 2 elements: the user message and the response message.
110
+ 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.
111
+ textbox: an instance of the gr.Textbox or gr.MultimodalTextbox component to use for the chat interface, if you would like to customize the textbox properties. If not provided, a default gr.Textbox or gr.MultimodalTextbox component will be created.
112
+ additional_inputs: an instance or list of instances of gradio components (or their string shortcuts) to use as additional inputs to the chatbot. If the components are not already rendered in a surrounding Blocks, then the components will be displayed under the chatbot, in an accordion. The values of these components will be passed into `fn` as arguments in order after the chat history.
113
+ 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.
114
+ additional_outputs: an instance or list of instances of gradio components to use as additional outputs from the chat function. These must be components that are already defined in the same Blocks scope. If provided, the chat function should return additional values for these components. See $demo/chatinterface_artifacts.
115
+ examples: sample inputs for the function; if provided, appear within the chatbot and can be clicked to populate the chatbot input. Should be a list of strings representing text-only examples, or a list of dictionaries (with keys `text` and `files`) representing multimodal examples. If `additional_inputs` are provided, the examples must be a list of lists, where the first element of each inner list is the string or dictionary example message and the remaining elements are the example values for the additional inputs -- in this case, the examples will appear under the chatbot.
116
+ example_labels: labels for the examples, to be displayed instead of the examples themselves. If provided, should be a list of strings with the same length as the examples list. Only applies when examples are displayed within the chatbot (i.e. when `additional_inputs` is not provided).
117
+ example_icons: icons for the examples, to be displayed above the examples. If provided, should be a list of string URLs or local paths with the same length as the examples list. Only applies when examples are displayed within the chatbot (i.e. when `additional_inputs` is not provided).
118
+ 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.
119
+ cache_mode: if "eager", all examples are cached at app launch. If "lazy", examples are cached for all users after the first use by any user of the app. If None, will use the GRADIO_CACHE_MODE environment variable if defined, or default to "eager".
120
+ 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.
121
+ description: a description for the interface; if provided, appears above the chatbot and beneath the title in regular font. Accepts Markdown and HTML content.
122
+ theme: a Theme object or a string representing a theme. If a string, will look for a built-in theme with that name (e.g. "soft" or "default"), or will attempt to load a theme from the Hugging Face Hub (e.g. "gradio/monochrome"). If None, will use the Default theme.
123
+ css: Custom css as a code string. This css will be included in the demo webpage.
124
+ css_paths: Custom css as a pathlib.Path to a css file or a list of such paths. This css files will be read, concatenated, and included in the demo webpage. If the `css` parameter is also set, the css from `css` will be included first.
125
+ js: Custom js as a code string. The custom js should be in the form of a single js function. This function will automatically be executed when the page loads. For more flexibility, use the head parameter to insert js inside <script> tags.
126
+ head: Custom html code to insert into the head of the demo webpage. This can be used to add custom meta tags, multiple scripts, stylesheets, etc. to the page.
127
+ head_paths: Custom html code as a pathlib.Path to a html file or a list of such paths. This html files will be read, concatenated, and included in the head of the demo webpage. If the `head` parameter is also set, the html from `head` will be included first.
128
+ analytics_enabled: whether to allow basic telemetry. If None, will use GRADIO_ANALYTICS_ENABLED environment variable if defined, or default to True.
129
+ autofocus: if True, autofocuses to the textbox when the page loads.
130
+ autoscroll: If True, will automatically scroll to the bottom of the chatbot when a new message appears, unless the user scrolls up. If False, will not scroll to the bottom of the chatbot automatically.
131
+ submit_btn: If True, will show a submit button with a submit icon within the textbox. If a string, will use that string as the submit button text in place of the icon. If False, will not show a submit button.
132
+ stop_btn: If True, will show a button with a stop icon during generator executions, to stop generating. If a string, will use that string as the submit button text in place of the stop icon. If False, will not show a stop button.
133
+ 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).
134
+ delete_cache: a tuple corresponding [frequency, age] both expressed in number of seconds. Every `frequency` seconds, the temporary files created by this Blocks instance will be deleted if more than `age` seconds have passed since the file was created. For example, setting this to (86400, 86400) will delete temporary files every day. The cache will be deleted entirely when the server restarts. If None, no cache deletion will occur.
135
+ show_progress: how to show the progress animation while event is running: "full" shows a spinner which covers the output component area as well as a runtime display in the upper right corner, "minimal" only shows the runtime display, "hidden" shows no progress animation at all
136
+ fill_height: if True, the chat interface will expand to the height of window.
137
+ fill_width: Whether to horizontally expand to fill container fully. If False, centers and constrains app to a maximum width.
138
+ api_name: the name of the API endpoint to use for the chat interface. Defaults to "chat". Set to False to disable the API endpoint.
139
+ """
140
+ super().__init__(
141
+ analytics_enabled=analytics_enabled,
142
+ mode="chat_interface",
143
+ title=title or "Gradio",
144
+ theme=theme,
145
+ css=css,
146
+ css_paths=css_paths,
147
+ js=js,
148
+ head=head,
149
+ head_paths=head_paths,
150
+ fill_height=fill_height,
151
+ fill_width=fill_width,
152
+ delete_cache=delete_cache,
153
+ )
154
+ self.transcribe = transcriber
155
+ self.api_name = api_name
156
+ self.type = type
157
+ self.multimodal = multimodal
158
+ self.concurrency_limit = concurrency_limit
159
+ self.fn = fn
160
+ self.is_async = inspect.iscoroutinefunction(
161
+ self.fn
162
+ ) or inspect.isasyncgenfunction(self.fn)
163
+ self.is_generator = inspect.isgeneratorfunction(
164
+ self.fn
165
+ ) or inspect.isasyncgenfunction(self.fn)
166
+ self.provided_chatbot = chatbot is not None
167
+ self.examples = examples
168
+ self.examples_messages = self._setup_example_messages(
169
+ examples, example_labels, example_icons
170
+ )
171
+ self.cache_examples = cache_examples
172
+ self.cache_mode = cache_mode
173
+ self.additional_inputs = [
174
+ get_component_instance(i)
175
+ for i in utils.none_or_singleton_to_list(additional_inputs)
176
+ ]
177
+ self.additional_outputs = utils.none_or_singleton_to_list(additional_outputs)
178
+ if additional_inputs_accordion is None:
179
+ self.additional_inputs_accordion_params = {
180
+ "label": "Additional Inputs",
181
+ "open": False,
182
+ }
183
+ elif isinstance(additional_inputs_accordion, str):
184
+ self.additional_inputs_accordion_params = {
185
+ "label": additional_inputs_accordion
186
+ }
187
+ elif isinstance(additional_inputs_accordion, Accordion):
188
+ self.additional_inputs_accordion_params = (
189
+ additional_inputs_accordion.recover_kwargs(
190
+ additional_inputs_accordion.get_config()
191
+ )
192
+ )
193
+ else:
194
+ raise ValueError(
195
+ f"The `additional_inputs_accordion` parameter must be a string or gr.Accordion, not {builtins.type(additional_inputs_accordion)}"
196
+ )
197
+ self._additional_inputs_in_examples = False
198
+ if self.additional_inputs and self.examples is not None:
199
+ for example in self.examples:
200
+ if not isinstance(example, list):
201
+ raise ValueError(
202
+ "Examples must be a list of lists when additional inputs are provided."
203
+ )
204
+ for idx, example_for_input in enumerate(example):
205
+ if example_for_input is not None and idx > 0:
206
+ self._additional_inputs_in_examples = True
207
+ break
208
+ if self._additional_inputs_in_examples:
209
+ break
210
+
211
+ with self:
212
+ with Column():
213
+ if title:
214
+ Markdown(
215
+ f"<h1 style='text-align: center; margin-bottom: 1rem'>{self.title}</h1>"
216
+ )
217
+ if description:
218
+ Markdown(description)
219
+ if chatbot:
220
+ if self.type:
221
+ if self.type != chatbot.type:
222
+ warnings.warn(
223
+ "The type of the gr.Chatbot does not match the type of the gr.ChatInterface."
224
+ f"The type of the gr.ChatInterface, '{self.type}', will be used."
225
+ )
226
+ chatbot.type = self.type
227
+ chatbot._setup_data_model()
228
+ else:
229
+ warnings.warn(
230
+ f"The gr.ChatInterface was not provided with a type, so the type of the gr.Chatbot, '{chatbot.type}', will be used."
231
+ )
232
+ self.type = chatbot.type
233
+ self.chatbot = cast(
234
+ Chatbot, get_component_instance(chatbot, render=True)
235
+ )
236
+ if self.chatbot.examples and self.examples_messages:
237
+ warnings.warn(
238
+ "The ChatInterface already has examples set. The examples provided in the chatbot will be ignored."
239
+ )
240
+ self.chatbot.examples = (
241
+ self.examples_messages
242
+ if not self._additional_inputs_in_examples
243
+ else None
244
+ )
245
+ self.chatbot._setup_examples()
246
+ else:
247
+ self.type = self.type or "tuples"
248
+ self.chatbot = Chatbot(
249
+ label="Chatbot",
250
+ scale=1,
251
+ height=200 if fill_height else None,
252
+ type=self.type,
253
+ autoscroll=autoscroll,
254
+ examples=self.examples_messages
255
+ if not self._additional_inputs_in_examples
256
+ else None,
257
+ )
258
+ with Group():
259
+ with Row():
260
+ if textbox:
261
+ textbox.show_label = False
262
+ textbox_ = get_component_instance(textbox, render=True)
263
+ if not isinstance(textbox_, (Textbox, MultimodalTextbox)):
264
+ raise TypeError(
265
+ f"Expected a gr.Textbox or gr.MultimodalTextbox component, but got {builtins.type(textbox_)}"
266
+ )
267
+ self.textbox = textbox_
268
+ else:
269
+ textbox_component = (
270
+ MultimodalTextbox if self.multimodal else Textbox
271
+ )
272
+ self.textbox = textbox_component(
273
+ show_label=False,
274
+ label="Message",
275
+ placeholder="Type a message...",
276
+ scale=7,
277
+ autofocus=autofocus,
278
+ submit_btn=submit_btn,
279
+ stop_btn=stop_btn,
280
+ render=False,
281
+ )
282
+
283
+ # Hide the stop button at the beginning, and show it with the given value during the generator execution.
284
+ self.original_stop_btn = self.textbox.stop_btn
285
+ self.textbox.stop_btn = False
286
+
287
+ self.fake_api_btn = Button("Fake API", visible=False)
288
+ self.fake_response_textbox = Textbox(
289
+ label="Response", visible=False
290
+ )
291
+
292
+ with Group():
293
+ with Row():
294
+ Interface(
295
+ self.transcribe,
296
+ Audio(sources="microphone"),
297
+ self.textbox,
298
+ flagging_mode="never",
299
+ )
300
+
301
+ if self.examples:
302
+ self.examples_handler = Examples(
303
+ examples=self.examples,
304
+ inputs=[self.textbox] + self.additional_inputs,
305
+ outputs=self.chatbot,
306
+ fn=self._examples_stream_fn
307
+ if self.is_generator
308
+ else self._examples_fn,
309
+ cache_examples=self.cache_examples,
310
+ cache_mode=self.cache_mode,
311
+ visible=self._additional_inputs_in_examples,
312
+ preprocess=self._additional_inputs_in_examples,
313
+ )
314
+
315
+ any_unrendered_inputs = any(
316
+ not inp.is_rendered for inp in self.additional_inputs
317
+ )
318
+ if self.additional_inputs and any_unrendered_inputs:
319
+ with Accordion(**self.additional_inputs_accordion_params): # type: ignore
320
+ for input_component in self.additional_inputs:
321
+ if not input_component.is_rendered:
322
+ input_component.render()
323
+
324
+ self.saved_input = State()
325
+ self.chatbot_state = (
326
+ State(self.chatbot.value) if self.chatbot.value else State([])
327
+ )
328
+ self.previous_input = State(value=[])
329
+ self.show_progress = show_progress
330
+ self._setup_events()
331
+ self._setup_api()
332
+
333
+ @staticmethod
334
+ def _setup_example_messages(
335
+ examples: list[str] | list[MultimodalValue] | list[list] | None,
336
+ example_labels: list[str] | None = None,
337
+ example_icons: list[str] | None = None,
338
+ ) -> list[ExampleMessage]:
339
+ examples_messages = []
340
+ if examples:
341
+ for index, example in enumerate(examples):
342
+ if isinstance(example, list):
343
+ example = example[0]
344
+ example_message: ExampleMessage = {}
345
+ if isinstance(example, str):
346
+ example_message["text"] = example
347
+ elif isinstance(example, dict):
348
+ example_message["text"] = example.get("text", "")
349
+ example_message["files"] = example.get("files", [])
350
+ if example_labels:
351
+ example_message["display_text"] = example_labels[index]
352
+ if example_icons:
353
+ example_message["icon"] = example_icons[index]
354
+ examples_messages.append(example_message)
355
+ return examples_messages
356
+
357
+ def _setup_events(self) -> None:
358
+ submit_triggers = [self.textbox.submit, self.chatbot.retry]
359
+ submit_fn = self._stream_fn if self.is_generator else self._submit_fn
360
+ if hasattr(self.fn, "zerogpu"):
361
+ submit_fn.__func__.zerogpu = self.fn.zerogpu # type: ignore
362
+
363
+ submit_event = (
364
+ self.textbox.submit(
365
+ self._clear_and_save_textbox,
366
+ [self.textbox, self.previous_input],
367
+ [self.textbox, self.saved_input, self.previous_input],
368
+ show_api=False,
369
+ queue=False,
370
+ )
371
+ .then(
372
+ self._display_input,
373
+ [self.saved_input, self.chatbot],
374
+ [self.chatbot],
375
+ show_api=False,
376
+ queue=False,
377
+ )
378
+ .then(
379
+ submit_fn,
380
+ [self.saved_input, self.chatbot] + self.additional_inputs,
381
+ [self.chatbot] + self.additional_outputs,
382
+ show_api=False,
383
+ concurrency_limit=cast(
384
+ Union[int, Literal["default"], None], self.concurrency_limit
385
+ ),
386
+ show_progress=cast(
387
+ Literal["full", "minimal", "hidden"], self.show_progress
388
+ ),
389
+ )
390
+ )
391
+ submit_event.then(
392
+ lambda: update(value=None, interactive=True),
393
+ None,
394
+ self.textbox,
395
+ show_api=False,
396
+ )
397
+
398
+ if (
399
+ isinstance(self.chatbot, Chatbot)
400
+ and self.examples
401
+ and not self._additional_inputs_in_examples
402
+ ):
403
+ if self.cache_examples:
404
+ self.chatbot.example_select(
405
+ self.example_clicked,
406
+ None,
407
+ [self.chatbot, self.saved_input],
408
+ show_api=False,
409
+ )
410
+ else:
411
+ self.chatbot.example_select(
412
+ self.example_clicked,
413
+ None,
414
+ [self.chatbot, self.saved_input],
415
+ show_api=False,
416
+ ).then(
417
+ submit_fn,
418
+ [self.saved_input, self.chatbot],
419
+ [self.chatbot] + self.additional_outputs,
420
+ show_api=False,
421
+ concurrency_limit=cast(
422
+ Union[int, Literal["default"], None], self.concurrency_limit
423
+ ),
424
+ show_progress=cast(
425
+ Literal["full", "minimal", "hidden"], self.show_progress
426
+ ),
427
+ )
428
+
429
+ retry_event = (
430
+ self.chatbot.retry(
431
+ self._delete_prev_fn,
432
+ [self.saved_input, self.chatbot],
433
+ [self.chatbot, self.saved_input],
434
+ show_api=False,
435
+ queue=False,
436
+ )
437
+ .then(
438
+ lambda: update(interactive=False, placeholder=""),
439
+ outputs=[self.textbox],
440
+ show_api=False,
441
+ )
442
+ .then(
443
+ self._display_input,
444
+ [self.saved_input, self.chatbot],
445
+ [self.chatbot],
446
+ show_api=False,
447
+ queue=False,
448
+ )
449
+ .then(
450
+ submit_fn,
451
+ [self.saved_input, self.chatbot] + self.additional_inputs,
452
+ [self.chatbot] + self.additional_outputs,
453
+ show_api=False,
454
+ concurrency_limit=cast(
455
+ Union[int, Literal["default"], None], self.concurrency_limit
456
+ ),
457
+ show_progress=cast(
458
+ Literal["full", "minimal", "hidden"], self.show_progress
459
+ ),
460
+ )
461
+ )
462
+ retry_event.then(
463
+ lambda: update(interactive=True),
464
+ outputs=[self.textbox],
465
+ show_api=False,
466
+ )
467
+
468
+ self._setup_stop_events(submit_triggers, [submit_event, retry_event])
469
+
470
+ self.chatbot.undo(
471
+ self._undo_msg,
472
+ [self.previous_input, self.chatbot],
473
+ [self.chatbot, self.textbox, self.saved_input, self.previous_input],
474
+ show_api=False,
475
+ queue=False,
476
+ )
477
+
478
+ self.chatbot.option_select(
479
+ self.option_clicked,
480
+ [self.chatbot],
481
+ [self.chatbot, self.saved_input],
482
+ show_api=False,
483
+ ).then(
484
+ submit_fn,
485
+ [self.saved_input, self.chatbot],
486
+ [self.chatbot] + self.additional_outputs,
487
+ show_api=False,
488
+ concurrency_limit=cast(
489
+ Union[int, Literal["default"], None], self.concurrency_limit
490
+ ),
491
+ show_progress=cast(
492
+ Literal["full", "minimal", "hidden"], self.show_progress
493
+ ),
494
+ )
495
+
496
+ def _setup_stop_events(
497
+ self, event_triggers: list[Callable], events_to_cancel: list[Dependency]
498
+ ) -> None:
499
+ textbox_component = MultimodalTextbox if self.multimodal else Textbox
500
+ if self.is_generator:
501
+ original_submit_btn = self.textbox.submit_btn
502
+ for event_trigger in event_triggers:
503
+ event_trigger(
504
+ utils.async_lambda(
505
+ lambda: textbox_component(
506
+ submit_btn=False,
507
+ stop_btn=self.original_stop_btn,
508
+ )
509
+ ),
510
+ None,
511
+ [self.textbox],
512
+ show_api=False,
513
+ queue=False,
514
+ )
515
+ for event_to_cancel in events_to_cancel:
516
+ event_to_cancel.then(
517
+ utils.async_lambda(
518
+ lambda: textbox_component(
519
+ submit_btn=original_submit_btn, stop_btn=False
520
+ )
521
+ ),
522
+ None,
523
+ [self.textbox],
524
+ show_api=False,
525
+ queue=False,
526
+ )
527
+ self.textbox.stop(
528
+ None,
529
+ None,
530
+ None,
531
+ cancels=events_to_cancel, # type: ignore
532
+ show_api=False,
533
+ )
534
+
535
+ def _setup_api(self) -> None:
536
+ if self.is_generator:
537
+
538
+ @functools.wraps(self.fn)
539
+ async def api_fn(message, history, *args, **kwargs): # type: ignore
540
+ if self.is_async:
541
+ generator = self.fn(message, history, *args, **kwargs)
542
+ else:
543
+ generator = await anyio.to_thread.run_sync(
544
+ self.fn, message, history, *args, **kwargs, limiter=self.limiter
545
+ )
546
+ generator = utils.SyncToAsyncIterator(generator, self.limiter)
547
+ try:
548
+ first_response = await utils.async_iteration(generator)
549
+ yield first_response, history + [[message, first_response]]
550
+ except StopIteration:
551
+ yield None, history + [[message, None]]
552
+ async for response in generator:
553
+ yield response, history + [[message, response]]
554
+ else:
555
+
556
+ @functools.wraps(self.fn)
557
+ async def api_fn(message, history, *args, **kwargs):
558
+ if self.is_async:
559
+ response = await self.fn(message, history, *args, **kwargs)
560
+ else:
561
+ response = await anyio.to_thread.run_sync(
562
+ self.fn, message, history, *args, **kwargs, limiter=self.limiter
563
+ )
564
+ history.append([message, response])
565
+ return response, history
566
+
567
+ self.fake_api_btn.click(
568
+ api_fn,
569
+ [self.textbox, self.chatbot_state] + self.additional_inputs,
570
+ [self.fake_response_textbox, self.chatbot_state],
571
+ api_name=cast(Union[str, Literal[False]], self.api_name),
572
+ concurrency_limit=cast(
573
+ Union[int, Literal["default"], None], self.concurrency_limit
574
+ ),
575
+ )
576
+
577
+ def _clear_and_save_textbox(
578
+ self,
579
+ message: str | MultimodalPostprocess,
580
+ previous_input: list[str | MultimodalPostprocess],
581
+ ) -> tuple[
582
+ Textbox | MultimodalTextbox,
583
+ str | MultimodalPostprocess,
584
+ list[str | MultimodalPostprocess],
585
+ ]:
586
+ if self.multimodal:
587
+ previous_input += [message]
588
+ return (
589
+ MultimodalTextbox("", interactive=False, placeholder=""),
590
+ message,
591
+ previous_input,
592
+ )
593
+ else:
594
+ previous_input += [message]
595
+ return (
596
+ Textbox("", interactive=False, placeholder=""),
597
+ message,
598
+ previous_input,
599
+ )
600
+
601
+ def _append_multimodal_history(
602
+ self,
603
+ message: MultimodalPostprocess,
604
+ response: MessageDict | str | None,
605
+ history: list[MessageDict] | TupleFormat,
606
+ ):
607
+ if self.type == "tuples":
608
+ for x in message.get("files", []):
609
+ if isinstance(x, dict):
610
+ history.append([(x.get("path"),), None]) # type: ignore
611
+ else:
612
+ history.append([(x,), None]) # type: ignore
613
+ if message["text"] is None or not isinstance(message["text"], str):
614
+ return
615
+ elif message["text"] == "" and message.get("files", []) != []:
616
+ history.append([None, response]) # type: ignore
617
+ else:
618
+ history.append([message["text"], cast(str, response)]) # type: ignore
619
+ else:
620
+ for x in message.get("files", []):
621
+ if isinstance(x, dict):
622
+ history.append( # type: ignore
623
+ {"role": "user", "content": cast(FileDataDict, x)} # type: ignore
624
+ )
625
+ else:
626
+ history.append({"role": "user", "content": (x,)}) # type: ignore
627
+ if message["text"] is None or not isinstance(message["text"], str):
628
+ return
629
+ else:
630
+ history.append({"role": "user", "content": message["text"]}) # type: ignore
631
+ if response:
632
+ history.append(cast(MessageDict, response)) # type: ignore
633
+
634
+ async def _display_input(
635
+ self,
636
+ message: str | MultimodalPostprocess,
637
+ history: TupleFormat | list[MessageDict],
638
+ ) -> tuple[TupleFormat, TupleFormat] | tuple[list[MessageDict], list[MessageDict]]:
639
+ if self.multimodal and isinstance(message, dict):
640
+ self._append_multimodal_history(message, None, history)
641
+ elif isinstance(message, str) and self.type == "tuples":
642
+ history.append([message, None]) # type: ignore
643
+ elif isinstance(message, str) and self.type == "messages":
644
+ history.append({"role": "user", "content": message}) # type: ignore
645
+ return history # type: ignore
646
+
647
+ def response_as_dict(self, response: MessageDict | Message | str) -> MessageDict:
648
+ if isinstance(response, Message):
649
+ new_response = response.model_dump()
650
+ elif isinstance(response, str):
651
+ return {"role": "assistant", "content": response}
652
+ else:
653
+ new_response = response
654
+ return cast(MessageDict, new_response)
655
+
656
+ def _process_msg_and_trim_history(
657
+ self,
658
+ message: str | MultimodalPostprocess,
659
+ history_with_input: TupleFormat | list[MessageDict],
660
+ ) -> tuple[str | MultimodalPostprocess, TupleFormat | list[MessageDict]]:
661
+ if isinstance(message, dict):
662
+ remove_input = len(message.get("files", [])) + int(
663
+ message["text"] is not None
664
+ )
665
+ history = history_with_input[:-remove_input]
666
+ else:
667
+ history = history_with_input[:-1]
668
+ return message, history
669
+
670
+ def _append_history(self, history, message, first_response=True):
671
+ if self.type == "tuples":
672
+ if history:
673
+ history[-1][1] = message # type: ignore
674
+ else:
675
+ history.append([message, None])
676
+ else:
677
+ message = self.response_as_dict(message)
678
+ if first_response:
679
+ history.append(message) # type: ignore
680
+ else:
681
+ history[-1] = message
682
+
683
+ async def _submit_fn(
684
+ self,
685
+ message: str | MultimodalPostprocess,
686
+ history_with_input: TupleFormat | list[MessageDict],
687
+ request: Request,
688
+ *args,
689
+ ) -> TupleFormat | list[MessageDict] | tuple[TupleFormat | list[MessageDict], ...]:
690
+ message_serialized, history = self._process_msg_and_trim_history(
691
+ message, history_with_input
692
+ )
693
+ inputs, _, _ = special_args(
694
+ self.fn, inputs=[message_serialized, history, *args], request=request
695
+ )
696
+
697
+ if self.is_async:
698
+ response = await self.fn(*inputs)
699
+ else:
700
+ response = await anyio.to_thread.run_sync(
701
+ self.fn, *inputs, limiter=self.limiter
702
+ )
703
+ additional_outputs = None
704
+ if isinstance(response, tuple):
705
+ response, *additional_outputs = response
706
+
707
+ self._append_history(history_with_input, response)
708
+
709
+ if additional_outputs:
710
+ return history_with_input, *additional_outputs
711
+ return history_with_input
712
+
713
+ async def _stream_fn(
714
+ self,
715
+ message: str | MultimodalPostprocess,
716
+ history_with_input: TupleFormat | list[MessageDict],
717
+ request: Request,
718
+ *args,
719
+ ) -> AsyncGenerator[
720
+ TupleFormat | list[MessageDict] | tuple[TupleFormat | list[MessageDict], ...],
721
+ None,
722
+ ]:
723
+ message_serialized, history = self._process_msg_and_trim_history(
724
+ message, history_with_input
725
+ )
726
+ inputs, _, _ = special_args(
727
+ self.fn, inputs=[message_serialized, history, *args], request=request
728
+ )
729
+
730
+ if self.is_async:
731
+ generator = self.fn(*inputs)
732
+ else:
733
+ generator = await anyio.to_thread.run_sync(
734
+ self.fn, *inputs, limiter=self.limiter
735
+ )
736
+ generator = utils.SyncToAsyncIterator(generator, self.limiter)
737
+
738
+ additional_outputs = None
739
+ try:
740
+ first_response = await utils.async_iteration(generator)
741
+ if isinstance(first_response, tuple):
742
+ first_response, *additional_outputs = first_response
743
+ self._append_history(history_with_input, first_response)
744
+ yield (
745
+ history_with_input
746
+ if not additional_outputs
747
+ else (history_with_input, *additional_outputs)
748
+ )
749
+ except StopIteration:
750
+ yield history_with_input
751
+ async for response in generator:
752
+ if isinstance(response, tuple):
753
+ response, *additional_outputs = response
754
+ self._append_history(history_with_input, response, first_response=False)
755
+ yield (
756
+ history_with_input
757
+ if not additional_outputs
758
+ else (history_with_input, *additional_outputs)
759
+ )
760
+
761
+ def option_clicked(
762
+ self, history: list[MessageDict], option: SelectData
763
+ ) -> tuple[TupleFormat | list[MessageDict], str | MultimodalPostprocess]:
764
+ """
765
+ When an option is clicked, the chat history is appended with the option value.
766
+ The saved input value is also set to option value. Note that event can only
767
+ be called if self.type is "messages" since options are only available for this
768
+ chatbot type.
769
+ """
770
+ history.append({"role": "user", "content": option.value})
771
+ return history, option.value
772
+
773
+ def example_clicked(
774
+ self, example: SelectData
775
+ ) -> Generator[
776
+ tuple[TupleFormat | list[MessageDict], str | MultimodalPostprocess], None, None
777
+ ]:
778
+ """
779
+ When an example is clicked, the chat history (and saved input) is initially set only
780
+ to the example message. Then, if example caching is enabled, the cached response is loaded
781
+ and added to the chat history as well.
782
+ """
783
+ if self.type == "tuples":
784
+ history = [(example.value["text"], None)]
785
+ for file in example.value.get("files", []):
786
+ history.append(((file["path"]), None))
787
+ else:
788
+ history = [MessageDict(role="user", content=example.value["text"])]
789
+ for file in example.value.get("files", []):
790
+ history.append(MessageDict(role="user", content=file))
791
+ message = example.value if self.multimodal else example.value["text"]
792
+ yield history, message
793
+ if self.cache_examples:
794
+ history = self.examples_handler.load_from_cache(example.index)[0].root
795
+ yield history, message
796
+
797
+ def _process_example(
798
+ self, message: ExampleMessage | str, response: MessageDict | str | None
799
+ ):
800
+ result = []
801
+ if self.multimodal:
802
+ message = cast(ExampleMessage, message)
803
+ if self.type == "tuples":
804
+ if "text" in message:
805
+ result.append([message["text"], None])
806
+ for file in message.get("files", []):
807
+ result.append([file, None])
808
+ result[-1][1] = response
809
+ else:
810
+ if "text" in message:
811
+ result.append({"role": "user", "content": message["text"]})
812
+ for file in message.get("files", []):
813
+ result.append({"role": "assistant", "content": file})
814
+ result.append({"role": "assistant", "content": response})
815
+ else:
816
+ message = cast(str, message)
817
+ if self.type == "tuples":
818
+ result = [[message, response]]
819
+ else:
820
+ result = [
821
+ {"role": "user", "content": message},
822
+ {"role": "assistant", "content": response},
823
+ ]
824
+ return result
825
+
826
+ async def _examples_fn(
827
+ self, message: ExampleMessage | str, *args
828
+ ) -> TupleFormat | list[MessageDict]:
829
+ inputs, _, _ = special_args(self.fn, inputs=[message, [], *args], request=None)
830
+ if self.is_async:
831
+ response = await self.fn(*inputs)
832
+ else:
833
+ response = await anyio.to_thread.run_sync(
834
+ self.fn, *inputs, limiter=self.limiter
835
+ )
836
+ return self._process_example(message, response) # type: ignore
837
+
838
+ async def _examples_stream_fn(
839
+ self,
840
+ message: str,
841
+ *args,
842
+ ) -> AsyncGenerator:
843
+ inputs, _, _ = special_args(self.fn, inputs=[message, [], *args], request=None)
844
+
845
+ if self.is_async:
846
+ generator = self.fn(*inputs)
847
+ else:
848
+ generator = await anyio.to_thread.run_sync(
849
+ self.fn, *inputs, limiter=self.limiter
850
+ )
851
+ generator = utils.SyncToAsyncIterator(generator, self.limiter)
852
+ async for response in generator:
853
+ yield self._process_example(message, response)
854
+
855
+ async def _delete_prev_fn(
856
+ self,
857
+ message: str | MultimodalPostprocess | None,
858
+ history: list[MessageDict] | TupleFormat,
859
+ ) -> tuple[list[MessageDict] | TupleFormat, str | MultimodalPostprocess]:
860
+ extra = 1 if self.type == "messages" else 0
861
+ if self.multimodal and isinstance(message, dict):
862
+ remove_input = (
863
+ len(message.get("files", [])) + 1
864
+ if message["text"] is not None
865
+ else len(message.get("files", []))
866
+ ) + extra
867
+ history = history[:-remove_input]
868
+ else:
869
+ history = history[: -(1 + extra)]
870
+ return history, message or "" # type: ignore
871
+
872
+ async def _undo_msg(
873
+ self,
874
+ previous_input: list[str | MultimodalPostprocess],
875
+ history: list[MessageDict] | TupleFormat,
876
+ ):
877
+ msg = previous_input.pop() if previous_input else None
878
+
879
+ history, msg = await self._delete_prev_fn(msg, history)
880
+ previous_msg = previous_input[-1] if len(previous_input) else msg
881
+ return history, msg, previous_msg, previous_input
882
+
883
+ def render(self) -> ChatInterface:
884
+ # If this is being rendered inside another Blocks, and the height is not explicitly set, set it to 400 instead of 200.
885
+ if get_blocks_context() and not self.provided_chatbot:
886
+ self.chatbot.height = 400
887
+ super().render()
888
+ return self