File size: 1,717 Bytes
5a55b58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import joblib
import pandas as pd
from flask import Flask, request, jsonify

# initialize the flask with a name
sales_forecast_api = Flask("Sales Forecast Predictor")

# load the trained sales forecast model
model = joblib.load("sales_prediction_model_v1_0.joblib")

# define the route for the home page
@sales_forecast_api.get('/')
def home():
    return "Welcome to the Sales Forecast Prediction API!"

# define the endpoint to predict sales forecast
@sales_forecast_api.post('/v1/predict')
def predict_sales():
    # get the JSON data from the request
    predict_data = request.get_json()


    # extract relevant features from the input data.
    sample = {
        'Product_Weight': predict_data['Product_Weight'],
        'Product_Sugar_Content': predict_data['Product_Sugar_Content'],
        'Product_Allocated_Area': predict_data['Product_Allocated_Area'],
        'Product_MRP': predict_data['Product_MRP'],
        'Store_Size': predict_data['Store_Size'],
        'Store_Location_City_Type': predict_data['Store_Location_City_Type'],
        'Store_Type': predict_data['Store_Type'],
        'Store_Years_In_Operation': predict_data['Store_Years_In_Operation'],
        'Product_Code': predict_data['Product_Code'],
        'Product_Category': predict_data['Product_Category']

    }

    # convert the extracted data into a DataFrame
    input_data = pd.DataFrame([sample])

    # make a sales forecast prediction using the trained model
    prediction = model.predict(input_data).tolist()[0]

    # return the prediction as a JSON response
    return jsonify({'Sales': prediction})

# Run the Flask app in debug mode
if __name__ == '__main__':
    sales_forecast_api.run(debug=True)