In this video you’ll see how to use Amazon CodeWhisperer in JupyterLab.
With this AI-powered tool, you can increase developer productivity by
accelerating any coding task, reduce risk with built-in security scanning,
and work with familiar JupyterLab tools for data science and machine learning tasks.
We’ll begin in a command-line terminal window, where we’ll install JupyterLab Version 3.6.3.
For demonstration purposes, we have a computer with both Python version 2 and Python version 3 installed.
To install JupyterLab, we’ll use the command “pip3 install jupyterlab.”
Next, we’ll run a command to start the JupyterLab server.
This opens the JupyterLab interface in a new browser window, where the local
working directory appears on the left side of the page.
The directory contains a few data files and Python files that we'll be working with shortly.
Next, let’s navigate to the extension manager to view the extensions that have been installed for JupyterLab.
Currently, only the Pygments extension is installed.
Let’s return to the command-line terminal and install the CodeWhisperer Amazon JupyterLab extension.
Next, we’ll enter a command to update JupyterLab with the newly installed extension.
Next, we’ll shut down the JupyterLab server in the JupyterLab command window by pressing Control C.
We’ll confirm the shutdown.
Next, we’ll restart the server with the same command we used earlier.
The Amazon CodeWhisperer JupyterLab extension has been installed.
Let’s start the extension.
To use CodeWhisperer, we need to sign in using an AWS Builder ID.
Let’s copy the access request code and proceed.
We’ll paste the code and sign in using our AWS Builder ID.
We’ll allow CodeWhisperer for JupyterLab to access our data.
Now, let’s return to the JupyterLab interface.
The checkmark next to CodeWhisperer indicates that we can begin hands-on coding with CodeWhisperer.
Let’s open a Python file to test some of CodeWhisperer’s core functionality.
In this Python file, we’ll use CodeWhisperer to create AWS Lambda functions.
First, let’s enter a comment to tell CodeWhisperer to create an AWS
Lambda function that retrieves a file from a URL.
CodeWhisperer suggested multiple functions, which we can cycle through
using the arrow keys on our keyboard.
Let’s accept the first suggestion by pressing the Tab key.
We can press Enter to receive suggestions for the rest of this function.
Next, we’ll create a function to upload an image to S3.
CodeWhisperer provides us with suggestions that include the API
definition for uploading images to S3, so we don’t need to spend time looking through documentation.
Let’s view some additional examples of CodeWhisperer in action.
In this Python file, we can create a function to perform a well-known algorithm for binary search.
CodeWhisperer quickly provides relevant recommendations to implement this algorithm.
We’ll accept the suggestions.
Our binary search function is now complete using an iterative methodology.
We can add more context to our comment to specify the algorithm methodology we want to use.
Next, let’s create a binary search algorithm that uses a recursive search method.
We’ll accept CodeWhisperer’s suggestions.
Next, let’s use CodeWhisperer to create a function that verifies email addresses
using an imported Regular Expression (RegEx) package.
CodeWhisperer recommended code that uses the imported RegEx match function,
which saves us from needing to go to a separate RegEx testing website to validate the regular expression.
Next, let's look at an example of how we can provide CodeWhisperer with
additional context to get more relevant code suggestions.
We’ll use the same comment as earlier,
along with some additional code to provide context to CodeWhisperer.
In this case, we imported the Boto3 Python package and created an
Amazon Simple Email Service (Amazon SES) client in Boto3.
CodeWhisperer accounts for the imported Boto3 Python package
and provides a recommendation to use the verify_email_identity API from Amazon SES.
This simplifies email validation through an API call rather than using a RegEx package.
Now let's open a JupyterLab notebook to see how we can use CodeWhisperer
for data science and machine learning-related use cases.
We’ll type a comment that tells CodeWhisperer to generate some fake user data.
Let’s accept the suggestions for some additional users by pressing the Tab key.
Next, let’s use CodeWhisperer to save this fake user data into a CSV file.
We’ll accept the suggestion.
Next, let’s prompt CodeWhisperer to save the file into an S3 bucket.
CodeWhisperer creates suggestions that utilize the best APIs for the job.
In this case, it suggests we import the Boto3 library.
Let’s accept the suggestions.
Now let's create a new Python 3 notebook to implement linear regression using CodeWhisperer.
We’ll begin by typing a comment.
CodeWhisperer suggests the libraries to import.
Next, we’ll add a comment that tells CodeWhisperer to read the data.csv file as a pandas dataframe.
Next, we’ll add comments to print the dataframe and get more information about it.
Let’s save and execute this code block.
Using our CodeWhisperer-generated code, we can see the data.csv file contains housing information.
Next, let's plot the data on a graph where the x values are from the square feet column,
and the y values are from the price column.
We’ll write comments to get the values, and use CodeWhisperer’s suggestions.
We can change the code if needed to match our table data.
In this case, we’ll capitalize “price.”
Next, let’s plot the values.
We’ll label the graph using CodeWhisperer’s suggestions.
Let's save and run this code block.
The code generates a scatter plot populated with the pricing and square footage values.
Now let’s create a linear regression model using the sklearn library.
CodeWhisperer has suggested a function that fits the housing data into a linear regression
model and then returns the model.
Let’s accept the suggestion.
Next, we’ll begin writing a comment to call the function.
CodeWhisperer suggests the rest of the comment.
We’ll accept the suggestion.
Finally, let's plot the regression line with x and y values.
CodeWhisperer suggests code to plot the linear regression line for our data.
Let's save the notebook and run this code block.
The graph contains the original data points as a scatter plot, and now includes the line of best fit.
Now let's create another Python notebook to implement KMeans clustering.
We'll name this notebook kmeans.
Let’s begin by manually importing the libraries we want to use.
CodeWhisperer knows we will need matplotlib to create a scatter plot,
so we‘ll accept the suggestion.
Lastly, we’ll import the KMeans module from the sklearn library.
We’ll write a comment to load the data.json file into a pandas dataframe.
Next, we'll get information about the dataframe.
Let's run this code block to preview the data.
The columns include data on miles, pace, heart rate, and elevation.
Let's create a scatter plot to visualize the heart rate and elevation columns.
We’ll accept the suggestions.
Now, let’s execute this code block to generate the scatter plot.
Here is the scatter plot, where heart rate is represented on the x-axis,
and the elevation is represented on the y-axis.
Next, we’ll implement KMeans clustering to group the data.
First, we'll turn the dataset into a set of coordinates.
By visually inspecting the scatter plot, we can estimate three sets of clusters.
Let's create a kmeans object with three clusters.
We’ll accept CodeWhisperer’s suggestion.
Next, let’s fit the kmeans object to the data coordinates.
CodeWhisperer created some suggestions for us.
The second option includes the next step, which is to get the cluster labels.
We’ll accept this suggestion.
Next, we’ll get the cluster centers and plot them.
Finally, we'll plot the x and y coordinates with labels.
Let's save the notebook and run the code block.
Each cluster is displayed in a unique color, and the central point of each
cluster is represented with a red dot.
You’ve just seen how to use Amazon CodeWhisperer in JupyterLab.
You can learn more about this topic in the description and links for this video.
Thanks for watching. Now it’s your turn to try.