gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
29.49k stars 2.19k forks source link

Change API for streams #8208

Open pngwn opened 2 weeks ago

pngwn commented 2 weeks ago

Is your feature request related to a problem? Please describe.

The streaming API for the @gradio/client isn't really that nice. I think we should switch to an async iterator, we would go from this:


const app = Client.create();
const job = app.submit("/endpoint", body)

job.on("data", data => {
  // do something with data
})
job.on("status",status => {
  // do something with status
})

To this:

const app = Client.create();
const stream = app.submit("/endpoint", body)

for await (let event of stream) {
  console.log('<<', event.type); // 'status' | 'data'
  console.log('<<', event.data);
} 

Benefits:

Other thoughts

@freddyaboulton @abidlabs Should the python client follow a similar pattern and return an iterator for streams instead? I've seen this pattern quite a bit recently when working with streams and found it really nice.

freddyaboulton commented 2 weeks ago

I really like the suggestion @pngwn ! Would match the api of the python client