-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathweb09_flask_server.py
More file actions
83 lines (49 loc) · 1.43 KB
/
Copy pathweb09_flask_server.py
File metadata and controls
83 lines (49 loc) · 1.43 KB
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
from flask import Flask, request, jsonify
from joblib import load
import pandas as pd
app = Flask(__name__)
@app.route('/', methods=['GET'])
def index():
return '<h1>Ciao Polletti</h1>'
@app.route('/json', methods=['GET'])
def json():
return jsonify({
'messaggio': 'Ciao polletti'
})
@app.route('/parrot', methods=['POST'])
def parrot():
body = request.json
print(body)
response = jsonify({
'parrot': body
})
return response
@app.route('/sentiment', methods=['POST'])
def sentiment():
# leggi payload
payload = request.json
X = [payload['sentence']]
# utilizza modello per la predizione
pred = sentiment_pipeline.predict(X)
pred_proba = sentiment_pipeline.predict_proba(X)
classes = sentiment_pipeline['model'].classes_
nice_prob = dict( zip(classes, pred_proba[0].tolist()) )
# invia al client la riposta
return jsonify({
'sentence': payload['sentence'],
'prediction': pred[0],
'probability': nice_prob
})
@app.route('/bikes', methods=['POST'])
def bikes():
payload = request.json
print(payload)
X = pd.DataFrame(payload)
pred = bikes_pipeline.predict(X)
return jsonify({
'prediction': pred.tolist()
})
if __name__ == '__main__':
sentiment_pipeline = load('sentiment_pipeline.joblib')
bikes_pipeline = load('bikes_pipeline.joblib')
app.run(debug=True, port=2228)