Spaces:
Runtime error
Runtime error
| from flask import Flask, request, jsonify | |
| import pandas as pd | |
| import joblib | |
| import os | |
| from backend_files.routes import welcome_message # ✅ Corrected import | |
| # Initialize Flask app | |
| superkart_sales_predictor_api = Flask(__name__) | |
| # Load model | |
| model_path = os.path.join("backend_files", "superkart_model_prediction_model_v1_0.joblib") | |
| model = joblib.load(model_path) | |
| # Home route | |
| def home(): | |
| return welcome_message() | |
| # Single prediction route | |
| def predict_sales_total(): | |
| product_data = request.get_json() | |
| sample = { | |
| 'Product Type': product_data['Product Type'], | |
| 'Product ID': product_data['Product ID'], | |
| 'Product Weight': product_data['Product Weight'], | |
| 'Product Sugar Content': product_data['Product Sugar Content'], | |
| 'Product Allocated Area': product_data['Product Allocated Area'], | |
| 'Product MRP': product_data['Product MRP'], | |
| 'Store ID': product_data['Store ID'], | |
| 'Store Establishment Year': product_data['Store Establishment Year'], | |
| 'Store Size': product_data['Store Size'], | |
| 'Store Location': product_data['Store Location'], | |
| 'City Size': product_data['City Size'], | |
| 'Store Type': product_data['Store Type'] | |
| } | |
| input_df = pd.DataFrame([sample]) | |
| prediction = model.predict(input_df)[0] | |
| return jsonify({'Predicted Product Store Sales Total': round(float(prediction), 2)}) | |
| # Batch prediction route | |
| def predict_sales_total_batch(): | |
| file = request.files['file'] | |
| input_df = pd.read_csv(file) | |
| predictions = model.predict(input_df).tolist() | |
| product_ids = input_df['Product ID'].tolist() | |
| results = dict(zip(product_ids, [round(float(p), 2) for p in predictions])) | |
| return jsonify(results) | |
| # Start app | |
| if __name__ == '__main__': | |
| superkart_sales_predictor_api.run(debug=True, host='0.0.0.0', port=5000) | |