import joblib import pandas as pd from flask import Flask, request, jsonify # Initialize Flask app app = Flask("Product sales prediction") # Load the product sales prediction model model = joblib.load("SuperKart_sales_prediction_v1_0.joblib") # Define a route for the home page @app.get('/') def home(): return "Welcome to the product sales prediction API" # Define an endpoint to predict sales @app.post('/v1/sales') def predict_sales_price(): # Get JSON data from the request product_data = request.get_json() # Extract relevant house features from the input data sample = { '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_Type': product_data['Product_Type'], 'Product_MRP': product_data['Product_MRP'], 'Store_Id': product_data['Store_Id'], 'Store_Size': product_data['Store_Size'], 'Store_Establishment_Year': product_data['Store_Establishment_Year'], 'Store_Location_City_Type': product_data['Store_Location_City_Type'], 'Store_Type': product_data['Store_Type'] } # Convert the extracted data into a DataFrame input_data = pd.DataFrame([sample.drop('Product_Id')]) input_data['Store_age'] = (2026 - sample['Store_Establishment_Year']) input_data.drop('Store_Establishment_Year', axis=1, inplace=True) # Make a prediction using the trained model prediction = model.predict(input_data).tolist()[0] # Return the prediction as a JSON response product_id = sample["Product_Id"] return jsonify({f'Predicted sales for {Product_Id}:', prediction}) # Define an endpoint to predict sales for a batch of products @app.post('/v1/salesbatch') def predict_sales_batch(): # Get the uploaded CSV file from the request file = request.files['file'] # Read the file into a DataFrame input_data = pd.read_csv(file) input_data_v2 = input_data.copy() input_data_v2.drop('Product_Id', axis=1, inplace=True) input_data_v2['Store_age'] = (2026 - input_data['Store_Establishment_Year']) input_data_v2.drop('Store_Establishment_Year', axis=1, inplace=True) # Make predictions for the batch data predictions = model.predict(input_data_v2).tolist() # Add predictions to the DataFrame input_data['Predicted_sales'] = predictions # Convert results to dictionary result = input_data.to_dict(orient="records") return jsonify(result) # Run the Flask app in debug mode if __name__ == '__main__': app.run(debug=True)