File size: 1,159 Bytes
2fd7082
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 joblib
import pandas as pd
from flask import Flask, request, jsonify

# Initialize Flask app with a name
sales_predictor_api = Flask("SuperKart Sales Predictor")

# Load the trained SuperKart Sales prediction model
model = joblib.load("superkart_sales_model.pkl")

# Define a route for the home page
@sales_predictor_api.get('/')
def home():
    return "Welcome to the SuperKart Sales Prediction API!"

# Define an endpoint to predict churn for a single customer
@sales_predictor_api.post('/v1/productsales')
def predict_sales():
    try:
        # Get JSON data
        sales_data = request.get_json()

        # Ensure input is a list of records
        if isinstance(sales_data, dict):
            sales_data = [sales_data]

        # Convert to DataFrame
        input_data = pd.DataFrame(sales_data)

        # Predict
        prediction = model.predict(input_data).tolist()[0]

        return jsonify({"prediction": float(prediction)})

    except Exception as e:
        return jsonify({"error": str(e)})

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