cheeka84 commited on
Commit
2f1c827
·
verified ·
1 Parent(s): 20b2fdd

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +29 -21
app.py CHANGED
@@ -62,35 +62,39 @@ def predict_sales():
62
 
63
 
64
  # Define an endpoint for batch prediction (POST request)
 
65
  @sales_predictor_api.post('/v1/salesbatch')
66
  def sales_price_batch():
67
  """
68
- This function handles POST requests to the '/v1/salesbatch' endpoint.
69
- It expects a CSV file containing property details for multiple properties
70
- and returns the predicted rental prices as a dictionary in the JSON response.
71
  """
72
- # Get the uploaded CSV file from the request
73
- file = request.files['file']
 
74
 
75
- # Read the CSV file into a Pandas DataFrame
76
- input_data_batch = pd.read_csv(file)
77
- input_data_batch['Store_Age'] = 2025 - input_data_batch['Store_Establishment_Year']
78
 
79
- product_ids = input_data_batch['Product_Id'].tolist() # Assuming 'Id' is the product ID column
80
- store_ids = input_data_batch['Store_Id'].tolist() # Assuming 'Id' is the store ID column
81
- input_data_batch = input_data_batch.drop(['Product_Id', 'Store_Id', 'Store_Establishment_Year'], axis=1)
82
 
83
- # Apply same transformations
84
- #input_transformed = preprocessor.transform(input_data)
85
 
86
- # Make predictions for all properties in the DataFrame (get log_prices)
87
- predictions = model.predict(input_data_batch).tolist()
 
88
 
89
- # Round predictions
90
- predicted_sales = [round(float(x), 2) for x in predictions]
91
 
92
- # Structure output as a list of dicts
93
- output_dict = [
 
 
 
94
  {
95
  "Product_Id": pid,
96
  "Store_Id": sid,
@@ -98,8 +102,12 @@ def sales_price_batch():
98
  }
99
  for pid, sid, psale in zip(product_ids, store_ids, predicted_sales)
100
  ]
101
- return jsonify({"predictions": output_dict})
102
 
103
- # Run the Flask application in debug mode if this script is executed directly
 
 
 
 
 
104
  if __name__ == '__main__':
105
  sales_predictor_api.run(debug=True)
 
62
 
63
 
64
  # Define an endpoint for batch prediction (POST request)
65
+ sales_predictor_api = Flask(__name__)
66
  @sales_predictor_api.post('/v1/salesbatch')
67
  def sales_price_batch():
68
  """
69
+ Endpoint to handle batch predictions from uploaded CSV.
70
+ Returns predicted sales for each (Product_Id, Store_Id) pair.
 
71
  """
72
+ try:
73
+ # Get the uploaded CSV file
74
+ file = request.files['file']
75
 
76
+ # Load and process data
77
+ input_data_batch = pd.read_csv(file)
78
+ input_data_batch['Store_Age'] = 2025 - input_data_batch['Store_Establishment_Year']
79
 
80
+ # Extract identifiers
81
+ product_ids = input_data_batch['Product_Id'].tolist()
82
+ store_ids = input_data_batch['Store_Id'].tolist()
83
 
84
+ # Drop unused columns
85
+ input_data_batch = input_data_batch.drop(['Product_Id', 'Store_Id', 'Store_Establishment_Year'], axis=1)
86
 
87
+ # Apply preprocessing if needed
88
+ # input_data_transformed = preprocessor.transform(input_data_batch)
89
+ # predictions = model.predict(input_data_transformed)
90
 
91
+ predictions = model.predict(input_data_batch) # Assuming already preprocessed or numeric
 
92
 
93
+ # Round predictions
94
+ predicted_sales = [round(float(x), 2) for x in predictions]
95
+
96
+ # Structure output as a list of dicts
97
+ output = [
98
  {
99
  "Product_Id": pid,
100
  "Store_Id": sid,
 
102
  }
103
  for pid, sid, psale in zip(product_ids, store_ids, predicted_sales)
104
  ]
 
105
 
106
+ return jsonify({"predictions": output})
107
+
108
+ except Exception as e:
109
+ return jsonify({"error": str(e)}), 500
110
+
111
+
112
  if __name__ == '__main__':
113
  sales_predictor_api.run(debug=True)