Created
December 7, 2017 02:53
-
-
Save nmwalsh/1c236b94fe54d3fa4c6a7cfc182cd4c6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PredictsResource(object): | |
def on_get(self, req, resp): | |
"""Handles GET requests""" | |
resp.status = falcon.HTTP_200 # This is the default status | |
resp.body = ('\nThis is the PREDICT endpoint. \n' | |
'Both requests and responses are served in JSON. \n' | |
'\n' | |
'INPUT: Flower Lengths (in cm) \n' | |
' "sepal_length":[num] \n' | |
' "sepal_width": [num] \n' | |
' "petal_length":[num] \n' | |
' "petal_width": [num] \n\n' | |
'OUTPUT: Prediction (Species) \n' | |
' "Species": [string] \n\n') | |
def on_post(self, req, resp): | |
"""Handles POST requests""" | |
try: | |
raw_json = req.stream.read() | |
except Exception as ex: | |
raise falcon.HTTPError(falcon.HTTP_400, | |
'Error', | |
ex.message) | |
try: | |
result_json = json.loads(raw_json.decode(), encoding='utf-8') | |
# For Python 2.x, replace with | |
# result_json = json.loads(raw_json, encoding='utf-8') | |
except ValueError: | |
raise falcon.HTTPError(falcon.HTTP_400, | |
'Malformed JSON', | |
'Could not decode the request body. The ' | |
'JSON was incorrect.') | |
resp.status = falcon.HTTP_200 | |
resp.body = json.dumps(invoke_predict(raw_json)) | |
# For Python 2.x, replace with | |
# resp.body = json.dumps(invoke_predict(raw_json), encoding='utf-8') encoding not necessary in python3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment