Skip to content

pratikkalein/deploy-tf-cloud-run

Repository files navigation

MNIST Digit App

An end to end project which shows how to deploy a Tensorflow model on Google Cloud
View Demo »

About The Project

Screenshot This project demonstrates how to train a ML model with TensorFlow and deploy the model to Google Cloud Run with the help of Cloud Build and Docker.

Built With

Python Flask Flask Flask

Getting Started

Set up the project locally.

Prerequisites

  1. Python
  2. Pip
  3. Google Cloud SDK

Installation

  1. Clone the repo

    git clone https://github.com/pratikkalein/deploy-tf-cloud-run.git
  2. Create and activate virtual environment

    python3 -m venv venv
    source venv/bin/activate
  3. Install requirements.txt

    pip install -r requirements.txt

Train the model

  1. Open and run the 01-train.py file

    python3 01-train.py

    You can try playing with the batch size and epochs. Once the training is done a .keras file will be saved into the root directory.

  2. Run the 02-load.py file to load and test the output of the model.

    python3 02-load.py

Deploying the Flask app

  1. Change your current working directory to deploy

    cd deploy
  2. Make sure you are authenticated with gcloud CLI

    gcloud auth login

    Your default browser will open once you run this command. Choose your Google account.

  3. Observe the 03-main.py file to understand how the flask API is working. Start the build using gcloud CLI.

    gcloud builds submit --tag gcr.io/<project_id>/<function_name>

    It usually takes 3-4 mins to build.

  4. Once the build is done deploy to cloud run.

    gcloud run deploy --image gcr.io/<project_id>/<function_name> --platform managed

    It usually takes 3-4 mins to deploy.

  5. Go to Google Cloud Console and open Cloud run. You can find the URL endpoint.

Testing

  1. Open the 04-st-app.py and add the URL you got from Cloud run and paste it at the location mentioned in the file.
  2. Run the file.
    streamlit run 04-st-app.py
  3. Upload the image and test the prediction output.

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

Pratik Kale

Twitter - @pratikkalein LinkedIn- /in/pratikkalein

ppvkale@gmail.com

Project Link: https://github.com/pratikkalein/deploy-tf-cloud-run Demo Link : http://pratik.tech/tf-run-demo