Spaces:
Sleeping
Sleeping
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| import traceback | |
| # Initialize Flask app | |
| product_sales_predictor_api = Flask("Product Sales Predictor") | |
| # Manual CORS headers | |
| ''' | |
| @product_sales_predictor_api.after_request | |
| def after_request(response): | |
| response.headers.add('Access-Control-Allow-Origin', '*') | |
| response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization') | |
| response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE') | |
| return response | |
| ''' | |
| # Load the trained model | |
| model = joblib.load("model/product_sales_prediction_model_v1_0.joblib") | |
| # Home endpoint | |
| def home(): | |
| return "Welcome to the SuperKart Product Sales Prediction API!" | |
| # Single product prediction with debug | |
| def predict_single_product(): | |
| try: | |
| # Get JSON data from request | |
| product_info = request.get_json() | |
| print(f"Received data: {product_info}") | |
| # Extract features | |
| prod_input = { | |
| 'Product_Weight': product_info['Product_Weight'], | |
| 'Product_Sugar_Content': product_info['Product_Sugar_Content'], | |
| 'Product_Allocated_Area': product_info['Product_Allocated_Area'], | |
| 'Product_Type': product_info['Product_Type'], | |
| 'Product_MRP': product_info['Product_MRP'], | |
| 'Store_Id': product_info['Store_Id'], | |
| 'Store_Establishment_Year': product_info['Store_Establishment_Year'], | |
| 'Store_Size': product_info['Store_Size'], | |
| 'Store_Location_City_Type': product_info['Store_Location_City_Type'], | |
| 'Store_Type': product_info['Store_Type'] | |
| } | |
| print(f"Processed input: {prod_input}") | |
| # Convert to DataFrame and predict | |
| input_data = pd.DataFrame([prod_input]) | |
| print(f"DataFrame shape: {input_data.shape}") | |
| print(f"DataFrame columns: {input_data.columns.tolist()}") | |
| prediction = model.predict(input_data)[0] | |
| print(f"Prediction: {prediction}") | |
| return jsonify({'predicted_sales': float(round(prediction, 2))}) | |
| except Exception as e: | |
| print(f"Error: {str(e)}") | |
| print(f"Traceback: {traceback.format_exc()}") | |
| return jsonify({'error': str(e)}), 500 | |
| # Batch prediction | |
| def predict_multiple_products(): | |
| try: | |
| # Get uploaded CSV file | |
| input_file = request.files['file'] | |
| input_data = pd.read_csv(input_file) | |
| # Remove Product_Id if present | |
| prediction_data = input_data.drop(['Product_Id'], axis=1, errors='ignore') | |
| # Make predictions | |
| predictions = model.predict(prediction_data).tolist() | |
| predictions = [round(pred, 2) for pred in predictions] | |
| # Create output dictionary | |
| if 'Product_Id' in input_data.columns: | |
| prod_id_list = input_data['Product_Id'].tolist() | |
| result = dict(zip(prod_id_list, predictions)) | |
| else: | |
| result = {f"product_{i+1}": pred for i, pred in enumerate(predictions)} | |
| return jsonify(result) | |
| except Exception as e: | |
| return jsonify({'error': str(e)}), 500 | |