How to deploy Machine Learning and Deep Learning Models in Google App Engine using Flask API

Anurag Bhatt
Analytics Vidhya
Published in
4 min readJan 28, 2020
Photo by JESHOOTS.COM on Unsplash

Note: In this article, I will only show deployment in Google App Engine of your Flask API to know how to create the project and enable billing in GCP you can go to this documentation.

Step 1: Go to the GCP console and go to any project or select an existing project you will see below like image. I have selected flaskapi name project.

Step 2: Now go to Navigation Bar and select Google App Engine Dashboard on Compute Section.

Step 3: Now, click on the Cloud Shell option there.

Step 4: Click on the Launch Editor option.

We will get the new tab which looks like the below image. You can see four projects are already their names 0, 1, 2, 3 I will upload new project.

Step 5: Now we will upload our compressed project folder which we have already tested on the localhost using flask API. Remember we have to create requirements.txt file in our project so app engine can install the packages.

Step 6: We will unzip the folder typing below command in Cloud Shell.

unzip <zip file name>

We can see our unzipped folder in the sidebar. Inside the folder, I had already created necessary files like requirements.txt, predictor.py for prediction and load.py for loading the models.

Step 7: Now we will create a new file app.yaml where our main.py fille is saved. This file helps us to set the configurations for our app engine like how much ram we need which version of python we should use etc.

Step 8: Now for basic deployment in the standard version of the google app engine, we will type the following command and saved it. To add more configuration you can go to google YAML documentation.

# How we launch our file
entrypoint: gunicorn -b :$PORT main:app
# Which runtime we will used i have used python 3.7
runtime: python37
# These setting are for Deep Learning Models
# cpu for our processor
# memory_gb for our ram
instance_class: F4
resources:
cpu: 2
memory_gb: 4

Note: You can comment the instance class, resources, CPU and memory_gb if you using machine learning models because the higher the settings more money you will burn.

Step 10: We will go to the console and type the command to select under which project we want to deploy our API.

# You can get your project id on your project dashboard.
gcloud config set project [PROJECT ID]

My project id is flaskapi-266011 yours can be different.

Step 11: Now from console we will get inside the folder and type the following command.

# app means our app.yaml file 
# --version [number]
gcloud app deploy --version 1

It will tell us to select the server I have selected us-central. And then it will show us the deployment processing like below image.

We will type Y and we can see our API in target url. And then we will just wait for our deployment process.

--

--