-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
49 lines (42 loc) · 1.46 KB
/
api.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
import os
import git
import pickle
import pandas as pd
from flask import Flask, request, jsonify
# Init Flask app
app = Flask(__name__)
# Load model objects
my_directory = os.path.dirname(__file__)
pickle_model_objects_path = os.path.join(my_directory, "model_objects.pkl")
with open(pickle_model_objects_path, "rb") as handle:
transformer, classifier = pickle.load(handle)
@app.route('/git_update', methods=['POST'])
def git_update():
if request.method == 'POST':
repo = git.Repo('./OpenClassrooms-P7')
origin = repo.remotes.origin
repo.create_head('main',origin.refs.main).set_tracking_branch(origin.refs.main).checkout()
origin.pull()
return '', 200
else:
return '', 400
@app.route("/")
def hello():
return "Machine learning API"
@app.route("/predict", methods=["POST","GET"])
def predict():
if request.method == "GET":
return "Prediction page"
if request.method == "POST":
# Parse data as JSON
client_input = request.get_json()
# Convert dictionary to pandas dataframe
client_input = pd.DataFrame(client_input)
# Transforming features
client_input = transformer.transform(client_input)
# Making predictions
pred = classifier.predict(client_input)[0]
proba = classifier.predict_proba(client_input)[0][pred]
return jsonify(prediction=int(pred), probability=round(100 * proba, 1))
if __name__ == '__main__':
app.run(debug=True)