saba000farahani commited on
Commit
5d967db
·
verified ·
1 Parent(s): fd1ef9f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -16
app.py CHANGED
@@ -1,25 +1,40 @@
 
1
  import gradio as gr
2
  import numpy as np
3
  import joblib
4
  from tensorflow.keras.models import load_model
5
 
6
  # Paths to your saved models and scaler
7
- scaler_path = '/Env_ContaminationLevel/blob/main/scaler_X.pkl'
8
- rf_model_path = '/Env_ContaminationLevel/blob/main/rf_model.pkl'
9
- mlp_model_path = '/Env_ContaminationLevel/blob/main/mlp_model.h5'
10
- meta_model_path = '/Env_ContaminationLevel/blob/main/meta_model.pkl'
11
- try:
12
- # Load the scaler and models
13
- scaler_X = joblib.load(scaler_path)
14
- loaded_rf_model = joblib.load(rf_model_path)
15
- loaded_mlp_model = load_model(mlp_model_path)
16
- loaded_meta_model = joblib.load(meta_model_path)
17
- print("Models and scaler loaded successfully.")
18
- except Exception as e:
19
- print(f"Error loading models or scaler: {e}")
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  def predict_new_values(new_input_data):
22
  try:
 
 
 
23
  # Ensure the new input data is in the correct format
24
  print(f"Raw Input Data: {new_input_data}")
25
  new_input_data = np.array(new_input_data).reshape(1, -1)
@@ -43,6 +58,8 @@ def predict_new_values(new_input_data):
43
  return loaded_meta_predictions_new[0]
44
  except Exception as e:
45
  print(f"Error in prediction: {e}")
 
 
46
  return ["Error", "Error", "Error", "Error", "Error", "Error"]
47
 
48
  def gradio_interface(velocity, temperature, precipitation, humidity):
@@ -61,12 +78,14 @@ def gradio_interface(velocity, temperature, precipitation, humidity):
61
  ]
62
  except Exception as e:
63
  print(f"Error in Gradio interface: {e}")
 
 
64
  return ["Error", "Error", "Error", "Error", "Error", "Error"]
65
 
66
  inputs = [
67
- gr.Slider(minimum=0, maximum=100, value=50,step=0.5, label="Velocity (mph)"),
68
- gr.Slider(minimum=-30, maximum=50, value=0,step=0.5, label="Temperature (°C)"),
69
- gr.Slider(minimum=0, maximum=10, value=0,step=0.01, label="Precipitation (inch)"),
70
  gr.Slider(minimum=0, maximum=100, value=50, label="Humidity (%)")
71
  ]
72
 
 
1
+ import os
2
  import gradio as gr
3
  import numpy as np
4
  import joblib
5
  from tensorflow.keras.models import load_model
6
 
7
  # Paths to your saved models and scaler
8
+ scaler_path = 'scaler_X.pkl'
9
+ rf_model_path = 'rf_model.pkl'
10
+ mlp_model_path = 'mlp_model.h5'
11
+ meta_model_path = 'meta_model.pkl'
12
+
13
+ def load_models_and_scaler():
14
+ try:
15
+ # Load the scaler and models
16
+ scaler_X = joblib.load(scaler_path)
17
+ print("Scaler loaded successfully.")
18
+ loaded_rf_model = joblib.load(rf_model_path)
19
+ print("Random Forest model loaded successfully.")
20
+ loaded_mlp_model = load_model(mlp_model_path)
21
+ print("MLP model loaded successfully.")
22
+ loaded_meta_model = joblib.load(meta_model_path)
23
+ print("Meta model loaded successfully.")
24
+ return scaler_X, loaded_rf_model, loaded_mlp_model, loaded_meta_model
25
+ except Exception as e:
26
+ print(f"Error loading models or scaler: {e}")
27
+ import traceback
28
+ traceback.print_exc()
29
+ return None, None, None, None
30
+
31
+ scaler_X, loaded_rf_model, loaded_mlp_model, loaded_meta_model = load_models_and_scaler()
32
 
33
  def predict_new_values(new_input_data):
34
  try:
35
+ if not all([scaler_X, loaded_rf_model, loaded_mlp_model, loaded_meta_model]):
36
+ raise Exception("Models or scaler not loaded properly.")
37
+
38
  # Ensure the new input data is in the correct format
39
  print(f"Raw Input Data: {new_input_data}")
40
  new_input_data = np.array(new_input_data).reshape(1, -1)
 
58
  return loaded_meta_predictions_new[0]
59
  except Exception as e:
60
  print(f"Error in prediction: {e}")
61
+ import traceback
62
+ traceback.print_exc()
63
  return ["Error", "Error", "Error", "Error", "Error", "Error"]
64
 
65
  def gradio_interface(velocity, temperature, precipitation, humidity):
 
78
  ]
79
  except Exception as e:
80
  print(f"Error in Gradio interface: {e}")
81
+ import traceback
82
+ traceback.print_exc()
83
  return ["Error", "Error", "Error", "Error", "Error", "Error"]
84
 
85
  inputs = [
86
+ gr.Slider(minimum=0, maximum=100, value=50, step=0.5, label="Velocity (mph)"),
87
+ gr.Slider(minimum=-30, maximum=50, value=0, step=0.5, label="Temperature (°C)"),
88
+ gr.Slider(minimum=0, maximum=10, value=0, step=0.01, label="Precipitation (inch)"),
89
  gr.Slider(minimum=0, maximum=100, value=50, label="Humidity (%)")
90
  ]
91