gradio_extensons.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import gradio as gr
  2. from modules import scripts
  3. def add_classes_to_gradio_component(comp):
  4. """
  5. this adds gradio-* to the component for css styling (ie gradio-button to gr.Button), as well as some others
  6. """
  7. comp.elem_classes = [f"gradio-{comp.get_block_name()}", *(comp.elem_classes or [])]
  8. if getattr(comp, 'multiselect', False):
  9. comp.elem_classes.append('multiselect')
  10. def IOComponent_init(self, *args, **kwargs):
  11. self.webui_tooltip = kwargs.pop('tooltip', None)
  12. if scripts.scripts_current is not None:
  13. scripts.scripts_current.before_component(self, **kwargs)
  14. scripts.script_callbacks.before_component_callback(self, **kwargs)
  15. res = original_IOComponent_init(self, *args, **kwargs)
  16. add_classes_to_gradio_component(self)
  17. scripts.script_callbacks.after_component_callback(self, **kwargs)
  18. if scripts.scripts_current is not None:
  19. scripts.scripts_current.after_component(self, **kwargs)
  20. return res
  21. def Block_get_config(self):
  22. config = original_Block_get_config(self)
  23. webui_tooltip = getattr(self, 'webui_tooltip', None)
  24. if webui_tooltip:
  25. config["webui_tooltip"] = webui_tooltip
  26. return config
  27. def BlockContext_init(self, *args, **kwargs):
  28. res = original_BlockContext_init(self, *args, **kwargs)
  29. add_classes_to_gradio_component(self)
  30. return res
  31. original_IOComponent_init = gr.components.IOComponent.__init__
  32. original_Block_get_config = gr.blocks.Block.get_config
  33. original_BlockContext_init = gr.blocks.BlockContext.__init__
  34. gr.components.IOComponent.__init__ = IOComponent_init
  35. gr.blocks.Block.get_config = Block_get_config
  36. gr.blocks.BlockContext.__init__ = BlockContext_init