Skip to content

Commit

Permalink
Make internal event handlers of gr.Interface and gr.ChatInterface asy…
Browse files Browse the repository at this point in the history
…nc (#7893)

* Make async

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
  • Loading branch information
freddyaboulton and gradio-pr-bot committed Mar 29, 2024
1 parent c7933ff commit f42d3e2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 17 deletions.
5 changes: 5 additions & 0 deletions .changeset/strong-hornets-push.md
@@ -0,0 +1,5 @@
---
"gradio": patch
---

feat:Make internal event handlers of gr.Interface and gr.ChatInterface async
24 changes: 13 additions & 11 deletions gradio/chat_interface.py
Expand Up @@ -29,7 +29,7 @@
from gradio.layouts import Accordion, Group, Row
from gradio.routes import Request
from gradio.themes import ThemeClass as Theme
from gradio.utils import SyncToAsyncIterator, async_iteration
from gradio.utils import SyncToAsyncIterator, async_iteration, async_lambda


@document()
Expand Down Expand Up @@ -378,7 +378,7 @@ def _setup_events(self) -> None:
show_api=False,
queue=False,
).then(
lambda x: x,
async_lambda(lambda x: x),
[self.saved_input],
[self.textbox],
show_api=False,
Expand All @@ -387,7 +387,7 @@ def _setup_events(self) -> None:

if self.clear_btn:
self.clear_btn.click(
lambda: ([], [], None),
async_lambda(lambda: ([], [], None)),
None,
[self.chatbot, self.chatbot_state, self.saved_input],
queue=False,
Expand All @@ -401,17 +401,19 @@ def _setup_stop_events(
if self.submit_btn:
for event_trigger in event_triggers:
event_trigger(
lambda: (
Button(visible=False),
Button(visible=True),
async_lambda(
lambda: (
Button(visible=False),
Button(visible=True),
)
),
None,
[self.submit_btn, self.stop_btn],
show_api=False,
queue=False,
)
event_to_cancel.then(
lambda: (Button(visible=True), Button(visible=False)),
async_lambda(lambda: (Button(visible=True), Button(visible=False))),
None,
[self.submit_btn, self.stop_btn],
show_api=False,
Expand All @@ -420,14 +422,14 @@ def _setup_stop_events(
else:
for event_trigger in event_triggers:
event_trigger(
lambda: Button(visible=True),
async_lambda(lambda: Button(visible=True)),
None,
[self.stop_btn],
show_api=False,
queue=False,
)
event_to_cancel.then(
lambda: Button(visible=False),
async_lambda(lambda: Button(visible=False)),
None,
[self.stop_btn],
show_api=False,
Expand Down Expand Up @@ -471,7 +473,7 @@ def _append_multimodal_history(
if message["text"] is not None and isinstance(message["text"], str):
history.append([message["text"], response])

def _display_input(
async def _display_input(
self, message: str | dict[str, list], history: list[list[str | tuple | None]]
) -> tuple[list[list[str | tuple | None]], list[list[str | tuple | None]]]:
if self.multimodal and isinstance(message, dict):
Expand Down Expand Up @@ -631,7 +633,7 @@ async def _examples_stream_fn(
async for response in generator:
yield [[message, response]]

def _delete_prev_fn(
async def _delete_prev_fn(
self,
message: str | dict[str, list],
history: list[list[str | tuple | None]],
Expand Down
20 changes: 14 additions & 6 deletions gradio/interface.py
Expand Up @@ -701,14 +701,16 @@ def attach_submit_events(
if _stop_btn:
extra_output = [_submit_btn, _stop_btn]

def cleanup():
async def cleanup():
return [Button(visible=True), Button(visible=False)]

predict_event = on(
triggers,
lambda: (
Button(visible=False),
Button(visible=True),
utils.async_lambda(
lambda: (
Button(visible=False),
Button(visible=True),
)
),
inputs=None,
outputs=[_submit_btn, _stop_btn],
Expand Down Expand Up @@ -827,7 +829,9 @@ def attach_flagging_events(
)
flag_method = FlagMethod(self.flagging_callback, label, value)
flag_btn.click(
lambda: Button(value="Saving...", interactive=False),
utils.async_lambda(
lambda: Button(value="Saving...", interactive=False)
),
None,
flag_btn,
queue=False,
Expand All @@ -842,7 +846,11 @@ def attach_flagging_events(
show_api=False,
)
_clear_btn.click(
flag_method.reset, None, flag_btn, queue=False, show_api=False
utils.async_lambda(flag_method.reset),
None,
flag_btn,
queue=False,
show_api=False,
)

def render_examples(self):
Expand Down
13 changes: 13 additions & 0 deletions gradio/utils.py
Expand Up @@ -26,6 +26,7 @@
from abc import ABC, abstractmethod
from collections import OrderedDict
from contextlib import contextmanager
from functools import wraps
from io import BytesIO
from numbers import Number
from pathlib import Path
Expand Down Expand Up @@ -1245,3 +1246,15 @@ def simplify_file_data_in_str(s):
if isinstance(payload, str):
return payload
return json.dumps(payload)


def async_lambda(f: Callable) -> Callable:
"""Turn a function into an async function.
Useful for internal event handlers defined as lambda functions used in the codebase
"""

@wraps(f)
async def function_wrapper(*args, **kwargs):
return f(*args, **kwargs)

return function_wrapper

0 comments on commit f42d3e2

Please sign in to comment.