Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -19,30 +19,30 @@ def home():
|
|
| 19 |
def sales_predict():
|
| 20 |
#get data from json request
|
| 21 |
sales_data = request.get_json()
|
| 22 |
-
|
| 23 |
#get relevant details
|
| 24 |
sample = {
|
| 25 |
-
'Product_Weight'
|
| 26 |
-
'Product_Sugar_Content'
|
| 27 |
-
'Product_Allocated_Area'
|
| 28 |
-
'Product_Type'
|
| 29 |
-
'Product_MRP'
|
| 30 |
-
'Store_Establishment_Year'
|
| 31 |
-
'Store_Size'
|
| 32 |
-
'Store_Location_City_Type'
|
| 33 |
-
'Store_Type'
|
| 34 |
}
|
| 35 |
|
| 36 |
-
|
| 37 |
|
| 38 |
#convert the categorical to dummies
|
| 39 |
-
|
| 40 |
-
|
| 41 |
|
| 42 |
-
|
| 43 |
-
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
|
| 48 |
#defining endpoint for batch
|
|
@@ -55,21 +55,26 @@ def sales_batch_predict():
|
|
| 55 |
input_data = pd.read_csv(file)
|
| 56 |
|
| 57 |
#convert the categorical to dummies
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
#predict
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
#run the flask app in debug mode
|
| 74 |
if __name__ == '__main__':
|
| 75 |
-
|
|
|
|
| 19 |
def sales_predict():
|
| 20 |
#get data from json request
|
| 21 |
sales_data = request.get_json()
|
| 22 |
+
|
| 23 |
#get relevant details
|
| 24 |
sample = {
|
| 25 |
+
'Product_Weight': sales_data['Product_Weight'],
|
| 26 |
+
'Product_Sugar_Content': sales_data['Product_Sugar_Content'],
|
| 27 |
+
'Product_Allocated_Area': sales_data['Product_Allocated_Area'],
|
| 28 |
+
'Product_Type': sales_data['Product_Type'],
|
| 29 |
+
'Product_MRP': sales_data['Product_MRP'],
|
| 30 |
+
'Store_Establishment_Year': sales_data['Store_Establishment_Year'],
|
| 31 |
+
'Store_Size': sales_data['Store_Size'],
|
| 32 |
+
'Store_Location_City_Type': sales_data['Store_Location_City_Type'],
|
| 33 |
+
'Store_Type': sales_data['Store_Type']
|
| 34 |
}
|
| 35 |
|
| 36 |
+
input_data = pd.DataFrame([sample])
|
| 37 |
|
| 38 |
#convert the categorical to dummies
|
| 39 |
+
categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
|
| 40 |
+
input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True)
|
| 41 |
|
| 42 |
+
#make model to predict
|
| 43 |
+
prediction = model.predict(input_df_dummies.reindex(columns=X_train.columns, fill_value=0))
|
| 44 |
|
| 45 |
+
return jsonify({'Prediction':prediction[0]})
|
| 46 |
|
| 47 |
|
| 48 |
#defining endpoint for batch
|
|
|
|
| 55 |
input_data = pd.read_csv(file)
|
| 56 |
|
| 57 |
#convert the categorical to dummies
|
| 58 |
+
categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
|
| 59 |
+
input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True)
|
| 60 |
+
input_df_aligned =input_df_dummies.reindex(columns=X_train.columns, fill_value=0)
|
| 61 |
+
|
| 62 |
+
#predict
|
| 63 |
+
predictions = model.predict(input_df_aligned).tolist() # Predict and convert to list
|
| 64 |
+
product_id_list = input_data.Product_Id.tolist() # Convert to list
|
| 65 |
+
store_id_list = input_data.Store_Id.tolist() # Convert to list
|
| 66 |
+
|
| 67 |
+
# Create a list of dictionaries for the output
|
| 68 |
+
output_list = []
|
| 69 |
+
for i in range(len(product_id_list)):
|
| 70 |
+
output_list.append({
|
| 71 |
+
'Product_Id': product_id_list[i],
|
| 72 |
+
'Store_Id': store_id_list[i],
|
| 73 |
+
'Prediction': predictions[i]
|
| 74 |
+
})
|
| 75 |
+
|
| 76 |
+
return jsonify(output_list)
|
| 77 |
|
| 78 |
#run the flask app in debug mode
|
| 79 |
if __name__ == '__main__':
|
| 80 |
+
sales_forecast_api.run(debug=True)
|