2022-10-15 06:43:24 +08:00
|
|
|
import time
|
|
|
|
import gradio as gr
|
2024-03-28 01:41:40 +08:00
|
|
|
import atexit
|
|
|
|
import pathlib
|
2022-10-15 06:43:24 +08:00
|
|
|
|
2024-06-06 20:16:14 +08:00
|
|
|
log_file = pathlib.Path(__file__).parent / "cancel_events_output_log.txt"
|
|
|
|
|
2022-10-15 06:43:24 +08:00
|
|
|
def fake_diffusion(steps):
|
2024-03-28 01:41:40 +08:00
|
|
|
log_file.write_text("")
|
2022-10-15 06:43:24 +08:00
|
|
|
for i in range(steps):
|
|
|
|
print(f"Current step: {i}")
|
2024-03-28 01:41:40 +08:00
|
|
|
with log_file.open("a") as f:
|
|
|
|
f.write(f"Current step: {i}\n")
|
|
|
|
time.sleep(0.2)
|
2022-10-15 06:43:24 +08:00
|
|
|
yield str(i)
|
|
|
|
|
|
|
|
def long_prediction(*args, **kwargs):
|
|
|
|
time.sleep(10)
|
|
|
|
return 42
|
|
|
|
|
|
|
|
with gr.Blocks() as demo:
|
|
|
|
with gr.Row():
|
|
|
|
with gr.Column():
|
|
|
|
n = gr.Slider(1, 10, value=9, step=1, label="Number Steps")
|
2023-06-30 23:59:50 +08:00
|
|
|
run = gr.Button(value="Start Iterating")
|
2022-10-15 06:43:24 +08:00
|
|
|
output = gr.Textbox(label="Iterative Output")
|
|
|
|
stop = gr.Button(value="Stop Iterating")
|
|
|
|
with gr.Column():
|
|
|
|
textbox = gr.Textbox(label="Prompt")
|
|
|
|
prediction = gr.Number(label="Expensive Calculation")
|
|
|
|
run_pred = gr.Button(value="Run Expensive Calculation")
|
|
|
|
with gr.Column():
|
2024-06-06 20:16:14 +08:00
|
|
|
cancel_on_change = gr.Textbox(
|
|
|
|
label="Cancel Iteration and Expensive Calculation on Change"
|
|
|
|
)
|
|
|
|
cancel_on_submit = gr.Textbox(
|
|
|
|
label="Cancel Iteration and Expensive Calculation on Submit"
|
|
|
|
)
|
2022-10-15 06:43:24 +08:00
|
|
|
echo = gr.Textbox(label="Echo")
|
|
|
|
with gr.Row():
|
|
|
|
with gr.Column():
|
2024-06-06 20:16:14 +08:00
|
|
|
image = gr.Image(
|
|
|
|
sources=["webcam"], label="Cancel on clear", interactive=True
|
|
|
|
)
|
2022-10-15 06:43:24 +08:00
|
|
|
with gr.Column():
|
2024-06-06 20:16:14 +08:00
|
|
|
video = gr.Video(
|
|
|
|
sources=["webcam"], label="Cancel on start recording", interactive=True
|
|
|
|
)
|
2022-10-15 06:43:24 +08:00
|
|
|
|
|
|
|
click_event = run.click(fake_diffusion, n, output)
|
|
|
|
stop.click(fn=None, inputs=None, outputs=None, cancels=[click_event])
|
2024-06-06 20:16:14 +08:00
|
|
|
pred_event = run_pred.click(
|
|
|
|
fn=long_prediction, inputs=[textbox], outputs=prediction
|
|
|
|
)
|
2022-10-15 06:43:24 +08:00
|
|
|
|
|
|
|
cancel_on_change.change(None, None, None, cancels=[click_event, pred_event])
|
2024-06-06 20:16:14 +08:00
|
|
|
cancel_on_submit.submit(
|
|
|
|
lambda s: s, cancel_on_submit, echo, cancels=[click_event, pred_event]
|
|
|
|
)
|
2023-11-03 08:35:07 +08:00
|
|
|
image.clear(None, None, None, cancels=[click_event, pred_event])
|
|
|
|
video.start_recording(None, None, None, cancels=[click_event, pred_event])
|
2022-10-15 06:43:24 +08:00
|
|
|
|
2023-10-31 17:41:19 +08:00
|
|
|
demo.queue(max_size=20)
|
2024-03-28 01:41:40 +08:00
|
|
|
atexit.register(lambda: log_file.unlink())
|
2022-10-15 06:43:24 +08:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2023-06-30 23:59:50 +08:00
|
|
|
demo.launch()
|