| |
|
| | |
| | import numpy as np |
| | import joblib |
| | import pandas as pd |
| | from flask import Flask, request, jsonify |
| |
|
| | |
| | superkart_sales_api = Flask("SuperKart Sales Predictor") |
| |
|
| | |
| | model = joblib.load("superkart_sales_prediction_model_v1_0.joblib") |
| |
|
| | |
| | @superkart_sales_api.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 Sales Prediction API!" |
| |
|
| | |
| | |
| | @superkart_sales_api.post('/v1/sales') |
| | def predict_sales(): |
| | """ |
| | Predict sales for a single product-outlet combination |
| | """ |
| | try: |
| | |
| | data = request.get_json() |
| |
|
| | |
| | sample = { |
| | 'Product_Weight': data['Product_Weight'], |
| | 'Product_Allocated_Area': data['Product_Allocated_Area'], |
| | 'Product_MRP': data['Product_MRP'], |
| | 'Store_Establishment_Year': data['Store_Establishment_Year'], |
| | 'Product_Sugar_Content': data['Product_Sugar_Content'], |
| | 'Store_Size': data['Store_Size'], |
| | 'Store_Location_City_Type': data['Store_Location_City_Type'], |
| | 'Store_Type': data['Store_Type'], |
| | 'Product_Type': data['Product_Type'] |
| |
|
| | } |
| | |
| | |
| | input_df = pd.DataFrame([sample]) |
| |
|
| | |
| | prediction = model.predict(input_df)[0] |
| |
|
| | |
| | prediction = round(float(prediction), 2) |
| |
|
| | return jsonify({"Predicted Sales": prediction}) |
| |
|
| | except Exception as e: |
| | return jsonify({"error": str(e)}), 500 |
| |
|
| | |
| | |
| | |
| | @superkart_sales_api.post('/v1/salesbatch') |
| | def predict_sales_batch(): |
| | """ |
| | Predict sales for multiple rows from a CSV file |
| | """ |
| | try: |
| | |
| | file = request.files['file'] |
| |
|
| | |
| | input_df = pd.read_csv(file) |
| |
|
| | |
| | predictions = model.predict(input_df).tolist() |
| | predictions = [round(float(p), 2) for p in predictions] |
| |
|
| | |
| | output_dict = {str(i): predictions[i] for i in range(len(predictions))} |
| |
|
| | return jsonify(output_dict) |
| |
|
| | except Exception as e: |
| | return jsonify({"error": str(e)}), 500 |
| |
|
| | |
| | |
| | |
| | if __name__ == '__main__': |
| | superkart_sales_api.run(debug=True, host="0.0.0.0", port=7860) |
| |
|
| |
|