andromeda01111 commited on
Commit
e34e966
Β·
verified Β·
1 Parent(s): 84c111e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -12
app.py CHANGED
@@ -9,21 +9,24 @@ from sklearn.preprocessing import StandardScaler
9
  import joblib
10
  import os
11
 
12
- # Set device
 
 
 
13
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
14
 
15
  # Load trained ViT model (PyTorch)
16
- vit_model = models.vit_b_16(pretrained=False)
17
  vit_model.heads = torch.nn.Linear(in_features=768, out_features=2) # Binary classification
18
 
19
- # Load ViT model weights
20
  vit_model_path = "vit_bc.pth"
21
  if os.path.exists(vit_model_path):
22
  vit_model.load_state_dict(torch.load(vit_model_path, map_location=device))
23
  vit_model.to(device)
24
  vit_model.eval()
25
 
26
- # Define ViT image transformations
27
  transform = transforms.Compose([
28
  transforms.Resize((224, 224)),
29
  transforms.ToTensor(),
@@ -35,7 +38,12 @@ class_names = ["Benign", "Malignant"]
35
 
36
  # Load trained Neural Network model (TensorFlow/Keras)
37
  nn_model_path = "my_NN_BC_model.keras"
38
- nn_model = tf.keras.models.load_model(nn_model_path) if os.path.exists(nn_model_path) else None
 
 
 
 
 
39
 
40
  # Load scaler for feature normalization
41
  scaler_path = "nn_bc_scaler.pkl"
@@ -97,13 +105,11 @@ with gr.Blocks() as demo:
97
 
98
  feature_inputs = [gr.Number(label=feature) for feature in feature_names]
99
 
100
- # Dynamically arrange feature inputs into rows of 3 columns
101
  with gr.Row():
102
- for i in range(0, len(feature_inputs), 3):
103
- with gr.Column():
104
- for j in range(3):
105
- if i + j < len(feature_inputs):
106
- feature_inputs[i + j].render()
107
 
108
  # Example buttons
109
  def fill_example(example):
@@ -116,7 +122,7 @@ with gr.Blocks() as demo:
116
 
117
  output_text = gr.Textbox(label="πŸ” Model Prediction", interactive=False)
118
 
119
- # Logic to toggle inputs based on model selection
120
  def toggle_inputs(choice):
121
  return gr.update(visible=(choice == "ViT")), gr.update(visible=(choice == "Neural Network"))
122
 
 
9
  import joblib
10
  import os
11
 
12
+ # Disable GPU for TensorFlow to avoid CUDA conflicts
13
+ os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
14
+
15
+ # Set PyTorch device
16
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
 
18
  # Load trained ViT model (PyTorch)
19
+ vit_model = models.vit_b_16(weights="DEFAULT") # Fixed deprecated 'pretrained'
20
  vit_model.heads = torch.nn.Linear(in_features=768, out_features=2) # Binary classification
21
 
22
+ # Load ViT model weights (if available)
23
  vit_model_path = "vit_bc.pth"
24
  if os.path.exists(vit_model_path):
25
  vit_model.load_state_dict(torch.load(vit_model_path, map_location=device))
26
  vit_model.to(device)
27
  vit_model.eval()
28
 
29
+ # Define image transformations for ViT
30
  transform = transforms.Compose([
31
  transforms.Resize((224, 224)),
32
  transforms.ToTensor(),
 
38
 
39
  # Load trained Neural Network model (TensorFlow/Keras)
40
  nn_model_path = "my_NN_BC_model.keras"
41
+ nn_model = None
42
+ if os.path.exists(nn_model_path):
43
+ try:
44
+ nn_model = tf.keras.models.load_model(nn_model_path)
45
+ except Exception as e:
46
+ print(f"Error loading NN model: {e}")
47
 
48
  # Load scaler for feature normalization
49
  scaler_path = "nn_bc_scaler.pkl"
 
105
 
106
  feature_inputs = [gr.Number(label=feature) for feature in feature_names]
107
 
108
+ # Organizing feature inputs into rows of 3 columns
109
  with gr.Row():
110
+ with gr.Column():
111
+ for i in range(0, len(feature_inputs), 3):
112
+ gr.Row([feature_inputs[j] for j in range(i, min(i+3, len(feature_inputs)))])
 
 
113
 
114
  # Example buttons
115
  def fill_example(example):
 
122
 
123
  output_text = gr.Textbox(label="πŸ” Model Prediction", interactive=False)
124
 
125
+ # Toggle input fields based on model selection
126
  def toggle_inputs(choice):
127
  return gr.update(visible=(choice == "ViT")), gr.update(visible=(choice == "Neural Network"))
128