Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
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 |
-
|
| 69 |
-
|
| 70 |
-
and returns the predicted rental prices as a dictionary in the JSON response.
|
| 71 |
"""
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
-
|
|
|
|
| 88 |
|
| 89 |
-
|
| 90 |
-
predicted_sales = [round(float(x), 2) for x in predictions]
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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)
|