Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 1 | #!/usr/bin/python2.4 |
| 2 | # -*- coding: utf-8 -*- |
| 3 | # |
| 4 | # Copyright (C) 2010 Google Inc. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | # you may not use this file except in compliance with the License. |
| 8 | # You may obtain a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | # See the License for the specific language governing permissions and |
| 16 | # limitations under the License. |
| 17 | |
| 18 | """Simple command-line sample for the Google Prediction API |
| 19 | |
Joe Gregorio | 65826f9 | 2011-06-03 11:20:29 -0400 | [diff] [blame] | 20 | Command-line application that trains on your input data. This sample does |
| 21 | the same thing as the Hello Prediction! example. You might want to run |
| 22 | the setup.sh script to load the sample data to Google Storage. |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 23 | |
| 24 | Usage: |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 25 | $ python prediction.py --object_name="bucket/object" --id="model_id" |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 26 | |
| 27 | You can also get help on all the command-line flags the program understands |
| 28 | by running: |
| 29 | |
| 30 | $ python prediction.py --help |
| 31 | |
| 32 | To get detailed log output run: |
| 33 | |
| 34 | $ python prediction.py --logging_level=DEBUG |
| 35 | """ |
| 36 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 37 | __author__ = ('jcgregorio@google.com (Joe Gregorio), ' |
| 38 | 'marccohen@google.com (Marc Cohen)') |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 39 | |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 40 | import argparse |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 41 | import os |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 42 | import pprint |
| 43 | import sys |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 44 | import time |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 45 | |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 46 | from apiclient import discovery |
| 47 | from apiclient import sample_tools |
| 48 | from oauth2client import client |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 49 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 50 | |
| 51 | # Time to wait (in seconds) between successive checks of training status. |
| 52 | SLEEP_TIME = 10 |
| 53 | |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 54 | |
| 55 | # Declare command-line flags. |
| 56 | argparser = argparse.ArgumentParser(add_help=False) |
| 57 | argparser.add_argument('object_name', |
| 58 | help='Full Google Storage path of csv data (ex bucket/object)') |
| 59 | argparser.add_argument('id', |
| 60 | help='Model Id of your choosing to name trained model') |
| 61 | |
| 62 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 63 | def print_header(line): |
| 64 | '''Format and print header block sized to length of line''' |
| 65 | header_str = '=' |
| 66 | header_line = header_str * len(line) |
| 67 | print '\n' + header_line |
| 68 | print line |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 69 | print header_line |
| 70 | |
| 71 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 72 | def main(argv): |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 73 | service, flags = sample_tools.init( |
| 74 | argv, 'prediction', 'v1.5', __doc__, __file__, parents=[argparser], |
| 75 | scope='https://www.googleapis.com/auth/prediction') |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 76 | |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 77 | try: |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 78 | # Get access to the Prediction API. |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 79 | papi = service.trainedmodels() |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 80 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 81 | # List models. |
| 82 | print_header('Fetching list of first ten models') |
| 83 | result = papi.list(maxResults=10).execute() |
| 84 | print 'List results:' |
| 85 | pprint.pprint(result) |
| 86 | |
| 87 | # Start training request on a data set. |
| 88 | print_header('Submitting model training request') |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 89 | body = {'id': flags.id, 'storageDataLocation': flags.object_name} |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 90 | start = papi.insert(body=body).execute() |
| 91 | print 'Training results:' |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 92 | pprint.pprint(start) |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 93 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 94 | # Wait for the training to complete. |
| 95 | print_header('Waiting for training to complete') |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 96 | while True: |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 97 | status = papi.get(id=flags.id).execute() |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 98 | state = status['trainingStatus'] |
| 99 | print 'Training state: ' + state |
| 100 | if state == 'DONE': |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 101 | break |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 102 | elif state == 'RUNNING': |
| 103 | time.sleep(SLEEP_TIME) |
| 104 | continue |
| 105 | else: |
| 106 | raise Exception('Training Error: ' + state) |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 107 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 108 | # Job has completed. |
| 109 | print 'Training completed:' |
| 110 | pprint.pprint(status) |
| 111 | break |
Robert Kaplow | 49cd5f8 | 2011-08-02 13:50:04 -0400 | [diff] [blame] | 112 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 113 | # Describe model. |
| 114 | print_header('Fetching model description') |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 115 | result = papi.analyze(id=flags.id).execute() |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 116 | print 'Analyze results:' |
| 117 | pprint.pprint(result) |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 118 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 119 | # Make a prediction using the newly trained model. |
| 120 | print_header('Making a prediction') |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 121 | body = {'input': {'csvInstance': ["mucho bueno"]}} |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 122 | result = papi.predict(body=body, id=flags.id).execute() |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 123 | print 'Prediction results...' |
| 124 | pprint.pprint(result) |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 125 | |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 126 | # Delete model. |
| 127 | print_header('Deleting model') |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 128 | result = papi.delete(id=flags.id).execute() |
Joe Gregorio | 968a958 | 2012-03-07 14:52:52 -0500 | [diff] [blame] | 129 | print 'Model deleted.' |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 130 | |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 131 | except client.AccessTokenRefreshError: |
Joe Gregorio | 7d79121 | 2011-05-16 21:58:52 -0700 | [diff] [blame] | 132 | print ("The credentials have been revoked or expired, please re-run" |
| 133 | "the application to re-authorize") |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 134 | |
Joe Gregorio | e839115 | 2013-06-28 01:30:57 -0400 | [diff] [blame^] | 135 | |
Joe Gregorio | 652898b | 2011-05-02 21:07:43 -0400 | [diff] [blame] | 136 | if __name__ == '__main__': |
| 137 | main(sys.argv) |