Python – Create UIs for prototyping Machine Learning model with Gradio
Gradio is an open-source python library which allows you to quickly create easy to use, customizable UI components for your ML model, any API, or any arbitrary function in just a few lines of code. You can integrate the GUI directly into your Python notebook, or you can share the link to anyone.
We can create interfaces with Gradio using gradio.Interface() function.
gradio.Interface(self, fn, inputs, outputs, examples=None, live=False,
capture_session=False, title=None, description=None)
- fn: (Callable)the function to wrap an interface.
- inputs: (Union[str, List[Union[str, AbstractInput]]]) a single Gradio input component, or list of Gradio input components.
- outputs: (Union[str, List[Union[str, AbstractOutput]]]) a single Gradio output component, or list of Gradio output components.
- live: (bool) whether the interface should automatically reload on change.
- capture_session: (bool) if True, captures the default graph and session (needed for Tensorflow 1.x)
- title: (str) a title for the interface; if provided, appears above the input and output components.
- description: (str) a description for the interface; if provided, appears above the input and output components.
- examples: (List[List[Any]]) sample inputs for the function; if provided, appears below the UI components and can be used to populate the interface. Should be nested list, in which the outer list consists of samples and each inner list consists of an input corresponding to each input component.
UI for the interface can be generated by gradio.Interface, launch() function.
share: (bool) - whether to create a publicly shareable link from your computer for the interface.
There are several Input and Output Component given for the inputs and outputs parameter of gradio.Interface().
|Input Components||Output Components|
|Microphone Textbox Slider Checkbox CheckboxGroup Radio Dropdown Image Sketchdown Webcam||Textbox Label Image Image KeyValues|
Code: function which returns the factorial of a number.
Now, to wrap this function with gradio interface write following code in the same file.
When you run the above code cells in a jupyter notebook. It will generate a UI like this:
You can also copy the link and share that to anyone, it will open the same UI in the browser. Now, we’ll show you how to make an interface for a Machine Learning model.
For the demo, we’ll load a pre-trained Inception Net Image Classification model with tensorflow. Since this is an Image Classification model we’ll use Image input interface. We’ll output a dictionary of labels and their corresponding confidence scores with the Label output interface.
When you run the above code cell it will generate the UI like this:
Here you can drag and drop the image in the left section of UI and click submit you will get the result like:
Also if you copy the link and paste it in your browser, your interface will look like this: