BlueSR commited on
Commit
66609d0
·
verified ·
1 Parent(s): 6af18bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +49 -7
app.py CHANGED
@@ -6,8 +6,10 @@ import gradio as gr
6
  import numpy as np
7
  import pickle
8
  import os
9
- import joblib
10
  from PIL import Image
 
 
 
11
 
12
  # Constants
13
  NUM_CLASSES = 4
@@ -37,11 +39,8 @@ def load_cnn_model():
37
 
38
  # Load the Random Forest model
39
  def load_rf_model():
40
- try:
41
- return joblib.load("rf_model.pkl")
42
- except Exception as e:
43
- print(f"Error loading RF model: {e}")
44
- return None
45
 
46
  # Preprocess image for CNN
47
  def preprocess_image_cnn(image):
@@ -105,6 +104,46 @@ def predict(img, model_choice, cnn_model, rf_model):
105
  except Exception as e:
106
  return f"Error: {str(e)}", "An error occurred during prediction."
107
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
108
  # Load models
109
  cnn_model = load_cnn_model()
110
  rf_model = load_rf_model()
@@ -134,4 +173,7 @@ with gr.Blocks() as demo:
134
  gr.Markdown("3. Click 'Classify' to get the prediction")
135
 
136
  # Launch the app
137
- demo.launch()
 
 
 
 
6
  import numpy as np
7
  import pickle
8
  import os
 
9
  from PIL import Image
10
+ import joblib
11
+ import json
12
+ from sklearn.tree import export_text
13
 
14
  # Constants
15
  NUM_CLASSES = 4
 
39
 
40
  # Load the Random Forest model
41
  def load_rf_model():
42
+ with open("rf_model.pkl", "rb") as f:
43
+ return pickle.load(f)
 
 
 
44
 
45
  # Preprocess image for CNN
46
  def preprocess_image_cnn(image):
 
104
  except Exception as e:
105
  return f"Error: {str(e)}", "An error occurred during prediction."
106
 
107
+ # Convert Random Forest to a safer JSON format
108
+ def convert_rf_to_safe_format():
109
+ """Convert Random Forest to a safer JSON format"""
110
+
111
+ # Load the existing model
112
+ rf_model = joblib.load("models/rf_model.pkl")
113
+
114
+ # Extract model parameters
115
+ model_params = {
116
+ 'n_estimators': rf_model.n_estimators,
117
+ 'max_depth': rf_model.max_depth,
118
+ 'min_samples_split': rf_model.min_samples_split,
119
+ 'random_state': rf_model.random_state,
120
+ 'classes': rf_model.classes_.tolist(),
121
+ 'n_features': rf_model.n_features_in_,
122
+ 'feature_importances': rf_model.feature_importances_.tolist()
123
+ }
124
+
125
+ # Save model parameters
126
+ with open("rf_model_params.json", "w") as f:
127
+ json.dump(model_params, f)
128
+
129
+ # Save individual tree parameters (simplified approach)
130
+ trees_data = []
131
+ for i, tree in enumerate(rf_model.estimators_):
132
+ tree_data = {
133
+ 'tree_id': i,
134
+ 'feature': tree.tree_.feature.tolist(),
135
+ 'threshold': tree.tree_.threshold.tolist(),
136
+ 'children_left': tree.tree_.children_left.tolist(),
137
+ 'children_right': tree.tree_.children_right.tolist(),
138
+ 'value': tree.tree_.value.tolist()
139
+ }
140
+ trees_data.append(tree_data)
141
+
142
+ with open("rf_trees_data.json", "w") as f:
143
+ json.dump(trees_data, f)
144
+
145
+ print("Model converted to safe JSON format")
146
+
147
  # Load models
148
  cnn_model = load_cnn_model()
149
  rf_model = load_rf_model()
 
173
  gr.Markdown("3. Click 'Classify' to get the prediction")
174
 
175
  # Launch the app
176
+ demo.launch()
177
+
178
+ if __name__ == "__main__":
179
+ convert_rf_to_safe_format()