Download app.py from TokenTutor/ProductSalesRevenuePrediction: direct link, hf CLI and curl.
- Browser
- Download file 1.91 kB
-
https://huggingface.co/spaces/TokenTutor/ProductSalesRevenuePrediction/resolve/main/app.py
- Command line
-
hf download hf://spaces/TokenTutor/ProductSalesRevenuePrediction/app.py
-
curl -L -o app.py https://huggingface.co/spaces/TokenTutor/ProductSalesRevenuePrediction/resolve/main/app.py
1.91 kB
| import joblib | |
| import pandas as pd | |
| from flask import Flask, request, jsonify | |
| # Initialize Flask app with a name | |
| sales_revenue_predictor_api = Flask("Product Sales Revenue Predictor") | |
| # Load the trained churn prediction model | |
| model = joblib.load("product_sales_revenue_prediction_model_v1_0.joblib") | |
| # Define a route for the home page | |
| def home(): | |
| return "Welcome to the Product Sales Revenue Prediction API" | |
| # Define an endpoint to predict churn for a single customer | |
| 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.get('Product_Weight'), | |
| 'Product_Sugar_Content': product_data.get('Product_Sugar_Content'), | |
| 'Product_Allocated_Area': product_data.get('Product_Allocated_Area'), | |
| 'Product_Type': product_data.get('Product_Type'), | |
| 'Product_MRP': product_data.get('Product_MRP'), | |
| 'Store_Id': product_data.get('Store_Id'), | |
| 'Store_Establishment_Year': product_data.get('Store_Establishment_Year'), | |
| 'Store_Size': product_data.get('Store_Size'), | |
| 'Store_Location_City_Type': product_data.get('Store_Location_City_Type'), | |
| 'Store_Type': product_data.get('Store_Type') | |
| } | |
| # Convert the extracted data into a DataFrame | |
| input_data = pd.DataFrame([sample]) | |
| # Make a churn prediction using the trained model | |
| predicted_sales = model.predict(input_data).tolist()[0] | |
| # Convert predicted_price to Python float | |
| predicted_sales = round(float(predicted_sales), 2) | |
| # Return the prediction as a JSON response | |
| return jsonify({'Prediction': predicted_sales}) | |
| # Run the Flask app in debug mode | |
| if __name__ == '__main__': | |
| app.run(debug=True) | |