viveksardey's picture
Upload folder using huggingface_hub
a1588da verified
# 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)