In this note, I will tell you how to deploy a model for free up to a certain level of use and not bother with writing a microservice. I note that such a solution is easily integrated, for example, into a web service. All you need is to use Google Cloud Functions.
Google Cloud Functions is a serverless approach, i.e. server services are provided without renting or purchasing equipment. With this approach, the provider manages infrastructure resources, configures and maintains them.
The main advantage of Google Cloud Functions is automatic scalability, high availability and fault tolerance.
Google Cloud Functions pricing depends on how long your function runs, how many times it is called, and how much resources you allocate to that function. If your function makes an outgoing network request, there is also an additional data transfer charge. But most importantly, Google Cloud Functions includes a lifetime free calling tier so you can experiment with the platform for free. From myself, I can say that for MVP and small projects, the free plan is more than enough.
Prices for accessing Google Cloud Functions
|Сalls per month||Price/mln.|
|First 2 million||Free|
|Over 2 million||$0.40|
Computation time is measured from the time your function receives a request to the time it completes, either through a completion signal, or through a timeout, another failure, or any other termination. Computation time is measured in 100 ms increments and rounded up to the nearest increment. For example, a function that runs for 260ms will be billed as 300ms.
More details can be found in the official documentation.
For one project, I had the task of determining its category by the text of a vacancy. For example: BACKEND, FRONTEND, MOBILE DEV, DESIGN, PRODUCT, DA/DS and others. CatBoostClassifier was trained as a model. Average ROC AUC Model 0.979
Confusion Matrix on validation:
This is the model we will deploy using Google Cloud Functions.
Setting up Google Cloud Functions
Go to google cloud console and click create function
- Environment select 2nd gen.
- We give the name of our function, for example: model1.
- In Authentication, select Allow unauthenticated invocations.
- We click on Next.
Then, to use the Python libraries, we need to register 8080 PORT in the tab Runtime, build, connections and security settings → BUILD
Next, you have two options to write the code for your Google Cloud Function:
- In the Code tab, you can select Inline editor. And there write the code of your function and specify it as an Entry Point.
- You can download the entire code in zip format. And specify the Entry Point as well.
I prefer the second option, it’s much easier and faster.
In the code, you should pay attention to 3 things:
- Use functions_framework library
- Use @functions_framework.http decorator for your function Entry Point
- It’s better not to specify library versions in requirements.txt
text = request.args.get('vactext')
corpus = remove_links_and_quotes(text)
corpus = ' '.join(re.split('\W+', corpus.lower()))
corpus = tokenize_me(corpus)
Then click Deploy.
If everything is in order, your service will be raised. You need to refer to it by URL.
In my case, the function uses vactext with a description of the vacancy, and the category of the vacancy is returned.
Google Cloud Function is a service that makes it easy to deploy simple functions. The entire feature deployment process can be completed in less than 1 hour, making it a good tool for MVP work or for a feature that doesn’t need many hits.