Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 37 ยท Models and Demos

Streaming Output and Progress Bars

Show results while work is still running: generator functions, gr.Progress and progress.tqdm, streaming outputs and live inputs, cancelling, with runnable generator stand-ins.

A model that takes 20 seconds to answer feels broken if the page shows nothing. Gradio gives you three ways to keep users informed: progress bars, partial results from generator functions, and streaming inputs such as a live webcam. All three rely on one ordinary Python feature you may already know: generators.

Real Gradio is read-only on this site. The runnable blocks use generators and a small progress helper in plain Python, so you can see what Gradio does with the values you yield or report.

Generators: yield instead of return

A function that contains yield produces a sequence of values over time. Gradio treats each yielded value as an update to the output component:

Python
import time
import gradio as gr

def countdown(n):
    for i in range(int(n), 0, -1):
        yield f"{i}..."
        time.sleep(1)
    yield "Liftoff!"

demo = gr.Interface(countdown, gr.Number(value=3), gr.Textbox())
demo.launch()

After pressing Submit, the output box shows 3..., then 2..., then 1..., then Liftoff! one second apart. The submit button becomes a stop button while the generator runs. Generators also work for images (yield a frame at a time for an animation or diffusion preview) and for several outputs (yield a tuple).

Python A generator you can step through

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The block prints four updates. In the real app the delays would come from time.sleep or from slow model steps between yields.

gr.Progress

For a fixed amount of work, a progress bar is nicer than text. Add a parameter whose default is gr.Progress(); Gradio injects the tracker:

Python
def slow_job(text, progress=gr.Progress()):
    progress(0, desc="Starting")
    words = text.split()
    result = []
    for w in progress.tqdm(words, desc="Processing"):
        time.sleep(0.5)
        result.append(w.upper())
    return " ".join(result)

While the job runs, a bar overlays the output component, showing the description and a fraction such as 3/7. You can drive it manually, progress(0.4, desc="Loading model") (a fraction from 0 to 1), or pass total and a count: progress((done, total)). progress.tqdm(iterable) works like the tqdm loop you may know. Unlike yielding, a progress bar does not change the output until the function returns.

Python A progress tracker (stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The bar fills in thirds, and the final line is MAKE THIS LOUD. The key idea: the tracker is passed in as an argument, so the function reports progress without knowing how it is drawn.

Streaming outputs: text, audio and video

For language models, yield the whole text so far (as in chatbots). For audio and video, set streaming=True on the output component and yield small chunks; the browser plays them as they arrive. Large generators often yield frames at roughly 10 to 30 steps a second, so keep the work between yields small.

Two events are made for streams on the input side: gr.Audio(streaming=True) and gr.Image(streaming=True, sources=["webcam"]) fire a .stream() event repeatedly while the user speaks or the camera runs. Use these for live captions or live filters, and keep per-chunk work short, or the queue backs up.

Python
with gr.Blocks() as demo:
    cam = gr.Image(sources=["webcam"], streaming=True)
    out = gr.Image()
    cam.stream(lambda frame: frame[:, ::-1], cam, out, stream_every=0.2)

Run, the page shows the webcam on the left and a mirrored copy on the right, refreshed about five times a second.

Cancelling and cleaning up

Pressing Stop closes the connection, and Gradio stops the generator at the next yield. Work that sits between yields still finishes, so slice long work into small steps. If you hold resources, use try/finally:

Python Cleanup when a generator is stopped

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The block prints step 1, step 2 and then cleanup done, because closing a generator raises an exception inside it at the paused yield, and finally runs. Gradio does the equivalent when the user presses Stop or closes the tab.

Queue interplay

Generators hold a slot in the queue until they finish. A handler that yields for five minutes occupies one of your concurrency slots all that time, so set concurrency_limit and consider max_size; the next lesson, queue and concurrency, explains how.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What makes a Python function a generator?

    Any function containing yield returns a generator; Gradio redraws the output on each yielded value.
  2. 2. How do you get a progress bar in a Gradio function?

    Gradio injects the tracker into a parameter whose default is gr.Progress().
  3. 3. For streamed chat text, what should each yield contain?

    Each yielded value replaces the previous bubble, so it must be the full partial reply.
  4. 4. Why does a long generator affect the queue?

    A running generator keeps its slot, so other users wait when slots run out.

Summary

  • yield turns a function into a stream of updates; the Submit button becomes Stop.
  • gr.Progress() as a default argument gives progress bars via progress(...) and progress.tqdm.
  • Stream text by yielding the growing string; stream audio or video with streaming=True.
  • Use try/finally for cleanup and remember long generators hold a queue slot.

Next: Capacity Planning, Share Links and Going Public