Skip to content

Commit

Permalink
Include example of marshalling of complex predictions
Browse files Browse the repository at this point in the history
We can encapsulate complex predictions inside our JSON response, this
change produces a simple example that illustrates it.
  • Loading branch information
alvarolopez committed Jun 8, 2022
1 parent 803962a commit d5b55f7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion deepaas/api/v2/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ async def post(self, request, wsk_args=None):
ret = open(ret.filename, 'rb')

accept = args.get("accept", "application/json")
if accept != "application/json":
if accept not in ["application/json", "*/*"]:
response = web.Response(
body=ret,
content_type=accept,
Expand Down
29 changes: 20 additions & 9 deletions deepaas/model/v2/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# License for the specific language governing permissions and limitations
# under the License.

import base64
import time

from oslo_log import log
Expand Down Expand Up @@ -45,6 +46,7 @@ class TestModel(base.BaseModel):
},
)
),
"data": fields.Str(),
}

_logo = (
Expand Down Expand Up @@ -82,15 +84,22 @@ def warm(self):

def predict(self, **kwargs):
LOG.debug("Got the following kw arguments: %s", kwargs)
d = {
"date": "2019-01-1",
"labels": [{"label": "foo", "probability": 1.0}]
}
if kwargs.get("accept") == "text/plain":
return str(d)
elif kwargs.get("accept") == "image/png":
if kwargs.get("accept") == "image/png":
return self._logo
return d

encoded = base64.b64encode(self._logo)
b64_str = str(encoded, 'utf-8')
b64_str = f"data:image/png;base64,{b64_str}"

if kwargs.get("accept") == "text/plain":
return b64_str
else:
d = {
"date": "2019-01-1",
"labels": [{"label": "foo", "probability": 1.0}],
"data": b64_str,
}
return d

def train(self, *args, **kwargs):
sleep = kwargs.get("sleep", 1)
Expand Down Expand Up @@ -120,7 +129,9 @@ def get_predict_args(self):
"accept": fields.Str(
description=("Media type(s) that is/are acceptable for the "
"response."),
validate=validate.OneOf(["text/plain", "image/png"]),
validate=validate.OneOf(["application/json",
"text/plain",
"image/png"]),
location="headers",
)
}
Expand Down

0 comments on commit d5b55f7

Please sign in to comment.