CaxtonEmeraldS commited on
Commit
994a3de
·
verified ·
1 Parent(s): 4b653eb

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -22
app.py CHANGED
@@ -8,7 +8,7 @@ import re
8
 
9
  # Step 1: Unzip models only once
10
  unzip_dir = "unzipped_models"
11
- zip_file = "Models.zip" # Make sure this is the filename inside your Space repo
12
 
13
  if not os.path.exists(unzip_dir):
14
  print("Extracting model zip file...")
@@ -18,7 +18,7 @@ if not os.path.exists(unzip_dir):
18
 
19
  # Step 2: Parse folders to dynamically populate dropdowns
20
 
21
- model_root = os.path.join(unzip_dir, 'Models') # Adjust this if your ZIP structure is different
22
 
23
  activations = []
24
  seeds_dict = dict()
@@ -43,24 +43,16 @@ for act in os.listdir(model_root):
43
 
44
  activations = sorted(activations)
45
 
46
-
47
- # Step 3: Load linear models
48
- # linear_rgb_path = os.path.join(unzip_dir, "linear_models/linear_rgb.joblib")
49
- # linear_grey_path = os.path.join(unzip_dir, "linear_models/linear_grey.joblib")
50
-
51
- # linear_rgb = joblib.load(linear_rgb_path)
52
- # linear_grey = joblib.load(linear_grey_path)
53
-
54
- # Step 4: Prediction function
55
  def predict(r, g, b, activation, seed, neurons):
56
  try:
57
  X = np.array([[r, g, b]])
58
 
59
- # Linear predictions
60
  lin_pred_rgb = (1.9221 * r) - (1.3817 * g) + (1.4058 * b) - 0.1318
61
 
62
  # ANN prediction
63
- keras_path = os.path.join(unzip_dir, activation, seed, f"model_{neurons}.keras")
64
  if not os.path.exists(keras_path):
65
  raise FileNotFoundError(f"Model not found: {keras_path}")
66
 
@@ -70,16 +62,15 @@ def predict(r, g, b, activation, seed, neurons):
70
  return ann_pred, lin_pred_rgb
71
 
72
  except Exception as e:
73
- return f"Error: {str(e)}", "", ""
74
-
75
- # Dynamic components for UI
76
 
 
77
  def update_seeds(activation):
78
- return {"choices": seeds_dict[activation], "value": seeds_dict[activation][0]}
79
 
80
  def update_neurons(activation, seed):
81
  neurons = neurons_dict[(activation, seed)]
82
- return {"choices": neurons, "value": neurons[0]}
83
 
84
  # Gradio Interface
85
  with gr.Blocks() as demo:
@@ -92,9 +83,9 @@ with gr.Blocks() as demo:
92
  b = gr.Number(label="B")
93
 
94
  with gr.Row():
95
- activation = gr.Dropdown(choices=activations, label="Activation Function")
96
- seed = gr.Dropdown(label="Seed")
97
- neurons = gr.Dropdown(label="Neurons")
98
 
99
  activation.change(update_seeds, inputs=[activation], outputs=[seed])
100
  seed.change(update_neurons, inputs=[activation, seed], outputs=[neurons])
@@ -105,7 +96,6 @@ with gr.Blocks() as demo:
105
  with gr.Row():
106
  ann_output = gr.Text(label="ANN Model Prediction")
107
  lin_rgb_output = gr.Text(label="Linear RGB Prediction")
108
- # lin_grey_output = gr.Text(label="Linear Grey Prediction")
109
 
110
  btn.click(
111
  fn=predict,
 
8
 
9
  # Step 1: Unzip models only once
10
  unzip_dir = "unzipped_models"
11
+ zip_file = "Models.zip" # Ensure this matches exactly the file name uploaded in your Space repo
12
 
13
  if not os.path.exists(unzip_dir):
14
  print("Extracting model zip file...")
 
18
 
19
  # Step 2: Parse folders to dynamically populate dropdowns
20
 
21
+ model_root = os.path.join(unzip_dir, 'Models') # Adjust if ZIP structure is different
22
 
23
  activations = []
24
  seeds_dict = dict()
 
43
 
44
  activations = sorted(activations)
45
 
46
+ # Step 3: Prediction function
 
 
 
 
 
 
 
 
47
  def predict(r, g, b, activation, seed, neurons):
48
  try:
49
  X = np.array([[r, g, b]])
50
 
51
+ # Linear prediction (you can replace this with your actual linear model)
52
  lin_pred_rgb = (1.9221 * r) - (1.3817 * g) + (1.4058 * b) - 0.1318
53
 
54
  # ANN prediction
55
+ keras_path = os.path.join(model_root, activation, seed, f"model_{neurons}.keras")
56
  if not os.path.exists(keras_path):
57
  raise FileNotFoundError(f"Model not found: {keras_path}")
58
 
 
62
  return ann_pred, lin_pred_rgb
63
 
64
  except Exception as e:
65
+ return f"Error: {str(e)}", ""
 
 
66
 
67
+ # Step 4: Dynamic UI update functions (Gradio 4.x compliant)
68
  def update_seeds(activation):
69
+ return gr.update(choices=seeds_dict[activation], value=seeds_dict[activation][0])
70
 
71
  def update_neurons(activation, seed):
72
  neurons = neurons_dict[(activation, seed)]
73
+ return gr.update(choices=neurons, value=neurons[0])
74
 
75
  # Gradio Interface
76
  with gr.Blocks() as demo:
 
83
  b = gr.Number(label="B")
84
 
85
  with gr.Row():
86
+ activation = gr.Dropdown(choices=activations, label="Activation Function", interactive=True)
87
+ seed = gr.Dropdown(label="Seed", interactive=True)
88
+ neurons = gr.Dropdown(label="Neurons", interactive=True)
89
 
90
  activation.change(update_seeds, inputs=[activation], outputs=[seed])
91
  seed.change(update_neurons, inputs=[activation, seed], outputs=[neurons])
 
96
  with gr.Row():
97
  ann_output = gr.Text(label="ANN Model Prediction")
98
  lin_rgb_output = gr.Text(label="Linear RGB Prediction")
 
99
 
100
  btn.click(
101
  fn=predict,