sid22669 commited on
Commit
9fdad88
·
verified ·
1 Parent(s): fd72276

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -12
app.py CHANGED
@@ -1,14 +1,19 @@
 
1
  from flask import Flask, render_template, request
2
  import pickle
3
 
 
4
  app = Flask(__name__)
5
 
6
- # Load the trained model
7
- # with open('random_forest_model.pkl', 'rb') as file:
8
- # model = pickle.load(file)
9
 
10
- import logging
11
- logging.basicConfig(level=logging.DEBUG)
 
 
 
 
 
12
 
13
  @app.route('/')
14
  def index():
@@ -17,21 +22,18 @@ def index():
17
 
18
  @app.route('/predict', methods=['POST'])
19
  def predict():
 
20
  try:
21
- # Log incoming form data
22
- app.logger.debug(f"Form data: {request.form}")
23
  precipitation = float(request.form['precipitation'])
24
  temp_max = float(request.form['temp_max'])
25
  temp_min = float(request.form['temp_min'])
26
  wind = float(request.form['wind'])
27
-
28
- # Log model prediction attempt
29
- app.logger.debug("Making a prediction.")
30
  prediction = model.predict([[precipitation, temp_max, temp_min, wind]])
31
  return render_template('result.html', prediction=prediction[0])
32
  except Exception as e:
33
- app.logger.error(f"Error during prediction: {e}")
34
- return "An error occurred. Check logs."
 
35
 
36
 
37
  if __name__ == '__main__':
 
1
+ import logging
2
  from flask import Flask, render_template, request
3
  import pickle
4
 
5
+ logging.basicConfig(level=logging.DEBUG)
6
  app = Flask(__name__)
7
 
8
+ app.logger.debug("Starting Flask app initialization.")
 
 
9
 
10
+ # Load the model
11
+ try:
12
+ with open("random_forest_model.pkl", "rb") as file:
13
+ model = pickle.load(file)
14
+ app.logger.debug("Model loaded successfully.")
15
+ except Exception as e:
16
+ app.logger.error(f"Error loading model: {e}")
17
 
18
  @app.route('/')
19
  def index():
 
22
 
23
  @app.route('/predict', methods=['POST'])
24
  def predict():
25
+ app.logger.debug("Predict route accessed.")
26
  try:
 
 
27
  precipitation = float(request.form['precipitation'])
28
  temp_max = float(request.form['temp_max'])
29
  temp_min = float(request.form['temp_min'])
30
  wind = float(request.form['wind'])
 
 
 
31
  prediction = model.predict([[precipitation, temp_max, temp_min, wind]])
32
  return render_template('result.html', prediction=prediction[0])
33
  except Exception as e:
34
+ app.logger.error(f"Error in predict route: {e}")
35
+ return "An error occurred during prediction."
36
+
37
 
38
 
39
  if __name__ == '__main__':