-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Refactor api logic, create JS API client #3334
Conversation
🎉 The demo notebooks match the run.py files! 🎉 |
All the demos for this PR have been deployed at https://huggingface.co/spaces/gradio-pr-deploys/pr-3334-all-demos |
To test this:
I have tested everything except embeds, doing that now. |
better pass now |
Did some testing @pngwn ! I am leaving some notes below but most demos are working well for me!
This branchMain |
Thanks @freddyaboulton! Will take a look at these issues. |
@freddyaboulton Are you sure they are the same event trigger? That apphas a situation where a.change updates b which trigger b.change. I think you are just seeing all of that happen instantly because you are local. Do you get the same behaviour if. you apply some throttling to you local network? Alternatively could you inspect the messages and check if their payloads are the same or different? |
@pngwn Doesn't happen locally on main 🤔 |
Hmm, let me take a look. |
Okay, I think I have fixed everything now @freddyaboulton. |
@pngwn Did another round of testing! I verified that all of the problems listed above are now fixed. Thank you! Found some other things I wanted to bring to your attention:
I should clarify that the
Tried a bunch of demos and they all seem to be working well besides this. Will continue looking! |
:[ |
@freddyaboulton this time it is definitely all working |
Suggestion for naming:
Not sure if this nomenclature would sound famililar to JS developers, maybe we need to tweak, but this sounds about right to me since the second one creates a job while not immediately returning a prediction. |
Will revisit later. |
Description
Implements JS API client.
Closes #3310. Closes #3262. Closes #3263. Closes #3269.
This PR implements a JS client for the gradio API. The API looks like this:
This is pretty idiomatic in javascript but more importantly it allows for a very clean and consistent interface.
By utilising events instead of awaiting singular responses we can have the same interface regardless of whether we are dealing with a simple prediction function, a generator with a finite amount of iterations or a generator which runs until cancelled (
every
).i did consider an API like:
But then we would have needed a separate method for generator functions with a very different API. The equivalent for generators would have been:
This number of 'key' arguments is really not very cool in JS, we could opt for an 'options' object instead but ideally options are optional, at least 3/4 of these options are required most of the time. The number of arguments also makes the payload a required argument, which isn't ideal. Passing
null
orundefined
is again not really ideal and not very idiomatic. The API proposed at the top (and implemented in this PR) is cleaner, more consistent, more flexible, and does a better job of abstracting away the implementation of the functions.predict.cancel
This is slightly different to how cancellation works in Gradio itself. Gradio has the concept of functions cancelling other functions but this doesn't make sense for the API. If we enabled functions to cancel other functions, a decision which is usually based on the input + output components, then UI logic around the predict functions would start to bleed into the API. Prediction functions are discreet operations that do not know anything about one another and are not tied to one another (we simply bind them together via the UI), this client API represents that. With that in mind, there is an explicit 'cancel' method which will cancel a generator function if it is one. It is a noop otherwise. Calling other endpoint while one is still running will not interfere with it in anyway.
Happy to discuss this but I think it is important that we treat the API as a true API. The person consuming the API maybe not be the original author of the Gradio app and may have different intentions, I think we should offer that flexibility.
TypeScript
The client is pretty well typed too, so you get really good type checking with different callback types for different event types. It was painful but I'm pretty pleased with the results.
Screen.Recording.2023-02-27.at.23.55.43.mov