Skip to content

Instantly share code, notes, and snippets.

@springcoil
Created September 23, 2017 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save springcoil/b8ef6d073349b9102ec49845b757f6b4 to your computer and use it in GitHub Desktop.
Save springcoil/b8ef6d073349b9102ec49845b757f6b4 to your computer and use it in GitHub Desktop.
Attempted API with flask and sampled
from flask import Flask,request, jsonify
from sampled import sampled
import numpy as np
import pymc3 as pm
import theano.tensor as tt
import pandas as pd
app = Flask(__name__)
@sampled
def student():
difficulty = pm.Beta('difficulty', alpha=5, beta=5)
intelligence = pm.Beta('intelligence', alpha=5, beta=5)
SAT = pm.Beta('SAT', alpha=20 * intelligence, beta=20 * (1 - intelligence))
grade_avg = 0.5 + 0.5 * tt.sqrt((1 - difficulty) * intelligence)
grade = pm.Beta('grade', alpha=20 * grade_avg, beta=20 * (1 - grade_avg))
recommendation = pm.Binomial('recommendation', n=1, p=0.7 * grade)
@app.route('/predict', methods=['POST'])
def predict():
json_ = request.json
with student(SAT=json_):
prior = pm.sample(draws=1000, tune=500)
return jsonify({'recommendation': prior.get_values('recommendation').mean()})
if (__name__=='__main__'):
app.run(port=5808)
@springcoil
Copy link
Author

For some reason this doesn't work - I've tried testing it with
curl -i -X POST -H "Content-Type: application/json" -d "{"SAT":"0.5"}" http://localhost:5808/predict

@ColCarroll
Copy link

It looks like the data is probably malformed (too many double quotes!) -d '{"SAT": 0.5}' is what I would try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment