Varun6299 commited on
Commit
cb2ff2c
·
verified ·
1 Parent(s): 302e6bd

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. app.py +7 -19
app.py CHANGED
@@ -14,27 +14,18 @@ MODEL_PATH = "final_xgboost_pipeline.pkl"
14
  # --- Initialize Flask App ---
15
  app = Flask("SuperKart Sales Predictor")
16
 
17
- # --- Global Model Loading ---
18
  try:
19
  model = joblib.load(MODEL_PATH)
20
  print("Model loaded successfully.") # Add logging
21
  except Exception as e:
22
  print(f"Error loading model: {e}") # Log error
23
 
24
- # --- API Routes ---
25
-
26
  @app.get('/')
27
- def health_check():
28
- """
29
- Health check and information endpoint.
30
- """
31
  print("Home route accessed.") # Add logging
32
- return jsonify({
33
- "status": "OK",
34
- "model_location": MODEL_PATH,
35
- "model_status": "Loaded" if model else "Error: See logs",
36
- "api_version": "No Versioning"
37
- })
38
 
39
 
40
  @app.route("/predict", methods=['POST']) # The simple, unversioned route
@@ -64,15 +55,12 @@ def predict_sales():
64
  # A well-built ML pipeline should handle all feature transformations internally.
65
  # input_df['Store_Establishment_Year'] = input_df['Store_Establishment_Year'].astype(str)
66
 
67
- print(f"[{APP_NAME}] Input data prepared for pipeline.")
68
  # output variable removed as it was only used for the previous error
69
 
70
  except KeyError as ke:
71
- print(f"[{APP_NAME}] Missing required feature: {ke}")
72
  return jsonify({'error': f'Missing required feature: {ke}. Please check all features are present.'}), 400
73
 
74
  except Exception as e:
75
- print(f"[{APP_NAME}] Error converting data to DataFrame: {e}")
76
  return jsonify({'error': 'Error processing input data format', 'details': str(e)}), 400
77
 
78
 
@@ -85,7 +73,7 @@ def predict_sales():
85
  else:
86
  predicted_sales = prediction # Should ideally be a float
87
 
88
- print(f"[{APP_NAME}] Prediction result: {predicted_sales:.2f}")
89
 
90
  # --- Response ---
91
  # 3. FIX: Return the actual predicted value instead of the string message
@@ -96,13 +84,13 @@ def predict_sales():
96
 
97
  except Exception as e:
98
  # Catch any unexpected runtime errors
99
- print(f"[{APP_NAME}] An unexpected internal error occurred: {e}")
100
  print(traceback.format_exc())
101
  return jsonify({
102
  'error': 'Internal Server Error during prediction',
103
  'details': str(e)
104
  }), 500
105
-
106
 
107
  # --- Local Runner (Optional: Comment out for production WSGI) ---
108
  if __name__ == '__main__':
 
14
  # --- Initialize Flask App ---
15
  app = Flask("SuperKart Sales Predictor")
16
 
17
+ # Load the model
18
  try:
19
  model = joblib.load(MODEL_PATH)
20
  print("Model loaded successfully.") # Add logging
21
  except Exception as e:
22
  print(f"Error loading model: {e}") # Log error
23
 
24
+ # Define a route for the home page
 
25
  @app.get('/')
26
+ def home():
 
 
 
27
  print("Home route accessed.") # Add logging
28
+ return "Welcome to the SuperKart Store Product Sales Prediction API."
 
 
 
 
 
29
 
30
 
31
  @app.route("/predict", methods=['POST']) # The simple, unversioned route
 
55
  # A well-built ML pipeline should handle all feature transformations internally.
56
  # input_df['Store_Establishment_Year'] = input_df['Store_Establishment_Year'].astype(str)
57
 
 
58
  # output variable removed as it was only used for the previous error
59
 
60
  except KeyError as ke:
 
61
  return jsonify({'error': f'Missing required feature: {ke}. Please check all features are present.'}), 400
62
 
63
  except Exception as e:
 
64
  return jsonify({'error': 'Error processing input data format', 'details': str(e)}), 400
65
 
66
 
 
73
  else:
74
  predicted_sales = prediction # Should ideally be a float
75
 
76
+ print(f"Prediction result: {predicted_sales:.2f}")
77
 
78
  # --- Response ---
79
  # 3. FIX: Return the actual predicted value instead of the string message
 
84
 
85
  except Exception as e:
86
  # Catch any unexpected runtime errors
87
+ print(f"An unexpected internal error occurred: {e}")
88
  print(traceback.format_exc())
89
  return jsonify({
90
  'error': 'Internal Server Error during prediction',
91
  'details': str(e)
92
  }), 500
93
+
94
 
95
  # --- Local Runner (Optional: Comment out for production WSGI) ---
96
  if __name__ == '__main__':