Skip to content

Azure sample application that analyzes meme sentiment

License

Notifications You must be signed in to change notification settings

neorusse/memealyzer

 
 

Repository files navigation

Memealyzer

Meme Analyzer = Memealyzer

The repo demonstrates Azure SDK usage via a complete application. This application takes in an image, uploads it to Blog Storage and enqueues a message into a Queue. A process receives that message and uses Form Recognizer to extract the text from the image, then uses Text Analytics to get the sentiment of the text, and then stores the results in Cosmos DB or Azure Table Storage.

If the text in the image is positive then the border color will change to green, if neutral then black, and if negative it will change to red.

Download the Azure SDK Releases

.NET Architecture

This is the current .NET architecture - we are in the process of developing for other languages and architectures as well.

Pre-reqs

The following are required to run this application.

  1. A Terminal - WSL, GitBash, PowerShell. The Terraform deployment will not work with Windows Command Prompt because it uses some shell scripts to fill Terraform gaps. You will need to run all the commands below in your selected terminal.
  2. Install Azure CLI
  3. Install Terraform
  4. Install Git
  5. Install VS Code
  6. Install Docker
  7. Azure Subscription

Azure Resources

The following Azure resources will be deployed with the Terraform script.

  1. Resource Group
  2. Storage Account
  3. Cognitive Services Form Recognizer
  4. Cognitive Services Text Analytics
  5. Cosmos DB
  6. Key Vault
  7. Azure Kubernetes Service
  8. Application Insights
  9. Azure SignalR Service
  10. Azure Functions

Code Setup

  1. Open Git Bash or WSL - The same terminal you used to install the pre-reqs above.
  2. Clone Repo git clone https://github.com/jongio/memealyzer

Azure Setup

  1. Azure CLI Login az login

  2. Select Azure Subscription - If you have more than one subscription, make sure you have the right one selected. az account set -s {SUBSCRIPTION_NAME}

  3. Set Terraform Variables

    1. Open .env file in the root of this project. Set the TF_VAR_basename setting to something unique
  4. Create Azure Resources with Terraform

    1. CD to iac/terraform

    2. Terraform init: terraform init

    3. Terraform plan:

      This will create a new Terraform workspace, activate it, and create a new plan file called tf.plan.

      1. For local dev: ./plan.sh
      2. For staging: ./plan.sh staging
      3. For prod: ./plan.sh prod

      You can pass any value as the first parameter. You just need a matching .env.{workspace} file in the root of the repo.

    4. Terraform apply: ./apply.sh - This will deploy the above resources to Azure. It will use the tf.plan that was generated from the previous step.

    If you get this error: Error validating token: IDX10223, then run az logout, az login, and then run ./plan.sh and ./apply.sh again.

  5. Update .env file

    1. Copy and paste the Terraform output values to the .env file in the root of this repo.

      NOTE: .env files do not allow spaces around the =, so please remove any spaces after you copy and paste.

Permissions Setup

This app uses the Azure CLI login to connect to Azure resources for local development. You need to run the following script to assign the appropriate roles to the Azure CLI user.

  1. CD to iac/terraform
  2. Run ./azcliuserperms.sh {basename}

    You need to replace {basename} with the basename you set in your TF_VAR_basename setting used above, such as memealyzerdev or memealyzerprod.

.NET Local Machine Setup

If you are running the .NET versino of this project locally, then you will need to install the following tools.

  1. .NET Core SDK - v3.1.402 minimum
  2. Azure Functions Core Tools - v3.0.2881 minimum

Configuration

Data Provider

You can configure which store the app uses to store your image metadata, either Cosmos DB or Azure Table Storage.

  1. Open .env file
  2. Find the AZURE_STORAGE_TYPE setting
  3. Set it to one of the following values:
    • COSMOS_SQL - This will instruct the app to use Cosmos DB.
    • STORAGE_TABLE - This will instruct the app to use Azure Storage Tables. As of 8/31/2020, this option uses a dev build of the Azure Tables client library, which will go to preview soon.

