Run  Flask application on Colab

Run Flask application on Colab

Recently I created a Tensorlfow model, which is trained on GPU (on Google Colab).

Then I thought of deploying it using Flask web framework. I wrote the Flask application which load the model(.h5) file and predict the predictions.

But

When I run that on my personal computer / local machine (which does not have much RAM, and No TPU, No Cuda Drivers). Machine totally stucks, because it consumes almost > 90% of memory.

Then, I thought why can't I run Flask application on Colab as well (which provide free GPU)

In first instance, I wrote the same code (without any modification) and executed on Colab as below.

Screenshot from 2021-02-03 20-39-06.png

So, it started my application on port 5000 at localhost. But here localhost is the Virtual Machine Provided by Google, on which our Colab is running.

if we try to access localhost:5000 in browser - We will not able to access. Because that will find the application running on port 5000 on our local machine, not on Virtual machine.

So, we need to find a way by which we can expose the application which is running on VM to the outer world. I know we can do it using 'ngrok' using command ngrok http port_number

But for that, first we need to install 'ngrok' on VM and then need to execute above command


After taking help of our best friend 'Google'. I found package 'flask_ngrok' already created by someone, which will do everything for us i.e. install ngrok on VM and then expose our application to outer world.

Below is the link


To use that, we need to first install the package on google colab

!pip install flask_ngrok

and then need to add 2 lines in our same code. i.e import flask_ngrok

from flask_ngrok import run_with_ngrok run_with_ngrok(app)

Screenshot from 2021-02-03 20-54-43.png

Now it exposes our application to the outer world, and provide the tunnel link ended with ngrok.io

We can use above link (ended with ngrok.io) to view our application.

Now, I no need to worry about GPU/TPU/RAM. (until Google or Ngrok stop this)