The installation

Gradio requires Python 3. Once you have Python, you can download Gradio using the latest version of PIP, as follows:

pip install gradio
Copy the code

Alternatively, pip3 Install Gradio if you have multiple Python installations, you may want to do this.

Basic usage

Creating an interface with Gradio requires only a few lines of existing code. For example, here’s how Gradio created the interface using the pre-trained Keras model:

import gradio, tensorflow as tf
image_mdl = tf.keras.applications.inception_v3.InceptionV3()
io = gradio.Interface(inputs="imageupload", outputs="label", model_type="keras", model=image_mdl)
io.launch()
Copy the code

Running the code above will open a new browser window with an image upload. Users can drag and drop their own images, which produces the following output:

 

 

The basic parameters

Running the GradIO interface requires creating an object that serves as an input parameter: – a string representing the input interface to use, or for other custom subclasses (see below). – A string representing the output interface to use, or for other custom subclasses (see below). – Represents a string of incoming model types. Supported types include keras. – Actual model for processing. Interface(inputs : str, outputs : str, model_type : str, model : Any) inputs“gradio.AbstractInput outputs“gradio.AbstractOutput model_type model

Instead of providing string names for inputs and outputs, you can provide objects that represent input and output interfaces. For example, the code in the Basic Usage section executes as follows:

import gradio, tensorflow as tf
image_mdl = tf.keras.applications.inception_v3.InceptionV3()
inp = gradio.inputs.ImageUpload()
out = gradio.outputs.Label()
io = gradio.Interface(inputs=inp, outputs=out, model_type="keras", model=mdl)
io.launch()
Copy the code

This allows you to customize the interface by passing parameters to input and output constructors. Each interface constructor takes the parameters described below.

Supported interfaces

This is a list of currently supported interfaces for GradIO. All input interfaces can be paired with any output interface.

The input interface

Inputs = "text"

Use this interface to enter text as input. Parameters: no

The input

 

Inputs = "imageupload"

Use this interface to upload images to the model. Parameter: Shape – A tuple whose shape should resize the uploaded image before passing in the model. Default :(224, 224, 3) image_mode-pil image mode, used to convert images to numpy arrays. Usually “RGB” (3 channels RGB) or “L” (1 channel gray). Default: ‘RGB’ scale- a floating point number used to rescale the value of each pixel in the image. Default: 1/127.5 Shift – A floating point number used to move the value of each pixel in the image after scaling. Default: -1 cropper_aspect_ratio- None or the floating point number is the aspect ratio of the clipper. Default: None

The input

Delete the image here

– or –

Click the upload

Inputs = "snapshot"

Use this interface to take a snapshot from the user’s webcam. Parameter: Shape – A tuple whose shape should resize the uploaded image before passing in the model. Default :(224, 224, 3) image_mode-pil image mode, used to convert images to numpy arrays. Usually “RGB” (3 channels RGB) or “L” (1 channel gray). Default: ‘RGB’ scale- a floating point number used to rescale the value of each pixel in the image. Default: 1/127.5 Shift – A floating point number used to move the value of each pixel in the image after scaling. Default: -1 cropper_aspect_ratio- None or the floating point number is the aspect ratio of the clipper. Default: None

The input

Click to upload a snapshot from the webcam.

Inputs = "sketchpad"

Use this interface to take simple monochrome Cketches as input. Parameter: Shape – A tuple whose shape should resize the uploaded image before passing in the model. Default values :(224, 224, 3) invert_colors- a Boolean value that specifies whether colors should be inverted before passing in the model. Default: True,

The input

Inputs = "microphone"

Use this interface for audio input to the microphone.

The input

Click to upload audio from the microphone.

Inputs = "audio_file"

Use this interface to upload audio to the model.

The input

Delete the audio file here

– or –

Click the upload

Output interface

"Classifier outputs ="

Use this interface to categorize. Respond with confidence intervals.

production

happy

happy

73%

surprised

20%

sad


anger


Outputs = "text"

Use this interface to display the output text.

production

 

Outputs = "image"

Use this interface to display the output text.

production

Custom interface

In fact, custom input and output interfaces are fairly typical, so they either preprocess the input in the way the model accepts it, or post process the results of the model in an appropriate way so that the output interface can display the results. For example, you might need to adjust the preprocessing of the image upload interface so that the image is resized to the correct size before it is entered into the model. This can be done in one of two ways :(1) instantiating gradio.input/gradio.output objects with custom parameters, or (2) providing custom preprocessing/post-processing capabilities.

Input/output objects with custom parameters

For small, common changes to the input and output interfaces, you can often simply change the parameters in the constructors of the input and output objects to affect preprocessing/post-processing. Here is an example of resizing the image to a different size before entering it into the model and adjusting the output interface to hide the confidence region and show the first 5 classes instead of the default 3:

