azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

How to read form-data within Azure ML endpoint run function?

Open abhishekperambai opened this issue 1 year ago • 8 comments

I'm trying to send form-data from postman to the ML endpoint in the backend which uses the Azure python SDK v1. Below is the code that I'm using to read the request from postman in the run function in my entry /scoring script.

@rawhttp
def run(data):
    try:
        aml_request = AMLRequest(data)
        file_data = aml_request.get_data()
        file_type = aml_request.get_header("file_type")
        return {"input_structure":file_data.read(),
               "file_type":file_type}
    except Exception as e:
        error = {"error": str(e)}
        return json.dumps(error)

The above function is just a dummy script to see how the endpoint returns the input structure being sent to the it. Apparently below is the error that I get when I send the post request in POSTMAN.

**_

"{"error": "AMLRequest.init() takes 1 positional argument but 2 were given"}"

_**

Furthermore, if I directly read the request and print it without the AMLRequest class, it prints an empty string which according to me means that the data is not being passed from postman to the backend endpoint.

PFB the screenshot of the postman request

image

The above screenshot has the file upload and file_type key indicating the extension of the file.

Can someone help me on how to read the request in the run function when the request sent is form-data from postman?

abhishekperambai avatar Nov 21 '23 04:11 abhishekperambai

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Azure/azure-ml-sdk @azureml-github.

github-actions[bot] avatar Nov 27 '23 15:11 github-actions[bot]

Thanks for the feedback! We are routing this to the appropriate team for follow-up. cc @Azure/azure-ml-sdk @azureml-github.

github-actions[bot] avatar Nov 27 '23 15:11 github-actions[bot]

Hi @abhishekperambai can you try to send raw from postman. When I am using the form-data its showing the error. image

Ramya-ML18 avatar Feb 09 '24 11:02 Ramya-ML18

Hi @abhishekperambai. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

github-actions[bot] avatar Feb 21 '24 09:02 github-actions[bot]

Hi @abhishekperambai can you try to send raw from postman. When I am using the form-data its showing the error. image

Hi Ramya,

My requirement is to send it as an attachment from the front-end as this endpoint will be integrated with UI.

Is there a way to do this?

abhishekperambai avatar Feb 28 '24 03:02 abhishekperambai

Hi Abhishek As per rest api(post method) accepts raw data. You can read file from upload element and set data in input parameter in rest endpoint as implemented in below code.You can take the data from here image

import urllib.request import json import os import ssl

def allowSelfSignedHttps(allowed): # bypass the server certificate verification on client side if allowed and not os.environ.get('PYTHONHTTPSVERIFY', '') and getattr(ssl, '_create_unverified_context', None): ssl._create_default_https_context = ssl._create_unverified_context

allowSelfSignedHttps(True) # this line is needed if you use self-signed certificate in your scoring service.

data = { "Inputs": { "input1": [ { "CulmenLength": 39.1, "CulmenDepth": 18.7, "FlipperLength": 181, "BodyMass": 3750 }, { "CulmenLength": 49.1, "CulmenDepth": 14.8, "FlipperLength": 220, "BodyMass": 5150 }, { "CulmenLength": 46.6, "CulmenDepth": 17.8, "FlipperLength": 193, "BodyMass": 3800 } ] }, "GlobalParameters": {} }

body = str.encode(json.dumps(data))

url = 'http://1488d2a0-f4eb-4048-89ae-7678d453f6c1.eastus.azurecontainer.io/score'

Replace this with the primary/secondary key or AMLToken for the endpoint

api_key = '' if not api_key: raise Exception("A key should be provided to invoke the endpoint")

headers = {'Content-Type':'application/json', 'Authorization':('Bearer '+ api_key)}

req = urllib.request.Request(url, body, headers)

try: response = urllib.request.urlopen(req)

result = response.read()
print(result)

except urllib.error.HTTPError as error: print("The request failed with status code: " + str(error.code))

# Print the headers - they include the requert ID and the timestamp, which are useful for debugging the failure
print(error.info())
print(error.read().decode("utf8", 'ignore'))

Ramya-ML18 avatar Feb 28 '24 14:02 Ramya-ML18

Hi @abhishekperambai. Thank you for opening this issue and giving us the opportunity to assist. To help our team better understand your issue and the details of your scenario please provide a response to the question asked above or the information requested above. This will help us more accurately address your issue.

github-actions[bot] avatar Mar 01 '24 10:03 github-actions[bot]

Hi @abhishekperambai, we're sending this friendly reminder because we haven't heard back from you in 7 days. We need more information about this issue to help address it. Please be sure to give us your input. If we don't hear back from you within 14 days of this comment the issue will be automatically closed. Thank you!

github-actions[bot] avatar Mar 08 '24 15:03 github-actions[bot]

By mistake clicked reopen.

isaudagar avatar Aug 14 '24 10:08 isaudagar