Bakalo's picture
Upload folder using huggingface_hub
4bb0692 verified
Raw History Blame Contribute Delete
1.29 kB
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)})