To get Gradio running with a simple example, follow these three steps:
1. Install Gradio from pip.
pip install gradio
2. Run the code below as a Python script or in a Python notebook (or in a colab notebook).
import gradio as gr def greet(name): return "Hello " + name + "!" gr.Interface(fn=greet, inputs="text", outputs="text").launch()
3. The interface below will appear automatically within the Python notebook, or pop in a browser on http://localhost:7860 if running from a script.
Gradio can wrap almost any Python function with an easy to use interface. That function could be anything from a simple tax calculator to a pretrained model.
Interface class is initialized with three parameters:
fn: the function to wrap
inputs: the input component type(s)
outputs: the output component type(s)
launch()them. But what if you want to change how the UI components look or behave?
What if we wanted to customize the input text field - for example, we wanted it to be larger and have a text
hint? If we use the actual input class for
Textbox instead of using the string shortcut, we have
access to much more customizability. To see a list of all the components we support and how you
can customize them, check out the Docs.
import gradio as gr def greet(name): return "Hello " + name + "!" gr.Interface( greet, gr.inputs.Textbox(lines=2, placeholder="Name Here..."), "text").launch()
Multiple Inputs and Outputs
Let's say we had a much more complex function, with multiple inputs and outputs. In the example below, we have a function that takes a string, boolean, and number, and returns a string and number. Take a look how we pass a list of input and output components.
import gradio as gr def greet(name, is_morning, temperature): salutation = "Good morning" if is_morning else "Good evening" greeting = "%s %s. It is %s degrees today" % (salutation, name, temperature) celsius = (temperature - 32) * 5 / 9 return greeting, round(celsius, 2) gr.Interface( greet, ["text", "checkbox", gr.inputs.Slider(0, 100)], ["text", "number"]).launch()
We simply wrap the components in a list. Furthermore, if we wanted to compare multiple functions that have the same input and return types, we can even pass a list of functions for quick comparison.
Working with Images
Let's try an image to image function. When using the
Image component, your function will receive a
numpy array of your specified size, with the shape
(width, height, 3), where the last dimension represents the RGB values. We'll return an image as well in the form of a numpy array.
import gradio as gr import numpy as np def sepia(img): sepia_filter = np.array([[.393, .769, .189], [.349, .686, .168], [.272, .534, .131]]) sepia_img = img.dot(sepia_filter.T) sepia_img /= sepia_img.max() return sepia_img gr.Interface( sepia, gr.inputs.Image(shape=(200, 200)), "image").launch()
Image input interface comes with
an 'edit' button which opens tools for cropping, flipping,
rotating, drawing over,
and applying filters to images. We've found that manipulating
in this way will often reveal hidden flaws in a model.
That's all you need to get started with Gradio! To see a list of all the components available, check out the Docs. To see some examples of machine learning models using Gradio, check out the ML Examples page.
Sharing Interfaces Publicly & Privacy
Interfaces can be easily shared publicly by setting
share=True in the
launch() method. Like this:
gr.Interface(classify_image, image, label).launch(share=True)
This generates a public, shareable link that you can send to anybody! When you send this link, the user on the other side can try out the model in their browser. Because the processing happens on your device (as long as your device stays on!), you don't have to worry about any dependencies. If you're working out of colab notebook, a share link is always automatically created. It usually looks something like this: XXXXX.gradio.app. Although the link is served through a gradio link, we are only a proxy for your local server, and do not store any data sent through the interfaces.
Keep in mind, however, that these links are publicly accessible, meaning that anyone can use your model
for prediction! Therefore, make sure not to expose any
sensitive information through the functions you write, or allow any critical changes to occur on
your device. If you set
share=False (the default), only a local link is created, which
can be shared by port-forwarding with specific
Links expire after 6 hours. Need longer links, or private links? Contact us for Gradio Teams.