File size: 1,287 Bytes
24fd809
 
 
4bb0692
 
 
 
 
 
 
 
24fd809
 
 
4bb0692
24fd809
4bb0692
 
24fd809
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import pandas as pd
from flask import Flask, request, jsonify
import joblib
import os
import numpy as np
import sklearn # Required for deserializing scikit-learn objects in the pipeline
from flask_cors import CORS # To handle Cross-Origin Resource Sharing
from dotenv import load_dotenv # To load environment variables

# Load environment variables (if any)
load_dotenv()

#Initialize Flask app with a name
Sales_forecasting_api = Flask("Sales Forecasting API")
CORS(Sales_forecasting_api) # Initialize CORS for the Flask app

#Load of the Saved Model Pipeline
pipeline = joblib.load(os.path.join(os.path.dirname(__file__), "superkart_pipeline.pkl"))

#Home Route
@Sales_forecasting_api.route("/")
def home():
    return"Welcome to the SuperKart Sales Prediction API!"
# Define an endpoint to predict Sales
@Sales_forecasting_api.route("/predict", methods=["POST"])
def predict():
    try:
        # Get JSON data from the request
        data = request.get_json()
        # Convert JSON to DataFrame
        input_df = pd.DataFrame([data])
          # Make predictions
        predictions = pipeline.predict(input_df)
        # Return predictions as JSON
        return jsonify({"predictions": predictions.tolist()})
    except Exception as e:
        return jsonify({"error": str(e)})