Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -11,7 +11,7 @@ EXCEL_PATH = "microalgae_pot_experiment_corrected_doses.xlsx"
|
|
| 11 |
MODEL_PATH = "EcoGrowAI_yield_model.json"
|
| 12 |
df = pd.read_excel(EXCEL_PATH)
|
| 13 |
|
| 14 |
-
#
|
| 15 |
for col in ["Crop", "Microalgae_Strain"]:
|
| 16 |
le = LabelEncoder()
|
| 17 |
df[col] = le.fit_transform(df[col])
|
|
@@ -19,7 +19,7 @@ for col in ["Crop", "Microalgae_Strain"]:
|
|
| 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 |
|
|
@@ -33,9 +33,8 @@ def parse_index(label):
|
|
| 33 |
return int(label.split("index=")[1].strip(")"))
|
| 34 |
|
| 35 |
def on_row_select(row_label):
|
| 36 |
-
|
| 37 |
-
|
| 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)
|
|
@@ -53,7 +52,8 @@ def on_predict(row_label):
|
|
| 53 |
error = abs(pred_rounded - actual)
|
| 54 |
error_rounded = float(np.round(error, 3))
|
| 55 |
|
| 56 |
-
# Markdown display
|
|
|
|
| 57 |
result_md = (
|
| 58 |
f"### Prediction Result\n\n"
|
| 59 |
f"**Predicted Yield (g per pot):** {pred_rounded}\n\n"
|
|
@@ -61,7 +61,7 @@ def on_predict(row_label):
|
|
| 61 |
f"**Absolute Error:** {error_rounded}"
|
| 62 |
)
|
| 63 |
|
| 64 |
-
return
|
| 65 |
|
| 66 |
# -------------------------------
|
| 67 |
# Dropdown options
|
|
@@ -69,7 +69,7 @@ def on_predict(row_label):
|
|
| 69 |
row_choices = [make_row_label(i) for i in range(len(df))]
|
| 70 |
|
| 71 |
# -------------------------------
|
| 72 |
-
# Gradio
|
| 73 |
# -------------------------------
|
| 74 |
with gr.Blocks(css="""
|
| 75 |
body {background:#f5f7fa;color:#0b1220;font-family:Inter,sans-serif;}
|
|
@@ -78,28 +78,26 @@ body {background:#f5f7fa;color:#0b1220;font-family:Inter,sans-serif;}
|
|
| 78 |
""") as demo:
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
-
# LEFT COLUMN:
|
| 82 |
with gr.Column(elem_classes="card", scale=1):
|
| 83 |
-
gr.Markdown("#
|
| 84 |
-
gr.Markdown("Select
|
| 85 |
-
|
| 86 |
row_dropdown = gr.Dropdown(
|
| 87 |
label="Select Row", choices=row_choices, value=row_choices[0], interactive=True
|
| 88 |
)
|
| 89 |
-
|
| 90 |
predict_button = gr.Button("Predict", variant="primary")
|
| 91 |
-
|
| 92 |
-
row_info = gr.Markdown("No row selected yet.") # Selected row info on left
|
| 93 |
|
| 94 |
-
# RIGHT COLUMN: prediction result
|
| 95 |
-
with gr.Column(elem_classes="card", scale=
|
| 96 |
-
|
|
|
|
| 97 |
|
| 98 |
# -------------------------------
|
| 99 |
-
#
|
| 100 |
# -------------------------------
|
| 101 |
-
row_dropdown.change(on_row_select, row_dropdown,
|
| 102 |
-
predict_button.click(on_predict, row_dropdown, [
|
| 103 |
|
| 104 |
# -------------------------------
|
| 105 |
# Launch
|
|
|
|
| 11 |
MODEL_PATH = "EcoGrowAI_yield_model.json"
|
| 12 |
df = pd.read_excel(EXCEL_PATH)
|
| 13 |
|
| 14 |
+
# Encode categorical columns
|
| 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 XGBoost model
|
| 23 |
model = XGBRegressor()
|
| 24 |
model.load_model(MODEL_PATH)
|
| 25 |
|
|
|
|
| 33 |
return int(label.split("index=")[1].strip(")"))
|
| 34 |
|
| 35 |
def on_row_select(row_label):
|
| 36 |
+
# Only return the selected row label (to show left)
|
| 37 |
+
return f"### Selected Row:\n{row_label}"
|
|
|
|
| 38 |
|
| 39 |
def on_predict(row_label):
|
| 40 |
idx = parse_index(row_label)
|
|
|
|
| 52 |
error = abs(pred_rounded - actual)
|
| 53 |
error_rounded = float(np.round(error, 3))
|
| 54 |
|
| 55 |
+
# Markdown display (Right column)
|
| 56 |
+
features_md = "### Input Features\n\n" + row.to_frame().to_markdown()
|
| 57 |
result_md = (
|
| 58 |
f"### Prediction Result\n\n"
|
| 59 |
f"**Predicted Yield (g per pot):** {pred_rounded}\n\n"
|
|
|
|
| 61 |
f"**Absolute Error:** {error_rounded}"
|
| 62 |
)
|
| 63 |
|
| 64 |
+
return features_md, result_md
|
| 65 |
|
| 66 |
# -------------------------------
|
| 67 |
# Dropdown options
|
|
|
|
| 69 |
row_choices = [make_row_label(i) for i in range(len(df))]
|
| 70 |
|
| 71 |
# -------------------------------
|
| 72 |
+
# Gradio Layout
|
| 73 |
# -------------------------------
|
| 74 |
with gr.Blocks(css="""
|
| 75 |
body {background:#f5f7fa;color:#0b1220;font-family:Inter,sans-serif;}
|
|
|
|
| 78 |
""") as demo:
|
| 79 |
|
| 80 |
with gr.Row():
|
| 81 |
+
# LEFT COLUMN: Dropdown + Predict + Selected row
|
| 82 |
with gr.Column(elem_classes="card", scale=1):
|
| 83 |
+
gr.Markdown("# EcoGrowAI — Yield Prediction")
|
| 84 |
+
gr.Markdown("Select a row to predict its Yield (g per pot).")
|
|
|
|
| 85 |
row_dropdown = gr.Dropdown(
|
| 86 |
label="Select Row", choices=row_choices, value=row_choices[0], interactive=True
|
| 87 |
)
|
|
|
|
| 88 |
predict_button = gr.Button("Predict", variant="primary")
|
| 89 |
+
selected_row_label = gr.Markdown("No row selected yet.") # only row label
|
|
|
|
| 90 |
|
| 91 |
+
# RIGHT COLUMN: input features + prediction result
|
| 92 |
+
with gr.Column(elem_classes="card", scale=2):
|
| 93 |
+
features_md = gr.Markdown("Input features will appear here.")
|
| 94 |
+
result_md = gr.Markdown("Prediction results will appear here.")
|
| 95 |
|
| 96 |
# -------------------------------
|
| 97 |
+
# Bind events
|
| 98 |
# -------------------------------
|
| 99 |
+
row_dropdown.change(on_row_select, row_dropdown, selected_row_label)
|
| 100 |
+
predict_button.click(on_predict, row_dropdown, [features_md, result_md])
|
| 101 |
|
| 102 |
# -------------------------------
|
| 103 |
# Launch
|