forked from swapnil123kapile/Project-13
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBreast-Cancer-Prediction-Docs.txt
161 lines (111 loc) · 4.63 KB
/
Breast-Cancer-Prediction-Docs.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
Sagemaker links -
https://towardsdatascience.com/a-practical-guide-to-mlops-in-aws-sagemaker-part-i-1d28003f565
https://aws.amazon.com/getting-started/hands-on/build-train-deploy-machine-learning-model-sagemaker/
---------------------------------------------------------
Build ML model using sagemaker Studio notebook -
Yiu tube - https://www.youtube.com/watch?v=1iSiN4sVMjE&t=1900s
https://github.com/seanpmorgan/studio-notebook-webinar
https://github.com/aws-samples/sagemaker-studio-lifecycle-config-examples/blob/main/scripts/install-lsp-features/on-jupyter-server-start.sh
pip install jupyterlab-code-formatter black
%pip install --ignore-installed --upgrade tensorflow
%pip install --upgrade boto3 sagemaker -U
pip install scikit-learn --upgrade
Go to lambda on AWS
name to lambda function
runtime - python 3.6
Architecture - x86_64
Create fucntion
copy the code form AWS
deploy
go to
-----------------------------------------------------------------------------------------------------------------------
Breast Cancer Prediction
# End to end sagemaker with lambda API
https://sagemaker-examples.readthedocs.io/en/latest/introduction_to_applying_machine_learning/breast_cancer_prediction/Breast%20Cancer%20Prediction.html
https://aws.amazon.com/blogs/machine-learning/call-an-amazon-sagemaker-model-endpoint-using-amazon-api-gateway-and-aws-lambda/
You Tube - https://www.youtube.com/watch?v=stD47vPDadI
Creatr Sagemaker Instance
instance type - ml.t.medium
Permission and encryption - IAM role - Create new role - Any S3 bucket - Create notebook instance
click on Open Jupyter
Go to Sagemaker Examples
use Breast Cancer Prediction.ipynb - Create Copy
It will open the Breast Cancer Prediction notebook
%pip install --upgrade boto3 sagemaker -U
Run all the cells.
Once all cells have been run and endpoint created go to Sagemaker Lambda
Create Function- Give name -
runtime - Python 3.9
replace the below code in lambda fucntion:
import os
import io
import boto3
import json
import csv
# grab environment variables
ENDPOINT_NAME = os.environ['ENDPOINT_NAME']
runtime= boto3.client('runtime.sagemaker')
def lambda_handler(event, context):
print("Received event: " + json.dumps(event, indent=2))
data = json.loads(json.dumps(event))
payload = data['data']
print(payload)
response = runtime.invoke_endpoint(EndpointName=ENDPOINT_NAME,
ContentType='text/csv',
Body=payload)
print(response)
result = json.loads(response['Body'].read().decode())
print(result)
pred = int(result['predictions'][0]['score'])
predicted_label = 'M' if pred == 1 else 'B'
return predicted_label
Deploy
go to configuration
Go to Variable Environment - edit - Create new environment
Key - ENDPOINT_NAME
Value - Genter the name of enpoint (Go to sagemaker inference to copy the endpoint name)
Go to IAM - role - select the lambda funtcion role-permission policies - select policy AWSLambdaBasicExecutionRole-b28097
edit policy- go to JSON and paste below code
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": "sagemaker:InvokeEndpoint",
"Resource": "*"
}
review-save changes
Go to API gateway in AWS Console
REST API - Build - ok -
Choose the protocol - REST
Create New API - New API
Settings - API name - give name
Description - give description
create API
Go to Actions - Create Resource -give resource name- Create Resource
Go to Actions - Create Method -select POST - Tick the checkbox next to it
Integration type - Lambda fuction
Lambda Function - select the name of your lambda fucntion created
Save -Ok
Test
in Request Body paste below data:
{"data": "1257815, 5, 1, 3, 1, 2, 2, 1, 1, 6, 4, 3, 2, 5, 5, 6, 5, 4, 4, 23, 6, 3, 2, 7, 8, 8, 3, 2, 1, 6"}
Test
It will give you a prediction for the given input as "B"
Go to Action- Deploy API
Deployment stage - [New Stage]
Stage Name - PROD
Stage Description - Production Environment
Deployment Sescription -
Deploy
It will create Invoke URL
Go to AWS console- Create T2.micro instance -
sudo su
sudo apt update
sudo apt upgrade
apt install python3-pip
sudo pip install --upgrade boto3
python3
import json
import requests
r=requests.post("https://ntvhjus37g.execute-api.us-east-1.amazonaws.com/PROD/api-ml-model", data=json.dumps({"data": "1257815, 5, 1, 3, 1, 2, 2, 1, 1, 6, 4, 3, 2, 5, 5, 6, 5, 4, 4, 23, 6, 3, 2, 7, 8, 8, 3, 2, 1, 6"}))
r.content
It will give you a prediction as "B"