kohya-ss / sd-webui-additional-networks

GNU Affero General Public License v3.0
1.78k stars 296 forks source link

Duplicate UI for AlwaysVisible extensions #27

Closed gmq closed 1 year ago

gmq commented 1 year ago

The XY fix seems to be causing other "AlwaysVisible" extensions to be shown twice, and also run twice.

To reproduce the issue:

# extensions/test/scripts/test.py
import modules.scripts as scripts
import gradio as gr

class Script(scripts.Script):
  def title(self):
    return 'Test'

  def show(self, is_img2img):
    return scripts.AlwaysVisible

  def ui(self, is_img2img):
    test = gr.Checkbox(label='test', default=True, elem_id=self.elem_id('test'))

    return [test]

image

kohya-ss commented 1 year ago

I don't seem to have the issue, with the latest webui.

image

If you have updated the webui to the latest, it may be the combination with another extension that causes the problem. Do you have any other extensions installed?

gmq commented 1 year ago

That was my mistake, sorry. I assumed it always happened so I fudged the # extensions/test/scripts/test.py path. Just in case I cloned automatic's repo and started from scratch and was able to reproduce the bug by putting the script in a folder above sd-webui-additional-networks in the extension list (so test doesn't cause the issue but a folder named r or a does).

Possibly related to that, on a just cloned webui, installing sd-webui-additional-networks seems to duplicate the X/Y plot entry image

kohya-ss commented 1 year ago

Thank you for clarification. I got duplicated X/Y plot, but I think it might be environment-dependent.

When I make the extension aaaa, it is shown twice.

I can reproduce the issue with following modified test.py.

# extensions/test/scripts/test.py
import modules.scripts as scripts
import gradio as gr
import os

class Script(scripts.Script):
  def title(self):
    return 'Test'

  def show(self, is_img2img):
    return scripts.AlwaysVisible

  def ui(self, is_img2img):
    test = gr.Checkbox(label='test', default=True, elem_id=self.elem_id('test'))

    return [test]

for script_class, path, basedir, script_module in scripts.scripts_data:
    if os.path.basename(path) == "xy_grid.py":
       xy_grid = script_module
       opt = xy_grid.AxisOption(f"Dummy option", float, lambda p, x, xs: print(p, x, xs), xy_grid.format_value_add_label, None)
       xy_grid.axis_options.append(opt)

Adding an axis option to xy_grid may cause the problem. I'm not familiar to Web UI and extensions, but I think this is an intended use for adding. So I wonder the Web UI may be something wrong...

Nyarlathotep885 commented 1 year ago

I was running into this issue as well, when running with the dynamic prompting extension and additional networks script. I believe the issue is with how scripts.scripts_data is being iterated through. Looks like if you iterate in the current way, it ends up instantiating script again, somehow.

Duplication Issue: (https://github.com/kohya-ss/sd-webui-additional-networks/tree/main/scripts)/additional_networks.py, 344cbc6ec5ffb73b1b5bd99d0c3879e58ef11156, line 417.

for script_class, path, basedir, script_module in scripts.scripts_data:
    if os.path.basename(path) == "xy_grid.py":
        xy_grid = script_module
        for i in range(MAX_MODEL_COUNT):
           model = xy_grid.AxisOption(f"AddNet Model {i+1}", str, lambda p, x, xs, i=i: apply_model(p, x, xs, i), format_lora_model, confirm_models)
           weight = xy_grid.AxisOption(f"AddNet Weight {i+1}", float, lambda p, x, xs, i=i: apply_weight(p, x, xs, i), xy_grid.format_value_add_label, None)
           xy_grid.axis_options.extend([model, weight])

Potential Fix:

for scriptDataTuple in scripts.scripts_data:
    if os.path.basename(scriptDataTuple.path) == "xy_grid.py":
        xy_grid = scriptDataTuple.module
        for i in range(MAX_MODEL_COUNT):
           model = xy_grid.AxisOption(f"AddNet Model {i+1}", str, lambda p, x, xs, i=i: apply_model(p, x, xs, i), format_lora_model, confirm_models)
           weight = xy_grid.AxisOption(f"AddNet Weight {i+1}", float, lambda p, x, xs, i=i: apply_weight(p, x, xs, i), xy_grid.format_value_add_label, None)
           xy_grid.axis_options.extend([model, weight])
kohya-ss commented 1 year ago

Thank you @Nyarlathotep885 !

I think these codes are same, because scripts.scripts_data is a named tuple. However, the issue seemed to be solved...

I will update the script as you suggested.

kohya-ss commented 1 year ago

I think this issue is solved. Thank you all!