Spaces:
Sleeping
Sleeping
File size: 4,119 Bytes
b50ba32 662b7d8 b50ba32 662b7d8 b50ba32 8e86d92 4df82c7 b50ba32 22796d0 b50ba32 2f1c827 b50ba32 2f1c827 b50ba32 2f1c827 ecb358c 2f1c827 429210b 2f1c827 662b7d8 b50ba32 2f1c827 20b2fdd 662b7d8 20b2fdd 2f1c827 20b2fdd b50ba32 2f1c827 b50ba32 |
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# 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 application
sales_predictor_api = Flask("SuperKart Sales Predictor")
# Load the trained machine learning model
model = joblib.load("sales_prediction_model_v1_0.joblib")
# Define a route for the home page (GET request)
@sales_predictor_api.get('/')
def home():
"""
This function handles GET requests to the root URL ('/') of the API.
It returns a simple welcome message.
"""
return "Welcome to SalesKart Sales Prediction API!"
# Define an endpoint for single property prediction (POST request)
@sales_predictor_api.post('/v1/sales')
def predict_sales():
"""
This function handles POST requests to the '/v1/sales' endpoint.
It expects a JSON payload containing property details and returns
the predicted rental price as a JSON response.
"""
# Get the JSON data from the request body
sales_data = request.get_json()
# Extract relevant features from the JSON data
sample = {
'Product_Weight': sales_data['Product_Weight'],
'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
'Product_Type': sales_data['Product_Type'],
'Product_MRP': sales_data['Product_MRP'],
'Store_Size': sales_data['Store_Size'],
'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
'Store_Type': sales_data['Store_Type'],
'Store_Age': sales_data['Store_Age'],
'Store_id': sales_data['Store_id']
}
# Convert the extracted data into a Pandas DataFrame
input_data = pd.DataFrame([sample])
# Make prediction (get log_sales)
predictions = np.exp(model.predict(input_data)[0])
# Round predictions
predicted_sales = round(float(predictions), 2)
# The conversion above is needed as we convert the model prediction (log price) to actual price using np.exp, which returns predictions as NumPy float32 values.
# When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
# Return the actual price
return jsonify({'Predicted Sales (in dollars)': predicted_sales})
# Define an endpoint for batch prediction (POST request)
#sales_predictor_api = Flask("SuperKart Sales Predictor")
@sales_predictor_api.post('/v1/salesbatch')
def sales_price_batch():
"""
Endpoint to handle batch predictions from uploaded CSV.
Returns predicted sales for each (Product_Id, Store_Id) pair.
"""
try:
# Get the uploaded CSV file
file = request.files['file']
# Load and process data
input_data_batch = pd.read_csv(file)
input_data_batch['Store_Age'] = 2025 - input_data_batch['Store_Establishment_Year']
# Extract identifiers
product_ids = input_data_batch['Product_Id'].tolist()
store_ids = input_data_batch['Store_Id'].tolist()
# Drop unused columns
input_data_batch = input_data_batch.drop(['Product_Id', 'Store_Establishment_Year'], axis=1)
# Apply preprocessing if needed
# input_data_transformed = preprocessor.transform(input_data_batch)
# predictions = model.predict(input_data_transformed)
predictions = np.exp(model.predict(input_data_batch)) # Assuming already preprocessed or numeric
# Round predictions
predicted_sales = [round(float(x), 2) for x in predictions]
# Structure output as a list of dicts
output = [
{
"Product_Id": pid,
"Store_Id": sid,
"Predicted_Sales": psale
}
for pid, sid, psale in zip(product_ids, store_ids, predicted_sales)
]
return jsonify({"predictions": output})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
sales_predictor_api.run(debug=True)
|