Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release updated Gradio Support #2013

Closed
jleibs opened this issue May 1, 2023 · 5 comments
Closed

Release updated Gradio Support #2013

jleibs opened this issue May 1, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request 🐍 Python API Python logging API

Comments

@jleibs
Copy link
Member

jleibs commented May 1, 2023

Background

A few of the complexities to figure out with Gradio:
(1) Gradio (and in particular hugging face spaces) run as a persistent service even across multiple users. This means we need to avoid, or at least be able to avoid global session state interfering across multiple user sessions.
(2) In Gradio's normal execution model, jobs fully execute, then produce an output. This is somewhat similar to how our current notebook cells works with a single static resource output. Instead, we would like the embedded rerun viewer context to exist the whole time the job is running with data incrementally streaming to it. Gradio already has its own incremental streaming mechanism used for things like streaming audio or video, so tying into this framework would be nice.
(3) HF spaces, including Gradio, do everything over a single port. This means we really don't want to depend on running our own websocket server. Somehow retrieving the data-stream back from Rerun and tunneling it through Gradios websocket, as done with the other data that Gradio transmits, would simplify integration. This is also somewhat related to user-isolation, since Gradio already manages user session context.

Next Steps

All of the above are functional in our PR: radames/gradio-rerun-viewer#1

Need to clean up PR to get it into a final state and then merge.

Live demonstration: https://huggingface.co/spaces/jleibs/rerun_streaming_poc

@jleibs jleibs added enhancement New feature or request 🐍 Python API Python logging API labels May 1, 2023
@jleibs jleibs changed the title Proper Gradio Support Prototype Gradio Support Apr 23, 2024
@jleibs
Copy link
Member Author

jleibs commented Apr 30, 2024

I have an idea for a possible direction building on top of https://github.com/radames/gradio-rerun-viewer/tree/main that I think would play nicely with the Gradio streaming execution model: https://www.gradio.app/guides/reactive-interfaces

This requires building a little bit of extra functionality onto the python and javascript APIs:

  • A small extension to the MemoryRecording that lets us drain the current buffer and return it to python as an in-memory RRD (while keeping the recording_id the same).
  • One more javascript API, exposed via our npm package, that lets us directly inject a buffer of bytes instead of an rrd url. This would call through to something similar to stream_rrd_from_event_listener but can probably skip the message-handler logic since without the iframe we can call wasm bindings directly.

This lets us set up Gradio in streaming mode, and have a job incrementally yield rrd chunks which are sent over Gradios comms mechanism and then added to the viewer where they are accumulated and a single RRD.

Theoretical API might look something like:

def stream_rrd(some_input):
    with rr.buffered_stream("rerun_stream_example") as stream:
        for id,frame in enumerate(some_input):
            rr.set_time_sequence("frame", id)
            rr.log("input", rr.Image(frame))
            output = process(frame)
            rr.log("output", rr.Image(output))

            yield stream.flush()

with gr.Blocks() as demo:
    with gr.Row():    
        input = gr.SomeInput()
        btn = gr.Button("Run")
    with gr.Row():    
        viewer = gr.Rerun(streaming=True)

    gr.on([btn.click], fn=stream_rrd, inputs=[input], outputs=[viewer])

jleibs added a commit that referenced this issue May 2, 2024
#6187)

### What
- This was motivated by #2013

The main piece of work, plumbing through drain was relatively
straight-forward.

In addition, when creating an example that roughly models the gradio
streaming workflow, I realized it would be helpful to introduce a
decorator which simplifies setting up an isolated recording stream, and
works with the sort of streaming generators we expect to see with
Gradio.

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6187?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6187?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6187)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
jleibs added a commit that referenced this issue May 2, 2024
…y as bytes (#6189)

### What
- This was motivated by #2013

This API means that an RRD file transmitted over a side-channel such as
gradio can be injected directly into the viewer instead of needing to
point the viewer at a separate url.

Tested with an adhoc svelte component:
```ts
<script lang="ts">
    import { WebViewer, LogChannel } from "@rerun-io/web-viewer";

    import { onMount } from "svelte";

    let rr: WebViewer;
    let ref: HTMLDivElement;
    let channel: LogChannel;

    onMount(() => {
        rr = new WebViewer();
        rr.start(undefined, ref).then(() => {
            channel = rr.open_channel("gradio");
        });

        return () => {
            channel.close();
            rr.stop();
        };
    });

    let data1 = '...'
    let data2 = '...'
    let data3 = '...'

    function push(data: string) {
        var intermediate = atob(data);
        var buff = new Uint8Array(intermediate.length);
        for (var i = 0; i < intermediate.length; i++) {
            {
                buff[i] = intermediate.charCodeAt(i);
            }
        }
        channel.send_rrd(buff);
    }

</script>

<div class="container">
    <button on:click={() => push(data1)}>
    Data1
    </button>
    <button on:click={() => push(data2)}>
    Data2
    </button>
    <button on:click={() => push(data3)}>
    Data3
    </button>
</div>

<div class="viewer" bind:this={ref} />
```

### Checklist
* [x] I have read and agree to [Contributor
Guide](https://github.com/rerun-io/rerun/blob/main/CONTRIBUTING.md) and
the [Code of
Conduct](https://github.com/rerun-io/rerun/blob/main/CODE_OF_CONDUCT.md)
* [x] I've included a screenshot or gif (if applicable)
* [x] I have tested the web demo (if applicable):
* Using examples from latest `main` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6189?manifest_url=https://app.rerun.io/version/main/examples_manifest.json)
* Using full set of examples from `nightly` build:
[rerun.io/viewer](https://rerun.io/viewer/pr/6189?manifest_url=https://app.rerun.io/version/nightly/examples_manifest.json)
* [x] The PR title and labels are set such as to maximize their
usefulness for the next release's CHANGELOG
* [x] If applicable, add a new check to the [release
checklist](https://github.com/rerun-io/rerun/blob/main/tests/python/release_checklist)!

- [PR Build Summary](https://build.rerun.io/pr/6189)
- [Recent benchmark results](https://build.rerun.io/graphs/crates.html)
- [Wasm size tracking](https://build.rerun.io/graphs/sizes.html)

To run all checks from `main`, comment on the PR with `@rerun-bot
full-check`.
@jleibs jleibs self-assigned this May 6, 2024
@jleibs
Copy link
Member Author

jleibs commented May 10, 2024

Reminder to self: give the gradio component a blueprint argument that does the right thing.

@jleibs jleibs changed the title Prototype Gradio Support Release updated Gradio Support May 27, 2024
@jleibs
Copy link
Member Author

jleibs commented May 29, 2024

@jleibs
Copy link
Member Author

jleibs commented Jun 3, 2024

This has been published.

@jleibs jleibs closed this as completed Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request 🐍 Python API Python logging API
Projects
None yet
Development

No branches or pull requests

2 participants