Skip to content

Demostration project for the Specialization Building Cloud Computing Solutions at Scale

License

Notifications You must be signed in to change notification settings

matiaspakua/ml-demo-project

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python Flask ML Demo Project with CI/CD

1. CI Status
(build and test)
2. CD status
(docker build and push in DockerHub)
3. CD Status
(AWS EC2 / AZURE - docker pull and run)
ci-build-python-app cd-build-publish-docker-image run-docker-image or cd-pull-and-run-docker-image-azure

Project Overview

This is the demonstration project for the Course - Duke university: Specialization Building Cloud Computing Solutions at Scale by Noah Gift.

This project demonstrates how to develop a Flask web application for image classification using the MobileNetV2 pre-trained model from TensorFlow Hub. MobileNetV2 is a lightweight convolutional neural network (CNN) architecture, making it ideal for deploying on mobile devices and web applications.

Pre-Trained Model webpage

The project utilizes the TensorFlow Hub library to load the pre-trained MobileNetV2 model and integrate it with the Flask application. Users can upload images to the application, which are then processed by the MobileNetV2 model to predict the class of the image. The predicted class is displayed to the user along with the model's confidence score.

TensoyFlow Project

Hub of pre-trained models

Live App

Run Locally

If the previous links are down (maybe my free-tier is over), you can run the app locally after installing python and the project dependencies:

Step 1: clone the repository. Step 2. Install Python 3.11 Step 3. Install dependencies.

$ pip install -r requirements.txt

Step 4. Run the project:

$ python app.py

Project Structure

The project is structured as follows:

  1. github\workflows: This directory contains the yaml files for CI configuration and CD configuration.
  2. /templates: This directory contains the static HTML and CSS files for the web application's user interface.
  3. app.py: This file defines the Flask application and runs the web server.
  4. app_test.py: this file contains the unit test for each method (using pytest).>
  5. Dockerfile: file that create the image of the APP to be deployed in a environment where docker is available.
  6. requirements.txt : file that contains all the dependency needed by the project.

Project Deployment Workflow

The following diagram describe the high level architecture of the Application and the CI/CD process:

Step 01

The code for the app is stored in a GitHub repository, which allows for easy version control and collaboration. The app is automatically build and test in a github workflow.

Link to YML: python-app.yml

Step 02

Then a docker image is create and pushed in DockerHub public repository in another workflow.

Link to YML: docker-image.yml

Step 03

A third workflow is executed when the first two are executed successfully and the image is pulled and deployed in a AWS Ubuntu EC2 instance or AZURE Container Instance:

Link to YML: docker-run.yml AWS EC2

or

Link to YML: docker-run.yml AZURE

Step 04

Validate that the workflows works properly:

Step 05

The last step is wait until the image is pulled and running in the EC2 Ubuntu or in AZURE Container instance and access the public URL to log into the landing page of the app: (Depending of the Free Tier that I have at the moment I use one or another)

Project Setup and Deployment

Setup

  1. Install Python v3.11 (does not work with python 3.12)
  2. Install Docker
  3. Install Git and create a github account. Configure credential and SSH connection.
  4. Execute first time in the console:
 matias@ml-demo-project$ pip install -r requirements.txt 
  1. validate that all the dependencies listed in the "requirements.txt" are properly installed.

Example of Setup AWS EC2 instance

Launch new instance of EC2 with Ubuntu 20.04, using all the default parameters of the Free-Tier.

Ensure that you enable all the traffic (in and out) for testing purposes:

Install docker in Ubuntu 20.04 instance:

Follow the next instructions to install docker on the Ubuntu instance.

After the installation, verify that docker is properly configured and running:

Configure GitHub Action Runner for Workflow

After the EC2 instance is properly up and running with docker, is needed to configure the workflows and runners in github to communicate to EC2 to pull and run the builded image of the application from a public repository, in this case: DockerHub.

To do this, follow the instructions on the github actions section:

After installed the runner daemon in the EC2 Ubuntu instance, validate that is properly installed and running in background:

Verify is running:

Development

The development flow need to follow the best practices:

  1. Clean code
  2. Modularity
  3. Cohesion
  4. Testing
  5. Commit (small) to a version control.
  6. Verify CI
  7. Verify CD
  8. Integration / acceptance testing
  9. Documentation.
  10. Load Test

Unit Testing with Pytest

The project comes with a very basic unit test in the file: "app_test.py".

To execute the tests and view the report, prompt the following:

$ pytest.exe -vv --html=report.html app_test.py

This command will execute and generate a HTML report as follow:

The report can be opened with any browser:

And even, the test execution and result can be monitored in the GitHub Action Pipeline:

Acceptance Testing

For the validation of the current application exits a series of images in the folder: /images/test/ that can be used to "acceptance testing" as follow:

Test Case Test input Images Expected Restult
01 image.jpg solar_dish

0.7601
02 image1.jpg boathouse

0.2617
03 image2.jpg tabby

0.2365
04 image3.jpg sports_car

0.5183
05 image4.jpg dining_table

0.3249

Each result gives 3 matches from the highest to the lowest probability of similarity, like the following example:

Load Test Cases

For the load test, the tool: https://locust.io/ was used. There is a "task" defined for the "/" home endpoint and another task defined for the "/predict" endpoint:

To the execution, run the following command on the root of the project:

To-Do

  1. Publish the docker image in DockerHub as private.
  2. Deploy the private image in ubuntu using SSH Keys.
  3. Change deploy platform to Digital Ocean: https://m.do.co/c/97f4ed2d8b7a

About

Demostration project for the Specialization Building Cloud Computing Solutions at Scale

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published