Update app.py
Browse files
app.py
CHANGED
|
@@ -4,67 +4,110 @@ import joblib
|
|
| 4 |
import numpy as np
|
| 5 |
import zipfile
|
| 6 |
import os
|
| 7 |
-
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
repo_id = "CaxtonEmeraldS/CholesterolConcentrationPredictor" # Replace with your actual repo name
|
| 11 |
-
|
| 12 |
-
# Unzip models only once
|
| 13 |
unzip_dir = "unzipped_models"
|
|
|
|
|
|
|
| 14 |
if not os.path.exists(unzip_dir):
|
| 15 |
-
print("
|
| 16 |
-
|
| 17 |
-
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 18 |
zip_ref.extractall(unzip_dir)
|
| 19 |
print("Extraction complete.")
|
| 20 |
|
| 21 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# linear_rgb_path = os.path.join(unzip_dir, "linear_models/linear_rgb.joblib")
|
| 23 |
# linear_grey_path = os.path.join(unzip_dir, "linear_models/linear_grey.joblib")
|
| 24 |
|
| 25 |
# linear_rgb = joblib.load(linear_rgb_path)
|
| 26 |
# linear_grey = joblib.load(linear_grey_path)
|
| 27 |
|
|
|
|
| 28 |
def predict(r, g, b, activation, seed, neurons):
|
| 29 |
try:
|
| 30 |
X = np.array([[r, g, b]])
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
# lin_pred_rgb = linear_rgb.predict(X)[0]
|
| 35 |
-
# lin_pred_grey = linear_grey.predict([[grey]])[0]
|
| 36 |
|
| 37 |
-
#
|
| 38 |
-
keras_path = os.path.join(unzip_dir, f"
|
| 39 |
if not os.path.exists(keras_path):
|
| 40 |
raise FileNotFoundError(f"Model not found: {keras_path}")
|
| 41 |
|
| 42 |
model = tf.keras.models.load_model(keras_path)
|
| 43 |
ann_pred = model.predict(X)[0][0]
|
| 44 |
|
| 45 |
-
return ann_pred, lin_pred_rgb
|
| 46 |
-
|
| 47 |
except Exception as e:
|
| 48 |
return f"Error: {str(e)}", "", ""
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
if __name__ == "__main__":
|
| 70 |
-
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
import zipfile
|
| 6 |
import os
|
| 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...")
|
| 15 |
+
with zipfile.ZipFile(zip_file, 'r') as zip_ref:
|
|
|
|
| 16 |
zip_ref.extractall(unzip_dir)
|
| 17 |
print("Extraction complete.")
|
| 18 |
|
| 19 |
+
# Step 2: Parse folders to dynamically populate dropdowns
|
| 20 |
+
activations = []
|
| 21 |
+
seeds_dict = dict() # activation -> list of seeds
|
| 22 |
+
neurons_dict = dict() # (activation, seed) -> list of neuron counts
|
| 23 |
+
|
| 24 |
+
for act in os.listdir(unzip_dir):
|
| 25 |
+
act_path = os.path.join(unzip_dir, act)
|
| 26 |
+
if os.path.isdir(act_path) and not act.startswith("linear_models"):
|
| 27 |
+
activations.append(act)
|
| 28 |
+
seeds = []
|
| 29 |
+
for seed_folder in os.listdir(act_path):
|
| 30 |
+
seed_path = os.path.join(act_path, seed_folder)
|
| 31 |
+
if os.path.isdir(seed_path):
|
| 32 |
+
seeds.append(seed_folder)
|
| 33 |
+
neuron_list = []
|
| 34 |
+
for model_file in os.listdir(seed_path):
|
| 35 |
+
match = re.match(r"model_(\d+)\.keras", model_file)
|
| 36 |
+
if match:
|
| 37 |
+
neuron_list.append(int(match.group(1)))
|
| 38 |
+
neurons_dict[(act, seed_folder)] = sorted(neuron_list)
|
| 39 |
+
seeds_dict[act] = sorted(seeds)
|
| 40 |
+
|
| 41 |
+
activations = sorted(activations)
|
| 42 |
+
|
| 43 |
+
# Step 3: Load linear models
|
| 44 |
# linear_rgb_path = os.path.join(unzip_dir, "linear_models/linear_rgb.joblib")
|
| 45 |
# linear_grey_path = os.path.join(unzip_dir, "linear_models/linear_grey.joblib")
|
| 46 |
|
| 47 |
# linear_rgb = joblib.load(linear_rgb_path)
|
| 48 |
# linear_grey = joblib.load(linear_grey_path)
|
| 49 |
|
| 50 |
+
# Step 4: Prediction function
|
| 51 |
def predict(r, g, b, activation, seed, neurons):
|
| 52 |
try:
|
| 53 |
X = np.array([[r, g, b]])
|
| 54 |
+
|
| 55 |
+
# Linear predictions
|
| 56 |
+
lin_pred_rgb = (1.9221 * r) - (1.3817 * g) + (1.4058 * b) - 0.1318
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
# ANN prediction
|
| 59 |
+
keras_path = os.path.join(unzip_dir, activation, seed, f"model_{neurons}.keras")
|
| 60 |
if not os.path.exists(keras_path):
|
| 61 |
raise FileNotFoundError(f"Model not found: {keras_path}")
|
| 62 |
|
| 63 |
model = tf.keras.models.load_model(keras_path)
|
| 64 |
ann_pred = model.predict(X)[0][0]
|
| 65 |
|
| 66 |
+
return ann_pred, lin_pred_rgb
|
| 67 |
+
|
| 68 |
except Exception as e:
|
| 69 |
return f"Error: {str(e)}", "", ""
|
| 70 |
|
| 71 |
+
# Dynamic components for UI
|
| 72 |
+
|
| 73 |
+
def update_seeds(activation):
|
| 74 |
+
return gr.Dropdown.update(choices=seeds_dict[activation], value=seeds_dict[activation][0])
|
| 75 |
+
|
| 76 |
+
def update_neurons(activation, seed):
|
| 77 |
+
neurons = neurons_dict[(activation, seed)]
|
| 78 |
+
return gr.Dropdown.update(choices=neurons, value=neurons[0])
|
| 79 |
+
|
| 80 |
+
# Gradio Interface
|
| 81 |
+
with gr.Blocks() as demo:
|
| 82 |
+
gr.Markdown("# ANN vs Linear Model Predictor")
|
| 83 |
+
gr.Markdown("Dynamically select models and predict cholesterol concentration.")
|
| 84 |
+
|
| 85 |
+
with gr.Row():
|
| 86 |
+
r = gr.Number(label="R")
|
| 87 |
+
g = gr.Number(label="G")
|
| 88 |
+
b = gr.Number(label="B")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
activation = gr.Dropdown(choices=activations, label="Activation Function")
|
| 92 |
+
seed = gr.Dropdown(label="Seed")
|
| 93 |
+
neurons = gr.Dropdown(label="Neurons")
|
| 94 |
+
|
| 95 |
+
activation.change(update_seeds, inputs=[activation], outputs=[seed])
|
| 96 |
+
seed.change(update_neurons, inputs=[activation, seed], outputs=[neurons])
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
btn = gr.Button("Predict")
|
| 100 |
+
|
| 101 |
+
with gr.Row():
|
| 102 |
+
ann_output = gr.Text(label="ANN Model Prediction")
|
| 103 |
+
lin_rgb_output = gr.Text(label="Linear RGB Prediction")
|
| 104 |
+
# lin_grey_output = gr.Text(label="Linear Grey Prediction")
|
| 105 |
+
|
| 106 |
+
btn.click(
|
| 107 |
+
fn=predict,
|
| 108 |
+
inputs=[r, g, b, activation, seed, neurons],
|
| 109 |
+
outputs=[ann_output, lin_rgb_output, lin_grey_output]
|
| 110 |
+
)
|
| 111 |
|
| 112 |
if __name__ == "__main__":
|
| 113 |
+
demo.launch()
|