|
import sys |
|
import json |
|
import requests |
|
|
|
class DataRobotPredictionError(Exception): |
|
"""Raised if there are issues getting predictions from DataRobot""" |
|
|
|
|
|
def make_datarobot_deployment_predictions(data, deployment_id): |
|
""" |
|
Make predictions on data provided using DataRobot deployment_id provided. |
|
See docs for details: |
|
https://app.datarobot.com/docs-jp/predictions/api/dr-predapi.html |
|
|
|
Parameters |
|
---------- |
|
data : str |
|
If using CSV as input: |
|
Feature1,Feature2 |
|
numeric_value,string |
|
|
|
Or if using JSON as input: |
|
[{"Feature1":numeric_value,"Feature2":"string"}] |
|
|
|
deployment_id : str |
|
The ID of the deployment to make predictions with. |
|
|
|
Returns |
|
------- |
|
Response schema: |
|
https://app.datarobot.com/docs-jp/predictions/api/dr-predapi.html#response-schema |
|
|
|
Raises |
|
------ |
|
DataRobotPredictionError if there are issues getting predictions from DataRobot |
|
""" |
|
|
|
headers = { |
|
|
|
|
|
|
|
'Content-Type': 'text/plain; charset=UTF-8', |
|
|
|
|
|
'Authorization': 'Bearer {}'.format('NjQwMDVmNGI0ZDQzZDFhYzI2YThmZDJiOnVZejljTXFNTXNoUnlKMStoUFhXSFdYMEZRck9lY3dobnEvRFZ1aVBHbVE9'), |
|
'DataRobot-Key': '84f96e49-d400-ec9c-92fc-30fc6e9329d1', |
|
} |
|
API_URL = 'https://jppdemo.orm.datarobot.com/predApi/v1.0/deployments/{deployment_id}/predictions' |
|
url = API_URL.format(deployment_id=deployment_id) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
params = { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
predictions_response = requests.post( |
|
url, |
|
data=data, |
|
headers=headers, |
|
|
|
|
|
|
|
) |
|
_raise_dataroboterror_for_status(predictions_response) |
|
|
|
return predictions_response.json() |
|
|
|
|
|
def _raise_dataroboterror_for_status(response): |
|
"""Raise DataRobotPredictionError if the request fails along with the response returned""" |
|
try: |
|
response.raise_for_status() |
|
except requests.exceptions.HTTPError: |
|
err_msg = '{code} Error: {msg}'.format( |
|
code=response.status_code, msg=response.text) |
|
raise DataRobotPredictionError(err_msg) |
|
|
|
|
|
def main(filename, deployment_id): |
|
""" |
|
Return an exit code on script completion or error. Codes > 0 are errors to the shell. |
|
Also useful as a usage demonstration of |
|
`make_datarobot_deployment_predictions(data, deployment_id)` |
|
""" |
|
MAX_PREDICTION_FILE_SIZE_BYTES = 52428800 |
|
if not filename: |
|
print( |
|
'Input file is required argument. ' |
|
'Usage: python datarobot-predict.py <input-file.csv>') |
|
return 1 |
|
data = open(filename, 'rb').read() |
|
data_size = sys.getsizeof(data) |
|
if data_size >= MAX_PREDICTION_FILE_SIZE_BYTES: |
|
print(( |
|
'Input file is too large: {} bytes. ' |
|
'Max allowed size is: {} bytes.' |
|
).format(data_size, MAX_PREDICTION_FILE_SIZE_BYTES)) |
|
return 1 |
|
try: |
|
predictions = make_datarobot_deployment_predictions(data, deployment_id) |
|
except DataRobotPredictionError as exc: |
|
print(exc) |
|
return 1 |
|
return predictions |
|
|
|
|
|
|
|
|