noelbundick / memealyzer

Azure sample application that analyzes meme sentiment

Home Page:https://aka.ms/azsdk

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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:
      1. For dev: ./plan.sh
      2. 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.

    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 Docker Compose

  1. CD to the src folder for the language you would like to run, i.e. for .NET, cd to src/net for Python, cd to src/python
  2. Run Docker Compose to start the API, WebApp, Service, and Azurite containers.
    1. Linux: ./run.sh
  3. Start Azure Function
    • Run ./func.sh
  4. Navigate to http://localhost:1080
  5. 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/k8s/local.
  3. Run ./run.sh
  4. Navigate to http://localhost:31389

Azure Kubernetes Service

  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.

    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. K8S 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. 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. API_ENDPOINT value to the Public IP address URI, i.e., https://52.250.2.137
    2. 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: CD to /pac/net/k8s/aks and run ./deploy.sh - this will build containers, push them to ACR, apply K8S files, and deploy the Azure Function.

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

About

Azure sample application that analyzes meme sentiment

https://aka.ms/azsdk

License:MIT License


Languages

Language:C# 46.0%Language:HCL 21.2%Language:HTML 10.6%Language:Shell 10.6%Language:Python 4.4%Language:CSS 3.7%Language:Dockerfile 3.5%