|
| 1 | +# Copyright 2019 Google Inc. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import argparse |
| 16 | +from datetime import datetime |
| 17 | +import logging |
| 18 | +import retrying |
| 19 | + |
| 20 | +from kfmd import metadata |
| 21 | + |
| 22 | +DATASET = 'dataset' |
| 23 | +MODEL = 'model' |
| 24 | +METADATA_SERVICE = "metadata-service.kubeflow:8080" |
| 25 | + |
| 26 | + |
| 27 | +def get_or_create_workspace(ws_name): |
| 28 | + return metadata.Workspace( |
| 29 | + # Connect to metadata-service in namesapce kubeflow in the k8s cluster. |
| 30 | + backend_url_prefix=METADATA_SERVICE, |
| 31 | + name=ws_name, |
| 32 | + description="a workspace for the GitHub summarization task", |
| 33 | + labels={"n1": "v1"}) |
| 34 | + |
| 35 | +def get_or_create_workspace_run(md_workspace, run_name): |
| 36 | + return metadata.Run( |
| 37 | + workspace=md_workspace, |
| 38 | + name=run_name, |
| 39 | + description="Metadata run for workflow %s" % run_name, |
| 40 | + ) |
| 41 | + |
| 42 | +@retrying.retry(stop_max_delay=180000) |
| 43 | +def log_model_info(ws, ws_run, model_uri): |
| 44 | + exec2 = metadata.Execution( |
| 45 | + name="execution" + datetime.utcnow().isoformat("T"), |
| 46 | + workspace=ws, |
| 47 | + run=ws_run, |
| 48 | + description="train action", |
| 49 | + ) |
| 50 | + _ = exec2.log_input( |
| 51 | + metadata.Model( |
| 52 | + description="t2t model", |
| 53 | + name="t2t-model", |
| 54 | + |
| 55 | + uri=model_uri, |
| 56 | + version="v1.0.0" |
| 57 | + )) |
| 58 | + |
| 59 | +@retrying.retry(stop_max_delay=180000) |
| 60 | +def log_dataset_info(ws, ws_run, data_uri): |
| 61 | + exec1 = metadata.Execution( |
| 62 | + name="execution" + datetime.utcnow().isoformat("T"), |
| 63 | + workspace=ws, |
| 64 | + run=ws_run, |
| 65 | + description="copy action", |
| 66 | + ) |
| 67 | + _ = exec1.log_input( |
| 68 | + metadata.DataSet( |
| 69 | + description="gh summarization data", |
| 70 | + name="gh-summ-data", |
| 71 | + |
| 72 | + uri=data_uri, |
| 73 | + version="v1.0.0" |
| 74 | + )) |
| 75 | + |
| 76 | + |
| 77 | +def main(): |
| 78 | + parser = argparse.ArgumentParser(description='Serving webapp') |
| 79 | + parser.add_argument( |
| 80 | + '--log-type', |
| 81 | + help='...', |
| 82 | + required=True) |
| 83 | + parser.add_argument( |
| 84 | + '--workspace-name', |
| 85 | + help='...', |
| 86 | + required=True) |
| 87 | + parser.add_argument( |
| 88 | + '--run-name', |
| 89 | + help='...', |
| 90 | + required=True) |
| 91 | + parser.add_argument( |
| 92 | + '--data-uri', |
| 93 | + help='...', |
| 94 | + ) |
| 95 | + parser.add_argument( |
| 96 | + '--model-uri', |
| 97 | + help='...', |
| 98 | + ) |
| 99 | + |
| 100 | + parser.add_argument('--cluster', type=str, |
| 101 | + help='GKE cluster set up for kubeflow. If set, zone must be provided. ' + |
| 102 | + 'If not set, assuming this runs in a GKE container and current ' + |
| 103 | + 'cluster is used.') |
| 104 | + parser.add_argument('--zone', type=str, help='zone of the kubeflow cluster.') |
| 105 | + args = parser.parse_args() |
| 106 | + |
| 107 | + ws = get_or_create_workspace(args.workspace_name) |
| 108 | + ws_run = get_or_create_workspace_run(ws, args.run_name) |
| 109 | + |
| 110 | + if args.log_type.lower() == DATASET: |
| 111 | + log_dataset_info(ws, ws_run, args.data_uri) |
| 112 | + elif args.log_type.lower() == MODEL: |
| 113 | + log_model_info(ws, ws_run, args.model_uri) |
| 114 | + else: |
| 115 | + logging.warning("Error: unknown metadata logging type %s", args.log_type) |
| 116 | + |
| 117 | + |
| 118 | + |
| 119 | +if __name__ == "__main__": |
| 120 | + main() |
0 commit comments