Skip to content

Commit

Permalink
Add online prediction instructions and print friendly msg in console. (
Browse files Browse the repository at this point in the history
  • Loading branch information
hongye-sun authored Mar 5, 2019
1 parent 3227325 commit 079a1fa
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
16 changes: 14 additions & 2 deletions component_sdk/python/kfp_component/core/_display.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ def display(obj):
if '_repr_kfpmetadata_' in obj_dir:
display_kfpmetadata(obj)

logging.info(str(obj))

def display_markdown(obj):
"""Display markdown representation to KFP UI.
"""
Expand All @@ -63,7 +65,6 @@ def display_kfpmetadata(obj):
_output_ui_metadata(kfp_metadata)

def _output_ui_metadata(output):
logging.info('Dumping metadata: {}'.format(output))
with _OUTPUT_FILE_LOCK:
metadata = {}
if os.path.isfile(_OUTPUT_PATH):
Expand All @@ -85,6 +86,9 @@ def __init__(self, data):
def _repr_markdown_(self):
return self._data

def __repr__(self):
return self._data

class Tensorboard(object):
"""Class to hold tensorboard metadata.
"""
Expand All @@ -97,9 +101,17 @@ def _repr_kfpmetadata_(self):
'source': self._job_dir
}

def __repr__(self):
return 'Open Tensorboard at: {}'.format(self._job_dir)

class Link(Markdown):
"""Class to hold an markdown hyperlink data.
"""
def __init__(self, href, text):
super(Link, self).__init__(
'## [{}]({})'.format(text, href))
'## [{}]({})'.format(text, href))
self._href = href
self._text = text

def __repr__(self):
return '{}: {}'.format(self._text, self._href)
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def __init__(self, model_name, version,
replace_existing, wait_interval):
self._ml = MLEngineClient()
self._model_name = model_name
self._project_id, self._model_short_name = self._parse_model_name(model_name)
self._project_id, self._model_id = self._parse_model_name(model_name)
# The name of the version resource, which is in the format
# of projects/*/models/*/versions/*
self._version_name = None
Expand Down Expand Up @@ -164,9 +164,28 @@ def _handle_completed_version(self, version):
def _dump_metadata(self):
display.display(display.Link(
'https://console.cloud.google.com/mlengine/models/{}/versions/{}?project={}'.format(
self._model_short_name, self._version_id, self._project_id),
self._model_id, self._version_id, self._project_id),
'Version Details'
))
display.display(display.Markdown('''
## Online Prediction
### REST endpoint
The REST endpoint for online prediction is as follows:
```
POST https://ml.googleapis.com/v1/{}:predict
```
Try the REST endpoint in [Google OAuth 2.0 Playgound](https://developers.google.com/oauthplayground/#step3\
&apisSelect=https://www.googleapis.com/auth/cloud-platform&postData={{"instances":[]}}\
&url=https://ml.googleapis.com/v1/{}:predict&content_type=application/json&http_method=POST).
### GCloud command
```bash
gcloud ml-engine predict --model {} \
--version {} \
--json-instances instances.json
```
'''.format(self._version_name, self._version_name, self._model_id, self._version_id)))

def _dump_version(self, version):
logging.info('Dumping version: {}'.format(version))
Expand Down
7 changes: 7 additions & 0 deletions component_sdk/python/tests/core/test__display.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,10 @@ def test_display_link(self, mock_open, mock_os, mock_json):
'storage': 'inline'
}]
}, mock.ANY)

def test___repr__(self, mock_open, mock_os, mock_json):
self.assertEqual('# Title', str(display.Markdown('# Title')))
self.assertEqual('Open Tensorboard at: gs://trained/model/',
str(display.Tensorboard('gs://trained/model/')))
self.assertEqual('title: https://test/uri',
str(display.Link('https://test/uri', 'title')))

0 comments on commit 079a1fa

Please sign in to comment.