sanchit-gandhi HF staff commited on
Commit
ee3575f
1 Parent(s): 746270e

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import math
3
+ import time
4
+
5
+ def stream(audio, chunk_length_s):
6
+ start_time = time.time()
7
+ sampling_rate, array = audio
8
+ chunk_length = int(chunk_length_s * sampling_rate)
9
+ time_length = chunk_length_s / 2 # always stream outputs faster than it takes to process
10
+ audio_length = len(array)
11
+ num_batches = math.ceil(audio_length / chunk_length)
12
+
13
+ for idx in range(num_batches):
14
+ time.sleep(time_length)
15
+ start_pos = idx * chunk_length
16
+ end_pos = min((idx + 1) * chunk_length, audio_length)
17
+ chunk = array[start_pos : end_pos]
18
+ if idx == 0:
19
+ first_time = round(time.time() - start_time, 2)
20
+ run_time = round(time.time() - start_time, 2)
21
+ yield (sampling_rate, chunk), first_time, run_time
22
+
23
+ with gr.Blocks() as demo:
24
+ with gr.Row():
25
+ with gr.Column():
26
+ audio_in = gr.Audio(value="librispeech.wav", sources=["upload"], type="numpy")
27
+ chunk_length = gr.Slider(minimum=1, maximum=3, value=2, step=1, label="Chunk length (s)")
28
+ run_button = gr.Button("Stream audio")
29
+ with gr.Column():
30
+ audio_out = gr.Audio(streaming=True, autoplay=True)
31
+ first_time = gr.Textbox(label="Time to first chunk (s)")
32
+ run_time = gr.Textbox(label="Time to current chunk (s)")
33
+
34
+ run_button.click(fn=stream, inputs=[audio_in, chunk_length], outputs=[audio_out, first_time, run_time])
35
+
36
+ demo.launch()