'str' object has no attribute 'items'

yjay 256 Reputation points
2021-02-15T04:21:12.313+00:00

I'm trying to consume a model that I deployed from Azure Machine Learning as a web service but I keep getting an error: 'str' object has no attribute 'items' Help: https://go.microsoft.com/fwlink/?linkid=2146748.

I am following the Consume an Azure Machine Learning model deployed as a web service tutorial for calling the service using python.

I followed the formatting as shown in the documentation but I still keep getting this error.

import requests  
import json  
  
# URL for the web service  
scoring_uri = 'http://00.00.00.00:00/api/v1/service/test/score'  
# If the service is authenticated, set the key or token  
  
  
# Two sets of data to score, so we get two results back  
data = {"data":  
        [  
            {'volume': 0.23,   
             'temp': 0.66, }  
        ]  
        }  
# Convert to JSON string  
input_data = json.dumps(data)  
print(input_data)  
  
# Set the content type  
headers = {'Content-Type': 'application/json'}  
  
  
# Make the request and display the response  
resp = requests.post(scoring_uri, input_data, headers=headers)  
print(resp.text)  

Any ideas would be great, thanks!

UPDATE:
Scoring script:

import os  
import json  
  
from azureml.studio.core.io.model_directory import ModelDirectory  
from pathlib import Path  
from azureml.studio.modules.ml.score.score_generic_module.score_generic_module import ScoreModelModule  
from azureml.designer.serving.dagengine.converter import create_dfd_from_dict  
from collections import defaultdict  
from azureml.designer.serving.dagengine.utils import decode_nan  
from azureml.studio.common.datatable.data_table import DataTable  
  
  
model_path = os.path.join(os.getenv('AZUREML_MODEL_DIR'), 'trained_model_outputs')  
schema_file_path = Path(model_path) / '_schema.json'  
with open(schema_file_path) as fp:  
    schema_data = json.load(fp)  
  
  
def init():  
    global model  
    model = ModelDirectory.load(model_path).model  
  
  
def run(data):  
    data = json.loads(data)  
    input_entry = defaultdict(list)  
    for row in data:  
        for key, val in row.items():  
            input_entry[key].append(decode_nan(val))  
  
    data_frame_directory = create_dfd_from_dict(input_entry, schema_data)  
    score_module = ScoreModelModule()  
    result, = score_module.run(  
        learner=model,  
        test_data=DataTable.from_dfd(data_frame_directory),  
        append_or_result_only=True)  
    return json.dumps({"result": result.data_frame.values.tolist()})  
Azure Machine Learning
Azure Machine Learning
An Azure machine learning service for building and deploying models.
2,583 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
6,962 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ramr-msft 17,616 Reputation points
    2021-02-24T10:51:25.007+00:00

    @yjay Thanks, Here is the example, for the Multiclass Classification - Letter Recognition sample, the inference results are as follows:

    71583-screenshot-211.png

    0 comments No comments