Data Science, ML and Analytics Engineering

Model deployment with Google Cloud Functions

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.

Price table:

Сalls per monthPrice/mln.
First 2 millionFree
Over 2 million$0.40
Prices for accessing Google Cloud Functions

Calculation time

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.

Usage example

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:

model confusion matrix
model confusion matrix

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

  1. Environment select 2nd gen.
  2. We give the name of our function, for example: model1.
  3. In Authentication, select Allow unauthenticated invocations.
  4. We click on Next.
Setting up Google Cloud Functions
Setting up Google Cloud Functions

Then, to use the Python libraries, we need to register 8080 PORT in the tab Runtime, build, connections and security settings → BUILD

Runtime, build, connections and security settings
Runtime, build, connections and security settings

Next, you have two options to write the code for your Google Cloud Function:

  1. In the Code tab, you can select Inline editor. And there write the code of your function and specify it as an Entry Point.
  2. 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

Code Example:

def main(request):
    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.

Deploy Google Cloud Function
Deploy Google Cloud Function

In my case, the function uses vactext with a description of the vacancy, and the category of the vacancy is returned.

Call Google Cloud Function
Call Google Cloud Function


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.

Additional material

Share it

If you liked the article - subscribe to my channel in the telegram or you can support me Become a Patron!

Other entries in this category: