| | |
| | import numpy as np |
| | import joblib |
| | import pandas as pd |
| | from flask import Flask, request, jsonify |
| | import io |
| |
|
| | |
| | product_sales_price_predictor_api = Flask("SuperKart Product Sales Predictor") |
| |
|
| | |
| | |
| | try: |
| | model = joblib.load("product_sales_prediction_model_v1_0.joblib") |
| | except FileNotFoundError: |
| | model = None |
| |
|
| | |
| | @product_sales_price_predictor_api.route('/', methods=['GET']) |
| | def home(): |
| | """ |
| | This function handles GET requests to the root URL ('/') of the API. |
| | It returns a simple welcome message. |
| | """ |
| | return "Welcome to the SuperKart Product Sales Prediction API!" |
| |
|
| | |
| | @product_sales_price_predictor_api.route('/v1/predict_sales', methods=['POST']) |
| | def predict_product_sales(): |
| | """ |
| | This function handles POST requests to the '/v1/predict_sales' endpoint. |
| | It expects a JSON payload containing product and store details and returns |
| | the predicted sales total as a JSON response. |
| | """ |
| | if model is None: |
| | return jsonify({'error': 'Model not loaded. Please check the server configuration.'}), 500 |
| |
|
| | |
| | data = request.get_json() |
| |
|
| | |
| | try: |
| | sample = { |
| | 'Product_Weight': data['Product_Weight'], |
| | 'Product_Sugar_Content': data['Product_Sugar_Content'], |
| | 'Product_Allocated_Area': data['Product_Allocated_Area'], |
| | 'Product_Type': data['Product_Type'], |
| | 'Product_MRP': data['Product_MRP'], |
| | 'Store_Establishment_Year': data['Store_Establishment_Year'], |
| | 'Store_Size': data['Store_Size'], |
| | 'Store_Location_City_Type': data['Store_Location_City_Type'], |
| | 'Store_Type': data['Store_Type'] |
| | } |
| | except KeyError as e: |
| | return jsonify({'error': f'Missing key in input data: {e}'}), 400 |
| |
|
| |
|
| | |
| | input_data = pd.DataFrame([sample]) |
| |
|
| | |
| | |
| | |
| | predicted_sales = model.predict(input_data)[0] |
| |
|
| | |
| | predicted_sales = round(float(predicted_sales), 2) |
| |
|
| | |
| | return jsonify({'Predicted_Sales': predicted_sales}) |
| |
|
| |
|
| | |
| | @product_sales_price_predictor_api.route('/v1/predict_sales_batch', methods=['POST']) |
| | def predict_product_sales_batch(): |
| | """ |
| | This function handles POST requests to the '/v1/predict_sales_batch' endpoint. |
| | It expects a CSV file containing details for multiple products |
| | and returns the predicted sales as a JSON response. |
| | """ |
| | if model is None: |
| | return jsonify({'error': 'Model not loaded. Please check the server configuration.'}), 500 |
| |
|
| | |
| | if 'file' not in request.files: |
| | return jsonify({'error': 'No file part in the request'}), 400 |
| |
|
| | file = request.files['file'] |
| |
|
| | |
| | if file.filename == '': |
| | return jsonify({'error': 'No selected file'}), 400 |
| |
|
| | if file: |
| | |
| | try: |
| | input_data = pd.read_csv(file) |
| | except Exception as e: |
| | return jsonify({'error': f'Could not process CSV file: {e}'}), 400 |
| |
|
| | |
| | required_columns = [ |
| | 'Product_Weight', 'Product_Sugar_Content', 'Product_Allocated_Area', |
| | 'Product_Type', 'Product_MRP', 'Store_Establishment_Year', |
| | 'Store_Size', 'Store_Location_City_Type', 'Store_Type' |
| | ] |
| | |
| | if not all(col in input_data.columns for col in required_columns): |
| | return jsonify({'error': f'CSV must contain the following columns: {required_columns}'}), 400 |
| |
|
| | |
| | predictions = model.predict(input_data).tolist() |
| |
|
| | |
| | rounded_predictions = [round(float(p), 2) for p in predictions] |
| | |
| | |
| | input_data['Predicted_Sales'] = rounded_predictions |
| |
|
| | |
| | return input_data.to_json(orient='records') |
| |
|
| | return jsonify({'error': 'An unknown error occurred'}), 500 |
| |
|
| | |
| | if __name__ == '__main__': |
| | |
| | product_sales_price_predictor_api.run(host='0.0.0.0', port=5000, debug=True) |
| |
|