Seasons of Serverless, Lovely Ladoos

Seasons of Serverless

Challenge 2: Lovely Ladoos



Serverless Compute

Advantages of using Serverless


Azure CLI

Azure CLI Commands

az login
az account list-locations \
--query "[].{Region:name}" \
--out table
# To see all the available subscriptions
az account list
# To set the subscription
az account set --subscription <Subscription-ID or Subscription-Name>

Create Azure Resource Group

az group create --name <Your-Resource-Group-Name> --location eastus

Blob Storage

az storage account create \
--name <Blob Storage Name> \
--resource-group <Resource Group Name> \
--location eastus \
--sku Standard_ZRS \
--encryption-services blob

Create Container

az ad signed-in-user show --query objectId -o tsv | az role assignment create \
--role "Storage Blob Data Contributor" \
--assignee @- \
--scope "/subscriptions/<Subscription>/resourceGroups/<Resource Group Name>/providers/Microsoft.Storage/storageAccounts/<Blob Storage Name>"
az storage container create \
--account-name <Storage Account> \
--name <Container Name> \
--auth-mode login

Azure Functions

Creating Functions Project

# This will generate a folder containing two files host.json and local.settings.json
func init Seasons-of-Serverless-Solution-Lovely-Ladoos --worker-runtime node
cd Seasons-of-Serverless-Solution-Lovely-Ladoos

Creating Functions Template

# Here a pre-built template for the Http Trigger will be created
# with two files, function.json and index.js
# We are going to write our logics in the index.js
# To create blob endpoint
func new --template "Http Trigger" --name blobs
# To create prediction endpoint
func new --template "Http Trigger" --name predict # To create blob endpoint

Initialize your nodeJs project with npm

npm init

List all the blobs in a Blob Storage Container

const { BlobServiceClient, StorageSharedKeyCredential, newPipeline } = require('@azure/storage-blob');const sharedKeyCredential = new StorageSharedKeyCredential(
const pipeline = newPipeline(sharedKeyCredential);
const containerName = process.env.CONTAINER_NAME;
const blobServiceClient = new BlobServiceClient(`https://${process.env.AZURE_STORAGE_ACCOUNT_NAME}`, pipeline );const containerClient = blobServiceClient.getContainerClient(containerName);const listBlobsResponse = await containerClient.listBlobFlatSegment();

Uploading the image to the Blob Storage

const { BlobServiceClient } = require('@azure/storage-blob');
const streamifier = require("streamifier");
const containerName = process.env.CONTAINER_NAME;const blobServiceClient = BlobServiceClient.fromConnectionString(process.env.AzureWebJobsStorage);const containerClient = blobServiceClient.getContainerClient(containerName);;const blockBlobClient = containerClient.getBlockBlobClient(fileName);const result = await blockBlobClient.uploadStream(streamifier.createReadStream(Buffer.from(fileData)), fileData.length);

Predict the image with image URL

// Import Dependencies
const msRest = require("@azure/ms-rest-js");
const { PredictionAPIClient } = require("@azure/cognitiveservices-customvision-prediction");
// Get the env variables
const projectId = process.env.PROJECT_ID
const publishedName = process.env.PUBLISHED_NAME
const predictionKey = process.env.PREDICTION_KEY
const endPoint = process.env.PREDICTION_ENDPOINT
const predictionResourceId = process.env.PREDICTION_RESOURCE_ID
const predictor_credentials = new msRest.ApiKeyCredentials({ inHeader: { "Prediction-key": predictionKey } });const predictor = new PredictionAPIClient(predictor_credentials, endPoint);const results = await predictor.classifyImageUrl(projectId, publishedName, { url: imageUrl });

Custom Vision API

Create Custom Vision
Name: <Any Name>
Description: <Any Description>
Resources: <The Resource just created>
Project Types: Classification
Classification Types: Multiclass (Single tag per image)
Domains: Food

Run locally

func start

Check the working of API Endpoints

GET: /api/blobs


POST: /api/blobs


GET: /api/predict?imageurl=<imageUrl>


Chefs for the Challenge

About the Author

Aditya Raman

Back End Developer | Software Engineer | DevOps Engineer | Data Science | Microsoft Learn Student Ambassador | Mentor MLH | Full Stack Developer