# 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 superMartSales_api = Flask("SuperMart_Sales_Revenue") # Load the trained model model = joblib.load("supermart_sales_prediction_model_v1_0.joblib") # Home route @superMartSales_api.get('/') def home(): return "Welcome to SuperKart sales Revenue Prediction" import traceback @superMartSales_api.post('/v1/superKartSales') def sale_pred_single(): try: sale_data = request.get_json() sample = { 'Product_Weight': sale_data.get('Product_Weight'), 'Product_Sugar_Content': sale_data.get('Product_Sugar_Content'), 'Product_Allocated_Area': sale_data.get('Product_Allocated_Area'), 'Product_Type': sale_data.get('Product_Type'), 'Product_MRP': sale_data.get('Product_MRP'), 'Store_Id': sale_data.get('Store_Id'), 'Store_Establishment_Year': sale_data.get('Store_Establishment_Year'), 'Store_Size': sale_data.get('Store_Size'), 'Store_Location_City_Type': sale_data.get('Store_Location_City_Type'), 'Store_Type': sale_data.get('Store_Type'), } input_data = pd.DataFrame([sample]) predicted_sale = model.predict(input_data)[0] response = { 'Store_Outlet': sample['Store_Id'], "Sale": round(float(predicted_sale), 2) } return jsonify(response) except Exception as e: print("❌ Error in /v1/superKartSales:", e) traceback.print_exc() # this will show in Hugging Face Space logs return jsonify({"error": str(e)}), 500 # Batch prediction route @superMartSales_api.post('/v1/superKartbatch') def sale_pred_batch(): file = request.files['file'] print("File Received:", file.filename) # Read input data input_data = pd.read_csv(file) # Make predictions predicted_sale = model.predict(input_data).tolist() # Add predictions to input data input_data['Predicted_Sale'] = predicted_sale # Group by Store_Id and sum the predicted sales grouped_sales = input_data.groupby('Store_Id')['Predicted_Sale'].sum().to_dict() # Create response response = { 'store_sales': { store_id: round(float(sale), 2) for store_id, sale in grouped_sales.items() } } print("Final Response:", response) return jsonify(response) # Run the application if __name__ == '__main__': superMartSales_api.run()