Create Controller.py
Browse files- Controller.py +33 -0
Controller.py
ADDED
@@ -0,0 +1,33 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#define train and fit model
|
2 |
+
#differentiate into train and test code
|
3 |
+
# service endpoint in seperate code- pass inputs to model convert it into json
|
4 |
+
import pandas as pd
|
5 |
+
import pickle
|
6 |
+
from flask import Flask, request, jsonify
|
7 |
+
|
8 |
+
# Create a Flask app
|
9 |
+
app = Flask(__name__)
|
10 |
+
|
11 |
+
# Load the machine learning model from a pickle file
|
12 |
+
model = pickle.load(open("model.pkl", "rb"))
|
13 |
+
|
14 |
+
@app.route('/keepalive', methods=['GET'])
|
15 |
+
def api_health():
|
16 |
+
return jsonify(Message="Success")
|
17 |
+
|
18 |
+
# Define a route for making predictions
|
19 |
+
@app.route("/predict", methods=["POST"])
|
20 |
+
def predict():
|
21 |
+
# Get JSON data from the request
|
22 |
+
json_ = request.json
|
23 |
+
|
24 |
+
# Convert JSON data into a DataFrame
|
25 |
+
df = pd.DataFrame(json_)
|
26 |
+
|
27 |
+
# Use the loaded model to make predictions on the DataFrame
|
28 |
+
prediction = model.predict(df)
|
29 |
+
|
30 |
+
# Return the predictions as a JSON response
|
31 |
+
return jsonify({"Prediction": list(prediction)})
|
32 |
+
|
33 |
+
# Run the Flask app when this script is executed
|