- Nov 2024
-
python.plainenglish.io python.plainenglish.io
-
Deploying Machine Learning Models with Flask and AWS Lambda: A Complete Guide
In essence, this article is about:
1) Training a sample model and uploading it to an S3 bucket:
```python from sklearn.datasets import load_iris from sklearn.model_selection import train_test_split from sklearn.linear_model import LogisticRegression import joblib
Load the Iris dataset
iris = load_iris() X, y = iris.data, iris.target
Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
Train the logistic regression model
model = LogisticRegression(max_iter=200) model.fit(X_train, y_train)
Save the trained model to a file
joblib.dump(model, 'model.pkl') ```
- Creating a sample Zappa config, because AWS Lambda doesn’t natively support Flask, we need to use Zappa, a tool that helps deploy WSGI applications (like Flask) to AWS Lambda:
```json { "dev": { "app_function": "app.app", "exclude": [ "boto3", "dateutil", "botocore", "s3transfer", "concurrent" ], "profile_name": null, "project_name": "flask-test-app", "runtime": "python3.10", "s3_bucket": "zappa-31096o41b" },
"production": { "app_function": "app.app", "exclude": [ "boto3", "dateutil", "botocore", "s3transfer", "concurrent" ], "profile_name": null, "project_name": "flask-test-app", "runtime": "python3.10", "s3_bucket": "zappa-31096o41b" }
} ```
- Writing a sample Flask app:
```python import boto3 import joblib import os
Initialize the Flask app
app = Flask(name)
S3 client to download the model
s3 = boto3.client('s3')
Download the model from S3 when the app starts
s3.download_file('your-s3-bucket-name', 'model.pkl', '/tmp/model.pkl') model = joblib.load('/tmp/model.pkl')
@app.route('/predict', methods=['POST']) def predict(): # Get the data from the POST request data = request.get_json(force=True)
# Convert the data into a numpy array input_data = np.array(data['input']).reshape(1, -1) # Make a prediction using the model prediction = model.predict(input_data) # Return the prediction as a JSON response return jsonify({'prediction': int(prediction[0])})
if name == 'main': app.run(debug=True) ```
- Deploying this app to production (to AWS):
bash zappa deploy production
and later eventually updating it:
bash zappa update production
- We should get a URL like this:
https://xyz123.execute-api.us-east-1.amazonaws.com/production
which we can query:
curl -X POST -H "Content-Type: application/json" -d '{"input": [5.1, 3.5, 1.4, 0.2]}' https://xyz123.execute-api.us-east-1.amazonaws.com/production/predict
-
- Mar 2023
-
github.com github.com
-
before_action -> { doorkeeper_authorize! :public }, only: :index
-
- Dec 2022
-
-
Java8 stream 中利用 groupingBy 进行多字段分组求和
-
-
-
JDK中Lambda表达式的序列化与SerializedLambda的巧妙使用
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
lambda演算求值顺序的问题?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
normalization property、type safety与soundness有什么联系?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
如何评价abstracting abstract machine?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
CPS变换后怎么实现的call/cc,可以详细解释吗?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
C++编译器是怎么处理lambda的?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
为什么Java闭包不能通过返回值之外的方式向外传递值?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Lambda 表达式、Block、闭包与匿名函数之间有什么区别?和 λ 演算有什么关系?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
为什么javascript closure(闭包)要叫闭包?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Python 中的 lambda 和「真正的」lambda 有什么区别?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Lambda 表达式有何用处?如何使用?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Java 8的新特性lambda表达式是否比匿名内部类具有更好的可读性?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Java中MethodHandle的使用问题?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
程序语言中的类型系统怎么理解,它有哪些要素?如何由它演化出一门编程语言的?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Java 的invokedynamic指令在是如何使用的?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Java8引入Lambda表达式的利弊?
Tags
Annotators
URL
-
-
-
java 8 lambda 表达式 编译后为什么在这种情况下没有生成 invokedynamic指令?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
Java中普通lambda表达式和方法引用本质上有什么区别?
Tags
Annotators
URL
-
-
www.zhihu.com www.zhihu.com
-
如何解释 Lisp 中 call/cc 的概念?
Tags
Annotators
URL
-
- Nov 2022
-
news.ycombinator.com news.ycombinator.com
-
Hello, Unison author here.This is definitely an issue that is real, and is currently a problem, and that we will fix; probably by giving the function author an option to salt the hash of new definitions that have some semantic meaning beyond their implementations (appropriate for most application/business logic). No salt for definitions whose meanings are defined by their implementations (appropriate for most generic "library" functions like `List.map`).We already make this distinction for data types, but not yet for value/function definitions.
.
-
- Oct 2021
-
twitter.com twitter.com
-
Pablo Tsukayama on Twitter. (n.d.). Twitter. Retrieved 4 October 2021, from https://twitter.com/pablotsukayama/status/1435725621599027202
-
- Jul 2021
-
-
Mandavilli, Apoorva. “J.&J. Vaccine May Be Less Effective Against Delta, Study Suggests.” The New York Times, July 20, 2021, sec. Health. https://www.nytimes.com/2021/07/20/health/coronavirus-johnson-vaccine-delta.html.
-
-
www.biorxiv.org www.biorxiv.org
-
Tada, Takuya, Hao Zhou, Marie I. Samanovic, Belinda M. Dcosta, Amber Cornelius, Mark J. Mulligan, and Nathaniel R. Landau. “Comparison of Neutralizing Antibody Titers Elicited by MRNA and Adenoviral Vector Vaccine against SARS-CoV-2 Variants.” BioRxiv, July 19, 2021, 2021.07.19.452771. https://doi.org/10.1101/2021.07.19.452771.
-
- Jun 2021
- May 2021
-
stackabuse.com stackabuse.com
-
you want to pass a function as an argument to higher-order functions
Functional programming - passing functions as arguments, as opposed to data objects.
-
Lambda functions are used when you need a function for a short period of time.
saves time writing (& maintaining, unit testing) private utility functions.
-
- Apr 2021
-
github.com github.com
-
# authenticated :user, lambda {|u| u.role == "admin"} do # root to: "admin/dashboard#show", as: :user_root # end
-
-
guides.rubyonrails.org guides.rubyonrails.org
-
You can also specify constraints as a lambda:
-
- Mar 2021
-
bugs.ruby-lang.org bugs.ruby-lang.org
-
Would it be desirable to specify the new object in a block? That would make it somewhat symmetrical to how Hash.new takes a block as a default value.
-
- May 2020
-
docs.aws.amazon.com docs.aws.amazon.com
-
When CloudFront receives a request, you can use a Lambda function to generate an HTTP response that CloudFront returns directly to the viewer without forwarding the response to the origin. Generating HTTP responses reduces the load on the origin, and typically also reduces latency for the viewer.
can be helpful when auth
-
-
aws.amazon.com aws.amazon.com
-
For this setup, do the following: 1. Create a custom AWS Identity and Access Management (IAM) policy and execution role for your Lambda function. 2. Create Lambda functions that stop and start your EC2 instances. 3. Create CloudWatch Events rules that trigger your function on a schedule. For example, you could create a rule to stop your EC2 instances at night, and another to start them again in the morning.
-
- Apr 2020
-
content.aws.training content.aws.training
-
Lambda authorizers–A Lambda authorizer is simply a Lambda function that you can write to perform any custom authorization that you need. There are two types of Lambda authorizers: token and request parameter. When a client calls your API, API Gateway verifies whether a Lambda authorizer is configured for the API method. If it is, API Gateway calls the Lambda function.In this call, API Gateway supplies the authorization token (or the request parameters, based on the type of authorizer), and the Lambda function returns a policy that allows or denies the caller’s request.API Gateway also supports an optional policy cache that you can configure for your Lambda authorizer. This feature increases performance by reducing the number of invocations of your Lambda authorizer for previously authorized tokens. And with this cache, you can configure a custom time to live (TTL).To make it easy to get started with this method, you can choose the API Gateway Lambda authorizer blueprint when creating your authorizer function from the Lambda console.
-
-
www.w3schools.com www.w3schools.com
-
Why Use Lambda Functions? The power of lambda is better shown when you use them as an anonymous function inside another function. Say you have a function definition that takes one argument, and that argument will be multiplied with an unknown number: def myfunc(n): return lambda a : a * n Use that function definition to make a function that always doubles the number you send in:
Tags
Annotators
URL
-
- Aug 2019
-
-
We can definitely observe that (memory) size matters! More memory dramatically reduces duration.
768-1024MB is optimal, and has diminishing returns
-
- Mar 2019
-
arxiv.org arxiv.org
-
A Tutorial Introduction to the Lambda Calculus
-
- Nov 2018
-
www.apsense.com www.apsense.com
-
Develop a Lightweight Project With the Help of AWS, Lambda and Serverless
Develop a Lightweight Project With the Help of AWS, Lambda and Serverless
-
- Nov 2017
-
docs.aws.amazon.com docs.aws.amazon.com
-
Lambda@Edge lets you run Lambda functions at AWS Regions and Amazon CloudFront edge locations in response to CloudFront events
Extremely happy to see such an amazing opportunity which I think will help create fined grain API's which are fast and can leverage Caching strategies which will be cheap.
-
- Oct 2017
-
docs.aws.amazon.com docs.aws.amazon.com
- Mar 2017
-
msdn.microsoft.com msdn.microsoft.com
-
The lambda expression matches the delegate it is assigned to, so it defines a method that takes one parameter of type Base and that has no return value.
Where's the Delegate the lambda expression supposedly matches
-
- Feb 2017
-
stackoverflow.com stackoverflow.com
-
private void setup(string someData) { Object.assignHandler((sender) => evHandler(sender,someData)); } public void evHandler(Object sender, string someData) { // need someData here!!! }
lambda is useful
-