This commit is contained in:
Haoming 2025-07-30 10:24:16 +08:00
parent ffb5a4d4a5
commit 62f6c016ae
2 changed files with 31 additions and 32 deletions

View File

@ -1,32 +1,30 @@
import inspect
import types
import warnings
from functools import wraps
import gradio as gr
import gradio.component_meta
from modules import scripts, ui_tempdir, patches
from modules import patches, scripts, ui_tempdir
class GradioDeprecationWarning(DeprecationWarning):
pass
def add_classes_to_gradio_component(comp):
def add_classes_to_gradio_component(comp: gr.components.Component):
"""
this adds gradio-* to the component for css styling (ie gradio-button to gr.Button), as well as some others
"""
comp.elem_classes = [f"gradio-{comp.get_block_name()}", *(getattr(comp, 'elem_classes', None) or [])]
comp.elem_classes = [f"gradio-{comp.get_block_name()}", *(getattr(comp, "elem_classes", None) or [])]
if getattr(comp, 'multiselect', False):
comp.elem_classes.append('multiselect')
if getattr(comp, "multiselect", False):
comp.elem_classes.append("multiselect")
def IOComponent_init(self, *args, **kwargs):
self.webui_tooltip = kwargs.pop('tooltip', None)
self.webui_tooltip = kwargs.pop("tooltip", None)
if scripts.scripts_current is not None:
scripts.scripts_current.before_component(self, **kwargs)
@ -48,11 +46,11 @@ def IOComponent_init(self, *args, **kwargs):
def Block_get_config(self):
config = original_Block_get_config(self)
webui_tooltip = getattr(self, 'webui_tooltip', None)
webui_tooltip = getattr(self, "webui_tooltip", None)
if webui_tooltip:
config["webui_tooltip"] = webui_tooltip
config.pop('example_inputs', None)
config.pop("example_inputs", None)
return config
@ -95,14 +93,14 @@ ui_tempdir.install_ui_tempdir_override()
def gradio_component_meta_create_or_modify_pyi(component_class, class_name, events):
if hasattr(component_class, 'webui_do_not_create_gradio_pyi_thank_you'):
if hasattr(component_class, "webui_do_not_create_gradio_pyi_thank_you"):
return
gradio_component_meta_create_or_modify_pyi_original(component_class, class_name, events)
# this prevents creation of .pyi files in webui dir
gradio_component_meta_create_or_modify_pyi_original = patches.patch(__file__, gradio.component_meta, 'create_or_modify_pyi', gradio_component_meta_create_or_modify_pyi)
gradio_component_meta_create_or_modify_pyi_original = patches.patch(__file__, gradio.component_meta, "create_or_modify_pyi", gradio_component_meta_create_or_modify_pyi)
# this function is broken and does not seem to do anything useful
gradio.component_meta.updateable = lambda x: x
@ -111,15 +109,15 @@ gradio.component_meta.updateable = lambda x: x
class EventWrapper:
def __init__(self, replaced_event):
self.replaced_event = replaced_event
self.has_trigger = getattr(replaced_event, 'has_trigger', None)
self.event_name = getattr(replaced_event, 'event_name', None)
self.callback = getattr(replaced_event, 'callback', None)
self.real_self = getattr(replaced_event, '__self__', None)
self.has_trigger = getattr(replaced_event, "has_trigger", None)
self.event_name = getattr(replaced_event, "event_name", None)
self.callback = getattr(replaced_event, "callback", None)
self.real_self = getattr(replaced_event, "__self__", None)
def __call__(self, *args, **kwargs):
if '_js' in kwargs:
kwargs['js'] = kwargs['_js']
del kwargs['_js']
if "_js" in kwargs:
kwargs["js"] = kwargs["_js"]
del kwargs["_js"]
return self.replaced_event(*args, **kwargs)
@property
@ -128,7 +126,7 @@ class EventWrapper:
def repair(grclass):
if not getattr(grclass, 'EVENTS', None):
if not getattr(grclass, "EVENTS", None):
return
@wraps(grclass.__init__)
@ -167,7 +165,7 @@ class Dependency(gr.events.Dependency):
def then(*xargs, _js=None, **xkwargs):
if _js:
xkwargs['js'] = _js
xkwargs["js"] = _js
return original_then(*xargs, **xkwargs)
@ -178,4 +176,3 @@ class Dependency(gr.events.Dependency):
gr.events.Dependency = Dependency
gr.Box = gr.Group

View File

@ -1,12 +1,13 @@
import os
import gradio as gr
from modules import localization, shared, scripts, util
from modules.paths import script_path, data_path
from modules import localization, scripts, shared, util
from modules.paths import data_path, script_path
def webpath(fn):
return f'file={util.truncate_path(fn)}?{os.path.getmtime(fn)}'
return f"file={util.truncate_path(fn)}?{os.path.getmtime(fn)}"
def javascript_html():
@ -23,7 +24,7 @@ def javascript_html():
head += f'<script type="module" src="{webpath(script.path)}"></script>\n'
if shared.cmd_opts.theme:
head += f'<script type="text/javascript">set_theme(\"{shared.cmd_opts.theme}\");</script>\n'
head += f'<script type="text/javascript">set_theme("{shared.cmd_opts.theme}");</script>\n'
return head
@ -42,9 +43,10 @@ def css_html():
head += stylesheet(user_css)
from modules.shared_gradio_themes import resolve_var
light = resolve_var('background_fill_primary')
dark = resolve_var('background_fill_primary_dark')
head += f'<style>html {{ background-color: {light}; }} @media (prefers-color-scheme: dark) {{ html {{background-color: {dark}; }} }}</style>'
light = resolve_var("background_fill_primary")
dark = resolve_var("background_fill_primary_dark")
head += f"<style>html {{ background-color: {light}; }} @media (prefers-color-scheme: dark) {{ html {{background-color: {dark}; }} }}</style>"
return head
@ -55,13 +57,13 @@ def reload_javascript():
def template_response(*args, **kwargs):
res = shared.GradioTemplateResponseOriginal(*args, **kwargs)
res.body = res.body.replace(b'</head>', f'{js}<meta name="referrer" content="no-referrer"/></head>'.encode("utf8"))
res.body = res.body.replace(b'</body>', f'{css}</body>'.encode("utf8"))
res.body = res.body.replace(b"</head>", f'{js}<meta name="referrer" content="no-referrer"/></head>'.encode("utf8"))
res.body = res.body.replace(b"</body>", f"{css}</body>".encode("utf8"))
res.init_headers()
return res
gr.routes.templates.TemplateResponse = template_response
if not hasattr(shared, 'GradioTemplateResponseOriginal'):
if not hasattr(shared, "GradioTemplateResponseOriginal"):
shared.GradioTemplateResponseOriginal = gr.routes.templates.TemplateResponse