saba000farahani
commited on
Commit
•
1770619
1
Parent(s):
0781dee
Update app.py
Browse files
app.py
CHANGED
@@ -11,7 +11,45 @@ import matplotlib.pyplot as plt
|
|
11 |
import os
|
12 |
import sklearn
|
13 |
|
14 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def predict_and_plot(velocity, temperature, precipitation, humidity):
|
17 |
try:
|
@@ -90,9 +128,6 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
90 |
plt.legend()
|
91 |
plt.grid(True)
|
92 |
|
93 |
-
# Save the plot to a file
|
94 |
-
plt.savefig('plot.png')
|
95 |
-
|
96 |
# Return the plot and predictions
|
97 |
return plt, [f"{val * 100:.2f}%" for val in contamination_levels[0]], [f"{val:.2f}" for val in cleaning_times]
|
98 |
|
|
|
11 |
import os
|
12 |
import sklearn
|
13 |
|
14 |
+
# Display library versions
|
15 |
+
print(f"Gradio version: {gr.__version__}")
|
16 |
+
print(f"NumPy version: {np.__version__}")
|
17 |
+
print(f"Scikit-learn version: {sklearn.__version__}")
|
18 |
+
print(f"Joblib version: {joblib.__version__}")
|
19 |
+
print(f"TensorFlow version: {tf.__version__}")
|
20 |
+
print(f"Pandas version: {pd.__version__}")
|
21 |
+
|
22 |
+
# Directory paths for the saved models
|
23 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
24 |
+
scaler_path = os.path.join(script_dir, 'toolkit', 'scaler_X.json')
|
25 |
+
rf_model_path = os.path.join(script_dir, 'toolkit', 'rf_model.joblib')
|
26 |
+
mlp_model_path = os.path.join(script_dir, 'toolkit', 'mlp_model.keras')
|
27 |
+
meta_model_path = os.path.join(script_dir, 'toolkit', 'meta_model.joblib')
|
28 |
+
image_path = os.path.join(script_dir, 'toolkit', 'car.png')
|
29 |
+
|
30 |
+
# Load the scaler and models
|
31 |
+
try:
|
32 |
+
# Load the scaler
|
33 |
+
with open(scaler_path, 'r') as f:
|
34 |
+
scaler_params = json.load(f)
|
35 |
+
scaler_X = MinMaxScaler()
|
36 |
+
scaler_X.scale_ = np.array(scaler_params["scale_"])
|
37 |
+
scaler_X.min_ = np.array(scaler_params["min_"])
|
38 |
+
scaler_X.data_min_ = np.array(scaler_params["data_min_"])
|
39 |
+
scaler_X.data_max_ = np.array(scaler_params["data_max_"])
|
40 |
+
scaler_X.data_range_ = np.array(scaler_params["data_range_"])
|
41 |
+
scaler_X.n_features_in_ = scaler_params["n_features_in_"]
|
42 |
+
scaler_X.feature_names_in_ = np.array(scaler_params["feature_names_in_"])
|
43 |
+
|
44 |
+
# Load the models
|
45 |
+
loaded_rf_model = load(rf_model_path)
|
46 |
+
print("Random Forest model loaded successfully.")
|
47 |
+
loaded_mlp_model = load_model(mlp_model_path)
|
48 |
+
print("MLP model loaded successfully.")
|
49 |
+
loaded_meta_model = load(meta_model_path)
|
50 |
+
print("Meta model loaded successfully.")
|
51 |
+
except Exception as e:
|
52 |
+
print(f"Error loading models or scaler: {e}")
|
53 |
|
54 |
def predict_and_plot(velocity, temperature, precipitation, humidity):
|
55 |
try:
|
|
|
128 |
plt.legend()
|
129 |
plt.grid(True)
|
130 |
|
|
|
|
|
|
|
131 |
# Return the plot and predictions
|
132 |
return plt, [f"{val * 100:.2f}%" for val in contamination_levels[0]], [f"{val:.2f}" for val in cleaning_times]
|
133 |
|