Border Style

You can configure the image border style with the Azure App Configuration service. It will default to solid, but you can change it to any valid CSS border style. You can either do this in the Azure Portal, or via the Azure CLI with this command:

az appconfig kv set -y -n {basename}appconfig --key borderStyle --value dashed

Replace {basename} with the basename you used when you created your Azure resources above.

After you change the setting, reload the WebApp to see the new style take effect.

Run Application

Local

Project Tye

  1. CD to /pac/net/tye/local and run ./run.sh dev
  2. Navigate to http://localhost:5000

Docker Compose

  1. CD to the pac/{lang}/docker/local folder for the language you would like to run, i.e. for .NET, cd to pac/net/docker/local.
  2. Open .env and set the following:
    1. API_ENDPOINT=http://localhost:2080
    2. FUNCTIONS_ENDPOINT=http://localhost:3080
  3. Run Docker Compose to start the API, WebApp, Service, and Azurite containers.
    • Run: ./run.sh
  4. Start Azure Function
    • Run ./func.sh
  5. Navigate to http://localhost:1080
  6. Add a Meme
    1. Enter meme url into the text box and click " ".
    2. Or to enter a random meme, just click " ".
    3. The image will be added to the grid. Wait for the service to pick it up. You will eventually see the text and the image border color will change indicating the image text sentiment.

Local Kubernetes

  1. In Docker Desktop settings, Enable Kubernetes and setup to use WSL 2 as backend. Docker Desktop WSL 2 backend
  2. CD to pac/net/kubectl/local.
  3. Run ./run.sh
  4. Navigate to http://localhost:31389

Azure Kubernetes Service (AKS)

  1. Terraform Workspace

    • It is recommended that you create another Azure deployment in a new Terraform workspace, so you can have a dev backend and a prod backend. i.e. iac/terraform/plan.sh prod, where prod is the name of the workspace and the name of the env file, i.e. env.prod.

    See the "Azure Setup" steps above for full Terraform deployment steps.

    The {basename} value is pulled from your .env file: TF_VAR_basename.

  2. AKS Credentials

    • Run the following command to get the AKS cluster credentials locally:

    az aks get-credentials --resource-group {basename}rg --name {basename}aks

    Replace {basename} with the basename you used when you created your Azure resources.

  3. Kubernetes Context

    • Make sure the K8S_CONTEXT setting in your .env file is set to the desired value, which will be {basename}aks.
  4. Nginx Ingress Controller Install

    1. Install Helm - This will be used for an nginx ingress controller that will expose a Public IP for our cluster and handle routing.
    2. Set your kubectl context with: kubectl config use-context {basename}aks
    3. Run ./scripts/nginx.sh to install the nginx-ingress controller to your AKS cluster.
  5. AKS Cluster IP Address

    • Run az network public-ip list -g memealyzerdevaksnodes --query '[0].ipAddress' --output tsv to find the AKS cluster's public IP address.
  6. Update .env File

    • Open ./.env and change. (Use ./.env.prod for production environment)
    1. FUNCTIONS_ENDPOINT to the URI of your functions endpoint, i.e. https://memealyzerdevfunction.azurewebsites.net - this was outputted by your Terraform run and can be found in your .env file.
  7. Deploy:

    You can choose to either deploy with kubectl or Project Tye.

    • With kubectl

      • CD to /pac/net/kubectl/aks
    • With Project Tye

      • CD to /pac/net/tye
    • Run ./deploy.sh {env}, where env is the name of the environment you want to deploy to, this will match your .env file in the project root.

    • This will build containers, push them to ACR, apply Kubernetes files, and deploy the Azure Function.

  8. Run

    • Open a browser and go to the AKS cluster's Public IP.

About

Azure sample application that analyzes meme sentiment

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 45.1%
  • HCL 21.0%
  • Shell 13.0%
  • HTML 10.1%
  • Python 4.4%
  • CSS 3.7%
  • Dockerfile 2.7%