blob: 24fdf19ebed40c3cad85fb0c587261e5fc68fb78 [file] [log] [blame]
Joe Gregorio652898b2011-05-02 21:07:43 -04001#!/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
20Command-line application that trains on some data. This sample does
21the same thing as the Hello Prediction! example.
22
23Usage:
24 $ python prediction.py
25
26You can also get help on all the command-line flags the program understands
27by running:
28
29 $ python prediction.py --help
30
31To get detailed log output run:
32
33 $ python prediction.py --logging_level=DEBUG
34"""
35
36__author__ = 'jcgregorio@google.com (Joe Gregorio)'
37
38import gflags
39import httplib2
40import logging
41import pprint
42import sys
43
44from apiclient.discovery import build
45from oauth2client.file import Storage
46from oauth2client.client import OAuth2WebServerFlow
47from oauth2client.tools import run
48
49FLAGS = gflags.FLAGS
50
51# Set up a Flow object to be used if we need to authenticate. This
52# sample uses OAuth 2.0, and we set up the OAuth2WebServerFlow with
53# the information it needs to authenticate. Note that it is called
54# the Web Server Flow, but it can also handle the flow for native
55# applications <http://code.google.com/apis/accounts/docs/OAuth2.html#IA>
56# The client_id client_secret are copied from the API Access tab on
57# the Google APIs Console <http://code.google.com/apis/console>. When
58# creating credentials for this application be sure to choose an Application
59# type of "Installed application".
60FLOW = OAuth2WebServerFlow(
61 client_id='433807057907.apps.googleusercontent.com',
62 client_secret='jigtZpMApkRxncxikFpR+SFg',
63 scope='https://www.googleapis.com/auth/prediction',
64 user_agent='prediction-cmdline-sample/1.0')
65
66# The gflags module makes defining command-line options easy for
67# applications. Run this program with the '--help' argument to see
68# all the flags that it understands.
69gflags.DEFINE_enum('logging_level', 'ERROR',
70 ['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'],
71 'Set the level of logging detail.')
72
73
74def main(argv):
75 # Let the gflags module process the command-line arguments
76 try:
77 argv = FLAGS(argv)
78 except gflags.FlagsError, e:
79 print '%s\\nUsage: %s ARGS\\n%s' % (e, argv[0], FLAGS)
80 sys.exit(1)
81
82 # Set the logging according to the command-line flag
83 logging.getLogger().setLevel(getattr(logging, FLAGS.logging_level))
84
85 # If the Credentials don't exist or are invalid run through the native client
86 # flow. The Storage object will ensure that if successful the good
87 # Credentials will get written back to a file.
88 storage = Storage('prediction.dat')
89 credentials = storage.get()
90 if credentials is None or credentials.invalid:
91 credentials = run(FLOW, storage)
92
93 # Create an httplib2.Http object to handle our HTTP requests and authorize it
94 # with our good Credentials.
95 http = httplib2.Http()
96 http = credentials.authorize(http)
97
98 service = build("prediction", "v1.2", http=http)
99
100 # Name of Google Storage bucket/object that contains the training data
101 OBJECT_NAME = "apiclient-prediction-sample/prediction_models/languages"
102
103 # Start training on a data set
104 train = service.training()
105 start = train.insert(data=OBJECT_NAME, body={}).execute()
106
107 print 'Started training'
108 pprint.pprint(start)
109
110 import time
111 # Wait for the training to complete
112 while True:
113 status = train.get(data=OBJECT_NAME).execute()
114 pprint.pprint(status)
115 if 'RUNNING' != status['trainingStatus']:
116 break
117 print 'Waiting for training to complete.'
118 time.sleep(10)
119 print 'Training is complete'
120
121 # Now make a prediction using that training
122 body = {'input': {'csvInstance': ["mucho bueno"]}}
123 prediction = service.predict(body=body, data=OBJECT_NAME).execute()
124 print 'The prediction is:'
125 pprint.pprint(prediction)
126
127
128
129if __name__ == '__main__':
130 main(sys.argv)