Skip to content

Latest commit

 

History

History
187 lines (143 loc) · 9.62 KB

README.md

File metadata and controls

187 lines (143 loc) · 9.62 KB

PetSpotR

如果你需要中文请访问 中文版本

🛠️ Here for a workshop? Go to the workshop folder to get started! 🛠️


PetSpotR allows you to use advanced AI models to report and find lost pets. It is a sample application that uses Azure Machine Learning to train a model to detect pets in images.

It also leverages popular open-source projects such as Dapr and Keda to provide a scalable and resilient architecture.

Logo

Featured technologies

Architecture

Note: This application is a demo app which is not intended to be used in production. It is intended to demonstrate how to use Azure Machine Learning and other Azure services to build a scalable and resilient application. Use at your own risk.

Local architecture

local architecture

Cloud architecture

Moving from local to cloud is as simple as swapping out the local Dapr components with their cloud equivalents:

cloud architecture

Microservices

PetSpotR is a microservices application that uses Azure Machine Learning to train a model to detect pets in images. It also uses Azure Blob Storage to store images and Azure Cosmos DB to store metadata, leveraging Dapr bindings and state management to abstract away the underlying infrastructure.

The frontend is a .NET Blazor application that allows users to upload images and view the results. The backend is a Python Flask application that uses Azure Machine Learning to train and score the model.

AI model training

The application uses Azure Machine Learning to train a model to detect pets in images. The model is sourced via Hugging Face, which is a community of AI researchers and developers. The model is trained using the Azure Machine Learning Compute Instance, which is a managed compute environment for data scientists and AI developers.

State management

The application uses the Dapr state management API to abstract away the underlying infrastructure. When the application is running locally, the state is stored in a lightweight Redis container. When the application is running in the cloud, the state is stored in Azure Cosmos DB.

Publish & subscribe messaging

The application uses the Dapr pub/sub API to abstract away the underlying infrastructure. When the application is running locally, the messages are sent via a lightweight Redis container. When the application is running in the cloud, the messages are sent via Azure Service Bus.

Bindings

The application uses the Dapr bindings API to abstract away the underlying infrastructure. When the application is running locally, the images are stored in the local filesystem. When the application is running in the cloud, the bindings are stored in Azure Blob Storage.

Scaling

The application scales using KEDA, which allows you to scale based on the number of messages in a queue. The application uses Azure Service Bus to queue messages for the backend to process, leveraging Dapr pub/sub to abstract away the underlying infrastructure.

Load testing

Azure Load Testing is used to simulate a large number of users uploading images to the application, which allows us to test the application's scalability.

Running PetSpotR

Prerequisites

Run in a Codespace

You can run this application in a GitHub Codespace. This is useful for development and testing. The ML training and scoring will run within an Azure Machine Learning workspace in your Azure subscription.

  1. Click the Code button and select Open with Codespaces
  2. Wait for the Codespace to be created
  3. Hit 'F5' or open the Run and Debug tab and select ✅ Debug with Dapr

Run locally

The services in this application can be run locally using the Dapr CLI. This is useful for development and testing. The ML training and scoring will run within an Azure Machine Learning workspace in your Azure subscription.

  1. Install the Dapr CLI
  2. Initialize Dapr
    dapr init
  3. Configure your Dapr images component for Windows or Mac
    1. Open ./iac/dapr/local/images.yaml
    2. Uncomment the appropriate section for your OS, and comment out the other section
  4. Deploy the required Azure resources
    az deployment group create --resource-group myrg --template-file ./iac/infra.bicep --parameters mode=dev
  5. Run the backend
    cd src/backend
    dapr run --app-id backend --app-port 6002 --components-path ../../iac/dapr/local -- python app.py
  6. Run the frontend
    cd src/frontend/PetSpotR
    dapr run --app-id frontend --app-port 5114 --components-path ../../../iac/dapr/local -- dotnet watch
  7. Navigate to http://localhost:5114

Deploy to Azure with the CLI

  1. Ensure you have access to an Azure subscription and the Azure CLI installed
    az login
    az account set --subscription "My Subscription"
  2. Clone this repository
    git clone https://github.com/azure-samples/petspotr.git
    cd petspotr
  3. Deploy the infrastructure
    az deployment group deployment create --resource-group myrg --template-file ./iac/infra.json
  4. Deploy the configuration
    az deployment group deployment create --resource-group myrg --template-file ./iac/config.json
  5. Get AKS credentials
    az aks get-credentials --resource-group myrg --name petspotr
  6. Install Helm Charts
    helm repo add dapr https://dapr.github.io/helm-charts/
    helm repo add kedacore https://kedacore.github.io/charts
    helm repo update
    helm upgrade dapr dapr/dapr --install --version=1.10 --namespace dapr-system --create-namespace --wait
    helm upgrade keda kedacore/keda --install --version=2.9.4 --namespace keda --create-namespace --wait
  7. Log into Azure Container Registry You can get your registry name from your resource group in the Azure Portal
    az acr login --name myacr
  8. Build and push containers
    docker build -t myacr.azurecr.io/backend:latest ./src/backend
    docker build -t myacr.azurecr.io/frontend:latest ./src/frontend
    docker push myacr.azurecr.io/petspotr:latest
    docker push myacr.azurecr.io/frontend:latest
  9. Deploy the application
    az deployment group deployment create --resource-group myrg --template-file ./iac/app.json
  10. Get your frontend URL
kubectl get svc
  1. Navigate to your frontend URL

Testing

We have added some Playwright tests in order to test the functionality of this website. There are tests for the website which include:

  • Does the webpage load?
  • Is there a correct title on the page?
  • Does the submit button on the home page redirect to the /submit page?
  • Does the submit form correctly fill out with a photo being uploaded?

You can run these directly in your Codespace, and all the test files are under "tests".