Spaces:
Build error
Build error
# Import necessary libraries | |
import numpy as np | |
import joblib # For loading the serialized model | |
import pandas as pd # For data manipulation | |
from flask import Flask, request, jsonify # For creating the Flask API | |
# Initialize the Flask app | |
app = Flask(__name__) | |
model = joblib.load("backend_files/superkart_sales_prediction_model_v1_0.joblib") | |
# Define a route for the home page (GET request) | |
def predict_store_sales(): | |
""" | |
Handles POST requests for predicting sales of a single store. | |
Expects JSON input with store details. | |
""" | |
# Get JSON input | |
store_data = request.get_json() | |
# Extract relevant features (match your model training columns) | |
sample = { | |
'Product_Weight': store_data['Product_Weight'], | |
'Product_Allocated_Area': store_data['Product_Allocated_Area'], | |
'Product_MRP': store_data['Product_MRP'], | |
'Store_Age': store_data['Store_Age'], | |
'Product_Sugar_Content': store_data['Product_Sugar_Content'], | |
'Product_Type': store_data['Product_Type'], | |
'Store_Size': store_data['Store_Size'], | |
'Store_Location_City_Type': store_data['Store_Location_City_Type'], | |
'Store_Type': store_data['Store_Type'], | |
'Store_Id': store_data['Store_Id'] | |
} | |
# Convert into DataFrame | |
input_data = pd.DataFrame([sample]) | |
# Make prediction | |
predicted_sales = model.predict(input_data)[0] | |
# Convert to Python float and round | |
predicted_sales = round(float(predicted_sales), 2) | |
# Return JSON response | |
return jsonify({'Predicted Store Sales': predicted_sales}) | |
# ...existing code... | |
# Run Flask app | |
if __name__ == '__main__': | |
store_sales_api.run(debug=True) | |