MBG0903 commited on
Commit
f5519f5
·
verified ·
1 Parent(s): fa25585

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +32 -33
app.py CHANGED
@@ -28,11 +28,11 @@ def predict_sales():
28
  It expects a JSON payload containing property details and returns
29
  the predicted rental price as a JSON response.
30
  """
31
- # Get the JSON data from the request body
32
- business_data = request.get_json()
33
 
34
- # Extract relevant features from the JSON data
35
- sample = {
36
  'Product_Weight': business_data['Product_Weight'] ,
37
  'Product_Sugar_Content': business_data['Product_Sugar_Content'],
38
  'Product_Allocated_Area': business_data['Product_Allocated_Area'],
@@ -44,17 +44,17 @@ sample = {
44
  'Store_Type': business_data['Store_Type']
45
  }
46
 
47
- # Convert the extracted data into a Pandas DataFrame
48
- input_data = pd.DataFrame([business_data])
49
 
50
- # Make a sales prediction using the sales model
51
- predicted_sales = model.predict(input_data)[0]
52
 
53
- # Convert predicted_price to Python float
54
- predicted_sales = float(predicted_sales)
55
 
56
- # Return the actual sales
57
- return jsonify({'Predicted sales': predicted_sales})
58
 
59
  if __name__ == "__main__":
60
  sales_prediction_api.run(debug=True)
@@ -62,27 +62,26 @@ if __name__ == "__main__":
62
  # Define an endpoint for batch prediction (POST request)
63
  @sales_prediction_api.post("/v1/predict/batch")
64
  def predict_sales_batch():
65
- """
66
- This function handles POST requests to the '/v1/predict/batch' endpoint.
67
- It expects a CSV file containing property details for multiple properties
68
- and returns the predicted rental prices as a dictionary in the JSON response.
69
- """
70
- # Get the uploaded CSV file from the request
71
- file = request.files['file']
72
-
73
- # Read the CSV file into a Pandas DataFrame
74
- input_data = pd.read_csv(file)
75
-
76
- # Make predictions for all properties in the DataFrame (get log_prices)
77
- predicted_log_sales = model.predict(input_data).tolist()
78
 
79
- # Create a dictionary of predictions with property IDs as keys
80
- Store_Type = input_data['Store_Type'].tolist()
81
- output_dict = dict(zip(Store_Type, predicted_sales)) # Use actual prices
82
 
83
- # Return the predictions dictionary as a JSON response
84
- return output_dict
 
85
 
86
- # Run the Flask application in debug mode if this script is executed directly
87
- if __name__ == '__main__':
88
- sales_prediction_api.run(debug=True)
 
 
 
28
  It expects a JSON payload containing property details and returns
29
  the predicted rental price as a JSON response.
30
  """
31
+ # Get the JSON data from the request body
32
+ business_data = request.get_json()
33
 
34
+ # Extract relevant features from the JSON data
35
+ sample = {
36
  'Product_Weight': business_data['Product_Weight'] ,
37
  'Product_Sugar_Content': business_data['Product_Sugar_Content'],
38
  'Product_Allocated_Area': business_data['Product_Allocated_Area'],
 
44
  'Store_Type': business_data['Store_Type']
45
  }
46
 
47
+ # Convert the extracted data into a Pandas DataFrame
48
+ input_data = pd.DataFrame([business_data])
49
 
50
+ # Make a sales prediction using the sales model
51
+ predicted_sales = model.predict(input_data)[0]
52
 
53
+ # Convert predicted_price to Python float
54
+ predicted_sales = float(predicted_sales)
55
 
56
+ # Return the actual sales
57
+ return jsonify({'Predicted sales': predicted_sales})
58
 
59
  if __name__ == "__main__":
60
  sales_prediction_api.run(debug=True)
 
62
  # Define an endpoint for batch prediction (POST request)
63
  @sales_prediction_api.post("/v1/predict/batch")
64
  def predict_sales_batch():
65
+ """
66
+ This function handles POST requests to the '/v1/predict/batch' endpoint.
67
+ It expects a CSV file containing property details for multiple properties
68
+ and returns the predicted rental prices as a dictionary in the JSON response.
69
+ """
70
+ # Get the uploaded CSV file from the request
71
+ file = request.files['file']
72
+
73
+ # Read the CSV file into a Pandas DataFrame
74
+ input_data = pd.read_csv(file)
 
 
 
75
 
76
+ # Make predictions for all properties in the DataFrame (get log_prices)
77
+ predicted_log_sales = model.predict(input_data).tolist()
 
78
 
79
+ # Create a dictionary of predictions with property IDs as keys
80
+ Store_Type = input_data['Store_Type'].tolist()
81
+ output_dict = dict(zip(Store_Type, predicted_sales)) # Use actual prices
82
 
83
+ # Return the predictions dictionary as a JSON response
84
+ return output_dict
85
+
86
+ if __name__ == '__main__':
87
+ sales_prediction_api.run(debug=False, host='0.0.0.0', port=7860)