Examples have been moved out into their own repository.
This commit is contained in:
parent
90338cb267
commit
49a4112548
|
@ -1,58 +0,0 @@
|
|||
# Buzz Ruby Sample
|
||||
This is a simple starter project written in Ruby which provides a minimal
|
||||
example of Buzz integration within a Sinatra web application.
|
||||
|
||||
Once you've run the starter project and played with the features it provides,
|
||||
this starter project provides a great place to start your experimentation into
|
||||
the API.
|
||||
|
||||
## Prerequisites
|
||||
Please make sure that all of these are installed before you try to run the
|
||||
sample.
|
||||
|
||||
- Ruby 1.8.7+
|
||||
- Ruby Gems 1.3.7+
|
||||
- Are you on a Mac? If so, be sure you have XCode 3.2+
|
||||
- A few gems (run 'sudo gem install <gem name>' to install)
|
||||
- sinatra
|
||||
- httpadapter
|
||||
- extlib
|
||||
- dm-sqlite-adapter
|
||||
- google-api-ruby-client
|
||||
|
||||
## Setup Authentication
|
||||
|
||||
This API uses OAuth 2.0. Learn more about Google APIs and OAuth 2.0 here:
|
||||
http://code.google.com/apis/accounts/docs/OAuth2.html
|
||||
|
||||
Or, if you'd like to dive right in, follow these steps.
|
||||
- Visit https://code.google.com/apis/console/ to register your application.
|
||||
- From the "Project Home" screen, activate access to "Buzz API".
|
||||
- Click on "API Access" in the left column
|
||||
- Click the button labeled "Create an OAuth2 client ID"
|
||||
- Give your application a name and click "Next"
|
||||
- Select "Web Application" as the "Application type"
|
||||
- Under "Your Site or Hostname" select "http://" as the protocol and enter
|
||||
"localhost" for the domain name
|
||||
- click "Create client ID"
|
||||
|
||||
Edit the buzz.rb file and enter the values for the following properties that
|
||||
you retrieved from the API Console:
|
||||
|
||||
- `oauth_client_id`
|
||||
- `oauth_client_secret`
|
||||
|
||||
Or, include them in the command line as the first two arguments.
|
||||
|
||||
## Running the Sample
|
||||
|
||||
I'm assuming you've checked out the code and are reading this from a local
|
||||
directory. If not check out the code to a local directory.
|
||||
|
||||
1. Start up the embedded Sinatra web server
|
||||
|
||||
$ ruby buzz.rb
|
||||
|
||||
2. Open your web browser and see your activities! Go to `http://localhost:4567/`
|
||||
|
||||
3. Be inspired and start hacking an amazing new web app!
|
|
@ -1,125 +0,0 @@
|
|||
$:.unshift('lib')
|
||||
#####!/usr/bin/ruby1.8
|
||||
|
||||
# Copyright:: Copyright 2011 Google Inc.
|
||||
# License:: All Rights Reserved.
|
||||
# Original Author:: Bob Aman
|
||||
# Maintainer:: Daniel Dobson (mailto:wolff@google.com)
|
||||
# Maintainer:: Jenny Murphy (mailto:mimming@google.com)
|
||||
|
||||
require 'rubygems'
|
||||
require 'sinatra'
|
||||
require 'google/api_client'
|
||||
require 'httpadapter/adapters/net_http'
|
||||
require 'pp'
|
||||
|
||||
use Rack::Session::Pool, :expire_after => 86400 # 1 day
|
||||
|
||||
# Configuration
|
||||
# See README for getting API id and secret
|
||||
|
||||
if (ARGV.size < 2)
|
||||
set :oauth_client_id, 'oauth_client_id'
|
||||
set :oauth_client_secret, 'oauth_client_secret'
|
||||
|
||||
if (settings.oauth_client_id == 'oauth_client_id')
|
||||
puts 'See README for getting API id and secret. Server terminated.'
|
||||
exit(0)
|
||||
end
|
||||
else
|
||||
set :oauth_client_id, ARGV[0]
|
||||
set :oauth_client_secret, ARGV[1]
|
||||
end
|
||||
|
||||
# Configuration that you probably don't have to change
|
||||
set :oauth_scopes, 'https://www.googleapis.com/auth/buzz'
|
||||
|
||||
class TokenPair
|
||||
@refresh_token
|
||||
@access_token
|
||||
@expires_in
|
||||
@issued_at
|
||||
|
||||
def update_token!(object)
|
||||
@refresh_token = object.refresh_token
|
||||
@access_token = object.access_token
|
||||
@expires_in = object.expires_in
|
||||
@issued_at = object.issued_at
|
||||
end
|
||||
|
||||
def to_hash
|
||||
return {
|
||||
:refresh_token => @refresh_token,
|
||||
:access_token => @access_token,
|
||||
:expires_in => @expires_in,
|
||||
:issued_at => Time.at(@issued_at)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
# At the beginning of any request, make sure the OAuth token is available.
|
||||
# If it's not available, kick off the OAuth 2 flow to authorize.
|
||||
before do
|
||||
@client = Google::APIClient.new(
|
||||
:authorization => :oauth_2,
|
||||
:host => 'www.googleapis.com',
|
||||
:http_adapter => HTTPAdapter::NetHTTPAdapter.new
|
||||
)
|
||||
|
||||
@client.authorization.client_id = settings.oauth_client_id
|
||||
@client.authorization.client_secret = settings.oauth_client_secret
|
||||
@client.authorization.scope = settings.oauth_scopes
|
||||
@client.authorization.redirect_uri = to('/oauth2callback')
|
||||
@client.authorization.code = params[:code] if params[:code]
|
||||
if session[:token]
|
||||
# Load the access token here if it's available
|
||||
@client.authorization.update_token!(session[:token].to_hash)
|
||||
end
|
||||
|
||||
@buzz = @client.discovered_api('buzz')
|
||||
unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
|
||||
redirect to('/oauth2authorize')
|
||||
end
|
||||
end
|
||||
|
||||
# Part of the OAuth flow
|
||||
get '/oauth2authorize' do
|
||||
<<OUT
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
||||
<title>Google Ruby API Buzz Sample</title>
|
||||
</head>
|
||||
<body>
|
||||
<header><h1>Google Ruby API Buzz Sample</h1></header>
|
||||
<div class="box">
|
||||
<a class='login' href='#{@client.authorization.authorization_uri.to_s}'>Connect Me!</a>
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
OUT
|
||||
end
|
||||
|
||||
# Part of the OAuth flow
|
||||
get '/oauth2callback' do
|
||||
@client.authorization.fetch_access_token!
|
||||
unless session[:token]
|
||||
token_pair = TokenPair.new
|
||||
token_pair.update_token!(@client.authorization)
|
||||
# Persist the token here
|
||||
session[:token] = token_pair
|
||||
end
|
||||
redirect to('/')
|
||||
end
|
||||
|
||||
# The method you're probably actually interested in. This one lists a page of your
|
||||
# most recent activities
|
||||
get '/' do
|
||||
response = @client.execute(
|
||||
@buzz.activities.list,
|
||||
'userId' => '@me', 'scope' => '@consumption', 'alt'=> 'json'
|
||||
)
|
||||
status, headers, body = response
|
||||
[status, {'Content-Type' => 'application/json'}, body]
|
||||
end
|
|
@ -1,96 +0,0 @@
|
|||
APIs Console Project Setup
|
||||
--------------------------
|
||||
If you have not yet, you must set your APIs Console project to enable Prediction
|
||||
API and Google Storage. Go to APIs Console https://code.google.com/apis/console/
|
||||
and select the project you want to use. Next, go to Services, and enable both
|
||||
Prediction API and Google Storage. You may also need to enable Billing (Billing)
|
||||
in the left menu.
|
||||
|
||||
|
||||
Data Setup
|
||||
----------
|
||||
Before you can run the prediction sample prediction.rb, you must load some csv
|
||||
formatted data into Google Storage.
|
||||
|
||||
1 - You must first create the bucket you want to use. This can be done
|
||||
with the gsutil function or via the web UI (Storage Access) in the Google
|
||||
APIs Console. i.e.
|
||||
|
||||
$ gsutil mb gs://BUCKET
|
||||
|
||||
OR
|
||||
|
||||
Go to APIs Console -> Storage Access (on left) and the Google Storage Manager,
|
||||
and create your bucket there.
|
||||
|
||||
2 - We now load the data you want to use to Google Storage. We have supplied a
|
||||
basic language identification dataset in the sample for testing.
|
||||
|
||||
$ chmod 744 setup.sh
|
||||
$ ./setup.sh BUCKET/OBJECT
|
||||
Note you need gsutil in your path for this to work.
|
||||
|
||||
If you have your own dataset, you can do this manually as well.
|
||||
|
||||
$ gsutil cp your_dataset.csv gs://BUCKET/your_dataset.csv
|
||||
|
||||
|
||||
In the script, you must then modify the datafile string. This must correspond with the
|
||||
bucket/object of your dataset (if you are using your own dataset). We have
|
||||
provided a setup.sh which will upload some basic sample data. The section is
|
||||
near the bottom of the script, under 'FILL IN DATAFILE'
|
||||
|
||||
API Setup
|
||||
---------
|
||||
We need to allow the application to use your API access. Go to APIs Console
|
||||
https://code.google.com/apis/console, and select the project you want, go to API
|
||||
Access, and create an OAuth2 client if you have not yet already. You should
|
||||
generate a client ID and secret.
|
||||
|
||||
This example will run through the server-side example, where the application
|
||||
gets authorization ahead of time, which is the normal use case for Prediction
|
||||
API. You can also set it up so the user can grant access.
|
||||
|
||||
First, run the google-api script to generate access and refresh tokens. Ex.
|
||||
|
||||
$ cd google-api-ruby-client
|
||||
$ google-api oauth-2-login --scope=https://www.googleapis.com/auth/prediction --client-id=NUMBER.apps.googleusercontent.com --client-secret=CLIENT_SECRET
|
||||
|
||||
Fill in your client-id and client-secret from the API Access page. You will
|
||||
probably have to set a redirect URI in your client ID
|
||||
(ex. http://localhost:12736/). You can do this by hitting 'Edit settings' in the
|
||||
API Access / Client ID section, and adding it to Authorized Redirect URIs. Not
|
||||
that this has to be exactly the same URI, http://localhost:12736 and
|
||||
http://localhost:12736/ are not the same in this case.
|
||||
|
||||
This should pop up a browser window, where you grant access. This will then
|
||||
generate a ~/.google-api.yaml file. You have two options here, you can either
|
||||
copy the the information directly in your code, or you can store this as a file
|
||||
and load it in the sample as a yaml. In this example we do the latter. NOTE: if
|
||||
you are loading it as a yaml, ensure you rename/move the file, as the
|
||||
~/.google-api.yaml file can get overwritten. The script will work as is if you
|
||||
move the .google-api.yaml file to the sample directory.
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
At this, point, you should have
|
||||
- Enabled your APIs Console account
|
||||
- Created a storage bucket, if required
|
||||
- Uploaded some data to Google Storage
|
||||
- Modified the script to point the 'datafile' variable to the BUCKET/OBJECT name
|
||||
- Modified the script to put your credentials in, either in the code or by
|
||||
loading the generated .yaml file
|
||||
|
||||
We can now run the service!
|
||||
|
||||
$ ruby prediction.rb
|
||||
|
||||
This should start a service on `http://localhost:4567`. When you hit the service,
|
||||
your ruby logs should show the Prediction API calls, and print the prediction
|
||||
output in the debug.
|
||||
|
||||
This sample currently does not cover some newer features of Prediction API such
|
||||
as streaming training, hosted models or class weights. If there are any
|
||||
questions or suggestions to improve the script please email us at
|
||||
prediction-api-discuss@googlegroups.com.
|
File diff suppressed because it is too large
Load Diff
|
@ -1,227 +0,0 @@
|
|||
#!/usr/bin/ruby1.8
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright:: Copyright 2011 Google Inc.
|
||||
# License:: All Rights Reserved.
|
||||
# Original Author:: Bob Aman, Winton Davies, Robert Kaplow
|
||||
# Maintainer:: Robert Kaplow (mailto:rkaplow@google.com)
|
||||
|
||||
require 'rubygems'
|
||||
require 'sinatra'
|
||||
require 'datamapper'
|
||||
require 'google/api_client'
|
||||
require 'yaml'
|
||||
|
||||
use Rack::Session::Pool, :expire_after => 86400 # 1 day
|
||||
|
||||
# Set up our token store
|
||||
DataMapper.setup(:default, 'sqlite::memory:')
|
||||
class TokenPair
|
||||
include DataMapper::Resource
|
||||
|
||||
property :id, Serial
|
||||
property :refresh_token, String
|
||||
property :access_token, String
|
||||
property :expires_in, Integer
|
||||
property :issued_at, Integer
|
||||
|
||||
def update_token!(object)
|
||||
self.refresh_token = object.refresh_token
|
||||
self.access_token = object.access_token
|
||||
self.expires_in = object.expires_in
|
||||
self.issued_at = object.issued_at
|
||||
end
|
||||
|
||||
def to_hash
|
||||
return {
|
||||
:refresh_token => refresh_token,
|
||||
:access_token => access_token,
|
||||
:expires_in => expires_in,
|
||||
:issued_at => Time.at(issued_at)
|
||||
}
|
||||
end
|
||||
end
|
||||
TokenPair.auto_migrate!
|
||||
|
||||
before do
|
||||
|
||||
# FILL IN THIS SECTION
|
||||
# This will work if your yaml file is stored as ./google-api.yaml
|
||||
# ------------------------
|
||||
oauth_yaml = YAML.load_file('.google-api.yaml')
|
||||
@client = Google::APIClient.new
|
||||
@client.authorization.client_id = oauth_yaml["client_id"]
|
||||
@client.authorization.client_secret = oauth_yaml["client_secret"]
|
||||
@client.authorization.scope = oauth_yaml["scope"]
|
||||
@client.authorization.refresh_token = oauth_yaml["refresh_token"]
|
||||
@client.authorization.access_token = oauth_yaml["access_token"]
|
||||
# -----------------------
|
||||
|
||||
@client.authorization.redirect_uri = to('/oauth2callback')
|
||||
|
||||
# Workaround for now as expires_in may be nil, but when converted to int it becomes 0.
|
||||
@client.authorization.expires_in = 1800 if @client.authorization.expires_in.to_i == 0
|
||||
|
||||
if session[:token_id]
|
||||
# Load the access token here if it's available
|
||||
token_pair = TokenPair.get(session[:token_id])
|
||||
@client.authorization.update_token!(token_pair.to_hash)
|
||||
end
|
||||
if @client.authorization.refresh_token && @client.authorization.expired?
|
||||
@client.authorization.fetch_access_token!
|
||||
end
|
||||
|
||||
|
||||
@prediction = @client.discovered_api('prediction', 'v1.3')
|
||||
unless @client.authorization.access_token || request.path_info =~ /^\/oauth2/
|
||||
redirect to('/oauth2authorize')
|
||||
end
|
||||
end
|
||||
|
||||
get '/oauth2authorize' do
|
||||
redirect @client.authorization.authorization_uri.to_s, 303
|
||||
end
|
||||
|
||||
get '/oauth2callback' do
|
||||
@client.authorization.fetch_access_token!
|
||||
# Persist the token here
|
||||
token_pair = if session[:token_id]
|
||||
TokenPair.get(session[:token_id])
|
||||
else
|
||||
TokenPair.new
|
||||
end
|
||||
token_pair.update_token!(@client.authorization)
|
||||
token_pair.save()
|
||||
session[:token_id] = token_pair.id
|
||||
redirect to('/')
|
||||
end
|
||||
|
||||
get '/' do
|
||||
# FILL IN DATAFILE:
|
||||
# ----------------------------------------
|
||||
datafile = "BUCKET/OBJECT"
|
||||
# ----------------------------------------
|
||||
# Train a predictive model.
|
||||
train(datafile)
|
||||
# Check to make sure the training has completed.
|
||||
if (is_done?(datafile))
|
||||
# Do a prediction.
|
||||
# FILL IN DESIRED INPUT:
|
||||
# -------------------------------------------------------------------------------
|
||||
# Note, the input features should match the features of the dataset.
|
||||
prediction,score = get_prediction(datafile, ["Alice noticed with some surprise."])
|
||||
# -------------------------------------------------------------------------------
|
||||
|
||||
# We currently just dump the results to output, but you can display them on the page if desired.
|
||||
puts prediction
|
||||
puts score
|
||||
end
|
||||
end
|
||||
|
||||
##
|
||||
# Trains a predictive model.
|
||||
#
|
||||
# @param [String] filename The name of the file in Google Storage. NOTE: this do *not*
|
||||
# include the gs:// part. If the Google Storage path is gs://bucket/object,
|
||||
# then the correct string is "bucket/object"
|
||||
def train(datafile)
|
||||
input = "{\"id\" : \"#{datafile}\"}"
|
||||
puts "training input: #{input}"
|
||||
result = @client.execute(:api_method => @prediction.training.insert,
|
||||
:merged_body => input,
|
||||
:headers => {'Content-Type' => 'application/json'}
|
||||
)
|
||||
status, headers, body = result.response
|
||||
end
|
||||
|
||||
##
|
||||
# Returns the current training status
|
||||
#
|
||||
# @param [String] filename The name of the file in Google Storage. NOTE: this do *not*
|
||||
# include the gs:// part. If the Google Storage path is gs://bucket/object,
|
||||
# then the correct string is "bucket/object"
|
||||
# @return [Integer] status The HTTP status code of the training job.
|
||||
def get_training_status(datafile)
|
||||
result = @client.execute(:api_method => @prediction.training.get,
|
||||
:parameters => {'data' => datafile})
|
||||
status, headers, body = result.response
|
||||
return status
|
||||
end
|
||||
|
||||
|
||||
##
|
||||
# Checks the training status until a model exists (will loop forever).
|
||||
#
|
||||
# @param [String] filename The name of the file in Google Storage. NOTE: this do *not*
|
||||
# include the gs:// part. If the Google Storage path is gs://bucket/object,
|
||||
# then the correct string is "bucket/object"
|
||||
# @return [Bool] exists True if model exists and can be used for predictions.
|
||||
|
||||
def is_done?(datafile)
|
||||
status = get_training_status(datafile)
|
||||
# We use an exponential backoff approach here.
|
||||
test_counter = 0
|
||||
while test_counter < 10 do
|
||||
puts "Attempting to check model #{datafile} - Status: #{status} "
|
||||
return true if status == 200
|
||||
sleep 5 * (test_counter + 1)
|
||||
status = get_training_status(datafile)
|
||||
test_counter += 1
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
|
||||
|
||||
##
|
||||
# Returns the prediction and most most likely class score if categorization.
|
||||
#
|
||||
# @param [String] filename The name of the file in Google Storage. NOTE: this do *not*
|
||||
# include the gs:// part. If the Google Storage path is gs://bucket/object,
|
||||
# then the correct string is "bucket/object"
|
||||
# @param [List] input_features A list of input features.
|
||||
#
|
||||
# @return [String or Double] prediction The returned prediction, String if categorization,
|
||||
# Double if regression
|
||||
# @return [Double] trueclass_score The numeric score of the most likely label. (Categorical only).
|
||||
|
||||
def get_prediction(datafile,input_features)
|
||||
# We take the input features and put it in the right input (json) format.
|
||||
input="{\"input\" : { \"csvInstance\" : #{input_features}}}"
|
||||
puts "Prediction Input: #{input}"
|
||||
result = @client.execute(:api_method => @prediction.training.predict,
|
||||
:parameters => {'data' => datafile},
|
||||
:merged_body => input,
|
||||
:headers => {'Content-Type' => 'application/json'})
|
||||
status, headers, body = result.response
|
||||
prediction_data = result.data
|
||||
puts status
|
||||
puts body
|
||||
puts prediction_data
|
||||
# Categorical
|
||||
if prediction_data["outputLabel"] != nil
|
||||
# Pull the most likely label.
|
||||
prediction = prediction_data["outputLabel"]
|
||||
# Pull the class probabilities.
|
||||
probs = prediction_data["outputMulti"]
|
||||
puts probs
|
||||
# Verify we are getting a value result.
|
||||
puts ["ERROR", input_features].join("\t") if probs.nil?
|
||||
return "error", -1.0 if probs.nil?
|
||||
|
||||
# Extract the score for the most likely class.
|
||||
trueclass_score = probs.select{|hash|
|
||||
hash["label"] == prediction
|
||||
}[0]["score"]
|
||||
|
||||
# Regression.
|
||||
else
|
||||
prediction = prediction_data["outputValue"]
|
||||
# Class core unused.
|
||||
trueclass_score = -1
|
||||
end
|
||||
|
||||
puts [prediction,trueclass_score,input_features].join("\t")
|
||||
return prediction,trueclass_score
|
||||
end
|
||||
|
|
@ -1,16 +0,0 @@
|
|||
#!/bin/bash
|
||||
#
|
||||
# Copyright 2011 Google Inc. All Rights Reserved.
|
||||
# Author: rkaplow@google.com (Robert Kaplow)
|
||||
#
|
||||
# Uploads a training data set to Google Storage to be used by this sample
|
||||
# application.
|
||||
#
|
||||
# Usage:
|
||||
# setup.sh bucket/object
|
||||
#
|
||||
# Requirements:
|
||||
# gsutil - a client application for interacting with Google Storage. It
|
||||
# can be downloaded from https://code.google.com/apis/storage/docs/gsutil.html
|
||||
OBJECT_NAME=$1
|
||||
gsutil cp data/language_id.txt gs://$OBJECT_NAME
|
Loading…
Reference in New Issue