From 0afebb95d13b2c64097a29900ce06eb185648e97 Mon Sep 17 00:00:00 2001 From: Russell Jimmies Date: Fri, 10 Nov 2023 19:43:27 +0000 Subject: [PATCH] Issue #30: model_metadata.py to retrieve model metadata from deployed endpoints --- .env.template | 5 +++- model_metadata.py | 62 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++- 3 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 model_metadata.py diff --git a/.env.template b/.env.template index 9b4bc3f9..e59a8d43 100644 --- a/.env.template +++ b/.env.template @@ -1,4 +1,7 @@ NACHET_AZURE_STORAGE_CONNECTION_STRING= NACHET_MODEL_ENDPOINT_REST_URL= NACHET_MODEL_ENDPOINT_ACCESS_KEY= -NACHET_DATA= \ No newline at end of file +NACHET_DATA= +NACHET_SUBSCRIPTION_ID= +NACHET_RESOURCE_GROUP= +NACHET_WORKSPACE= diff --git a/model_metadata.py b/model_metadata.py new file mode 100644 index 00000000..6387aafb --- /dev/null +++ b/model_metadata.py @@ -0,0 +1,62 @@ +import os +import re +import json +import yaml +from dotenv import load_dotenv +from azure.ai.ml import MLClient, Input +from azure.ai.ml.entities import Model +from azure.ai.ml.constants import AssetTypes +from azure.identity import DefaultAzureCredential + +load_dotenv() + +NACHET_SUBSCRIPTION_ID = os.getenv("NACHET_SUBSCRIPTION_ID") +NACHET_RESOURCE_GROUP = os.getenv("NACHET_RESOURCE_GROUP") +NACHET_WORKSPACE = os.getenv("NACHET_WORKSPACE") + +def generate_model_metadata(): + """ + Retrieves deployed online_endpoints and generates metadata json + """ + + model_metadata = [] + model_json = {} + + ml_client = MLClient(DefaultAzureCredential(), NACHET_SUBSCRIPTION_ID, NACHET_RESOURCE_GROUP, NACHET_WORKSPACE) + + # Retrieve all endpoints containing "nachet" + endpoints = ml_client.online_endpoints.list() + nachet_endpoints = [endpoint for endpoint in endpoints if 'nachet' in endpoint.name.lower()] + + ep = nachet_endpoints[0] + + # Retrieve online_deployment + deployment = ml_client.online_deployments.get(endpoint_name=ep.name, name=list(ep.traffic.keys())[0]) + + # Retrieve deployment's model (from filePath) + model_filepath = deployment.model + pattern = re.compile(r"models/([^/]+)/versions/(\d+)") + match = pattern.search(model_filepath) + if match: + model_name = match.group(1) + model_version = match.group(2) + else: + print("No match found") + + # Retrieve the job object from model + model = ml_client.models.get(name=model_name, version=model_version) + job = ml_client.jobs.get(name=model.job_name) + + # Because json.dumps(job) returns an empty json, use the dump() method from the Job class to write contents of job object to YAML file + job.dump(dest='output.yaml') + + # Read the YAML file and convert it to a dictionary + with open('output.yaml', 'r') as file: + job_data = yaml.safe_load(file) + + return job_data + + +if __name__ == "__main__": + job_json = generate_model_metadata() + print(job_json["component"]) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 584dc092..db3853bb 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,4 +4,6 @@ azure-identity quart quart-cors python-dotenv -hypercorn \ No newline at end of file +hypercorn +azure-ai-ml +azure-identity