Skip to content

Instantly share code, notes, and snippets.

@SpicySyntax
Last active August 14, 2019 16:54
Show Gist options
  • Save SpicySyntax/ab2fed3babbf6d6f980b668352ca193d to your computer and use it in GitHub Desktop.
Save SpicySyntax/ab2fed3babbf6d6f980b668352ca193d to your computer and use it in GitHub Desktop.
Python ML AZ Func
import azure.functions as func
import json
from . import predict
from ..shared import logger
# Azure Function Main Method
async def main(classifyBatch: func.QueueMessage, res: func.Out[func.QueueMessage]) -> None:
await queue_classification_results(classifyBatch, res)
# Method to obtain and enqueue classification results
async def queue_classification_results(classifyBatch: func.QueueMessage, res: func.Out[func.QueueMessage]) -> None:
"""Queue Trigger Azure Function that obtains and enqueus classification results
Args:
classifyBatch: meta-data queue message for a batch images to be classified
res: resulting queue message with classification results
Returns:
void
"""
# Get Message Args
infer_batch_message = classifyBatch.get_json()
jwt = infer_batch_message.get('Jwt')
image_uris = infer_batch_message.get('BlobUris')
activity_id = infer_batch_message.get('ActivityId')
image_metas = infer_batch_message.get('ImageMetas')
image_ids = list(map(lambda _ : _['FileId'], image_metas))
user_id = infer_batch_message.get('UserId')
job_id = infer_batch_message.get('JobId')
cv_model = infer_batch_message.get('CvModel')
# Init Dependencies
log = logger.Logger(activity_id)
predictor = predict.Predictor(log)
log.log_info('Python Classificaion Queue Triggered')
# Initialize model
log.log_info("Initializing model...")
predictor.initialize()
# Prediction step
log.log_info('Classifying images: {}'.format(image_uris))
results = predictor.predict_uris(image_uris)
inference_results = []
for i, result in enumerate(results):
inference_result = {
"ImageId": image_ids[i],
"Results": [result],
}
inference_results.append(inference_result)
inference_results = {
"InferenceResults": inference_results,
"UserId": user_id,
"JobId": job_id,
"CvModel": cv_model
}
# Push Inference Results back onto Azure Message Queue
result_json = json.dumps(inference_results)
log.log_info('Obtained results: {}'.format(result_json))
log.dispatch_logs(jwt)
res.set(result_json)
FROM mcr.microsoft.com/azure-functions/python:2.0
COPY . /home/site/wwwroot
RUN cd /home/site/wwwroot && \
pip install -r requirements.txt
azure-functions==1.0.0b5
azure-functions-worker==1.0.0b10
grpcio==1.20.1
grpcio-tools==1.20.1
keras==2.2.4
keras-applications==1.0.7
numpy==1.15.4
pillow==5.4.1
protobuf==3.6.1
requests==2.20.1
tensorflow==1.12.0
azure-storage-blob==2.1.0
urllib3==1.21.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment