-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
109 lines (86 loc) · 2.37 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import sys
import pickle
import pandas as pd
from flask import Flask, render_template, request, jsonify, Response
app = Flask(__name__)
model = None
@app.route('/', methods=['GET'])
def home():
"""
http --verbose :3333
open /Applications/Google\ Chrome.app http://localhost:3333
"""
return "<h1>hello world</h1>"
@app.route('/version', methods=['GET'])
def version():
"""
http --verbose :3333/version
"""
return sys.version
@app.route('/square', methods=['POST'])
def square():
"""
http -b POST :3333/square x:=7
"""
req = request.get_json()
x = req['x']
return jsonify({'input': x, 'output': x**2})
@app.route('/jdemo', methods=['POST'])
def jdemo():
"""
http --verbose POST :3333/jdemo a=1 b:=2 c:='[2.718,3,"z",true]' d:='{"point":[5,7]}' data:[email protected]
http -b POST :3333/jdemo a=1 b:=2 c:='[2.718,3,"z",true]' d:='{"point":[5,7]}' data:[email protected]
"""
req = request.get_json()
fido = req['data']['dogs'][0]
return jsonify(fido)
@app.route('/about', methods=['GET'])
def about():
"""
http --verbose :3333/about
open /Applications/Google\ Chrome.app http://localhost:3333/about
"""
return render_template('about.html')
@app.route('/faq', methods=['GET'])
def faq():
"""
http --verbose :3333/faq
open /Applications/Google\ Chrome.app http://localhost:3333/faq
"""
return render_template('faq.html')
@app.route('/mpg', methods=['GET'])
def mpg():
"""
http --verbose :3333/mpg
open /Applications/Google\ Chrome.app http://localhost:3333/mpg
"""
return render_template('mpg.html')
@app.route('/inference', methods=['POST'])
def inference():
"""
called from jquery
"""
req = request.get_json()
predictions = model.predict(
[[req['cylinders'], req['horsepower'], req['weight']]])
return jsonify(predictions[0])
@app.route('/reload', methods=['GET'])
def reload():
"""
called from model
"""
global model
model = pickle.load(open("linreg.p", "rb"))
print(model.coef_)
return 'OK'
@app.route('/plotly', methods=['GET'])
def d3():
"""
called from jquery
"""
df = pd.read_csv('cars.csv')
data = list(zip(df.mpg.values.tolist(), df.weight.values.tolist()))
return jsonify({'data': data})
if __name__ == '__main__':
reload()
app.run(host='0.0.0.0', port=3333, debug=True)