HacksterT commited on
Commit
87b47e1
·
verified ·
1 Parent(s): 0fd527a

Force rebuild: Updated requirements.txt to trigger container rebuild

Browse files
Files changed (1) hide show
  1. app.py +24 -33
app.py CHANGED
@@ -13,22 +13,18 @@ app = Flask(__name__)
13
  # Enable CORS for all routes and origins
14
  CORS(app)
15
 
16
- # Global variables for model components
17
- model_pipeline = None
18
- model_metadata = None
19
 
20
- def load_model_components():
21
- """Load the serialized model components"""
22
- global model_pipeline, model_metadata
23
 
24
  try:
25
- # Load the complete pipeline
26
- model_pipeline = joblib.load('models/superkart_pipeline_20250804_140625.pkl')
27
 
28
- # Load model metadata
29
- model_metadata = joblib.load('models/superkart_metadata_20250804_140625.pkl')
30
-
31
- print("Model components loaded successfully")
32
  return True
33
 
34
  except Exception as e:
@@ -41,7 +37,7 @@ def home():
41
  return jsonify({
42
  'status': 'healthy',
43
  'message': 'SuperKart Sales Forecasting API',
44
- 'model_version': model_metadata['version'] if model_metadata else 'unknown',
45
  'timestamp': datetime.now().isoformat()
46
  })
47
 
@@ -50,7 +46,7 @@ def predict_sales():
50
  """Main prediction endpoint"""
51
  try:
52
  # Check if model is loaded
53
- if model_pipeline is None:
54
  return jsonify({
55
  'error': 'Model not loaded',
56
  'status': 'failed'
@@ -68,16 +64,15 @@ def predict_sales():
68
  # Convert to DataFrame
69
  input_df = pd.DataFrame([data])
70
 
71
- # Make prediction using the pipeline
72
- prediction = model_pipeline.predict_sales(input_df)
73
 
74
  # Return prediction
75
  return jsonify({
76
  'prediction': float(prediction[0]),
77
  'prediction_formatted': f"${prediction[0]:.2f}",
78
  'status': 'success',
79
- 'timestamp': datetime.now().isoformat(),
80
- 'model_version': model_metadata['version']
81
  })
82
 
83
  except Exception as e:
@@ -89,19 +84,16 @@ def predict_sales():
89
 
90
  @app.route('/model-info', methods=['GET'])
91
  def model_info():
92
- """Get model information and performance metrics"""
93
- if model_metadata is None:
94
  return jsonify({
95
- 'error': 'Model metadata not available',
96
  'status': 'failed'
97
  }), 500
98
 
99
  return jsonify({
100
- 'model_name': model_metadata['model_name'],
101
- 'algorithm': model_metadata['algorithm'],
102
- 'version': model_metadata['version'],
103
- 'performance_metrics': model_metadata['performance_metrics'],
104
- 'created_date': model_metadata['created_date'],
105
  'status': 'success'
106
  })
107
 
@@ -109,7 +101,7 @@ def model_info():
109
  def predict_batch():
110
  """Batch prediction endpoint for multiple records"""
111
  try:
112
- if model_pipeline is None:
113
  return jsonify({
114
  'error': 'Model not loaded',
115
  'status': 'failed'
@@ -127,8 +119,8 @@ def predict_batch():
127
  # Convert to DataFrame
128
  input_df = pd.DataFrame(data)
129
 
130
- # Make predictions
131
- predictions = model_pipeline.predict_sales(input_df)
132
 
133
  # Format results
134
  results = []
@@ -143,8 +135,7 @@ def predict_batch():
143
  'predictions': results,
144
  'total_records': len(results),
145
  'status': 'success',
146
- 'timestamp': datetime.now().isoformat(),
147
- 'model_version': model_metadata['version']
148
  })
149
 
150
  except Exception as e:
@@ -155,8 +146,8 @@ def predict_batch():
155
  }), 500
156
 
157
  if __name__ == '__main__':
158
- # Load model components on startup
159
- if load_model_components():
160
  print("Starting SuperKart Sales Forecasting API...")
161
  app.run(host='0.0.0.0', port=5000, debug=True)
162
  else:
 
13
  # Enable CORS for all routes and origins
14
  CORS(app)
15
 
16
+ # Global variable for model
17
+ model = None
 
18
 
19
+ def load_model():
20
+ """Load the serialized model"""
21
+ global model
22
 
23
  try:
24
+ # Load the complete model (simplified approach)
25
+ model = joblib.load('models/superkart_pipeline_20250804_140625.pkl')
26
 
27
+ print("Model loaded successfully")
 
 
 
28
  return True
29
 
30
  except Exception as e:
 
37
  return jsonify({
38
  'status': 'healthy',
39
  'message': 'SuperKart Sales Forecasting API',
40
+ 'model_loaded': model is not None,
41
  'timestamp': datetime.now().isoformat()
42
  })
43
 
 
46
  """Main prediction endpoint"""
47
  try:
48
  # Check if model is loaded
49
+ if model is None:
50
  return jsonify({
51
  'error': 'Model not loaded',
52
  'status': 'failed'
 
64
  # Convert to DataFrame
65
  input_df = pd.DataFrame([data])
66
 
67
+ # Make prediction using the model (simplified)
68
+ prediction = model.predict(input_df)
69
 
70
  # Return prediction
71
  return jsonify({
72
  'prediction': float(prediction[0]),
73
  'prediction_formatted': f"${prediction[0]:.2f}",
74
  'status': 'success',
75
+ 'timestamp': datetime.now().isoformat()
 
76
  })
77
 
78
  except Exception as e:
 
84
 
85
  @app.route('/model-info', methods=['GET'])
86
  def model_info():
87
+ """Get model information"""
88
+ if model is None:
89
  return jsonify({
90
+ 'error': 'Model not available',
91
  'status': 'failed'
92
  }), 500
93
 
94
  return jsonify({
95
+ 'model_type': str(type(model)),
96
+ 'model_loaded': True,
 
 
 
97
  'status': 'success'
98
  })
99
 
 
101
  def predict_batch():
102
  """Batch prediction endpoint for multiple records"""
103
  try:
104
+ if model is None:
105
  return jsonify({
106
  'error': 'Model not loaded',
107
  'status': 'failed'
 
119
  # Convert to DataFrame
120
  input_df = pd.DataFrame(data)
121
 
122
+ # Make predictions (simplified)
123
+ predictions = model.predict(input_df)
124
 
125
  # Format results
126
  results = []
 
135
  'predictions': results,
136
  'total_records': len(results),
137
  'status': 'success',
138
+ 'timestamp': datetime.now().isoformat()
 
139
  })
140
 
141
  except Exception as e:
 
146
  }), 500
147
 
148
  if __name__ == '__main__':
149
+ # Load model on startup
150
+ if load_model():
151
  print("Starting SuperKart Sales Forecasting API...")
152
  app.run(host='0.0.0.0', port=5000, debug=True)
153
  else: