Custom Components GalleryNEW
ExploreCustom Components GalleryNEW
ExploreFinally, we cover how to get Gradio demos to refresh automatically or continuously stream data.
You can make interfaces automatically refresh by setting live=True
in the interface. Now the interface will recalculate as soon as the user input changes.
import gradio as gr
def calculator(num1, operation, num2):
if operation == "add":
return num1 + num2
elif operation == "subtract":
return num1 - num2
elif operation == "multiply":
return num1 * num2
elif operation == "divide":
return num1 / num2
demo = gr.Interface(
calculator,
[
"number",
gr.Radio(["add", "subtract", "multiply", "divide"]),
"number"
],
"number",
live=True,
)
demo.launch()
Note there is no submit button, because the interface resubmits automatically on change.
Some components have a “streaming” mode, such as Audio
component in microphone mode, or the Image
component in webcam mode. Streaming means data is sent continuously to the backend and the Interface
function is continuously being rerun.
The difference between gr.Audio(source='microphone')
and gr.Audio(source='microphone', streaming=True)
, when both are used in gr.Interface(live=True)
, is that the first Component
will automatically submit data and run the Interface
function when the user stops recording, whereas the second Component
will continuously send data and run the Interface
function during recording.
Here is example code of streaming images from the webcam.
import gradio as gr
import numpy as np
def flip(im):
return np.flipud(im)
demo = gr.Interface(
flip,
gr.Image(sources=["webcam"], streaming=True),
"image",
live=True
)
demo.launch()
Streaming can also be done in an output component. A gr.Audio(streaming=True)
output component can take a stream of audio data yielded piece-wise by a generator function and combines them into a single audio file.
import gradio as gr
from pydub import AudioSegment
from time import sleep
with gr.Blocks() as demo:
input_audio = gr.Audio(label="Input Audio", type="filepath", format="mp3")
with gr.Row():
with gr.Column():
stream_as_file_btn = gr.Button("Stream as File")
format = gr.Radio(["wav", "mp3"], value="wav", label="Format")
stream_as_file_output = gr.Audio(streaming=True)
def stream_file(audio_file, format):
audio = AudioSegment.from_file(audio_file)
i = 0
chunk_size = 1000
while chunk_size * i < len(audio):
chunk = audio[chunk_size * i : chunk_size * (i + 1)]
i += 1
if chunk:
file = f"/tmp/{i}.{format}"
chunk.export(file, format=format)
yield file
sleep(0.5)
stream_as_file_btn.click(
stream_file, [input_audio, format], stream_as_file_output
)
gr.Examples(
[["audio/cantina.wav", "wav"], ["audio/cantina.wav", "mp3"]],
[input_audio, format],
fn=stream_file,
outputs=stream_as_file_output,
cache_examples=True,
)
with gr.Column():
stream_as_bytes_btn = gr.Button("Stream as Bytes")
stream_as_bytes_output = gr.Audio(format="bytes", streaming=True)
def stream_bytes(audio_file):
chunk_size = 20_000
with open(audio_file, "rb") as f:
while True:
chunk = f.read(chunk_size)
if chunk:
yield chunk
sleep(1)
else:
break
stream_as_bytes_btn.click(stream_bytes, input_audio, stream_as_bytes_output)
if __name__ == "__main__":
demo.queue().launch()
For a more detailed example, see our guide on performing automatic speech recognition with Gradio.