gowdhamankarthikeyan/GL_Backend
0
1import joblib2import pandas as pd3from flask import Flask, request, jsonify4 5# Initialize Flask app6SuperKartSales_ForecastModel = Flask("SuperKartSales_ForecastModel")7 8# Load the trained Boston housing model9model = joblib.load("SuperKartSales_ForecastModel_v1_0.joblib")10 11# Define a route for the home page12@SuperKartSales_ForecastModel.get('/')13def home():14 return "Welcome to the SuperKart sales forecast!"15 16# Define an endpoint to predict price for a Single Store Sales17@SuperKartSales_ForecastModel.post('/v1/sales')18def predict_sales():19 # Get JSON data from the request20 sales_data = request.get_json()21 22 # Extract relevant house features from the input data23 sample = {24 'Product_Id': sales_data['Product_Id'],25 'Product_Weight': sales_data['Product_Weight'],26 'Product_Sugar_Content': sales_data['Product_Sugar_Content'],27 'Product_Allocated_Area': sales_data['Product_Allocated_Area'],28 'Product_Type': sales_data['Product_Type'],29 'Product_MRP': sales_data['Product_MRP'],30 'Store_Id': sales_data['Store_Id'],31 'Store_Establishment_Year': sales_data['Store_Establishment_Year'],32 'Store_Size': sales_data['Store_Size'],33 'Store_Location_City_Type': sales_data['Store_Location_City_Type'],34 'Store_Type': sales_data['Store_Type']35 }36 37 # Convert the extracted data into a DataFrame38 input_data = pd.DataFrame([sample])39 40 # Make a prediction using the trained model41 prediction = model.predict(input_data).tolist()[0]42 43 # Return the prediction as a JSON response44 return jsonify({'Predicted Store Sales': prediction})45 46# Define an endpoint to predict sales for batch of input47@SuperKartSales_ForecastModel.post('/v1/salesbatch')48def predict_sales_forecast_batch():49 # Get the uploaded CSV file from the request50 file = request.files['file']51 52 # Read the file into a DataFrame53 input_data = pd.read_csv(file)54 55 # Make predictions for the batch data56 predictions = model.predict(input_data).tolist()57 58 # Add predictions to the DataFrame59 input_data['Predicted_Store_Sales'] = predictions60 61 # Convert results to dictionary62 result = input_data.to_dict(orient="records")63 64 return jsonify(result)65 66# Run the Flask app in debug mode67if __name__ == '__main__':68 SuperKartSales_ForecastModel.run(debug=True)69 