import gradio, tensorflow as tf
image_mdl = tf.keras.applications.inception_v3.InceptionV3()
inp = gradio.inputs.ImageUpload(shape=(299, 299, 3))
out = gradio.outputs.Label(num_top_classes=5)
io = gradio.Interface(inputs=inp, outputs=out, model_type="keras", model=mdl)
io.launch()
Copy the code

Custom preprocessing/post-processing functions

Alternatively, you can completely override the default preprocessing/post-processing capabilities by providing your own functionality. For example, here we modify the ImageUpload interface’s preprocessing function to add some noise to the image before feeding it into the model.

import gradio, base64, numpy as np, tensorflow as tf
from io import BytesIO
from PIL import Image
image_mdl = tf.keras.applications.inception_v3.InceptionV3()

def pre(inp):
    im = gradio.preprocessing_utils.encoding_to_image(inp)
    im = gradio.preprocessing_utils.resize_and_crop(im, (299, 299))
    im = np.array(im).flatten()
    im = im * 1/127.5 - 1
    im = im + np.random.normal(0, 0.1, im.shape)   # Adding the noise
    array = im.reshape(1, 299, 299, 3)
    return array

inp = gradio.inputs.ImageUpload(preprocessing_fn=pre)
io = gradio.Interface(inputs=inp, outputs="label", model_type="keras", model=mdl)
io.launch()
Copy the code

Model type

We currently support the following models:

model_type="sklearn"

This allows you to pass in the SciKit-Learn model and get predictions from it. This is a complete example of training the SKLearn model and gradio creating interfaces around it.

from sklearn import datasets, svm import gradio digits = datasets.load_digits() n_samples = len(digits.images) data = digits.images.reshape((n_samples, -1)) # flatten the images # Create a classifier: Classifier = svm.SVC(gamma=0.001) classifier. Fit (data, digits.target) # The sklearn digits dataset is different from MNIST: it is 8x8 and consists of black digits on a white background. inp = gradio.inputs.Sketchpad(shape=(8, 8), flatten=True, scale=16/255, invert_colors=False) io = gradio.Interface(inputs=inp, outputs="label", model_type="sklearn", model=classifier) io.launch()Copy the code

model_type="keras"

This allows you to pass in the KerAS model and get the predictions from the model. This is a complete example of training the Keras model and gradio creating interfaces around it.

import gradio, tensorflow as tf (x_train, y_train),(x_test, y_test) = tf.keras.datasets.mnist.load_data() x_train, X_test = x_train / 255.0, x_test / 255.0 model = tf keras. Models. Sequential ([tf. Keras. The layers. Flatten (), Tf. Keras. The layers. Dense (512, activation = tf. Nn. Relu), tf. Keras. The layers. The Dropout (0.2), tf. Keras. The layers. The Dense (10, activation=tf.nn.softmax) ]) model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(x_train, y_train, epochs=5) loss, accuracy = model.evaluate(x_test, y_test) io = gradio.Interface(inputs="sketchpad", outputs="label", model=model, model_type='keras') io.launch(inline=True, share=True)Copy the code

Run this code in a Colab notebook to see the interface embedded in the notebook.

model_type="pytorch"

This allows you to pass in the PyTorch model and get the predictions from the model. This is a complete example of training the PyTorch model and gradio creating interfaces around it.

import torch import torch.nn as nn import torchvision import torchvision.transforms as transforms import gradio # Device  configuration device = torch.device('cpu') # Hyper-parameters input_size = 784 hidden_size = 500 num_classes = 10 Num_epochs = 2 batch_size = 100 learning_rate = 0.001 # MNIST dataset train_dataset = torchvision.datasets.MNIST(root='.. /.. /data', train=True, transform=transforms.ToTensor(), download=True) test_dataset = torchvision.datasets.MNIST(root='.. /.. /data',train=False, transform=transforms.ToTensor()) train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size,shuffle=True) test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False) # Fully connected neural network with one hidden layer class NeuralNet(nn.Module): def __init__ (self, input_size, hidden_size, num_classes): super(NeuralNet, self).__init__() self.fc1 = nn.Linear(input_size, hidden_size) self.relu = nn.ReLU() self.fc2 = nn.Linear(hidden_size, num_classes) def forward(self, x): out = self.fc1(x) out = self.relu(out) out = self.fc2(out) return out model = NeuralNet(input_size, hidden_size, num_classes).to(device) # Loss and optimizer criterion = nn.CrossEntropyLoss() optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate) # Train the model total_step = len(train_loader) for epoch in range(num_epochs): for i, (images, labels) in enumerate(train_loader): # Move tensors to the configured device images = images.reshape(-1, 28*28).to(device) labels = labels.to(device) # Forward pass outputs = model(images) loss = criterion(outputs, labels) # Backward and optimize optimizer.zero_grad() loss.backward() optimizer.step() inp = gradio.inputs.Sketchpad(flatten=True, scale=1/255, dtype='float32') io = gradio.Interface(inputs=inp, outputs="label", model_type="pytorch", model=model) io.launch()Copy the code

