Documentation for Using AWS
SageMaker with Python
1. Use Cases
Using AWS SageMaker with Python can be beneficial for:
- Model Training: Train machine learning models using large datasets in a scalable manner.
- Model Deployment: Deploy trained models to a production environment with minimal
effort.
- Real-Time Predictions: Get real-time predictions by invoking deployed models.
- Batch Transformations: Process large batches of data efficiently.
- Experiment Management: Track experiments, model versions, and performance metrics.
2. Step-by-Step Implementation
Step 1: Set Up AWS Credentials
Ensure you have AWS credentials (Access Key and Secret Key) with the necessary
permissions for SageMaker operations.
Step 2: Install Required Libraries
Run the following command to install the necessary libraries:
pip install requests requests-aws4auth
Step 3: Create the AWS4Auth Object
```python
from requests_aws4auth import AWS4Auth
access_key = 'YOUR_ACCESS_KEY'
secret_key = 'YOUR_SECRET_KEY'
region = 'us-east-1'
service = 'sagemaker'
auth = AWS4Auth(access_key, secret_key, region, service)
```
Step 4: Define the SageMaker Endpoint
```python
endpoint_url = 'https://runtime.sagemaker.us-east-1.amazonaws.com/endpoints/your-
endpoint/invocations'
```
Step 5: Define the Input Payload
```python
input_payload = {
"inputs": "Your input text here"
}
```
Step 6: Send the Request
```python
import requests
import json
headers = {
'Content-Type': 'application/json'
}
response = requests.post(endpoint_url, auth=auth, headers=headers,
data=json.dumps(input_payload))
if response.status_code == 200:
print("Response: ", response.json())
else:
print("Error: ", response.status_code, response.text)
```
Complete Code:
```python
import requests
import json
from requests_aws4auth import AWS4Auth
access_key = 'YOUR_ACCESS_KEY'
secret_key = 'YOUR_SECRET_KEY'
region = 'us-east-1'
service = 'sagemaker'
auth = AWS4Auth(access_key, secret_key, region, service)
endpoint_url = 'https://runtime.sagemaker.us-east-1.amazonaws.com/endpoints/your-
endpoint/invocations'
input_payload = {
"inputs": "Your input text here"
}
print("Payload being sent:", json.dumps(input_payload))
headers = {
'Content-Type': 'application/json'
}
response = requests.post(endpoint_url, auth=auth, headers=headers,
data=json.dumps(input_payload))
if response.status_code == 200:
print("Response: ", response.json())
else:
print("Error: ", response.status_code, response.text)
```
3. Advantages
- Scalability: Automatically scales to handle large datasets and workloads.
- Integration: Easily integrates with other AWS services.
- Flexibility: Supports multiple machine learning frameworks.
- Cost-Effective: Pay only for the resources you use.
- Managed Environment: Simplifies the process of model training, deployment, and
management.
4. Disadvantages
- Complexity: May require a steep learning curve for beginners.
- Cost: Can become expensive with large-scale operations.
- Vendor Lock-In: Dependency on AWS services can make migration difficult.
5. How to Create the Endpoint
1. Train the Model: Train your machine learning model using SageMaker.
2. Deploy the Model:
- Navigate to the SageMaker console.
- Choose "Endpoints" from the left-hand menu.
- Click "Create endpoint".
- Provide a name and select the model you trained.
- Configure the endpoint settings and deploy.
6. Purpose
The purpose of using AWS SageMaker with Python is to leverage the powerful, scalable
machine learning capabilities of SageMaker for various tasks such as training, deploying,
and invoking machine learning models. This allows data scientists and developers to
efficiently manage the end-to-end machine learning workflow, from data preparation and
model training to deployment and monitoring, all within a managed and scalable AWS
environment.
---------------------------------------------------live Test code ---------------------------------
import requests
import json
from requests_aws4auth import AWS4Auth
# Define your AWS credentials
access_key = 'AKIAZI2LGGNXZWWP7UGU'
secret_key = 'kzuGz64PDaKAkgEqPOzBgBYId5wZ+4IPnANIXsgo'
# Define your AWS region and service
region = 'us-east-1'
service = 'sagemaker'
# Create AWS4Auth object
auth = AWS4Auth(access_key, secret_key, region, service)
# Define the SageMaker endpoint
endpoint_url = 'https://runtime.sagemaker.us-east-1.amazonaws.com/endpoints/canvas-meta/
invocations'
# Define the input payload
input_payload = {
"inputs": "what is zoho"
# Print the JSON payload for debugging
print("Payload being sent:", json.dumps(input_payload))
# Define headers
headers = {
'Content-Type': 'application/json'
# Send the request
response = requests.post(endpoint_url, auth=auth, headers=headers,
data=json.dumps(input_payload))
# Check the response
if response.status_code == 200:
print("Response: ", response.json())
else:
print("Error: ", response.status_code, response.text)
Output_------
PS D:\sahibfolder> &
C:/Users/Sahib/AppData/Local/Programs/Python/Python310/python.exe
d:/sahibfolder/openai.py
Payload being sent: {"inputs": "what is internet"}
Response: [{'generated_text': ' of things (IoT) and its applications\n\nThe Internet of
Things (IoT'}]
PS D:\sahibfolder> &
C:/Users/Sahib/AppData/Local/Programs/Python/Python310/python.exe
d:/sahibfolder/openai.py
Payload being sent: {"inputs": "what is zoho"}
Response: [{'generated_text': ' crm\n\nZoho CRM is a cloud-based customer relationship
management (CRM'}]
PS D:\sahibfolder>