Upload folder using huggingface_hub
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ logger = logging.getLogger(__name__)
|
|
| 11 |
#initialise flask app
|
| 12 |
sales_forecast_api = Flask('Sales forecasting')
|
| 13 |
|
| 14 |
-
# load the model
|
| 15 |
try:
|
| 16 |
# Log current working directory and files
|
| 17 |
current_dir = os.getcwd()
|
|
@@ -19,14 +19,23 @@ try:
|
|
| 19 |
files_in_dir = os.listdir(current_dir)
|
| 20 |
logger.info(f"Files in current directory: {files_in_dir}")
|
| 21 |
|
| 22 |
-
model
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
except FileNotFoundError:
|
| 25 |
-
logger.error("Model file not found!")
|
| 26 |
-
model = None
|
|
|
|
| 27 |
except Exception as e:
|
| 28 |
-
logger.error(f"Error loading model: {e}")
|
| 29 |
-
model = None
|
|
|
|
| 30 |
|
| 31 |
|
| 32 |
#define home page
|
|
@@ -37,8 +46,8 @@ def home():
|
|
| 37 |
#define an endpoint for prediction
|
| 38 |
@sales_forecast_api.post('/v1/sales')
|
| 39 |
def sales_predict():
|
| 40 |
-
if model is None:
|
| 41 |
-
return jsonify({"error": "Model not loaded"}), 500
|
| 42 |
|
| 43 |
#get data from json request
|
| 44 |
sales_data = request.get_json()
|
|
@@ -62,8 +71,11 @@ def sales_predict():
|
|
| 62 |
categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
|
| 63 |
input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True)
|
| 64 |
|
|
|
|
|
|
|
|
|
|
| 65 |
#make model to predict
|
| 66 |
-
prediction = model.predict(
|
| 67 |
|
| 68 |
return jsonify({'Prediction':prediction[0]})
|
| 69 |
|
|
@@ -72,8 +84,8 @@ def sales_predict():
|
|
| 72 |
@sales_forecast_api.post('/v1/salesbatch')
|
| 73 |
|
| 74 |
def sales_batch_predict():
|
| 75 |
-
if model is None:
|
| 76 |
-
return jsonify({"error": "Model not loaded"}), 500
|
| 77 |
|
| 78 |
#get the file from the request
|
| 79 |
file = request.files['file']
|
|
@@ -83,7 +95,9 @@ def sales_batch_predict():
|
|
| 83 |
#convert the categorical to dummies
|
| 84 |
categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
|
| 85 |
input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True)
|
| 86 |
-
|
|
|
|
|
|
|
| 87 |
|
| 88 |
#predict
|
| 89 |
predictions = model.predict(input_df_aligned).tolist() # Predict and convert to list
|
|
|
|
| 11 |
#initialise flask app
|
| 12 |
sales_forecast_api = Flask('Sales forecasting')
|
| 13 |
|
| 14 |
+
# load the model and training columns
|
| 15 |
try:
|
| 16 |
# Log current working directory and files
|
| 17 |
current_dir = os.getcwd()
|
|
|
|
| 19 |
files_in_dir = os.listdir(current_dir)
|
| 20 |
logger.info(f"Files in current directory: {files_in_dir}")
|
| 21 |
|
| 22 |
+
# Assuming model and columns are saved as a dictionary using joblib
|
| 23 |
+
model_and_columns_path = 'sales_forecast_model_and_columns.joblib' # Update path if needed
|
| 24 |
+
loaded_object = joblib.load(model_and_columns_path)
|
| 25 |
+
|
| 26 |
+
model = loaded_object['model']
|
| 27 |
+
training_columns = loaded_object['columns']
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
logger.info("Model and training columns loaded successfully.")
|
| 31 |
except FileNotFoundError:
|
| 32 |
+
logger.error(f"Model and training columns file not found at {model_and_columns_path}!")
|
| 33 |
+
model = None
|
| 34 |
+
training_columns = None # Ensure training_columns is also None if file not found
|
| 35 |
except Exception as e:
|
| 36 |
+
logger.error(f"Error loading model or training columns: {e}")
|
| 37 |
+
model = None
|
| 38 |
+
training_columns = None
|
| 39 |
|
| 40 |
|
| 41 |
#define home page
|
|
|
|
| 46 |
#define an endpoint for prediction
|
| 47 |
@sales_forecast_api.post('/v1/sales')
|
| 48 |
def sales_predict():
|
| 49 |
+
if model is None or training_columns is None:
|
| 50 |
+
return jsonify({"error": "Model or training columns not loaded"}), 500
|
| 51 |
|
| 52 |
#get data from json request
|
| 53 |
sales_data = request.get_json()
|
|
|
|
| 71 |
categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
|
| 72 |
input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True)
|
| 73 |
|
| 74 |
+
# Reindex input_df_dummies to match the columns of X_train used during training
|
| 75 |
+
input_df_aligned = input_df_dummies.reindex(columns=training_columns, fill_value=0)
|
| 76 |
+
|
| 77 |
#make model to predict
|
| 78 |
+
prediction = model.predict(input_df_aligned)
|
| 79 |
|
| 80 |
return jsonify({'Prediction':prediction[0]})
|
| 81 |
|
|
|
|
| 84 |
@sales_forecast_api.post('/v1/salesbatch')
|
| 85 |
|
| 86 |
def sales_batch_predict():
|
| 87 |
+
if model is None or training_columns is None:
|
| 88 |
+
return jsonify({"error": "Model or training columns not loaded"}), 500
|
| 89 |
|
| 90 |
#get the file from the request
|
| 91 |
file = request.files['file']
|
|
|
|
| 95 |
#convert the categorical to dummies
|
| 96 |
categorical_columns_for_dummies = ['Product_Sugar_Content','Product_Type','Store_Size','Store_Location_City_Type','Store_Type']
|
| 97 |
input_df_dummies = pd.get_dummies(input_data, columns=categorical_columns_for_dummies, drop_first=True)
|
| 98 |
+
|
| 99 |
+
# Reindex input_df_dummies to match the columns of X_train used during training
|
| 100 |
+
input_df_aligned =input_df_dummies.reindex(columns=training_columns, fill_value=0)
|
| 101 |
|
| 102 |
#predict
|
| 103 |
predictions = model.predict(input_df_aligned).tolist() # Predict and convert to list
|