Spaces:
Sleeping
Sleeping
| from flask import Flask, request, jsonify | |
| import joblib | |
| import pandas as pd | |
| app = Flask("Superkart Sales Predictor") | |
| # Load the trained churn prediction model | |
| model = joblib.load("superkart_sales_prediction_model_v1_0.joblib") | |
| #def hello_world(): | |
| # return "Hello, World from Hugging Face Space!" | |
| def home(): | |
| return "Welcome to the Superkart Sales Prediction API !" | |
| #@app.post('/v1/Product') | |
| def predict_churn(): | |
| # Get JSON data from the request | |
| product_data = request.get_json() | |
| # Extract relevant customer features from the input data | |
| sample = { | |
| 'Product_Weight':product_data['Product_Weight'], | |
| 'Product_Allocated_Area':product_data['Product_Allocated_Area'], | |
| 'Product_MRP':product_data['Product_MRP'], | |
| 'Store_Establishment_Year':product_data['Store_Establishment_Year'], | |
| 'Product_Sugar_Content':product_data['Product_Sugar_Content'], | |
| 'Product_Type':product_data['Product_Type'], | |
| 'Store_Id':product_data['Store_Id'], | |
| 'Store_Size':product_data['Store_Size'], | |
| '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]) | |
| # Make a churn prediction using the trained model | |
| prediction = model.predict(input_data).tolist()[0] | |
| # Map prediction result to a human-readable label | |
| prediction_value = prediction | |
| # Return the prediction as a JSON response | |
| return jsonify({'Prediction': prediction_value}) | |
| # Define an endpoint to predict churn for a batch of customers | |
| def predict_churn_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) | |
| # Make predictions for the batch data and convert raw predictions into a readable format | |
| predictions = model.predict(input_data.drop("Product_Id",axis=1)).tolist() | |
| product_id_list = input_data.Product_Id.values.tolist() | |
| output_dict = dict(zip(product_id_list, predictions)) | |
| return output_dict | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |