Save the extensions after Gradio 4 (#5632)

This commit is contained in:
oobabooga 2024-03-05 07:54:34 -03:00 committed by GitHub
parent ae12d045ea
commit d61e31e182
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,9 +1,72 @@
'''
Copied from: https://github.com/AUTOMATIC1111/stable-diffusion-webui/pull/14184
'''
import inspect
import warnings
from functools import wraps
import gradio as gr
def Box(*args, **kwargs):
return gr.Blocks(*args, **kwargs)
class GradioDeprecationWarning(DeprecationWarning):
pass
if not hasattr(gr, 'Box'):
gr.Box = Box
def repair(grclass):
if not getattr(grclass, 'EVENTS', None):
return
@wraps(grclass.__init__)
def __repaired_init__(self, *args, tooltip=None, source=None, original=grclass.__init__, **kwargs):
if source:
kwargs["sources"] = [source]
allowed_kwargs = inspect.signature(original).parameters
fixed_kwargs = {}
for k, v in kwargs.items():
if k in allowed_kwargs:
fixed_kwargs[k] = v
else:
warnings.warn(f"unexpected argument for {grclass.__name__}: {k}", GradioDeprecationWarning, stacklevel=2)
original(self, *args, **fixed_kwargs)
self.webui_tooltip = tooltip
for event in self.EVENTS:
replaced_event = getattr(self, str(event))
def fun(*xargs, _js=None, replaced_event=replaced_event, **xkwargs):
if _js:
xkwargs['js'] = _js
return replaced_event(*xargs, **xkwargs)
setattr(self, str(event), fun)
grclass.__init__ = __repaired_init__
grclass.update = gr.update
for component in set(gr.components.__all__ + gr.layouts.__all__):
repair(getattr(gr, component, None))
class Dependency(gr.events.Dependency):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def then(*xargs, _js=None, **xkwargs):
if _js:
xkwargs['js'] = _js
return original_then(*xargs, **xkwargs)
original_then = self.then
self.then = then
gr.events.Dependency = Dependency
gr.Box = gr.Group