abidlabs HF Staff commited on
Commit
2bbf371
·
verified ·
1 Parent(s): fc2e67f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -60
app.py CHANGED
@@ -1,67 +1,12 @@
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()
 
1
  import time
2
  import gradio as gr
 
 
3
 
4
+ def slow_echo(message, history):
5
+ for i in range(len(message)):
6
+ time.sleep(0.05)
7
+ yield "You typed: " + message[: i + 1]
8
 
9
+ demo = gr.ChatInterface(slow_echo, type="messages")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  if __name__ == "__main__":
12
  demo.launch()