-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
40 lines (35 loc) · 1.14 KB
/
app.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
# Import libraries
from flask import Flask, request, jsonify
from utils import cleaned_df
from utils import check_for_token
import pickle
import pandas as pd
app = Flask(__name__)
# Load the model
model = pickle.load(open('model.pkl', 'rb'))
@app.route('/api', methods=['POST'])
def predict():
if not check_for_token(request):
return "Unauthorized", 401
else:
# Get the data from the POST request.
input = request.get_json(force=True)
model_data = pd.DataFrame(input)[[
'sign_in_count',
'personal_url',
'about',
'avatar',
'extended_data',
'followers_count',
'following_count',
'invitations_count',
'failed_attempts',
'admin']]
cleaned_data = cleaned_df(model_data)
# Make prediction using model loaded from disk as per the data.
prediction = model.predict_proba(cleaned_data)[:, 1]
for index, element in enumerate(input):
element['spam_probability'] = round(float(prediction[index]), 4)
return jsonify(input)
if __name__ == '__main__':
app.run()