-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Removing .update and get_config, attempt 2 #5240
Changes from 8 commits
853b354
7745823
3d9bb9b
e6aeba1
0a512ae
ec95e4b
6f52f81
9f0a42c
967cbdc
e0a8fb7
2158698
904eebd
0d011bd
9911390
33f5922
2d4b9ad
f92523b
3f1b544
e81cc48
42b1f28
559079d
715c21a
d3fdf6d
f3f182c
ac3aa0c
036c8f5
113bc7c
a892bcd
a282042
01db73f
0f2aefa
0159898
06a2027
df7f322
fd040e9
c2ba111
af00a1e
2feda23
2db78d0
5426e9e
bf37be4
8061297
ea73321
ee2e2f5
02bfde4
d00dacd
d36607d
81fe76a
0feb9de
82c9405
0868c25
b5c50fc
9a52f9c
08f7baa
1f4d71b
4c15cca
efaddcc
616f9d1
c210d37
58075aa
d2909aa
b6486c4
aa617d8
d3317cf
ab40a47
a15058c
3b58172
b0a3575
46bf1a9
f3b284e
8cf05d9
b0b55c8
1fc8b24
1dac13c
e69b833
e552936
5811531
1a4048d
0a3d751
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
--- | ||
"gradio": minor | ||
--- | ||
|
||
feat:Cleanup of .update and .get_config per component | ||
|
||
get_config is removed, the config used is simply any attribute that is in the Block that shares a name with one of the constructor paramaters. | ||
|
||
update is not removed for backwards compatibility, but deprecated. Instead return the component itself. Created a updateable decorator that simply checks to see if we're in an update, and if so, skips the constructor and wraps the args and kwargs in an update dictionary. easy peasy. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: blocks_essay"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "\n", "def change_textbox(choice):\n", " if choice == \"short\":\n", " return gr.Textbox.update(lines=2, visible=True)\n", " elif choice == \"long\":\n", " return gr.Textbox.update(lines=8, visible=True)\n", " else:\n", " return gr.Textbox.update(visible=False)\n", "\n", "\n", "with gr.Blocks() as demo:\n", " radio = gr.Radio(\n", " [\"short\", \"long\", \"none\"], label=\"What kind of essay would you like to write?\"\n", " )\n", " text = gr.Textbox(lines=2, interactive=True).style(show_copy_button=True)\n", "\n", " radio.change(fn=change_textbox, inputs=radio, outputs=text)\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} | ||
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: blocks_essay"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "\n", "\n", "def change_textbox(choice):\n", " if choice == \"short\":\n", " return gr.Textbox(lines=2, visible=True)\n", " elif choice == \"long\":\n", " return gr.Textbox(lines=8, visible=True, value=\"Lorem ipsum dolor sit amet\")\n", " else:\n", " return gr.Textbox(visible=False)\n", "\n", "\n", "with gr.Blocks() as demo:\n", " radio = gr.Radio(\n", " [\"short\", \"long\", \"none\"], label=\"What kind of essay would you like to write?\"\n", " )\n", " text = gr.Textbox(lines=2, interactive=True, show_copy_button=True)\n", " radio.change(fn=change_textbox, inputs=radio, outputs=text)\n", "\n", " with gr.Row():\n", " num = gr.Number(minimum=0, maximum=100, label=\"input\")\n", " out = gr.Number(label=\"output\")\n", " min = gr.Slider(0, 100, 0, label=\"min\")\n", " max = gr.Slider(0, 100, 100, label=\"max\")\n", "\n", " def reset_bounds(min, max):\n", " return gr.Number(minimum=min, maximum=max)\n", " \n", " min.change(reset_bounds, [min, max], outputs=num)\n", " max.change(reset_bounds, [min, max], outputs=num)\n", " num.submit(lambda x:x, num, out)\n", "\n", "\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: chatbot_multimodal"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "import time\n", "\n", "# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.\n", "\n", "def add_text(history, text):\n", " history = history + [(text, None)]\n", " return history, gr.update(value=\"\", interactive=False)\n", "\n", "\n", "def add_file(history, file):\n", " history = history + [((file.name,), None)]\n", " return history\n", "\n", "\n", "def bot(history):\n", " response = \"**That's cool!**\"\n", " history[-1][1] = \"\"\n", " for character in response:\n", " history[-1][1] += character\n", " time.sleep(0.05)\n", " yield history\n", "\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot([], elem_id=\"chatbot\").style(height=750)\n", "\n", " with gr.Row():\n", " with gr.Column(scale=0.85):\n", " txt = gr.Textbox(\n", " show_label=False,\n", " placeholder=\"Enter text and press enter, or upload an image\",\n", " ).style(container=False)\n", " with gr.Column(scale=0.15, min_width=0):\n", " btn = gr.UploadButton(\"\ud83d\udcc1\", file_types=[\"image\", \"video\", \"audio\"])\n", "\n", " txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(\n", " bot, chatbot, chatbot\n", " )\n", " txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)\n", " file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(\n", " bot, chatbot, chatbot\n", " )\n", "\n", "demo.queue()\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} | ||
{"cells": [{"cell_type": "markdown", "id": 302934307671667531413257853548643485645, "metadata": {}, "source": ["# Gradio Demo: chatbot_multimodal"]}, {"cell_type": "code", "execution_count": null, "id": 272996653310673477252411125948039410165, "metadata": {}, "outputs": [], "source": ["!pip install -q gradio "]}, {"cell_type": "code", "execution_count": null, "id": 288918539441861185822528903084949547379, "metadata": {}, "outputs": [], "source": ["import gradio as gr\n", "import random\n", "import time\n", "\n", "# Chatbot demo with multimodal input (text, markdown, LaTeX, code blocks, image, audio, & video). Plus shows support for streaming text.\n", "\n", "def add_text(history, text):\n", " history = history + [(text, None)]\n", " return history, gr.update(value=\"\", interactive=False)\n", "\n", "\n", "def add_file(history, file):\n", " history = history + [((file.name,), None)]\n", " return history\n", "\n", "\n", "def bot(history):\n", " response = \"**That's cool!**\"\n", " history[-1][1] = \"\"\n", " for character in response:\n", " history[-1][1] += character\n", " time.sleep(0.05)\n", " yield history\n", "\n", "\n", "with gr.Blocks() as demo:\n", " chatbot = gr.Chatbot([], elem_id=\"chatbot\", height=750)\n", "\n", " with gr.Row():\n", " with gr.Column(scale=0.85):\n", " txt = gr.Textbox(\n", " show_label=False,\n", " placeholder=\"Enter text and press enter, or upload an image\",\n", " container=False)\n", " with gr.Column(scale=0.15, min_width=0):\n", " btn = gr.UploadButton(\"\ud83d\udcc1\", file_types=[\"image\", \"video\", \"audio\"])\n", "\n", " txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=False).then(\n", " bot, chatbot, chatbot\n", " )\n", " txt_msg.then(lambda: gr.update(interactive=True), None, [txt], queue=False)\n", " file_msg = btn.upload(add_file, [chatbot, btn], [chatbot], queue=False).then(\n", " bot, chatbot, chatbot\n", " )\n", "\n", "demo.queue()\n", "if __name__ == \"__main__\":\n", " demo.launch()\n"]}], "metadata": {}, "nbformat": 4, "nbformat_minor": 5} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import webbrowser | ||
from abc import abstractmethod | ||
from collections import defaultdict | ||
from functools import wraps | ||
from pathlib import Path | ||
from types import ModuleType | ||
from typing import TYPE_CHECKING, Any, AsyncIterator, Callable, Literal, cast | ||
|
@@ -113,6 +114,7 @@ def __init__( | |
self._skip_init_processing = _skip_init_processing | ||
self.parent: BlockContext | None = None | ||
self.is_rendered: bool = False | ||
self.update_config: dict | ||
|
||
if render: | ||
self.render() | ||
|
@@ -307,12 +309,13 @@ def set_event_trigger( | |
return dependency, len(Context.root_block.dependencies) - 1 | ||
|
||
def get_config(self): | ||
return { | ||
"visible": self.visible, | ||
"elem_id": self.elem_id, | ||
"elem_classes": self.elem_classes, | ||
"root_url": self.root_url, | ||
} | ||
config = {} | ||
signature = inspect.signature(self.__class__.__init__) | ||
for parameter in signature.parameters.values(): | ||
if hasattr(self, parameter.name): | ||
value = getattr(self, parameter.name) | ||
config[parameter.name] = value | ||
return {**config, "root_url": self.root_url, "name": self.get_block_name()} | ||
|
||
@staticmethod | ||
@abstractmethod | ||
|
@@ -1327,6 +1330,8 @@ def postprocess_data( | |
output.append(None) | ||
else: | ||
prediction_value = predictions[i] | ||
if isinstance(prediction_value, Block): | ||
prediction_value = prediction_value.update_config | ||
if utils.is_update(prediction_value): | ||
assert isinstance(prediction_value, dict) | ||
prediction_value = postprocess_update_dict( | ||
|
@@ -2241,3 +2246,29 @@ def queue_enabled_for_fn(self, fn_index: int): | |
if self.dependencies[fn_index]["queue"] is None: | ||
return self.enable_queue | ||
return self.dependencies[fn_index]["queue"] | ||
|
||
|
||
def is_update(): | ||
from gradio import context | ||
|
||
return hasattr(context.thread_data, "blocks") | ||
|
||
|
||
def updateable(fn): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can automatically wrap the init with updateable in a metaclass so that custom component authors don't even have to worry about this when creating their own components 👀 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. was trying to think how to do this. Not familiar with metaclasses, will take a look! |
||
@wraps(fn) | ||
def wrapper(*args, **kwargs): | ||
if is_update(): | ||
fn_args = inspect.getfullargspec(fn).args | ||
self = args[0] | ||
for i, arg in enumerate(args): | ||
if i == 0: # skip self | ||
continue | ||
arg_name = fn_args[i] | ||
kwargs[arg_name] = arg | ||
kwargs["__type__"] = "update" | ||
self.update_config = kwargs | ||
return None | ||
else: | ||
return fn(*args, **kwargs) | ||
|
||
return wrapper |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need to make the changeset thing support multiline comments for fix + feat. I'll do that tomorrow, should be quick.