DukeDeploy Python


Step 1 Register here to obtain your API key.



Step 2 Install the dukedeploy Python package.

sudo pip install dukedeploy

See example installation and deployment script below

import dukedeploy   
import numpy as np
from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression

api_username = 'try_it'
api_key  = 'db1542b66f16aba5768d8a19c27dec4facf9168a'
endpoint = '/api/v1.0'

client = dukedeploy.api(api_username, api_key, endpoint)

data   = load_boston()
clf    = LinearRegression()

clf.fit(data.data, data.target)
print client.deploy_model(clf,'python_model_1')

test_data = np.array(data.data[1:2])
predicted = clf.predict(test_data.tolist())
print predicted
print client.predict('python_model_1',test_data)

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;