Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import tensorflow as tf
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
import zipfile
|
| 6 |
+
import os
|
| 7 |
+
from huggingface_hub import hf_hub_download
|
| 8 |
+
|
| 9 |
+
# Hugging Face repository ID
|
| 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("Downloading and extracting model zip file...")
|
| 16 |
+
zip_path = hf_hub_download(repo_id=repo_id, filename="Models.zip") # Replace with your actual uploaded ZIP filename
|
| 17 |
+
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
|
| 18 |
+
zip_ref.extractall(unzip_dir)
|
| 19 |
+
print("Extraction complete.")
|
| 20 |
+
|
| 21 |
+
# Load linear models
|
| 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 |
+
# grey = 0.2989 * r + 0.5870 * g + 0.1140 * b
|
| 32 |
+
|
| 33 |
+
# # Linear predictions
|
| 34 |
+
# lin_pred_rgb = linear_rgb.predict(X)[0]
|
| 35 |
+
# lin_pred_grey = linear_grey.predict([[grey]])[0]
|
| 36 |
+
|
| 37 |
+
# Load corresponding ANN model
|
| 38 |
+
keras_path = os.path.join(unzip_dir, f"{activation}/seed_{seed}/model_{neurons}.keras")
|
| 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, lin_pred_grey
|
| 46 |
+
|
| 47 |
+
except Exception as e:
|
| 48 |
+
return f"Error: {str(e)}", "", ""
|
| 49 |
+
|
| 50 |
+
iface = gr.Interface(
|
| 51 |
+
fn=predict,
|
| 52 |
+
inputs=[
|
| 53 |
+
gr.Number(label="R"),
|
| 54 |
+
gr.Number(label="G"),
|
| 55 |
+
gr.Number(label="B"),
|
| 56 |
+
gr.Textbox(label="Activation (folder name)"),
|
| 57 |
+
gr.Number(label="Seed (folder name)"),
|
| 58 |
+
gr.Number(label="Neurons (model number)")
|
| 59 |
+
],
|
| 60 |
+
outputs=[
|
| 61 |
+
gr.Text(label="ANN Model Prediction"),
|
| 62 |
+
gr.Text(label="Linear RGB Prediction"),
|
| 63 |
+
gr.Text(label="Linear Grey Prediction"),
|
| 64 |
+
],
|
| 65 |
+
title="ANN vs Linear Model Predictor",
|
| 66 |
+
description="Dynamically load models from Hugging Face repo and predict."
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
iface.launch()
|