Added Prediction sample

This commit is contained in:
Joe Gregorio
2011-02-23 16:08:54 -05:00
parent 1089444cd1
commit 266c64466d
4 changed files with 96 additions and 4 deletions

View File

@@ -164,7 +164,8 @@ def build_from_document(
auth_discovery = {}
if model is None:
model = JsonModel('dataWrapper' in service.get('features', ['dataWrapper']))
features = service.get('features', ['dataWrapper'])
model = JsonModel('dataWrapper' in features)
resource = createResource(http, base, model, requestBuilder, developerKey,
service, future)
@@ -217,7 +218,6 @@ def createResource(http, baseUrl, model, requestBuilder,
def createMethod(theclass, methodName, methodDesc, futureDesc):
pathUrl = methodDesc['restPath']
pathUrl = re.sub(r'\{', r'{+', pathUrl)
httpMethod = methodDesc['httpMethod']
methodId = methodDesc['rpcMethod']

View File

@@ -0,0 +1,79 @@
#!/usr/bin/python2.4
#
# -*- coding: utf-8 -*-
#
# Copyright 2011 Google Inc. All Rights Reserved.
"""Simple command-line example for Google Prediction API.
Command-line application that trains on some data. This
sample does the same thing as the Hello Prediction! example.
http://code.google.com/apis/predict/docs/hello_world.html
"""
__author__ = 'jcgregorio@google.com (Joe Gregorio)'
import httplib2
import pprint
import time
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from oauth2client.tools import run
# Uncomment to get low level HTTP logging
#httplib2.debuglevel = 4
# Name of Google Storage bucket/object that contains the training data
OBJECT_NAME = "apiclient-prediction-sample/prediction_models/languages"
def main():
storage = Storage('prediction.dat')
credentials = storage.get()
if credentials is None or credentials.invalid == True:
flow = OAuth2WebServerFlow(
# You MUST put in your client id and secret here for this sample to
# work. Visit https://code.google.com/apis/console to get your client
# credentials.
client_id='<Put Your Client ID Here>',
client_secret='<Put Your Client Secret Here>',
scope='https://www.googleapis.com/auth/prediction',
user_agent='prediction-cmdline-sample/1.0',
xoauth_displayname='Prediction Example App')
credentials = run(flow, storage)
http = httplib2.Http()
http = credentials.authorize(http)
service = build("prediction", "v1.1", http=http)
# Start training on a data set
train = service.training()
start = train.insert(data=OBJECT_NAME, body={}).execute()
print 'Started training'
pprint.pprint(start)
# Wait for the training to complete
while 1:
status = train.get(data=OBJECT_NAME).execute()
pprint.pprint(status)
if 'accuracy' in status['modelinfo']:
break
print 'Waiting for training to complete.'
time.sleep(10)
print 'Training is complete'
# Now make a prediction using that training
body = {'input': {'mixture':["mucho bueno"]}}
prediction = service.predict(body=body, data=OBJECT_NAME).execute()
print 'The prediction is:'
pprint.pprint(prediction)
if __name__ == '__main__':
main()

13
samples/prediction/setup.sh Executable file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
#
# Copyright 2011 Google Inc. All Rights Reserved.
# Author: jcgregorio@google.com (Joe Gregorio)
#
# Uploads a training data set to Google Storage to be used by this sample
# application. Download the 'language.txt' file from
# http://code.google.com/apis/predict/docs/hello_world.html
#
# Requirements:
# gsutil - a client application for interacting with Google Storage. It
# can be downloaded from https://code.google.com/apis/storage/docs/gsutil.html
gsutil cp ./language_id.txt gs://apiclient-prediction-sample/prediction_models/languages

View File

@@ -56,7 +56,7 @@ def _tostring_query(varname, value, explode, operator, safe=""):
varprefix = ""
if operator == "?":
joiner = "&"
varprefix = varname + "="
varprefix = varname + "="
if type(value) == type([]):
if 0 == len(value):
return ""
@@ -101,7 +101,7 @@ def expand(template, vars):
operator = ''
varlist = groupdict.get('varlist')
safe = ""
safe = "@"
if operator == '+':
safe = RESERVED
varspecs = varlist.split(",")