gauravsahu1990 commited on
Commit
1c2785f
·
verified ·
1 Parent(s): 48bfd00

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -3,12 +3,18 @@ from flask import Flask, request, jsonify
3
  import joblib
4
  import numpy as np
5
  import logging
 
6
 
7
  # -----------------------
8
- # Setup logging
9
  # -----------------------
10
- logging.basicConfig(level=logging.INFO)
11
- logger = logging.getLogger(__name__)
 
 
 
 
 
12
 
13
  # -----------------------
14
  # Initialize Flask app
@@ -19,13 +25,15 @@ app = Flask("Store Capacity Predictor")
19
  pipeline = joblib.load("catbooster_model_v1_0.joblib")
20
 
21
  # -----------------------
22
- # Routes
23
  # -----------------------
24
  @app.get('/')
25
  def home():
26
  return "Welcome to the Store Capacity Prediction API"
27
 
28
- # ----------------------- SINGLE PREDICTION -----------------------
 
 
29
  @app.post('/v1/predict')
30
  def predict_capacity():
31
  try:
@@ -38,22 +46,24 @@ def predict_capacity():
38
  # Predict
39
  prediction = pipeline.predict(input_data).tolist()[0]
40
 
41
- # Sanitize and convert to integer
42
- if pd.isna(prediction) or np.isinf(prediction) or not np.isfinite(prediction):
43
  logger.warning("Single prediction invalid (%s), replacing with 0", prediction)
44
  prediction = 0
45
  else:
46
- prediction = int(np.clip(prediction, 0, 10000)) # Adjust max if needed
47
 
48
  logger.info("Single prediction output: %s", prediction)
49
 
50
  return jsonify({'Predicted_Capacity': prediction})
51
 
52
  except Exception as e:
53
- logger.error("Error in single prediction: %s", e)
54
  return jsonify({'error': str(e)}), 400
55
 
56
- # ----------------------- BATCH PREDICTION -----------------------
 
 
57
  @app.post('/v1/predict_batch')
58
  def predict_capacity_batch():
59
  try:
@@ -69,7 +79,7 @@ def predict_capacity_batch():
69
  clean_predictions = []
70
 
71
  for idx, p in enumerate(predictions):
72
- if pd.isna(p) or np.isinf(p) or not np.isfinite(p):
73
  logger.warning("Row %d prediction invalid (%s), replacing with 0", idx, p)
74
  clean_predictions.append(0)
75
  else:
@@ -86,7 +96,7 @@ def predict_capacity_batch():
86
  return output_df.to_html(index=False)
87
 
88
  except Exception as e:
89
- logger.error("Error in batch prediction: %s", e)
90
  return jsonify({"error": str(e)}), 400
91
 
92
  # -----------------------
 
3
  import joblib
4
  import numpy as np
5
  import logging
6
+ import sys
7
 
8
  # -----------------------
9
+ # Setup logger (stdout captured by HF Spaces)
10
  # -----------------------
11
+ logger = logging.getLogger("capacity_logger")
12
+ logger.setLevel(logging.INFO)
13
+ handler = logging.StreamHandler(sys.stdout) # important for HF Spaces
14
+ handler.setLevel(logging.INFO)
15
+ formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
16
+ handler.setFormatter(formatter)
17
+ logger.addHandler(handler)
18
 
19
  # -----------------------
20
  # Initialize Flask app
 
25
  pipeline = joblib.load("catbooster_model_v1_0.joblib")
26
 
27
  # -----------------------
28
+ # Home route
29
  # -----------------------
30
  @app.get('/')
31
  def home():
32
  return "Welcome to the Store Capacity Prediction API"
33
 
34
+ # -----------------------
35
+ # Single prediction
36
+ # -----------------------
37
  @app.post('/v1/predict')
38
  def predict_capacity():
39
  try:
 
46
  # Predict
47
  prediction = pipeline.predict(input_data).tolist()[0]
48
 
49
+ # Sanitize prediction
50
+ if not np.isfinite(prediction):
51
  logger.warning("Single prediction invalid (%s), replacing with 0", prediction)
52
  prediction = 0
53
  else:
54
+ prediction = int(np.clip(prediction, 0, 10000))
55
 
56
  logger.info("Single prediction output: %s", prediction)
57
 
58
  return jsonify({'Predicted_Capacity': prediction})
59
 
60
  except Exception as e:
61
+ logger.error("Error in single prediction: %s", e, exc_info=True)
62
  return jsonify({'error': str(e)}), 400
63
 
64
+ # -----------------------
65
+ # Batch prediction
66
+ # -----------------------
67
  @app.post('/v1/predict_batch')
68
  def predict_capacity_batch():
69
  try:
 
79
  clean_predictions = []
80
 
81
  for idx, p in enumerate(predictions):
82
+ if not np.isfinite(p):
83
  logger.warning("Row %d prediction invalid (%s), replacing with 0", idx, p)
84
  clean_predictions.append(0)
85
  else:
 
96
  return output_df.to_html(index=False)
97
 
98
  except Exception as e:
99
+ logger.error("Error in batch prediction: %s", e, exc_info=True)
100
  return jsonify({"error": str(e)}), 400
101
 
102
  # -----------------------