Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,24 +1,67 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
def calculator(num1, operation, num2):
|
4 |
-
if operation == "add":
|
5 |
-
return num1 + num2
|
6 |
-
elif operation == "subtract":
|
7 |
-
return num1 - num2
|
8 |
-
elif operation == "multiply":
|
9 |
-
return num1 * num2
|
10 |
-
elif operation == "divide":
|
11 |
-
return num1 / num2
|
12 |
-
|
13 |
-
demo = gr.Interface(
|
14 |
-
calculator,
|
15 |
-
[
|
16 |
-
"number",
|
17 |
-
gr.Radio(["add", "subtract", "multiply", "divide"]),
|
18 |
-
"number"
|
19 |
-
],
|
20 |
-
"number",
|
21 |
-
live=True,
|
22 |
-
)
|
23 |
if __name__ == "__main__":
|
24 |
demo.launch()
|
|
|
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"
|
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 |
+
def long_prediction(*args, **kwargs):
|
18 |
+
time.sleep(10)
|
19 |
+
return 42
|
20 |
+
|
21 |
+
with gr.Blocks() as demo:
|
22 |
+
with gr.Row():
|
23 |
+
with gr.Column():
|
24 |
+
n = gr.Slider(1, 10, value=9, step=1, label="Number Steps")
|
25 |
+
run = gr.Button(value="Start Iterating")
|
26 |
+
output = gr.Textbox(label="Iterative Output")
|
27 |
+
stop = gr.Button(value="Stop Iterating")
|
28 |
+
with gr.Column():
|
29 |
+
textbox = gr.Textbox(label="Prompt")
|
30 |
+
prediction = gr.Number(label="Expensive Calculation")
|
31 |
+
run_pred = gr.Button(value="Run Expensive Calculation")
|
32 |
+
with gr.Column():
|
33 |
+
cancel_on_change = gr.Textbox(
|
34 |
+
label="Cancel Iteration and Expensive Calculation on Change"
|
35 |
+
)
|
36 |
+
cancel_on_submit = gr.Textbox(
|
37 |
+
label="Cancel Iteration and Expensive Calculation on Submit"
|
38 |
+
)
|
39 |
+
echo = gr.Textbox(label="Echo")
|
40 |
+
with gr.Row():
|
41 |
+
with gr.Column():
|
42 |
+
image = gr.Image(
|
43 |
+
sources=["webcam"], label="Cancel on clear", interactive=True
|
44 |
+
)
|
45 |
+
with gr.Column():
|
46 |
+
video = gr.Video(
|
47 |
+
sources=["webcam"], label="Cancel on start recording", interactive=True
|
48 |
+
)
|
49 |
+
|
50 |
+
click_event = run.click(fake_diffusion, n, output)
|
51 |
+
stop.click(fn=None, inputs=None, outputs=None, cancels=[click_event])
|
52 |
+
pred_event = run_pred.click(
|
53 |
+
fn=long_prediction, inputs=[textbox], outputs=prediction
|
54 |
+
)
|
55 |
+
|
56 |
+
cancel_on_change.change(None, None, None, cancels=[click_event, pred_event])
|
57 |
+
cancel_on_submit.submit(
|
58 |
+
lambda s: s, cancel_on_submit, echo, cancels=[click_event, pred_event]
|
59 |
+
)
|
60 |
+
image.clear(None, None, None, cancels=[click_event, pred_event])
|
61 |
+
video.start_recording(None, None, None, cancels=[click_event, pred_event])
|
62 |
+
|
63 |
+
demo.queue(max_size=20)
|
64 |
+
atexit.register(lambda: log_file.unlink())
|
65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
if __name__ == "__main__":
|
67 |
demo.launch()
|