ui_loadsave.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. import json
  2. import os
  3. import gradio as gr
  4. from modules import errors
  5. from modules.ui_components import ToolButton, InputAccordion
  6. def radio_choices(comp): # gradio 3.41 changes choices from list of values to list of pairs
  7. return [x[0] if isinstance(x, tuple) else x for x in getattr(comp, 'choices', [])]
  8. class UiLoadsave:
  9. """allows saving and restoring default values for gradio components"""
  10. def __init__(self, filename):
  11. self.filename = filename
  12. self.ui_settings = {}
  13. self.component_mapping = {}
  14. self.error_loading = False
  15. self.finalized_ui = False
  16. self.ui_defaults_view = None
  17. self.ui_defaults_apply = None
  18. self.ui_defaults_review = None
  19. try:
  20. if os.path.exists(self.filename):
  21. self.ui_settings = self.read_from_file()
  22. except Exception as e:
  23. self.error_loading = True
  24. errors.display(e, "loading settings")
  25. def add_component(self, path, x):
  26. """adds component to the registry of tracked components"""
  27. assert not self.finalized_ui
  28. def apply_field(obj, field, condition=None, init_field=None):
  29. key = f"{path}/{field}"
  30. if getattr(obj, 'custom_script_source', None) is not None:
  31. key = f"customscript/{obj.custom_script_source}/{key}"
  32. if getattr(obj, 'do_not_save_to_config', False):
  33. return
  34. saved_value = self.ui_settings.get(key, None)
  35. if isinstance(obj, gr.Accordion) and isinstance(x, InputAccordion) and field == 'value':
  36. field = 'open'
  37. if saved_value is None:
  38. self.ui_settings[key] = getattr(obj, field)
  39. elif condition and not condition(saved_value):
  40. pass
  41. else:
  42. if isinstance(obj, gr.Textbox) and field == 'value': # due to an undesirable behavior of gr.Textbox, if you give it an int value instead of str, everything dies
  43. saved_value = str(saved_value)
  44. elif isinstance(obj, gr.Number) and field == 'value':
  45. try:
  46. saved_value = float(saved_value)
  47. except ValueError:
  48. return
  49. setattr(obj, field, saved_value)
  50. if init_field is not None:
  51. init_field(saved_value)
  52. if field == 'value' and key not in self.component_mapping:
  53. self.component_mapping[key] = obj
  54. if type(x) in [gr.Slider, gr.Radio, gr.Checkbox, gr.Textbox, gr.Number, gr.Dropdown, ToolButton, gr.Button] and x.visible:
  55. apply_field(x, 'visible')
  56. if type(x) == gr.Slider:
  57. apply_field(x, 'value')
  58. apply_field(x, 'minimum')
  59. apply_field(x, 'maximum')
  60. apply_field(x, 'step')
  61. if type(x) == gr.Radio:
  62. apply_field(x, 'value', lambda val: val in radio_choices(x))
  63. if type(x) == gr.Checkbox:
  64. apply_field(x, 'value')
  65. if type(x) == gr.Textbox:
  66. apply_field(x, 'value')
  67. if type(x) == gr.Number:
  68. apply_field(x, 'value')
  69. if type(x) == gr.Dropdown:
  70. def check_dropdown(val):
  71. choices = radio_choices(x)
  72. if getattr(x, 'multiselect', False):
  73. return all(value in choices for value in val)
  74. else:
  75. return val in choices
  76. apply_field(x, 'value', check_dropdown, getattr(x, 'init_field', None))
  77. if type(x) == InputAccordion:
  78. if x.accordion.visible:
  79. apply_field(x.accordion, 'visible')
  80. apply_field(x, 'value')
  81. apply_field(x.accordion, 'value')
  82. def check_tab_id(tab_id):
  83. tab_items = list(filter(lambda e: isinstance(e, gr.TabItem), x.children))
  84. if type(tab_id) == str:
  85. tab_ids = [t.id for t in tab_items]
  86. return tab_id in tab_ids
  87. elif type(tab_id) == int:
  88. return 0 <= tab_id < len(tab_items)
  89. else:
  90. return False
  91. if type(x) == gr.Tabs:
  92. apply_field(x, 'selected', check_tab_id)
  93. def add_block(self, x, path=""):
  94. """adds all components inside a gradio block x to the registry of tracked components"""
  95. if hasattr(x, 'children'):
  96. if isinstance(x, gr.Tabs) and x.elem_id is not None:
  97. # Tabs element can't have a label, have to use elem_id instead
  98. self.add_component(f"{path}/Tabs@{x.elem_id}", x)
  99. for c in x.children:
  100. self.add_block(c, path)
  101. elif x.label is not None:
  102. self.add_component(f"{path}/{x.label}", x)
  103. elif isinstance(x, gr.Button) and x.value is not None:
  104. self.add_component(f"{path}/{x.value}", x)
  105. def read_from_file(self):
  106. with open(self.filename, "r", encoding="utf8") as file:
  107. return json.load(file)
  108. def write_to_file(self, current_ui_settings):
  109. with open(self.filename, "w", encoding="utf8") as file:
  110. json.dump(current_ui_settings, file, indent=4, ensure_ascii=False)
  111. def dump_defaults(self):
  112. """saves default values to a file unless tjhe file is present and there was an error loading default values at start"""
  113. if self.error_loading and os.path.exists(self.filename):
  114. return
  115. self.write_to_file(self.ui_settings)
  116. def iter_changes(self, current_ui_settings, values):
  117. """
  118. given a dictionary with defaults from a file and current values from gradio elements, returns
  119. an iterator over tuples of values that are not the same between the file and the current;
  120. tuple contents are: path, old value, new value
  121. """
  122. for (path, component), new_value in zip(self.component_mapping.items(), values):
  123. old_value = current_ui_settings.get(path)
  124. choices = radio_choices(component)
  125. if isinstance(new_value, int) and choices:
  126. if new_value >= len(choices):
  127. continue
  128. new_value = choices[new_value]
  129. if isinstance(new_value, tuple):
  130. new_value = new_value[0]
  131. if new_value == old_value:
  132. continue
  133. if old_value is None and new_value == '' or new_value == []:
  134. continue
  135. yield path, old_value, new_value
  136. def ui_view(self, *values):
  137. text = ["<table><thead><tr><th>Path</th><th>Old value</th><th>New value</th></thead><tbody>"]
  138. for path, old_value, new_value in self.iter_changes(self.read_from_file(), values):
  139. if old_value is None:
  140. old_value = "<span class='ui-defaults-none'>None</span>"
  141. text.append(f"<tr><td>{path}</td><td>{old_value}</td><td>{new_value}</td></tr>")
  142. if len(text) == 1:
  143. text.append("<tr><td colspan=3>No changes</td></tr>")
  144. text.append("</tbody>")
  145. return "".join(text)
  146. def ui_apply(self, *values):
  147. num_changed = 0
  148. current_ui_settings = self.read_from_file()
  149. for path, _, new_value in self.iter_changes(current_ui_settings.copy(), values):
  150. num_changed += 1
  151. current_ui_settings[path] = new_value
  152. if num_changed == 0:
  153. return "No changes."
  154. self.write_to_file(current_ui_settings)
  155. return f"Wrote {num_changed} changes."
  156. def create_ui(self):
  157. """creates ui elements for editing defaults UI, without adding any logic to them"""
  158. gr.HTML(
  159. f"This page allows you to change default values in UI elements on other tabs.<br />"
  160. f"Make your changes, press 'View changes' to review the changed default values,<br />"
  161. f"then press 'Apply' to write them to {self.filename}.<br />"
  162. f"New defaults will apply after you restart the UI.<br />"
  163. )
  164. with gr.Row():
  165. self.ui_defaults_view = gr.Button(value='View changes', elem_id="ui_defaults_view", variant="secondary")
  166. self.ui_defaults_apply = gr.Button(value='Apply', elem_id="ui_defaults_apply", variant="primary")
  167. self.ui_defaults_review = gr.HTML("")
  168. def setup_ui(self):
  169. """adds logic to elements created with create_ui; all add_block class must be made before this"""
  170. assert not self.finalized_ui
  171. self.finalized_ui = True
  172. self.ui_defaults_view.click(fn=self.ui_view, inputs=list(self.component_mapping.values()), outputs=[self.ui_defaults_review])
  173. self.ui_defaults_apply.click(fn=self.ui_apply, inputs=list(self.component_mapping.values()), outputs=[self.ui_defaults_review])