Spaces:
Sleeping
Sleeping
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize Flask app with a name | |
| superkart_api = Flask("Superkart Sales Forecast") | |
| # Load the trained superkart sales forecast model | |
| model = joblib.load("tuned_rf_model.pkl") | |
| # Define a route for the home page | |
| def home(): | |
| return "Welcome to the Superkart Sales Forecasting API!" | |
| # Define an endpoint to predict churn for a single customer | |
| def predict_sales(): | |
| # Get JSON data from the request | |
| data = request.get_json() | |
| # Extract relevant customer features from the input data | |
| 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'] | |
| } | |
| # Converting the data into a DataFrame | |
| input_df = pd.DataFrame([sample]) | |
| # Making a prediction using the trained model | |
| prediction = model.predict(input_df).tolist()[0] | |
| # return the prediction as a JSON response | |
| return jsonify({'Predicted_Sales': prediction}) | |
| def predict_sales_batch(): | |
| # Get the uploaded CSV file from the request | |
| file = request.files['file'] | |
| # Read the file into a dataframe | |
| input_df = pd.read_csv(file) | |
| # Make predictions for the batch data and convert raw predictions into a readable format | |
| predictions = model.predict(input_df).tolist() | |
| input_df['Predicted_Sales'] = predictions | |
| return input_df[['Predicted_Sales']].to_json(orient='records') | |
| # Run the app in debug mode | |
| if __name__ == "__main__": | |
| superkart_api.run(debug=True) | |