Artificial Intelligence and Machine Learning Discussions
Engage in AI and ML discussions. Collaborate on innovative solutions, explore SAP's AI innovations, and discuss use cases, challenges, and future possibilities.
cancel
Showing results for 
Search instead for 
Did you mean: 

Leonardo ML Functional API Python examples

Former Member
0 Kudos

Could anyone please provide example API usage with Python? Currently the "Generate Code" option displays examples in Javascript, Java, Swift, Curl, ABAP and SAPUI5. For example this answer describes using prodimgclassifier and I would like to know ways to call other APIs such as time-series prediction, language translation etc.
Thanks.

1 ACCEPTED SOLUTION

former_member492038
Participant
0 Kudos

The link you provided makes this out to be a little more complicated than it needs to be. At it's core with any REST service, you need the url you want to retrieve, the headers you'd like to send along with it and in the case of a POST the data you want to send.

Here's an example for the Time Series Forecast API you mentioned. Assuming you have the requests library installed (if not, install it) all you have to do is specify the url, create the headers and data as a dict, and allow the requests library to take care of the post action for you.

import requests
import json

headers = {
    'APIKey' : 'your API Key',
    'Accept' : 'application/json',
    'content-type' : 'multipart/form-data'
    }

data = {
    'options' : 'string'
    }

url = "https://sandbox.api.sap.com/ml/timeseriesforecast/inference_sync"

resp = requests.post(url, data=json.dumps(data), headers=headers)
print(r.text)

View solution in original post

3 REPLIES 3

former_member492038
Participant
0 Kudos

The link you provided makes this out to be a little more complicated than it needs to be. At it's core with any REST service, you need the url you want to retrieve, the headers you'd like to send along with it and in the case of a POST the data you want to send.

Here's an example for the Time Series Forecast API you mentioned. Assuming you have the requests library installed (if not, install it) all you have to do is specify the url, create the headers and data as a dict, and allow the requests library to take care of the post action for you.

import requests
import json

headers = {
    'APIKey' : 'your API Key',
    'Accept' : 'application/json',
    'content-type' : 'multipart/form-data'
    }

data = {
    'options' : 'string'
    }

url = "https://sandbox.api.sap.com/ml/timeseriesforecast/inference_sync"

resp = requests.post(url, data=json.dumps(data), headers=headers)
print(r.text)

0 Kudos

I have tried your snippet and modified it accordingly. PFB the snippet

import requests

import json

headers = { 'APIKey' : 'xyz', 'Accept' : 'application/json', 'content-type' : 'application/json' }

data = { 'options' : {"period":"12"},'texts':['1,2,3,4,5,6,7,8,9,10,11,12'] }

url = "https://sandbox.api.sap.com/ml/timeseriesforecast/inference_sync"

r = requests.post(url, data=json.dumps(data), headers=headers)

print(r.text)

Output:

{ "_id": "adb69b92-8f38-463c-aff4-54bae08a5637", "error": "Invalid request", "error_description": "This service requires at least 1 file or 1 text Please put your input into the \"files\" or \"text\" field of the POST request", "processed_time": "Thu, 19 Jul 2018 08:31:59 GMT", "request": { "files": null, "options": {}, "tenantName": "imgclassif-tech-user", "texts": [] }, "status": "FAILED", "status_code": 400, "tenantName": "imgclassif-tech-user" }

Please Help!!!!!

0 Kudos

Your request is being made with a `texts` parameter but the error message seems to suggest you should be sending `text`