Browse Source

Basic prediction webserver

Phyks (Lucas Verney) 2 years ago
parent
commit
e40dada301
2 changed files with 689 additions and 1457 deletions
  1. 36
    0
      app.py
  2. 653
    1457
      notebook.ipynb

+ 36
- 0
app.py View File

@@ -0,0 +1,36 @@
1
+from bottle import hook, request, response, route, run
2
+from sklearn.externals import joblib
3
+
4
+mlb, classifier = joblib.load('offClassifier.pkl')
5
+
6
+
7
+@hook('after_request')
8
+def enable_cors():
9
+    """
10
+    You need to add some headers to each request.
11
+    Don't use the wildcard '*' for Access-Control-Allow-Origin in production.
12
+    """
13
+    response.headers['Access-Control-Allow-Origin'] = '*'
14
+    response.headers['Access-Control-Allow-Methods'] = 'PUT, GET, POST, DELETE, OPTIONS'
15
+    response.headers['Access-Control-Allow-Headers'] = 'Origin, Accept, Content-Type, X-Requested-With, X-CSRF-Token'
16
+
17
+
18
+@route('/predict', method=['OPTIONS', 'POST'])
19
+def predict():
20
+    if request.method == 'OPTIONS':
21
+        return {}
22
+
23
+    products = request.json
24
+    predictions = mlb.inverse_transform(
25
+        classifier.predict([p['name'] for p in products])
26
+    )
27
+    return {
28
+        'data': [
29
+            product.update({'predictedCategories': categories}) or product
30
+            for product, categories in zip(products, predictions)
31
+        ]
32
+    }
33
+
34
+
35
+if __name__ == '__main__':
36
+    run(host='localhost', port=4242)

+ 653
- 1457
notebook.ipynb
File diff suppressed because it is too large
View File