Spaces:
Sleeping
Sleeping
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize Flask app | |
| sales_predictor_api = Flask("Product_Store_Total_Sales_Predictor") | |
| # Load the trained model | |
| model = joblib.load("product_sales_total_prediction_v1_0.joblib") | |
| #Label encoding Store Size as this is ordinal | |
| size_map = {'Small': 1, 'Medium': 2, 'High': 3} | |
| feasibility_map = { | |
| 'Fruits and Vegetables': 'Perishable', | |
| 'Hard Drinks': 'Non Perishable', | |
| 'Snack Foods': 'Perishable', | |
| 'Dairy': 'Perishable', | |
| 'Canned': 'Perishable', | |
| 'Baking Goods': 'Perishable', | |
| 'Breads': 'Perishable', | |
| 'Breakfast': 'Perishable', | |
| 'Frozen Foods': 'Perishable', | |
| 'Health and Hygiene': 'Perishable', | |
| 'Household': 'Perishable', | |
| 'Seafood': 'Perishable', | |
| 'Starchy Foods': 'Perishable', | |
| 'Others': 'Non Perishable', | |
| 'Meat': 'Perishable', | |
| 'Soft Drinks': 'Non Perishable', | |
| } | |
| # Home endpoint | |
| def home(): | |
| return "Welcome to the Product Store Total Sales Prediction API!" | |
| # Prediction endpoint | |
| def predict_product_sales(): | |
| try: | |
| product_data = request.get_json() | |
| # Construct input DataFrame | |
| input_data = pd.DataFrame([{ | |
| 'Product_Weight': product_data['Product_Weight'], | |
| 'Product_Sugar_Content': product_data['Product_Sugar_Content'], | |
| 'Product_Allocated_Area': product_data['Product_Allocated_Area'], | |
| 'Product_Type': product_data['Product_Type'], | |
| 'Product_MRP': product_data['Product_MRP'], | |
| 'Store_Establishment_Year': product_data['Store_Establishment_Year'], | |
| 'Store_Size': size_map[product_data['Store_Size']], | |
| 'Store_Location_City_Type': product_data['Store_Location_City_Type'], | |
| 'Store_Type': product_data['Store_Type'], | |
| 'Store_Age': 2025 - product_data['Store_Establishment_Year'], | |
| 'Product_Perishability': feasibility_map[product_data['Product_Type']] | |
| }]) | |
| # Predict | |
| prediction = model.predict(input_data).tolist()[0] | |
| return jsonify({'Product Store Sales Total': prediction}) | |
| except Exception as e: | |
| return jsonify({'error': str(e)}), 500 | |
| # Run the app | |
| if __name__ == '__main__': | |
| sales_predictor_api.run(debug=True) | |