aws lambda



(Introduction)

In this project, we will evaluate the performance and the predictive power of a model trained and tested on data collected from houses in Californian districts. Upon we will use this model to predict median house values in Californian districts. This is a regression problem to predict california housing prices. The dataset contains 20640 entries and 10 variables.

在此项目中,我们将评估从加利福尼亚地区房屋收集的数据中经过训练和测试的模型的性能和预测能力。 在此之后,我们将使用此模型来预测加利福尼亚地区的房价中位数。 这是预测加利福尼亚房屋价格的回归问题。 数据集包含20640个条目和10个变量。

  • Longitude
  • Latitude
  • Housing Median Age
  • Total Rooms
  • Total Bedrooms
  • Population
  • Households
  • Median Income
  • Median House Value
  • Ocean Proximity

But my main agenda is to deploy this project on AWS , you can find link to the notebook . Execute the cells in the notebook and after execution you will see model weights being saved. Create a new folder and place notebook and weights in that folder.

但是我的主要议程是在AWS上部署该项目,您可以找到笔记本的链接。 在笔记本中执行单元,执行后您将看到模型权重已保存。 创建一个新文件夹,然后将笔记本和砝码放在该文件夹中。

Building models is never sufficient for real-time products. ML models need to be integrated with web or mobile applications. One of the best ways to solve this problem is by deploying the model as API and inferencing the results whenever required.

建立模型永远不足以提供实时产品。 ML模型需要与Web或移动应用程序集成。 解决此问题的最佳方法之一是通过将模型部署为API并在需要时推断结果。

Deploying AWS Lambda functions with the serverless framework is arguably the easiest way to deploy functions and configure how they get triggered. If you want to automate your function deployment, you will most likely do so via your CI/CD workflow.

使用无服务器框架部署AWS Lambda函数可以说是部署函数和配置如何触发函数的最简单方法。 如果要自动化功能部署,则很可能会通过CI / CD工作流程来实现。

Workflow: The client sends a request to the API. API trigger is added to the Lambda function and returning predictions back to the client through API.

工作流程 :客户端向API发送请求。 API触发器已添加到Lambda函数,并通过API将预测返回给客户端。

入门 (Getting Started)

First install the serverless framework and generate a skeleton for a Python function: npm install -g serverless

首先安装无服务器框架并为Python函数生成框架: npm install -g serverless

  1. Set up credentials to the cloud service provider . For eg in case of aws go to IAM role and create a role and note down the key and secret key
  2. Enter the credentials of service provider and connect to using :
sls config credentials --provider aws --key ____ --secret ___

3. Write the following cmd to initialize the handler.py(Lambda Function) and serverless.yml files . Open the terminal inside the new folder created above and execute the following command :

3.编写以下cmd来初始化handler.py(Lambda函数)和serverless.yml文件。 在上面创建的新文件夹中打开终端,然后执行以下命令:

sls create --template aws-python3 --name california-housing

The function is stored in the handler.py file and the manifest describing how to deploy the function and how it will gets invoked is serverless.yml.

该函数存储在handler.py文件中,用于描述如何部署该函数以及如何调用该函数的清单是serverless.yml。




4. Edit the handler.py

4.编辑handler.py

5. In handler.py we need to load the weights we have created after running Model Preparation.ipynb

5.在handler.py中,我们需要加载在运行Model Preparation.ipynb之后创建的权重。

The lambda handler function is expected to receive two parameters: event and context. The event parameter is usually a dictionary that contains the details of the event that triggered the execution of the lambda. The context parameter is a dictionary that holds information about the function execution and the execution environment.

lambda处理函数应接收两个参数: eventcontext 。 event参数通常是一个字典,其中包含触发lambda执行的事件的详细信息。 上下文参数是一个字典,其中包含有关函数执行和执行环境的信息。


6. We need the packages to be installed ,so create a new file requirements.txt in the same folder

6.我们需要安装软件包,因此在同一文件夹中创建一个新文件requirements.txt。


7. Edit serverless.yml

7.编辑serverless.yml

The serverless framework provides a command for starting a python lambda project. In this we usually provide information : Who is the provider?,What is the language used?What roles it should have ?Which file to include or exclude ?Region,parameter which aceepts the input ,handler function ,How are we going to communicate with our lambda function ie using get or post method? etc….

无服务器框架提供了用于启动python lambda项目的命令。 我们通常在其中提供以下信息:提供者是谁?使用的语言是什么?它应该扮演什么角色?要包含或排除的文件是什么?区域,接受输入的参数,处理函数,如何与之通信我们的lambda函数,即使用get或post方法? 等等…。


Now for our code to work, we need the packages to be installed, and we have already put them in a requirements.txt file, so we need to tell serverless to install those requirements, and for that we will use a Plugin called serverless-python-requirements.

现在,为了使我们的代码正常工作,我们需要安装软件包,并且已经将它们放入了requirements.txt文件中,因此我们需要告诉无服务器来安装这些要求,为此,我们将使用一个名为serverless-的插件。 python需求。

8. Create a file event.json in the same folder

8.在同一文件夹中创建一个文件event.json


9. In order to check the handler.py run the handler.py file with do_main() function and we can see the output in the terminal

9.为了检查handler.py,使用do_main()函数运行handler.py文件,我们可以在终端中看到输出

python handler.py


10. To test the lambda function locally run the following command :

10.要在本地测试lambda函数,请运行以下命令:

sls invoke local --function predict-price --path event.json

11. So, it is deployment time:

11.因此,这是部署时间:

sls deploy


12. We can eve test the lambda function that we uploaded globally using:

12.我们可以使用以下命令测试我们全局上传的lambda函数:

sls invoke global --function predict-price --path event.json


13. Now we can send request to the above mentioned url using postman mentioned in the endpoints get method to predict the house price

13.现在,我们可以使用端点get方法中提到的邮递员将请求发送到上述url,以预测房价

Finally…………..

最后…………..


Congrats, you have deployed your model in a AWS lambdas function and now can serve you.

恭喜 ,您已经在AWS lambdas函数中部署了模型,现在可以为您服务。

Link to the github : https://github.com/sarthak-sriw/california-house-prediction-deployment-aws

链接到github: https : //github.com/sarthak-sriw/california-house-prediction-deployment-aws

翻译自: https://medium.com/analytics-vidhya/deployment-of-california-house-price-prediction-model-on-aws-331ead5738b6

aws lambda