Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -33,7 +33,7 @@ def predict_sales():
|
|
| 33 |
"""
|
| 34 |
# Get the JSON data from the request body
|
| 35 |
input_data = request.get_json()
|
| 36 |
-
|
| 37 |
# Extract relevant features from the JSON data
|
| 38 |
sample = {
|
| 39 |
'Product_Weight': input_data['Product_Weight'],
|
|
@@ -46,32 +46,32 @@ def predict_sales():
|
|
| 46 |
'Store_Location_City_Type': input_data['Store_Location_City_Type'],
|
| 47 |
'Store_Type': input_data['Store_Type']
|
| 48 |
}
|
| 49 |
-
|
| 50 |
# Convert the extracted data into a Pandas DataFrame
|
| 51 |
features_df = pd.DataFrame([sample])
|
| 52 |
-
|
| 53 |
# Apply one-hot encoding for nominal columns (matching training)
|
| 54 |
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 55 |
-
|
| 56 |
# Apply ordinal encoding (based on provided orders)
|
| 57 |
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 58 |
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 59 |
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 60 |
-
|
| 61 |
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 62 |
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 63 |
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
| 64 |
-
|
| 65 |
# Make prediction (assuming direct sales prediction; adjust if log-transformed)
|
| 66 |
predicted_sales = model.predict(features_df)[0]
|
| 67 |
-
|
| 68 |
# If your model predicts log(sales), uncomment and use this instead:
|
| 69 |
# predicted_log_sales = model.predict(features_df)
|
| 70 |
# predicted_sales = np.exp(predicted_log_sales)
|
| 71 |
-
|
| 72 |
# Convert to Python float and round to 2 decimals
|
| 73 |
predicted_sales = round(float(predicted_sales), 2)
|
| 74 |
-
|
| 75 |
# Return the predicted sales total
|
| 76 |
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 77 |
|
|
|
|
| 33 |
"""
|
| 34 |
# Get the JSON data from the request body
|
| 35 |
input_data = request.get_json()
|
| 36 |
+
|
| 37 |
# Extract relevant features from the JSON data
|
| 38 |
sample = {
|
| 39 |
'Product_Weight': input_data['Product_Weight'],
|
|
|
|
| 46 |
'Store_Location_City_Type': input_data['Store_Location_City_Type'],
|
| 47 |
'Store_Type': input_data['Store_Type']
|
| 48 |
}
|
| 49 |
+
|
| 50 |
# Convert the extracted data into a Pandas DataFrame
|
| 51 |
features_df = pd.DataFrame([sample])
|
| 52 |
+
|
| 53 |
# Apply one-hot encoding for nominal columns (matching training)
|
| 54 |
features_df = pd.get_dummies(features_df, columns=['Product_Type', 'Store_Type'], drop_first=True)
|
| 55 |
+
|
| 56 |
# Apply ordinal encoding (based on provided orders)
|
| 57 |
sugar_mapping = {'No Sugar': 0, 'Low Sugar': 1, 'Regular': 2}
|
| 58 |
size_mapping = {'Small': 0, 'Medium': 1, 'High': 2}
|
| 59 |
city_mapping = {'Tier 3': 0, 'Tier 2': 1, 'Tier 1': 2}
|
| 60 |
+
|
| 61 |
features_df['Product_Sugar_Content'] = features_df['Product_Sugar_Content'].map(sugar_mapping)
|
| 62 |
features_df['Store_Size'] = features_df['Store_Size'].map(size_mapping)
|
| 63 |
features_df['Store_Location_City_Type'] = features_df['Store_Location_City_Type'].map(city_mapping)
|
| 64 |
+
|
| 65 |
# Make prediction (assuming direct sales prediction; adjust if log-transformed)
|
| 66 |
predicted_sales = model.predict(features_df)[0]
|
| 67 |
+
|
| 68 |
# If your model predicts log(sales), uncomment and use this instead:
|
| 69 |
# predicted_log_sales = model.predict(features_df)
|
| 70 |
# predicted_sales = np.exp(predicted_log_sales)
|
| 71 |
+
|
| 72 |
# Convert to Python float and round to 2 decimals
|
| 73 |
predicted_sales = round(float(predicted_sales), 2)
|
| 74 |
+
|
| 75 |
# Return the predicted sales total
|
| 76 |
return jsonify({'Predicted Sales Total (in dollars)': predicted_sales})
|
| 77 |
|