Spaces:
Sleeping
Sleeping
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize Flask app with a name | |
| app = Flask("SuperKart Sales Predictor") | |
| # Load the trained sales prediction model | |
| model = joblib.load("best_sales_forecasting_model.joblib") | |
| # Define a route for the home page | |
| def home(): | |
| return "Welcome to the SuperKart Sales Prediction API" | |
| # Define an endpoint to predict sales | |
| def predict_price(): | |
| # Get JSON data from the request | |
| sales_data = request.get_json() | |
| return model | |
| # Extract relevant sales features from the input 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_Id': sales_data['Store_Id'], | |
| 'Store_Size': sales_data['Store_Size'], | |
| 'Store_Location_City_Type': sales_data['Store_Location_City_Type'], | |
| 'Store_Type': sales_data['Store_Type'], | |
| 'Store_Current_Age': sales_data['Store_Current_Age'] | |
| } | |
| # Convert the extracted data into a DataFrame | |
| input_data = pd.DataFrame([sample]) | |
| # Make a sales prediction using the trained model | |
| prediction = model.predict(input_data)[0] | |
| # Return the prediction as a JSON response | |
| return jsonify({'predicted_sales': float(round(prediction, 2))}) | |
| # @app.route('/predict_batch', methods=['POST']) | |
| # def predict_batch(): | |
| # """ | |
| # API endpoint for batch predictions. | |
| # Expects a JSON array of JSON objects, where each object is a product-store combination. | |
| # """ | |
| # try: | |
| # data = request.get_json(force=True) | |
| # # Convert the list of dictionaries to a pandas DataFrame | |
| # df = pd.DataFrame(data) | |
| # # Ensure columns are in the same order as during training | |
| # df = df[model.feature_names_in_] | |
| # predictions = model.predict(df) | |
| # # Return the predictions as a JSON response | |
| # return jsonify({'Predictions': predictions.tolist()}) | |
| # except Exception as e: | |
| # return jsonify({'error': str(e)}) | |
| # if __name__ == '__main__': | |
| # # Run the Flask app | |
| # # Setting debug=True allows for automatic reloading and provides a debugger | |
| # app.run(debug=True, host='0.0.0.0', port=7860) | |