question

yjay-4307 avatar image
0 Votes"
yjay-4307 asked ramr-msft answered

'str' object has no attribute 'items'

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-learningazure-webapps-content-deployment
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@yjay-4307 Thanks for the question. The data str object expecting list of items. The structure of the data needs to match what the scoring script and model in the service expect. The scoring script might modify the data before passing it to the model.

0 Votes 0 ·

@ramr-msft thanks so much for your answer, I have tried modifying my code to contain a list of items but am still getting the error. I also tried following sample.json that I found for the trained model but that didn't either work. I have added the scoring script for further reference. Thanks again.

0 Votes 0 ·

@yjay-4307 Thanks for the details. You can diagnose the web service using get_logs method.

webservice.get_logs()

Please follow the samples for deployment and consume the deployed webservice.


71440-screenshot-210.png

or
71486-screenshot-209.png


0 Votes 0 ·
screenshot-210.png (20.5 KiB)
screenshot-209.png (17.7 KiB)

1 Answer

ramr-msft avatar image
0 Votes"
ramr-msft answered

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

71583-screenshot-211.png



screenshot-211.png (107.7 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.