JohnsonSAimlarge commited on
Commit
626919b
·
verified ·
1 Parent(s): a169eb7

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +27 -71
app.py CHANGED
@@ -1,13 +1,11 @@
1
- # Import necessary libraries
2
  import numpy as np
3
- import joblib # For loading the serialized model
4
- import pandas as pd # For data manipulation
5
- from flask import Flask, request, jsonify # For creating the Flask API
6
 
7
- # Initialize the Flask application
8
  sales_revenue_predictor_api = Flask("Superkart sales Revenue Predictor")
9
 
10
- # Load the trained machine learning model
11
  model = joblib.load("Sales_revenue_prediction_model_v1_0.joblib")
12
 
13
  # Define the expected columns (based on your dataset)
@@ -18,87 +16,45 @@ EXPECTED_COLUMNS = [
18
  'Store_Location_City_Type', 'Store_Type'
19
  ]
20
 
21
-
22
-
23
- # Define a route for the home page (GET request)
24
  @sales_revenue_predictor_api.get('/')
25
  def home():
26
- """
27
- This function handles GET requests to the root URL ('/') of the API.
28
- It returns a simple welcome message.
29
- """
30
- return "Welcome to the SuperKart Sales revenue Prediction API!"
31
 
32
- # Define an endpoint for single property prediction (POST request)
33
  @sales_revenue_predictor_api.post('/v1/sales')
34
- def predict_product_sales():
35
- """
36
- This function handles POST requests to the '/v1/sales' endpoint.
37
- It expects a JSON payload containing property details and returns
38
- the predicted product total sales revenue as a JSON response.
39
- """
40
- # Get the JSON data from the request body
41
  sales_data = request.get_json()
42
 
43
- # Extract relevant features from the JSON data
44
- sample = {
45
- 'Product_Weight': sales_data['Product_Weight'],
46
- 'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
47
- 'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
48
- 'Product_Type': sales_data['Product_Type'],
49
- 'Product_MRP': sales_data['Product_MRP'],
50
- 'Store_Id': sales_data['Store_Id'],
51
- 'Store_Establishment_Year': sales_data['Store_Establishment_Year'],
52
- 'Store_Size': sales_data['Store_Size'],
53
- 'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
54
- 'Store_Type': sales_data['Store_Type']
55
- }
56
-
57
- # Convert the extracted data into a Pandas DataFrame
58
- input_data = pd.DataFrame([sample])
59
-
60
- # Make prediction (get product sales)
61
- predicted_Sales = model.predict(input_data)[0]
62
 
63
- # Calculate actual product sales
64
- predicted_product_sales = np.exp(predicted_Sales)
 
 
65
 
66
- # Convert predicted_product_sales to Python float
67
- predicted_product_sales = round(float(predicted_product_sales), 2)
68
- # The conversion above is needed as we convert the model prediction (predicted_Sales) to actual sales using np.exp, which returns predictions as NumPy float32 values.
69
- # When we send this value directly within a JSON response, Flask's jsonify function encounters a datatype error
70
 
71
- # Return the actual product sales total
72
- return jsonify({'Predicted total product sales': predicted_product_sales})
73
-
74
-
75
- # Define an endpoint for batch prediction (POST request)
76
  @sales_revenue_predictor_api.post('/v1/salesbatch')
77
- def predict_product_sales_batch():
78
- """
79
- This function handles POST requests to the '/v1/salesbatch' endpoint.
80
- It expects a CSV file containing property details for multiple properties
81
- and returns the predicted total product sales revenue as a dictionary in the JSON response.
82
- """
83
- # Get the uploaded CSV file from the request
84
  file = request.files['file']
85
-
86
- # Read the CSV file into a Pandas DataFrame
87
  input_data = pd.read_csv(file)
88
 
89
- # Make predictions for all properties in the DataFrame (get predicted_Sales)
90
- predicted_Sales = model.predict(input_data).tolist()
 
 
91
 
92
- # Calculate actual sales
93
- predicted_Sales = [round(float(np.exp(Product_Store_Sales_Total)), 2) for Product_Store_Sales_Total in predicted_Sales]
 
94
 
95
- # Create a dictionary of predictions with property IDs as keys
96
- Product_Ids = input_data['Product_Id'].tolist() # Assuming 'Product_Id' is the product ID column
97
- output_dict = dict(zip(Product_Ids, predicted_Sales)) # Use actual sales
98
 
99
- # Return the predictions dictionary as a JSON response
100
- return output_dict
101
 
102
- # Run the Flask application in debug mode if this script is executed directly
103
  if __name__ == '__main__':
104
  sales_revenue_predictor_api.run(debug=True)
 
 
 
1
  import numpy as np
2
+ import joblib
3
+ import pandas as pd
4
+ from flask import Flask, request, jsonify
5
 
 
6
  sales_revenue_predictor_api = Flask("Superkart sales Revenue Predictor")
7
 
8
+ # Load trained model
9
  model = joblib.load("Sales_revenue_prediction_model_v1_0.joblib")
10
 
11
  # Define the expected columns (based on your dataset)
 
16
  'Store_Location_City_Type', 'Store_Type'
17
  ]
18
 
 
 
 
19
  @sales_revenue_predictor_api.get('/')
20
  def home():
21
+ return "Welcome to the SuperKart Sales Prediction API!"
 
 
 
 
22
 
 
23
  @sales_revenue_predictor_api.post('/v1/sales')
24
+ def predict_sales():
 
 
 
 
 
 
25
  sales_data = request.get_json()
26
 
27
+ # Manually build DataFrame with missing/default values
28
+ input_data = pd.DataFrame([sales_data])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
30
+ # Add missing expected columns if any
31
+ missing_cols = set(EXPECTED_COLUMNS) - set(input_data.columns)
32
+ if missing_cols:
33
+ return jsonify({"error": f"columns are missing: {missing_cols}"}), 400
34
 
35
+ # Predict
36
+ prediction = model.predict(input_data)[0]
37
+ return jsonify({"Sales": round(float(prediction), 2)})
 
38
 
 
 
 
 
 
39
  @sales_revenue_predictor_api.post('/v1/salesbatch')
40
+ def predict_sales_batch():
 
 
 
 
 
 
41
  file = request.files['file']
 
 
42
  input_data = pd.read_csv(file)
43
 
44
+ # Check for missing columns
45
+ missing_cols = set(EXPECTED_COLUMNS) - set(input_data.columns)
46
+ if missing_cols:
47
+ return jsonify({"error": f"columns are missing: {missing_cols}"}), 400
48
 
49
+ # Predict
50
+ predictions = model.predict(input_data).tolist()
51
+ predictions = [round(float(p), 2) for p in predictions]
52
 
53
+ # Use ID column or row index
54
+ ids = input_data['Product_Id'].tolist() if 'Product_Id' in input_data.columns else list(range(1, len(predictions) + 1))
 
55
 
56
+ return jsonify(dict(zip(ids, predictions)))
 
57
 
 
58
  if __name__ == '__main__':
59
  sales_revenue_predictor_api.run(debug=True)
60
+