|
|
import joblib |
|
|
import pandas as pd |
|
|
from flask import Flask, request, jsonify |
|
|
|
|
|
|
|
|
app = Flask(__name__) |
|
|
|
|
|
|
|
|
model = joblib.load("backend_files/final_sales_forecasting_model.joblib") |
|
|
|
|
|
|
|
|
@app.route('/') |
|
|
def home(): |
|
|
return "Welcome to the SuperKart Sales Forecasting API" |
|
|
|
|
|
|
|
|
@app.route('/predict_single', methods=['POST']) |
|
|
def predict_single(): |
|
|
|
|
|
data = request.get_json() |
|
|
|
|
|
|
|
|
|
|
|
try: |
|
|
sample = { |
|
|
'Product_Id': data['Product_Id'], |
|
|
'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_Id': data['Store_Id'], |
|
|
'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'] |
|
|
} |
|
|
|
|
|
|
|
|
input_data = pd.DataFrame([sample]) |
|
|
|
|
|
|
|
|
prediction = model.predict(input_data).tolist()[0] |
|
|
|
|
|
|
|
|
return jsonify({'predicted_sales': prediction}) |
|
|
|
|
|
except KeyError as e: |
|
|
return jsonify({'error': f'Missing data for key: {e}'}), 400 |
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
|
|
|
|
|
|
|
|
|
@app.route('/predict_batch', methods=['POST']) |
|
|
def predict_batch(): |
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
predictions = model.predict(input_data).tolist() |
|
|
|
|
|
|
|
|
return jsonify({'predicted_sales': predictions}) |
|
|
|
|
|
except Exception as e: |
|
|
return jsonify({'error': str(e)}), 500 |
|
|
else: |
|
|
return jsonify({'error': 'Something went wrong with file upload'}), 500 |
|
|
|
|
|
|
|
|
if __name__ == '__main__': |
|
|
|
|
|
app.run(debug=True, host='0.0.0.0', port=5000) |
|
|
|