model_type="pyfunc"

This allows you to pass in any Python function and get the output from the function. This is a very simple example of a “model” with gradio interface.

import gradio

 # A very simplistic function that capitalizes each letter in the given string
def big(x):
    return x.upper()

io = gradio.Interface(inputs="textbox", outputs="textbox", model=big, model_type='pyfunc')
io.launch(inline=True, share=True)
Copy the code

A more realistic example of a PyFunc use case might be as follows, where we want to use a TensorFlow session with a training model for prediction. So we wrap the session in a Python function like this:

import tensorflow as tf import gradio n_classes = 10 (x_train, y_train),(x_test, Y_test) = tf. Keras. Datasets. Mnist. Load_data () x_train, x_test = x_train. Reshape (1, 784) / 255.0, x_test. Reshape (1, 784) / 255.0 y_train = tf.keras.utils.to_categorical(y_train, n_classes).astype(float) y_test = tf.keras.utils.to_categorical(y_test, N_classes). Astype (float) Learning_rate = 0.5 epochs = 5 batCH_size = 100 x = tf.placeholder(tf.float32, [None, 784], name="x") y = tf.placeholder(tf.float32, [None, 10], name="y") W1 = tf.Variable(tf.random_normal([784, 300], Stddev =0.03), name='W1') b1 = tf.Variable(tf.random_normal([300]), name='b1') W2 = tf.Variable(tf.random_normal([300]) W2 = tf.Variable(tf.random_normal([300]) 10], stddev=0.03), name='W2') hidden_out = tf.add(tf.matmul(x, W1), b1) hidden_out = tf.nn.relu(hidden_out) y_ = tf.matmul(hidden_out, W2) probs = tf.nn.softmax(y_) cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=y_, labels=y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy) init_op = tf.global_variables_initializer() correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) sess = tf.Session() sess.run(init_op) total_batch = int(len(y_train) / batch_size) for epoch in range(epochs): avg_cost = 0 for start, end in zip(range(0, len(y_train), batch_size), range(batch_size, len(y_train)+1, batch_size)): batch_x = x_train[start: end] batch_y = y_train[start: end] _, c = sess.run([optimizer, cross_entropy], feed_dict={x: batch_x, y: batch_y}) avg_cost += c / total_batch def predict(inp): return sess.run(probs, feed_dict={x:inp}) inp = gradio.inputs.Sketchpad(flatten=True) io = gradio.Interface(inputs=inp, outputs="label", model_type="pyfunc", model=predict) io.launch(inline=True, share=True)Copy the code

Significant figure

The Imageupload interface also supports a salience model in which a heat map is superimposed on top of the input image. This can be used to display characteristic attributes, for example as an interpretation method. The user provides his own saliency function, which should take three parameters: model object, input element, and input label. Here is an example of a significant function and what it might produce:

import numpy as np
import tensorflow as tf
from deepexplain.tensorflow import DeepExplain
from tensorflow.keras import backend as K
from tensorflow.keras.models import Sequential, Model
import gradio

model = tf.keras.applications.MobileNet()

def saliency(model, x, y):
    y = y.reshape(1, 1, 1, 1000)
    with DeepExplain(session=K.get_session()) as de:
        input_tensor = model.layers[0].input
        fModel = Model(inputs=input_tensor, outputs = model.layers[-3].output)
        target_tensor = fModel(input_tensor)

        attributions_gradin = de.explain('grad*input', target_tensor, input_tensor, x, ys=y)
        sal = np.sum(np.abs(attributions_gradin.squeeze()), axis=-1)
        sal = (sal - sal.min()) / (sal.max() - sal.min())
        return sal

inp = gradio.inputs.ImageUpload()
out = gradio.outputs.Label(label_names='imagenet1000', max_label_words=1, word_delimiter=",")

io = gradio.Interface(inputs=inp,
                      outputs=out,
                      model=model,
                      model_type='keras',
                      saliency=saliency)

io.launch();
Copy the code

Produce this:

Startup options

When launching the interface, you can choose to pass several Boolean parameters to determine how the interface should appear. The following example shows all possible parameters:

io.launch(inbrowser=True, inline=False, validate=False, share=True)
Copy the code

Inbrowser – Whether the model should start in a new browser window. Inline – Whether the model should be embedded in an interactive Python environment (such as Jupyter notebooks or Colab notebooks). Validate-gradio should try to verify interface model compatibility before launching. Share – Whether a public link to the shared model should be created. Used for processing.