首页 > 解决方案 > 将批量数据发送到 Azure ML 终结点

问题描述

我有一个 Azure ML 端点,用于在我以 json 格式提供数据时获得评分。

import requests
import json

# URL for the web service
scoring_uri = 'http://107a119d-9c23-4792-b5db-065e9d3af1e6.eastus.azurecontainer.io/score'  

# If the service is authenticated, set the key or token
key = '##########################'


data = {"data":
           [{'Associate_Gender': 'Male', 'Associate_Age': 20, 'Education': 'Under Graduate', 'Source_Hiring': 'Internal Movement', 'Count_of_Incoming_Calls_6_month': None, 'Count_of_Incoming_Calls_6_month_bucket': 'Greater than equal to 0 and less than 4', 'Internal_Quality_IQ_Score_Last_6_Months': '93%', 'Internal_Quality_IQ_Score_Last_6_Months_Bucket': 'Greater than 75%', 'Associate_Tenure_Floor_Bucket': 'Greater than 0 and less than 90', 'Current_Call_Repeat_Yes_No': False, 'Historical_CSAT_score': 'Greater than equal to 7 and less than 9', 'Customer_Age': 54, 'Customer_Age_Bucket': 'Greater than equal to 46', 'Network_Region_Originating_Call': 'East London', 'Length_of_Relationship_with_Customer': 266, 'Length_of_Relationship_with_Customer_bucket': 'Greater than 90', 'Call_Reason_Type_L1': 'Voice', 'Call_Reason_Type_L2': 'Prepaid', 'Call_Reason_Type_L3': 'Request for Reversal Provisioning', 'Number_of_VAS_services_active': 6, 'Customer_Category': 'Mercury', 'Customer_monthly_ARPU_GBP_Bucket': 'More than 30', 'Customer_Location': 'Houslow'}]
                        }
# Convert to JSON string
input_data = json.dumps(data)

# Set the content type
headers = {'Content-Type': 'application/json'}
# If authentication is enabled, set the authorization header
headers['Authorization'] = f'Bearer {key}'

# Make the request and display the response
resp = requests.post(scoring_uri, input_data, headers=headers)
print(resp.text)  

如何批量发送文件中的输入数据并获取输出。或者发送大量数据在端点上进行评分是不可行的?
也欢迎任何有关在 azure 上评分的替代建议。

标签: pythonazuremachine-learningendpoint

解决方案


假设您有一个名为 的文件夹json_data,其中存储了所有 json 文件,然后您将打开这些文件并将它们发布到您的端点,如下所示:

import requests
import json
import os
import glob

your_uri = 'https://jsonplaceholder.typicode.com/'
folder_path = './json_data'

for filename in glob.glob(os.path.join(folder_path, '*.json')):
    with open(filename, 'r') as f:
        json_input_data = json.load(f)
        resp = requests.post(your_uri, json_input_data)
        print(resp)

要展示成功的 http 响应 201,jsonplaceholder.typicode.com您必须在 python 文件的同一目录中创建一个文件夹并将其命名json_data,然后在该文件夹中创建一些 json 文件并将一些数据粘贴到文件中,例如:

文件 1.json:

{
  "title": "some title name 1",
  "body": "some body content 1"
} 

文件2.json:

{
  "title": "some title name 2",
  "body": "some body content 2"
} 

等等

您可以轻松地重写它并使用您自己的 uri、密钥、标题等。


推荐阅读