Amazon CodeWhisperer with JupyterLab extension for Amazon SageMaker Studio – Part 4

What’s new with Amazon CodeWhisperer ?

On 10 May 2023, Senior Principal Technologist at AWS Brian Granger announced extensions to generative AI on Jupyter including:

  • Jupyter AI, an open-source project to bring generative AI to Jupyter notebooks
  • Introducing new Jupyter extensions to build, train, and deploy ML at scale
  • Amazon CodeWhisperer Jupyter extension

For developers, cloud architects, data scientists, data analysts and machine learning engineers in particular this is exciting news allowing JupyterLab users to install Amazon CodeWhisperer to use Python notebooks and also Amazon SageMaker Studio.

You may read all of the Jupyter extensions here.

Learning Objective

You will learn:

  • How to install Amazon CodeWhisperer with JupyterLab extension to generate code recommendations within Amazon SageMaker Studio

What’s the benefit of using Amazon CodeWhisperer?

  • You may get started and up and running on a machine learning project and increase your developer productivity with auto-completion, provides code suggestions by learning from code snippets and python comments.
  • You may also code in other languages such as:

Python, Java, JavaScript, TypeScript, C#, Go, Rust, PHP, Ruby, Kotlin, C, C++, Shell scripting, SQL, and Scala in various IDEs.

iden

You may read my previous blog post if you would like to know how to install Amazon CodeWhisperer for VS Code IDE.

I recently used Amazon CodeWhisperer to kickstart a personal machine learning project which allowed me to code faster.

By using Amazon CodeWhisperer in VS code editor, I was able to complete exploratory data analysis (EDA) in python, I simply typed words into VS code editor and asked a question like a friend using natural language. EDA was completed within 15 minutes and learned about my intent by reading my python comments and making the next suggestion in the data analytics workflow.

ask

hist

Get code recommendations for Amazon SageMaker Studio

Amazon SageMaker Studio launched in 2019 as an IDE using JupyterLab
to allow data science teams to collaborate on the end to end data science workflow which includes build models, train models, fine-tune hyper parameters, debug, deploy, and monitor model drift.

On the 10 May 2023, there were new announcements

You may now receive real-time code recommendations from Amazon CodeWhisperer as you code and build your machine learning pipeline within Amazon SageMaker Studio.

Pre-requisites

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "CodeWhispererPermissions",
      "Effect": "Allow",
      "Action": ["codewhisperer:GenerateRecommendations"],
      "Resource": "*"
    }
  ]
}

Tutorial: Add the CodeWhisperer-related permissions to your SageMaker execution role.

For this tutorial please follow the steps from the Amazon CodeWhisperer Developer Guide.

Step 1: Login to your AWS account as an Admin User and choose your preferred AWS region e.g. Asia Pacific Sydney (ap-southeast-2).

Step 2: Navigate to the IAM dashboard.

IAM

Step 3: Create an IAM policy that contains the following actions to enable Amazon CodeWhisperer for Amazon SageMaker Studio role.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "CodeWhispererPermissions",
      "Effect": "Allow",
      "Action": ["codewhisperer:GenerateRecommendations"],
      "Resource": "*"
    }
  ]
}

Navigate to Policies on the left-handside menu.

policy

Step 4: Click Create policy.

Create policy

Step 5: Click json.

jsonn

Step 6: Copy the new IAM policy into the json statement and click Next.

copy

Step 7: Provide a policy name e.g. Attach_Amazon_CodeWhisperer_to SageMaker_Studio and click Create policy.

policy ame

The policy was successfully created.

view

You may inspect the policy.

created

Navigate to IAM and click Roles, search for ‘Sagemakerfullaccess role’ and check if there are any policies already attached.

sagemakerfull

Step 8: Navigate to Policies and search for policy ‘Attach_Amazon_CodeWhisperer_to SageMaker_Studio’ and attach the following IAM entity:

SageMakerfullaccess

attach polocu

Step 9: Navigate to the Amazon SageMaker dashboard and on the left-handside menu click Getting Started and on the right select Create a role.

sae

Step 10: Enter the following details such as providing a role name with the pre-fix of SageMaker. e.g. DataScientist, MLEngineer

DataScientist_SageMaker

You may also provide a short description of the role e.g. create a role to enable Amazon CodeWhisperer recommendations for Amazon SageMaker Studio.

Retain the other default settings and click Next.

role

Step 11: Configure the role by selecting the ML activities.

configure

You may select more activities or de-select the ML activities that you do not require.

Step 12: Scroll down and enter the name of one of your Amazon S3 buckets to store your training and test data. Click Next.

sagemaker–<AWS account number

bucket

Step 13: In the search bar, start typing the name of the IAM policy that was created in Step 7.

Attach_Amazon_CodeWhisperer_to SageMaker_Studio

search policy

Check the box of the policy and click Next.

seelct

Step 14: Inspect and review the role details.

reviewn

Review the additional policy attached to the SageMaker execution role and click Submit.

submit tole

Step 15: The Sagemaker_DataScientist_SageMaker role was successfully created and you may view the role in IAM.

sagemaker role

You can see that this new role has an additional policy attached to enable Amazon CodeWhisperer to be used under the SageMaker execution role for Amazon SageMaker Studio.

Iiam code

Step 16: Enable Amazon CodeWhisperer within Amazon SageMaker Studio

Select Studio on the left-handside menu and click Open Studio.

open

Step 17: In Amazon SageMaker Studio, click Open Launcher.

open launchr

Step 18: Under the heading ‘Utilities and file’, click System terminal.

system terminal

Step 19: Copy the code into terminal:

conda activate studio
pip install amazon-codewhisperer-jupyterlab-ext
jupyter server extension enable amazon_codewhisperer_jupyterlab_ext
conda deactivate
restart-jupyter-server

The installation process will take a few seconds to complete.

comeplt

Step 20: Navigate to the Launcher homepage and select Create Notebook.

create notebookn

I received the following notification message.

notification

Step 21: Close the Amazon SageMaker Studio session.

Refresh your browser and navigate to Studio and click Open Studio and wait for Amazon SageMaker Studio to open.

Under the notebook you will be able to see a tick against CodeWhisperer to confirm that code recommendations are enabled for the code cells.

Step 22: Launch Studio again and you will be taken to JupyterLab which will open the last notebook and start typing Python code into the code cells to generate code recommendations.

stat tryping

Reference

Until the next lesson, happy learning! 😀

Build on Generative AI – Using your Python scripts in Sagemaker

You may be interested to learn more from to change your Python scripts to run on SageMaker Training? 🦅Join guest Giuseppe Angelo Porcelli, along with cohosts Darko Mesaroš & Emily Webber to learn more on Build on Generative AI

原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/302266.html

(0)
上一篇 2023年9月7日 16:00
下一篇 2023年9月7日 21:50

相关推荐

发表回复

登录后才能评论