Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,14 +2,16 @@ import pandas as pd
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from xgboost import XGBRegressor
|
| 5 |
-
from sklearn.preprocessing import LabelEncoder
|
| 6 |
|
|
|
|
|
|
|
|
|
|
| 7 |
EXCEL_PATH = "microalgae_pot_experiment_corrected_doses.xlsx"
|
| 8 |
MODEL_PATH = "EcoGrowAI_yield_model.json"
|
| 9 |
-
|
| 10 |
df = pd.read_excel(EXCEL_PATH)
|
| 11 |
|
| 12 |
-
#
|
| 13 |
for col in ["Crop", "Microalgae_Strain"]:
|
| 14 |
le = LabelEncoder()
|
| 15 |
df[col] = le.fit_transform(df[col])
|
|
@@ -17,37 +19,37 @@ for col in ["Crop", "Microalgae_Strain"]:
|
|
| 17 |
drop_cols = ["Yield_g_per_pot", "Relative_Yield_%"]
|
| 18 |
feature_cols = [c for c in df.columns if c not in drop_cols]
|
| 19 |
|
|
|
|
| 20 |
model = XGBRegressor()
|
| 21 |
model.load_model(MODEL_PATH)
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
| 24 |
def make_row_label(i):
|
| 25 |
return f"Row {i+1} (index={i})"
|
| 26 |
|
| 27 |
-
|
| 28 |
def parse_index(label):
|
| 29 |
return int(label.split("index=")[1].strip(")"))
|
| 30 |
|
| 31 |
-
|
| 32 |
def on_row_select(row_label):
|
| 33 |
idx = parse_index(row_label)
|
| 34 |
row_df = df.iloc[[idx]][feature_cols]
|
| 35 |
return "### Selected Row (Input Features)\n\n" + row_df.T.to_markdown()
|
| 36 |
|
| 37 |
-
|
| 38 |
def on_predict(row_label):
|
| 39 |
idx = parse_index(row_label)
|
| 40 |
row = df.iloc[idx][feature_cols]
|
| 41 |
X_row = row.astype(float).values.reshape(1, -1)
|
| 42 |
-
|
| 43 |
# Prediction
|
| 44 |
pred = model.predict(X_row)[0]
|
| 45 |
pred_rounded = float(np.round(pred, 3))
|
| 46 |
|
| 47 |
# Ground truth
|
| 48 |
actual = float(df.iloc[idx]["Yield_g_per_pot"])
|
| 49 |
-
|
| 50 |
-
# Error
|
| 51 |
error = abs(pred_rounded - actual)
|
| 52 |
error_rounded = float(np.round(error, 3))
|
| 53 |
|
|
@@ -62,34 +64,47 @@ def on_predict(row_label):
|
|
| 62 |
|
| 63 |
return "✅ Prediction complete", features_md, result_md
|
| 64 |
|
| 65 |
-
|
|
|
|
|
|
|
| 66 |
row_choices = [make_row_label(i) for i in range(len(df))]
|
| 67 |
|
|
|
|
|
|
|
|
|
|
| 68 |
with gr.Blocks(css="""
|
| 69 |
body {background:#f5f7fa;color:#0b1220;font-family:Inter,sans-serif;}
|
| 70 |
.gradio-container{max-width:1200px;margin:20px auto;}
|
| 71 |
-
.card{background:white;padding:20px;border-radius:10px;
|
| 72 |
-
box-shadow:0 4px 16px rgba(0,0,0,0.05);}
|
| 73 |
""") as demo:
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
row_dropdown.change(on_row_select, row_dropdown, row_info)
|
| 92 |
predict_button.click(on_predict, row_dropdown, [status, features_md, result_md])
|
| 93 |
|
|
|
|
|
|
|
|
|
|
| 94 |
if __name__ == "__main__":
|
| 95 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 2 |
import numpy as np
|
| 3 |
import gradio as gr
|
| 4 |
from xgboost import XGBRegressor
|
| 5 |
+
from sklearn.preprocessing import LabelEncoder
|
| 6 |
|
| 7 |
+
# -------------------------------
|
| 8 |
+
# Paths and Data
|
| 9 |
+
# -------------------------------
|
| 10 |
EXCEL_PATH = "microalgae_pot_experiment_corrected_doses.xlsx"
|
| 11 |
MODEL_PATH = "EcoGrowAI_yield_model.json"
|
|
|
|
| 12 |
df = pd.read_excel(EXCEL_PATH)
|
| 13 |
|
| 14 |
+
# Apply encoding used during training
|
| 15 |
for col in ["Crop", "Microalgae_Strain"]:
|
| 16 |
le = LabelEncoder()
|
| 17 |
df[col] = le.fit_transform(df[col])
|
|
|
|
| 19 |
drop_cols = ["Yield_g_per_pot", "Relative_Yield_%"]
|
| 20 |
feature_cols = [c for c in df.columns if c not in drop_cols]
|
| 21 |
|
| 22 |
+
# Load model
|
| 23 |
model = XGBRegressor()
|
| 24 |
model.load_model(MODEL_PATH)
|
| 25 |
|
| 26 |
+
# -------------------------------
|
| 27 |
+
# Helper functions
|
| 28 |
+
# -------------------------------
|
| 29 |
def make_row_label(i):
|
| 30 |
return f"Row {i+1} (index={i})"
|
| 31 |
|
|
|
|
| 32 |
def parse_index(label):
|
| 33 |
return int(label.split("index=")[1].strip(")"))
|
| 34 |
|
|
|
|
| 35 |
def on_row_select(row_label):
|
| 36 |
idx = parse_index(row_label)
|
| 37 |
row_df = df.iloc[[idx]][feature_cols]
|
| 38 |
return "### Selected Row (Input Features)\n\n" + row_df.T.to_markdown()
|
| 39 |
|
|
|
|
| 40 |
def on_predict(row_label):
|
| 41 |
idx = parse_index(row_label)
|
| 42 |
row = df.iloc[idx][feature_cols]
|
| 43 |
X_row = row.astype(float).values.reshape(1, -1)
|
| 44 |
+
|
| 45 |
# Prediction
|
| 46 |
pred = model.predict(X_row)[0]
|
| 47 |
pred_rounded = float(np.round(pred, 3))
|
| 48 |
|
| 49 |
# Ground truth
|
| 50 |
actual = float(df.iloc[idx]["Yield_g_per_pot"])
|
| 51 |
+
|
| 52 |
+
# Error
|
| 53 |
error = abs(pred_rounded - actual)
|
| 54 |
error_rounded = float(np.round(error, 3))
|
| 55 |
|
|
|
|
| 64 |
|
| 65 |
return "✅ Prediction complete", features_md, result_md
|
| 66 |
|
| 67 |
+
# -------------------------------
|
| 68 |
+
# Dropdown options
|
| 69 |
+
# -------------------------------
|
| 70 |
row_choices = [make_row_label(i) for i in range(len(df))]
|
| 71 |
|
| 72 |
+
# -------------------------------
|
| 73 |
+
# Gradio App Layout
|
| 74 |
+
# -------------------------------
|
| 75 |
with gr.Blocks(css="""
|
| 76 |
body {background:#f5f7fa;color:#0b1220;font-family:Inter,sans-serif;}
|
| 77 |
.gradio-container{max-width:1200px;margin:20px auto;}
|
| 78 |
+
.card{background:white;padding:20px;border-radius:10px;box-shadow:0 4px 16px rgba(0,0,0,0.05);}
|
|
|
|
| 79 |
""") as demo:
|
| 80 |
+
|
| 81 |
+
with gr.Row():
|
| 82 |
+
# LEFT COLUMN: controls
|
| 83 |
+
with gr.Column(elem_classes="card", scale=1):
|
| 84 |
+
gr.Markdown("# 🌿 EcoGrowAI — Yield Prediction")
|
| 85 |
+
gr.Markdown("Select any experimental row and predict **Yield (g per pot)** using the trained XGBoost model.")
|
| 86 |
+
|
| 87 |
+
row_dropdown = gr.Dropdown(
|
| 88 |
+
label="Select Row", choices=row_choices, value=row_choices[0], interactive=True
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
predict_button = gr.Button("Predict", variant="primary")
|
| 92 |
+
status = gr.Markdown("")
|
| 93 |
+
|
| 94 |
+
# RIGHT COLUMN: display features and results
|
| 95 |
+
with gr.Column(elem_classes="card", scale=2):
|
| 96 |
+
row_info = gr.Markdown("No row selected yet.")
|
| 97 |
+
features_md = gr.Markdown("")
|
| 98 |
+
result_md = gr.Markdown("")
|
| 99 |
+
|
| 100 |
+
# -------------------------------
|
| 101 |
+
# Event bindings
|
| 102 |
+
# -------------------------------
|
| 103 |
row_dropdown.change(on_row_select, row_dropdown, row_info)
|
| 104 |
predict_button.click(on_predict, row_dropdown, [status, features_md, result_md])
|
| 105 |
|
| 106 |
+
# -------------------------------
|
| 107 |
+
# Launch
|
| 108 |
+
# -------------------------------
|
| 109 |
if __name__ == "__main__":
|
| 110 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|