DukeDeploy R


Step 1 Register here to obtain your API key.



Step 2 Install the ddeploy R package. See example installation and deployment script below

install.packages("ddeploy")
library(ddeploy)
duke_config <-
  list(
    user_name="try_it",
    api_key="db1542b66f16aba5768d8a19c27dec4facf9168a",
    endpoint="/api/v1.0"
  )
# create simple model for testing
train <- mtcars[1:30,];test <- mtcars[31:32,];
model <- lm(mpg~hp+wt,data=train) # horse power and weight as predictors 
#Deploy the model
duke_deploy(auth_details=duke_config,model_object=model)
#Make local predictions using this model
predict(model,newdata=test)
#Make predictions using deployed model
duke_predict(duke_config,"model",test)


Step 3 Make predictions using the deployed model from your language of choice.

<?php
function duke_predict($user_name,$key,$model_name,$new_data)
{
    $hostname = 'http://deploy.dukeanalytics.com';
    $endpoint = '/api/v1.0/predict'; 
    $curl_post_data = array("Model" => $model_name,"New_Data" => $new_data);
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $hostname.$endpoint."/".$user_name."/".$key);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $curl_post_data);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $curl_response = curl_exec($ch);
    curl_close($ch);
    return $curl_response;
};
print duke_predict('try_it','db1542b66f16aba5768d8a19c27dec4facf9168a','model','[{"hp":335,"wt":3.57},{"hp":109,"wt":2.78}]');
?>
import requests,json

user_name= 'try_it'
key      = 'db1542b66f16aba5768d8a19c27dec4facf9168a' 
hostname = 'http://deploy.dukeanalytics.com';
endpoint = '/api/v1.0/predict';  
new_data = '[{"hp":335,"wt":3.57},{"hp":109,"wt":2.78}]'
data = {'Model':'model','New_Data': new_data}

resp = requests.post(hostname+endpoint+'/'+user_name+'/'+key,data=data)
print 'predict: API returned status %s: %s' % (resp.status_code, resp.text)
print json.loads(resp.text)
		
require 'net/http'
user_name = 'try_it'
key       = 'db1542b66f16aba5768d8a19c27dec4facf9168a' 
hostname  = 'http://deploy.dukeanalytics.com'
endpoint  = '/api/v1.0/predict'
new_data  = [{"hp" => 335,"wt" => 3.57},{"hp" => 109,"wt" => 2.78}]

data = {
    'Model' => 'model' ,
    'New_Data' => new_data
}
uri = URI(hostname+endpoint+'/'+user_name+'/'+key)
res = Net::HTTP.post_form(uri, data)
puts "predict: API returned status #{res.code}: #{res.body}"
puts res.body

res.each_header do |header_name, header_value|
  puts "#{header_name} : #{header_value}"
end
var request = require('request');
function sendRequest (callback) {
    var user_name= 'try_it';
    var key = 'db1542b66f16aba5768d8a19c27dec4facf9168a';
	var hostname = 'http://deploy.dukeanalytics.com';
	var endpoint = '/api/v1.0/predict';
	var new_data = '[{"hp":335,"wt":3.57},{"hp":109,"wt":2.78}]';
	var data = {'Model': 'model', 'New_Data': new_data};
	var options = {
	    url: hostname + endpoint+'/'+user_name+'/'+key,
	    method: 'POST',
	    form: data
	};
	request(options, function (error, response, body) {
	        console.log('predict: API returned status %s: %s', response.statusCode, body);
	        console.log(JSON.parse(body));
	});
}
sendRequest();
exports.sendRequest = sendRequest;