June 14, 2023
Companies around the world use Snowflake to securely store, manage and process their data at…
Machine learning is a branch of artificial intelligence that gives computers the ability to learn without being explicitly programmed to do so. It is used to teach computers to do things like identify objects in photos, spot faces on security footage, and translate one language to another, among many other things.
One way to test if a machine learning model behaves as expected, is by using a demo interface. A demo interface can help you debug any issues that you may be having with your machine learning model. For example, you can use Gradio, a very popular Python library, which permits you to build a testing web interface very quickly.
The good news is that you can use Gradio to interact with your Cometexperiments as well! In this article, we will go over how to build a demo interface in Gradio, and integrate it with Comet.
The article is organized as follows:
As an example, we will use the popular iris dataset, provided by scikit-learn, to build a K-Nearest Neighbors classification model. Then we will build a Gradio interface, which will permit you to provide new input, and use the model to predict the class label. Finally, we will track the model in Comet.
Firstly, we should import all the required libraries. Make sure to import the libraries in the correct order; the Comet library should be imported before all the other libraries:
from comet_ml import Experiment
import gradio as gr
Next, we define the Gradio interface (which requires three main components for our example):
Firstly, we define the prediction function:
model = None
def predict(input1, input2):
class_names = ['setosa', 'versicolor', 'virginica']
prediction = model.predict_proba([[input1,input2]])
return {class_names[i]:
float(prediction[0][i]) for i in range(len(class_names))}
We set the model variable to None
here, as we have not yet defined the model. To make Gradio work properly with Comet, we should define the Gradio interface firstly, and then we can build the machine learning model afterwards. The predict()
function returns the prediction probability for each target class.
Next, we define the inputs and the output of the Gradio interface:
inputs = [gr.inputs.Number(label='x1'),gr.inputs.Number(label='x2')]
label = gr.outputs.Label()
We create a new interface:
io = gr.Interface(fn = predict,inputs = inputs,outputs = label, title="Classification")io.launch(inline=False)
Note here that we have also launched the interface with the option inline=False
, in order to not run the interface within the notebook.
Now, we load the iris dataset:
from sklearn.datasets import load_irisiris = load_iris() X = iris.data[:, [2, 3]] y = iris.target
We consider only two input features. In this article, we do not apply any transformation to the dataset because the focus here is to illustrate how to integrate Comet with Gradio.
We split the dataset into training and test sets:
from sklearn.model_selection import train_test_splitX_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)
Now, we are ready to create the Comet experiment:
experiment = Experiment()
And, the classifier:
from sklearn.neighbors import KNeighborsClassifiermodel = KNeighborsClassifier(n_neighbors=3) model.fit(X_train, y_train)
We log the accuracy of the model in Comet:
experiment.log_metric("accuracy", model.score(X_test, y_test))
Finally, we log the Gradio interface in Comet:
io.integrate(comet_ml=experiment)
The final statement above also terminates the Comet experiment, so you do not need to call the experiment.end()
method (if you are using a notebook).
Prompt engineering plus Comet plus Gradio? What comes out is amazing AI-generated art! Take a closer look at our public logging project to see some of the amazing creations that have come out of this fun experiment.
To see the Gradio interface in Comet, you should create a new panel. To add the Gradio panel, you can click on Add → New Panel → PUBLIC →Gradio Panel →Add →Done.
The following figure shows the added Panel:
You can select the experiment, and then insert the inputs, click on the Submit button, and then see the predicted class label, with its related probability.
Congratulations! You have just learned how to integrate Comet and Gradio!
Using a demo interface is a great way to test your machine learning models to make sure they are behaving as expected. Additionally, demo interfaces can help you debug issues you may be having with your machine learning model. Gradio is a great option for those looking for a quick and easy way to build a testing web interface. And the good news is that you can use the Gradio interface directly in Comet!
Happy coding! Happy Comet!