picobyte / stable-diffusion-webui-wd14-tagger

Labeling extension for Automatic1111's Web UI
539 stars 64 forks source link

After interrogation is started, can't abort - Batch interrogate #76

Open scofano opened 9 months ago

scofano commented 9 months ago

After it is started, can't abort Batch interrogate. I started a folder with 100 images and then realized it was the wrong folder, I had to close SD because I didn't find how to stop the process.

picobyte commented 9 months ago

well yeah, that happens.

picobyte commented 9 months ago

I guess the interrogate button could change in Cancel a bit similar to txt2img or img2img.

scofano commented 9 months ago

It would be great.

picobyte commented 9 months ago

The button to change into Cancel after a press. The press of this button should function according a toggled state (e.g. a static interrogator bool) As you can read here, under behavior by usinng the label button as output it is possible to change the button label (temporarily)

The loop to break Also make sure to reinstate the interrogate button after interrogation is done, also after errors.

WSH032 commented 9 months ago

I think maybe it would be easier to implement and maintain with two buttons.

Maybe like this, two buttons, Interrogate and Cancel When Interrogate is pressed, it hides itself and displays Cancel. When Cancel is pressed, do the similar thing.

refer to https://www.gradio.app/docs/button#initialization, we can use visible or interactive to implement


This is demo:

import time
from threading import Event

import gradio as gr

############################################

cancel_event = Event()

SHOW_INTERROGATE = [
    gr.update(visible = True),
    gr.update(visible = False),
]
SHOW_CANCEL = SHOW_INTERROGATE[::-1]

############################################

def fake_interrogate():

    cancel_event.clear()  # reset cancel event

    yield SHOW_CANCEL

    # fake interrogate
    for i in range(10):
        if cancel_event.isSet():  # check if cancel event is set
            print("canceled")
            break
        time.sleep(1)
        print(i)
    yield SHOW_INTERROGATE

def fake_cancel():
    cancel_event.set()  # set cancel event to stop the interrogation
    return SHOW_INTERROGATE

############################################

with gr.Blocks() as demo:

    with gr.Row():
        interrogate = gr.Button(value="interrogate")
        cancel = gr.Button(value="cancel", visible=False)

    interrogate.click(
        fn = fake_interrogate,
        outputs=[interrogate, cancel],
    )

    cancel.click(
        fn = fake_cancel,
        outputs=[interrogate, cancel],
    )

# queue is required to yield, and `concurrency_count` must be > 1 (in A1111 seems 64)
demo.queue(concurrency_count=2).launch(debug=True)
picobyte commented 9 months ago

Thanks for your input, you are right, It is difficult to return and change the label before interrogation. Maybe it can be done by queuing th interrogation, then returning, but then the double button may be easier.

RoelKluin commented 9 months ago

It might also be possible to use a state variable, let the button just handle the label change and change the state of the state variable. Then the state variable, on change should trigger either the batch interrogation or the cancel action, dependent on its (inverted) state. Another approach could be to use the elem_id and change the button label in javascript, rather than relying on gradio.