Spaces:
Sleeping
Sleeping
| # 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) | |
| 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) | |
| 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") | |
| 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) | |