Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- app.py +21 -20
- requirements.txt +6 -6
app.py
CHANGED
|
@@ -19,13 +19,13 @@ def home():
|
|
| 19 |
"""
|
| 20 |
return "Welcome to the SuperKart Sales Prediction API!"
|
| 21 |
|
| 22 |
-
# Define an endpoint for single
|
| 23 |
-
@superkart_sales_predictor_api.post('/v1/
|
| 24 |
-
def
|
| 25 |
"""
|
| 26 |
-
This function handles POST requests to the '/v1/
|
| 27 |
-
It expects a JSON payload containing
|
| 28 |
-
the predicted
|
| 29 |
"""
|
| 30 |
# Get the JSON data from the request body
|
| 31 |
product_data = request.get_json()
|
|
@@ -34,12 +34,13 @@ def predict_sales_price():
|
|
| 34 |
sample = {
|
| 35 |
'Product_Id': product_data['Product_Id'],
|
| 36 |
'Product_Weight': product_data['Product_Weight'],
|
| 37 |
-
'Product_Allocated_Area': product_data['Product_Allocated_Area'],
|
| 38 |
-
'Product_MRP': product_data[''],
|
| 39 |
-
'Store_Establishment_Year': product_data['Store_Establishment_Year'],
|
| 40 |
'Product_Sugar_Content': product_data['Product_Sugar_Content'],
|
|
|
|
| 41 |
'Product_Type': product_data['Product_Type'],
|
|
|
|
| 42 |
'Store_Id': product_data['Store_Id'],
|
|
|
|
|
|
|
| 43 |
'Store_Location_City_Type': product_data['Store_Location_City_Type'],
|
| 44 |
'Store_Type': product_data['Store_Type']
|
| 45 |
}
|
|
@@ -47,23 +48,23 @@ def predict_sales_price():
|
|
| 47 |
# Convert the extracted data into a Pandas DataFrame
|
| 48 |
input_data = pd.DataFrame([sample])
|
| 49 |
|
| 50 |
-
# Make prediction
|
| 51 |
predicted_sales = model.predict(input_data)[0]
|
| 52 |
|
| 53 |
# Convert predicted_price to Python float
|
| 54 |
-
|
| 55 |
|
| 56 |
# Return the actual price
|
| 57 |
-
return jsonify({'Predicted
|
| 58 |
|
| 59 |
|
| 60 |
# Define an endpoint for batch prediction (POST request)
|
| 61 |
-
@superkart_sales_predictor_api.post('/v1/
|
| 62 |
def predict_superkart_sales_batch():
|
| 63 |
"""
|
| 64 |
-
This function handles POST requests to the '/v1/
|
| 65 |
-
It expects a CSV file containing
|
| 66 |
-
and returns the predicted
|
| 67 |
"""
|
| 68 |
# Get the uploaded CSV file from the request
|
| 69 |
file = request.files['file']
|
|
@@ -74,12 +75,12 @@ def predict_superkart_sales_batch():
|
|
| 74 |
# Make predictions for all properties in the DataFrame (get log_prices)
|
| 75 |
predicted_sales = model.predict(input_data).tolist()
|
| 76 |
|
| 77 |
-
# Calculate
|
| 78 |
-
|
| 79 |
|
| 80 |
# Create a dictionary of predictions with product IDs as keys
|
| 81 |
-
product_ids = input_data['
|
| 82 |
-
output_dict = dict(zip(product_ids,
|
| 83 |
|
| 84 |
# Return the predictions dictionary as a JSON response
|
| 85 |
return output_dict
|
|
|
|
| 19 |
"""
|
| 20 |
return "Welcome to the SuperKart Sales Prediction API!"
|
| 21 |
|
| 22 |
+
# Define an endpoint for single property prediction (POST request)
|
| 23 |
+
@superkart_sales_predictor_api.post('/v1/sales')
|
| 24 |
+
def predict_superkart_sales():
|
| 25 |
"""
|
| 26 |
+
This function handles POST requests to the '/v1/sales' endpoint.
|
| 27 |
+
It expects a JSON payload containing property details and returns
|
| 28 |
+
the predicted rental price as a JSON response.
|
| 29 |
"""
|
| 30 |
# Get the JSON data from the request body
|
| 31 |
product_data = request.get_json()
|
|
|
|
| 34 |
sample = {
|
| 35 |
'Product_Id': product_data['Product_Id'],
|
| 36 |
'Product_Weight': product_data['Product_Weight'],
|
|
|
|
|
|
|
|
|
|
| 37 |
'Product_Sugar_Content': product_data['Product_Sugar_Content'],
|
| 38 |
+
'Product_Allocated_Area': product_data['Product_Allocated_Area'],
|
| 39 |
'Product_Type': product_data['Product_Type'],
|
| 40 |
+
'Product_MRP': product_data['Product_MRP'],
|
| 41 |
'Store_Id': product_data['Store_Id'],
|
| 42 |
+
'Store_Establishment_Year': product_data['Store_Establishment_Year'],
|
| 43 |
+
'Store_Size': product_data['Store_Size'],
|
| 44 |
'Store_Location_City_Type': product_data['Store_Location_City_Type'],
|
| 45 |
'Store_Type': product_data['Store_Type']
|
| 46 |
}
|
|
|
|
| 48 |
# Convert the extracted data into a Pandas DataFrame
|
| 49 |
input_data = pd.DataFrame([sample])
|
| 50 |
|
| 51 |
+
# Make prediction (get log_price)
|
| 52 |
predicted_sales = model.predict(input_data)[0]
|
| 53 |
|
| 54 |
# Convert predicted_price to Python float
|
| 55 |
+
predicted_rounded_sales = round(float(predicted_sales), 2)
|
| 56 |
|
| 57 |
# Return the actual price
|
| 58 |
+
return jsonify({'Predicted Sales (in dollars)': predicted_rounded_sales})
|
| 59 |
|
| 60 |
|
| 61 |
# Define an endpoint for batch prediction (POST request)
|
| 62 |
+
@superkart_sales_predictor_api.post('/v1/salesbatch')
|
| 63 |
def predict_superkart_sales_batch():
|
| 64 |
"""
|
| 65 |
+
This function handles POST requests to the '/v1/rentalbatch' endpoint.
|
| 66 |
+
It expects a CSV file containing property details for multiple properties
|
| 67 |
+
and returns the predicted rental prices as a dictionary in the JSON response.
|
| 68 |
"""
|
| 69 |
# Get the uploaded CSV file from the request
|
| 70 |
file = request.files['file']
|
|
|
|
| 75 |
# Make predictions for all properties in the DataFrame (get log_prices)
|
| 76 |
predicted_sales = model.predict(input_data).tolist()
|
| 77 |
|
| 78 |
+
# Calculate predicted sales
|
| 79 |
+
predicted_rounded_sales = [round(float(sales), 2) for sales in predicted_sales]
|
| 80 |
|
| 81 |
# Create a dictionary of predictions with product IDs as keys
|
| 82 |
+
product_ids = input_data['Product_Id'].tolist()
|
| 83 |
+
output_dict = dict(zip(product_ids, predicted_rounded_sales)) # Use actual prices
|
| 84 |
|
| 85 |
# Return the predictions dictionary as a JSON response
|
| 86 |
return output_dict
|
requirements.txt
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
pandas==2.2.2
|
| 2 |
-
numpy==2.0.2
|
| 3 |
-
scikit-learn==1.6.1
|
| 4 |
-
xgboost==2.1.4
|
| 5 |
-
joblib==1.4.2
|
| 6 |
Werkzeug==2.2.2
|
| 7 |
flask==2.2.2
|
| 8 |
gunicorn==20.1.0
|
| 9 |
-
requests==2.
|
| 10 |
uvicorn[standard]
|
| 11 |
streamlit==1.43.2
|
|
|
|
| 1 |
+
pandas==2.2.2
|
| 2 |
+
numpy==2.0.2
|
| 3 |
+
scikit-learn==1.6.1
|
| 4 |
+
xgboost==2.1.4
|
| 5 |
+
joblib==1.4.2
|
| 6 |
Werkzeug==2.2.2
|
| 7 |
flask==2.2.2
|
| 8 |
gunicorn==20.1.0
|
| 9 |
+
requests==2.32.3
|
| 10 |
uvicorn[standard]
|
| 11 |
streamlit==1.43.2
|