File size: 1,000 Bytes
d8be1a8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e16ca0f
d8be1a8
e16ca0f
 
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
#define train and fit model
#differentiate into train and test code 
# service endpoint in seperate code- pass inputs to model convert it into json 
import pandas as pd
import pickle
from flask import Flask, request, jsonify

# Create a Flask app
app = Flask(__name__)

# Load the machine learning model from a pickle file
model = pickle.load(open("model.pkl", "rb"))

@app.route('/keepalive', methods=['GET'])
def api_health():
    return jsonify(Message="Success")

# Define a route for making predictions
@app.route("/predict", methods=["POST"])
def predict():
    # Get JSON data from the request
    json_ = request.json

    # Convert JSON data into a DataFrame
    df = pd.DataFrame(json_)

    # Use the loaded model to make predictions on the DataFrame
    prediction = model.predict(df)

    # Return the predictions as a JSON response
    return jsonify({"Prediction": list(prediction)})


# Run the Flask app when this script is executed
if __name__ == "__main__":
    app.run(debug=True)