Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import time
|
2 |
+
import gradio as gr
|
3 |
+
import atexit
|
4 |
+
import pathlib
|
5 |
+
|
6 |
+
log_file = (pathlib.Path(__file__).parent / "cancel_events_output_log.txt").resolve()
|
7 |
+
|
8 |
+
def fake_diffusion(steps):
|
9 |
+
log_file.write_text("")
|
10 |
+
for i in range(steps):
|
11 |
+
print(f"Current step: {i}")
|
12 |
+
with log_file.open("a") as f:
|
13 |
+
f.write(f"Current step: {i}\n")
|
14 |
+
time.sleep(0.2)
|
15 |
+
yield str(i)
|
16 |
+
|
17 |
+
|
18 |
+
def long_prediction(*args, **kwargs):
|
19 |
+
time.sleep(10)
|
20 |
+
return 42
|
21 |
+
|
22 |
+
|
23 |
+
with gr.Blocks() as demo:
|
24 |
+
with gr.Row():
|
25 |
+
with gr.Column():
|
26 |
+
n = gr.Slider(1, 10, value=9, step=1, label="Number Steps")
|
27 |
+
run = gr.Button(value="Start Iterating")
|
28 |
+
output = gr.Textbox(label="Iterative Output")
|
29 |
+
stop = gr.Button(value="Stop Iterating")
|
30 |
+
with gr.Column():
|
31 |
+
textbox = gr.Textbox(label="Prompt")
|
32 |
+
prediction = gr.Number(label="Expensive Calculation")
|
33 |
+
run_pred = gr.Button(value="Run Expensive Calculation")
|
34 |
+
with gr.Column():
|
35 |
+
cancel_on_change = gr.Textbox(label="Cancel Iteration and Expensive Calculation on Change")
|
36 |
+
cancel_on_submit = gr.Textbox(label="Cancel Iteration and Expensive Calculation on Submit")
|
37 |
+
echo = gr.Textbox(label="Echo")
|
38 |
+
with gr.Row():
|
39 |
+
with gr.Column():
|
40 |
+
image = gr.Image(sources=["webcam"], label="Cancel on clear", interactive=True)
|
41 |
+
with gr.Column():
|
42 |
+
video = gr.Video(sources=["webcam"], label="Cancel on start recording", interactive=True)
|
43 |
+
|
44 |
+
click_event = run.click(fake_diffusion, n, output)
|
45 |
+
stop.click(fn=None, inputs=None, outputs=None, cancels=[click_event])
|
46 |
+
pred_event = run_pred.click(fn=long_prediction, inputs=[textbox], outputs=prediction)
|
47 |
+
|
48 |
+
cancel_on_change.change(None, None, None, cancels=[click_event, pred_event])
|
49 |
+
cancel_on_submit.submit(lambda s: s, cancel_on_submit, echo, cancels=[click_event, pred_event])
|
50 |
+
image.clear(None, None, None, cancels=[click_event, pred_event])
|
51 |
+
video.start_recording(None, None, None, cancels=[click_event, pred_event])
|
52 |
+
|
53 |
+
demo.queue(max_size=20)
|
54 |
+
atexit.register(lambda: log_file.unlink())
|
55 |
+
|
56 |
+
if __name__ == "__main__":
|
57 |
+
demo.launch()
|