mirror of
https://github.com/AUTOMATIC1111/stable-diffusion-webui.git
synced 2026-03-04 12:40:06 +00:00
gradio4
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
from functools import wraps
|
||||
|
||||
import gradio as gr
|
||||
from modules import gradio_extensions
|
||||
|
||||
|
||||
class FormComponent:
|
||||
webui_do_not_create_gradio_pyi_thank_you = True
|
||||
|
||||
def get_expected_parent(self):
|
||||
return gr.components.Form
|
||||
|
||||
@@ -9,12 +14,13 @@ class FormComponent:
|
||||
gr.Dropdown.get_expected_parent = FormComponent.get_expected_parent
|
||||
|
||||
|
||||
class ToolButton(FormComponent, gr.Button):
|
||||
class ToolButton(gr.Button, FormComponent):
|
||||
"""Small button with single emoji as text, fits inside gradio forms"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
classes = kwargs.pop("elem_classes", [])
|
||||
super().__init__(*args, elem_classes=["tool", *classes], **kwargs)
|
||||
@wraps(gr.Button.__init__)
|
||||
def __init__(self, value="", *args, elem_classes=None, **kwargs):
|
||||
elem_classes = elem_classes or []
|
||||
super().__init__(value=value, *args, elem_classes=["tool", *elem_classes], **kwargs)
|
||||
|
||||
def get_block_name(self):
|
||||
return "button"
|
||||
@@ -22,7 +28,9 @@ class ToolButton(FormComponent, gr.Button):
|
||||
|
||||
class ResizeHandleRow(gr.Row):
|
||||
"""Same as gr.Row but fits inside gradio forms"""
|
||||
webui_do_not_create_gradio_pyi_thank_you = True
|
||||
|
||||
@wraps(gr.Row.__init__)
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(**kwargs)
|
||||
|
||||
@@ -32,79 +40,92 @@ class ResizeHandleRow(gr.Row):
|
||||
return "row"
|
||||
|
||||
|
||||
class FormRow(FormComponent, gr.Row):
|
||||
class FormRow(gr.Row, FormComponent):
|
||||
"""Same as gr.Row but fits inside gradio forms"""
|
||||
|
||||
def get_block_name(self):
|
||||
return "row"
|
||||
|
||||
|
||||
class FormColumn(FormComponent, gr.Column):
|
||||
class FormColumn(gr.Column, FormComponent):
|
||||
"""Same as gr.Column but fits inside gradio forms"""
|
||||
|
||||
def get_block_name(self):
|
||||
return "column"
|
||||
|
||||
|
||||
class FormGroup(FormComponent, gr.Group):
|
||||
class FormGroup(gr.Group, FormComponent):
|
||||
"""Same as gr.Group but fits inside gradio forms"""
|
||||
|
||||
def get_block_name(self):
|
||||
return "group"
|
||||
|
||||
|
||||
class FormHTML(FormComponent, gr.HTML):
|
||||
class FormHTML(gr.HTML, FormComponent):
|
||||
"""Same as gr.HTML but fits inside gradio forms"""
|
||||
|
||||
def get_block_name(self):
|
||||
return "html"
|
||||
|
||||
|
||||
class FormColorPicker(FormComponent, gr.ColorPicker):
|
||||
class FormColorPicker(gr.ColorPicker, FormComponent):
|
||||
"""Same as gr.ColorPicker but fits inside gradio forms"""
|
||||
|
||||
def get_block_name(self):
|
||||
return "colorpicker"
|
||||
|
||||
|
||||
class DropdownMulti(FormComponent, gr.Dropdown):
|
||||
class DropdownMulti(gr.Dropdown, FormComponent):
|
||||
"""Same as gr.Dropdown but always multiselect"""
|
||||
|
||||
@wraps(gr.Dropdown.__init__)
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(multiselect=True, **kwargs)
|
||||
kwargs['multiselect'] = True
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def get_block_name(self):
|
||||
return "dropdown"
|
||||
|
||||
|
||||
class DropdownEditable(FormComponent, gr.Dropdown):
|
||||
class DropdownEditable(gr.Dropdown, FormComponent):
|
||||
"""Same as gr.Dropdown but allows editing value"""
|
||||
|
||||
@wraps(gr.Dropdown.__init__)
|
||||
def __init__(self, **kwargs):
|
||||
super().__init__(allow_custom_value=True, **kwargs)
|
||||
kwargs['allow_custom_value'] = True
|
||||
super().__init__(**kwargs)
|
||||
|
||||
def get_block_name(self):
|
||||
return "dropdown"
|
||||
|
||||
|
||||
class InputAccordion(gr.Checkbox):
|
||||
class InputAccordionImpl(gr.Checkbox):
|
||||
"""A gr.Accordion that can be used as an input - returns True if open, False if closed.
|
||||
|
||||
Actaully just a hidden checkbox, but creates an accordion that follows and is followed by the state of the checkbox.
|
||||
"""
|
||||
|
||||
webui_do_not_create_gradio_pyi_thank_you = True
|
||||
|
||||
global_index = 0
|
||||
|
||||
def __init__(self, value, **kwargs):
|
||||
@wraps(gr.Checkbox.__init__)
|
||||
def __init__(self, value=None, setup=False, **kwargs):
|
||||
if not setup:
|
||||
super().__init__(value=value, **kwargs)
|
||||
return
|
||||
|
||||
self.accordion_id = kwargs.get('elem_id')
|
||||
if self.accordion_id is None:
|
||||
self.accordion_id = f"input-accordion-{InputAccordion.global_index}"
|
||||
InputAccordion.global_index += 1
|
||||
self.accordion_id = f"input-accordion-{InputAccordionImpl.global_index}"
|
||||
InputAccordionImpl.global_index += 1
|
||||
|
||||
kwargs_checkbox = {
|
||||
**kwargs,
|
||||
"elem_id": f"{self.accordion_id}-checkbox",
|
||||
"visible": False,
|
||||
}
|
||||
super().__init__(value, **kwargs_checkbox)
|
||||
super().__init__(value=value, **kwargs_checkbox)
|
||||
|
||||
self.change(fn=None, _js='function(checked){ inputAccordionChecked("' + self.accordion_id + '", checked); }', inputs=[self])
|
||||
|
||||
@@ -115,6 +136,7 @@ class InputAccordion(gr.Checkbox):
|
||||
"elem_classes": ['input-accordion'],
|
||||
"open": value,
|
||||
}
|
||||
|
||||
self.accordion = gr.Accordion(**kwargs_accordion)
|
||||
|
||||
def extra(self):
|
||||
@@ -143,3 +165,6 @@ class InputAccordion(gr.Checkbox):
|
||||
def get_block_name(self):
|
||||
return "checkbox"
|
||||
|
||||
|
||||
def InputAccordion(value=None, **kwargs):
|
||||
return InputAccordionImpl(value=value, setup=True, **kwargs)
|
||||
|
||||
Reference in New Issue
Block a user