File size: 2,096 Bytes
63bb0cd 726d422 63bb0cd 6340092 63bb0cd 726d422 63bb0cd 726d422 63bb0cd 74ebd4b 63bb0cd a1588da 63bb0cd 726d422 |
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 52 53 54 55 56 57 58 59 60 61 62 |
# Import necessary libraries
import numpy as np
import pandas as pd
import joblib # For loading the trained ML model
from flask import Flask, request, jsonify
# Initialize the Flask application
superkart_revenue_predictor_api = Flask("SuperKart Sales Forecast API")
# Load the trained machine learning model
model = joblib.load("superkart_prediction_model_v1_0.joblib") # Ensure this file is present in the same directory
# Define a route for the home page (GET request)
@superkart_revenue_predictor_api.get('/')
def home():
"""
Handles GET requests to the root URL.
Returns a welcome message.
"""
return "Welcome to the SuperKart Sales Forecast API!"
# Define a route for single prediction (POST request)
@superkart_revenue_predictor_api.post('/v1/forecast')
def predict_sales():
"""
Handles POST requests to the '/v1/forecast' endpoint.
Accepts product and store details in JSON format and returns the predicted sales revenue.
"""
# Get JSON data from request body
data = request.get_json()
# Extract features for prediction
sample = {
#'Product_Id': data['Product_Id'],
'Product_Weight': data['Product_Weight'],
'Product_Sugar_Content': data['Product_Sugar_Content'],
'Product_Allocated_Area': data['Product_Allocated_Area'],
'Product_Type': data['Product_Type'],
'Product_MRP': data['Product_MRP'],
#'Store_Id': data['Store_Id'],
'Store_Establishment_Year': data['Store_Establishment_Year'],
'Store_Size': data['Store_Size'],
'Store_Location_City_Type': data['Store_Location_City_Type'],
'Store_Type': data['Store_Type']
}
# Convert to DataFrame
input_df = pd.DataFrame([sample])
# Make prediction
predicted_sales = model.predict(input_df)[0]
# Round off and convert to float
predicted_sales = round(float(predicted_sales), 2)
# Return response
return jsonify({'Predicted_Sales_Revenue': predicted_sales})
# Run the Flask app
if __name__ == '__main__':
superkart_revenue_predictor_api.run(debug=True)
|