From 08c68820cda88f254a1a0d8b871e7695f6d83073 Mon Sep 17 00:00:00 2001 From: w-e-w <40751091+w-e-w@users.noreply.github.com> Date: Mon, 27 Nov 2023 20:30:49 +0900 Subject: [PATCH] gradio 3.41.2 attempt --- modules/api/models.py | 4 +-- modules/gradio_extensons.py | 71 ++++++++++++++++++++++++++++++++++++- modules/ui.py | 4 +-- modules/ui_tempdir.py | 2 +- requirements.txt | 2 +- requirements_versions.txt | 4 +-- 6 files changed, 78 insertions(+), 9 deletions(-) diff --git a/modules/api/models.py b/modules/api/models.py index a0d80af8c..b1d3f03ac 100644 --- a/modules/api/models.py +++ b/modules/api/models.py @@ -93,8 +93,8 @@ class PydanticModelGenerator: d.field: (d.field_type, Field(default=d.field_value, alias=d.field_alias, exclude=d.field_exclude)) for d in self._model_def } DynamicModel = create_model(self._model_name, **fields) - DynamicModel.__config__.allow_population_by_field_name = True - DynamicModel.__config__.allow_mutation = True + DynamicModel.model_config['populate_by_name'] = True + DynamicModel.model_config['frozen'] = True return DynamicModel StableDiffusionTxt2ImgProcessingAPI = PydanticModelGenerator( diff --git a/modules/gradio_extensons.py b/modules/gradio_extensons.py index e6b6835ad..fe1840a8d 100644 --- a/modules/gradio_extensons.py +++ b/modules/gradio_extensons.py @@ -1,3 +1,5 @@ +from inspect import signature +from functools import wraps import gradio as gr from modules import scripts, ui_tempdir, patches @@ -64,10 +66,77 @@ def Blocks_get_config_file(self, *args, **kwargs): return config -original_IOComponent_init = patches.patch(__name__, obj=gr.components.IOComponent, field="__init__", replacement=IOComponent_init) +def gradio_component_compatibility_layer(component_function): + @wraps(component_function) + def patched_function(*args, **kwargs): + original_signature = signature(component_function).parameters + valid_kwargs = {k: v for k, v in kwargs.items() if k in original_signature} + result = component_function(*args, **valid_kwargs) + return result + + return patched_function + + +sub_events = ['then', 'success'] + + +def gradio_component_events_compatibility_layer(component_function): + @wraps(component_function) + def patched_function(*args, **kwargs): + kwargs['js'] = kwargs.get('js', kwargs.pop('_js', None)) + original_signature = signature(component_function).parameters + valid_kwargs = {k: v for k, v in kwargs.items() if k in original_signature} + + result = component_function(*args, **valid_kwargs) + + for sub_event in sub_events: + component_event_then_function = getattr(result, sub_event, None) + if component_event_then_function: + patched_component_event_then_function = gradio_component_sub_events_compatibility_layer(component_event_then_function) + setattr(result, sub_event, patched_component_event_then_function) + # original_component_event_then_function = patches.patch(f'{__name__}.', obj=result, field='then', replacement=patched_component_event_then_function) + + return result + + return patched_function + + +def gradio_component_sub_events_compatibility_layer(component_function): + @wraps(component_function) + def patched_function(*args, **kwargs): + kwargs['js'] = kwargs.get('js', kwargs.pop('_js', None)) + original_signature = signature(component_function).parameters + valid_kwargs = {k: v for k, v in kwargs.items() if k in original_signature} + result = component_function(*args, **valid_kwargs) + return result + + return patched_function + + +for component_name in set(gr.components.__all__ + gr.layouts.__all__): + try: + component = getattr(gr, component_name) + component_init = getattr(component, '__init__') + patched_component_init = gradio_component_compatibility_layer(component_init) + original_IOComponent_init = patches.patch(f'{__name__}.{component_name}', obj=component, field="__init__", replacement=patched_component_init) + + component_events = set(getattr(component, 'EVENTS')) + for component_event in component_events: + component_event_function = getattr(component, component_event) + patched_component_event_function = gradio_component_events_compatibility_layer(component_event_function) + original_component_event_function = patches.patch(f'{__name__}.{component_name}.{component_event}', obj=component, field=component_event, replacement=patched_component_event_function) + except Exception as e: + print(e) + pass + +gr.Box = gr.Group + + +original_IOComponent_init = patches.patch(__name__, obj=gr.components.base.Component, field="__init__", replacement=IOComponent_init) original_Block_get_config = patches.patch(__name__, obj=gr.blocks.Block, field="get_config", replacement=Block_get_config) original_BlockContext_init = patches.patch(__name__, obj=gr.blocks.BlockContext, field="__init__", replacement=BlockContext_init) original_Blocks_get_config_file = patches.patch(__name__, obj=gr.blocks.Blocks, field="get_config_file", replacement=Blocks_get_config_file) ui_tempdir.install_ui_tempdir_override() + diff --git a/modules/ui.py b/modules/ui.py index 08e0ad775..e5fd33253 100644 --- a/modules/ui.py +++ b/modules/ui.py @@ -32,8 +32,8 @@ from modules.generation_parameters_copypaste import image_from_url_text create_setting_component = ui_settings.create_setting_component -warnings.filterwarnings("default" if opts.show_warnings else "ignore", category=UserWarning) -warnings.filterwarnings("default" if opts.show_gradio_deprecation_warnings else "ignore", category=gr.deprecation.GradioDeprecationWarning) +# warnings.filterwarnings("default" if opts.show_warnings else "ignore", category=UserWarning) +# warnings.filterwarnings("default" if opts.show_gradio_deprecation_warnings else "ignore", category=gr.deprecation.GradioDeprecationWarning) # this is a fix for Windows users. Without it, javascript files will be served with text/html content-type and the browser will not show any UI mimetypes.init() diff --git a/modules/ui_tempdir.py b/modules/ui_tempdir.py index 85015db56..52d2ac312 100644 --- a/modules/ui_tempdir.py +++ b/modules/ui_tempdir.py @@ -61,7 +61,7 @@ def save_pil_to_file(self, pil_image, dir=None, format="png"): def install_ui_tempdir_override(): """override save to file function so that it also writes PNG info""" - gradio.components.IOComponent.pil_to_temp_file = save_pil_to_file + # gradio.components.IOComponent.pil_to_temp_file = save_pil_to_file def on_tmpdir_changed(): diff --git a/requirements.txt b/requirements.txt index 80b438455..d75b847c2 100644 --- a/requirements.txt +++ b/requirements.txt @@ -8,7 +8,7 @@ clean-fid einops fastapi>=0.90.1 gfpgan -gradio==3.41.2 +gradio==4.7.1 inflection jsonmerge kornia diff --git a/requirements_versions.txt b/requirements_versions.txt index cb7403a9d..50261e89a 100644 --- a/requirements_versions.txt +++ b/requirements_versions.txt @@ -5,9 +5,9 @@ basicsr==1.4.2 blendmodes==2022 clean-fid==0.1.35 einops==0.4.1 -fastapi==0.94.0 +fastapi==0.104.1 gfpgan==1.3.8 -gradio==3.41.2 +gradio==4.7.1 httpcore==0.15 inflection==0.5.1 jsonmerge==1.8.0 -- GitLab