Skip to Content
0

Leonardo ML Functional API Python examples

Sep 20, 2017 at 09:19 PM

267

avatar image
Former Member

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.

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
William Ayd Oct 06, 2017 at 07:11 PM
0

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)
Show 2 Share
10 |10000 characters needed characters left characters exceeded

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
Lukish Yadav

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

0