hsaluja431 commited on
Commit
0856265
·
verified ·
1 Parent(s): 48f9404

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +30 -32
app.py CHANGED
@@ -2,46 +2,44 @@ import joblib
2
  import pandas as pd
3
  from flask import Flask, request, jsonify
4
 
5
- # Initialize Flask app with a name
6
  sales_predictor_api = Flask("Product_Store_Total_Sales_Predictor")
7
 
8
- # Load the trained Product Store Total Sales prediction model
9
  model = joblib.load("product_sales_total_prediction_v1_0.joblib")
10
 
11
- # Define a route for the home page
12
  @sales_predictor_api.get('/')
13
  def home():
14
  return "Welcome to the Product Store Total Sales Prediction API!"
15
 
16
- # Define an endpoint to predict product sales for a specific store
17
  @sales_predictor_api.post('/v1/product')
18
  def predict_product_sales():
19
- # Get JSON data from the request
20
- product_data = request.get_json()
21
-
22
- # Extract relevant product features from the input data
23
- sample = {
24
- 'Product_Weight': product_data['Product_Weight'],
25
- 'Product_Sugar_Content': product_data['Product_Sugar_Content'],
26
- 'Product_Allocated_Area': product_data['Product_Allocated_Area'],
27
- 'Product_Type': product_data['Product_Type'],
28
- 'Product_MRP': product_data['Product_MRP'],
29
- 'Store_Establishment_Year': product_data['Store_Establishment_Year'],
30
- 'Store_Size': product_data['Store_Size'].map(size_map),
31
- 'Store_Location_City_Type': product_data['Store_Location_City_Type'],
32
- 'Store_Type': product_data['Store_Type'],
33
- 'Store_Age': 2025 - product_data['Store_Establishment_Year']
34
- }
35
-
36
- # Convert the extracted data into a DataFrame
37
- input_data = pd.DataFrame([sample])
38
-
39
- # Make a churn prediction using the trained model
40
- prediction = model.predict(input_data).tolist()[0]
41
-
42
- # Return the prediction as a JSON response
43
- return jsonify({'Product Store Sales Total': prediction})
44
-
45
- # Run the Flask app in debug mode
46
  if __name__ == '__main__':
47
- app.run(debug=True)
 
2
  import pandas as pd
3
  from flask import Flask, request, jsonify
4
 
5
+ # Initialize Flask app
6
  sales_predictor_api = Flask("Product_Store_Total_Sales_Predictor")
7
 
8
+ # Load the trained model
9
  model = joblib.load("product_sales_total_prediction_v1_0.joblib")
10
 
11
+ # Home endpoint
12
  @sales_predictor_api.get('/')
13
  def home():
14
  return "Welcome to the Product Store Total Sales Prediction API!"
15
 
16
+ # Prediction endpoint
17
  @sales_predictor_api.post('/v1/product')
18
  def predict_product_sales():
19
+ try:
20
+ product_data = request.get_json()
21
+
22
+ # Construct input DataFrame
23
+ input_data = pd.DataFrame([{
24
+ 'Product_Weight': product_data['Product_Weight'],
25
+ 'Product_Sugar_Content': product_data['Product_Sugar_Content'],
26
+ 'Product_Allocated_Area': product_data['Product_Allocated_Area'],
27
+ 'Product_Type': product_data['Product_Type'],
28
+ 'Product_MRP': product_data['Product_MRP'],
29
+ 'Store_Establishment_Year': product_data['Store_Establishment_Year'],
30
+ 'Store_Size': product_data['Store_Size'],
31
+ 'Store_Location_City_Type': product_data['Store_Location_City_Type'],
32
+ 'Store_Type': product_data['Store_Type'],
33
+ 'Store_Age': 2025 - product_data['Store_Establishment_Year']
34
+ }])
35
+
36
+ # Predict
37
+ prediction = model.predict(input_data).tolist()[0]
38
+ return jsonify({'Product Store Sales Total': prediction})
39
+
40
+ except Exception as e:
41
+ return jsonify({'error': str(e)}), 500
42
+
43
+ # Run the app
 
 
44
  if __name__ == '__main__':
45
+ sales_predictor_api.run(debug=True)