Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,11 +3,9 @@ import numpy as np
|
|
| 3 |
import gradio as gr
|
| 4 |
from xgboost import XGBRegressor
|
| 5 |
|
| 6 |
-
# === Configuration ===
|
| 7 |
EXCEL_PATH = "microalgae_pot_experiment_corrected_doses.xlsx"
|
| 8 |
MODEL_PATH = "EcoGrowAI_yield_model.json"
|
| 9 |
|
| 10 |
-
# === Load data and model ===
|
| 11 |
df = pd.read_excel(EXCEL_PATH)
|
| 12 |
drop_cols = ["Yield_g_per_pot", "Relative_Yield_%"]
|
| 13 |
feature_cols = [c for c in df.columns if c not in drop_cols]
|
|
@@ -15,78 +13,56 @@ feature_cols = [c for c in df.columns if c not in drop_cols]
|
|
| 15 |
model = XGBRegressor()
|
| 16 |
model.load_model(MODEL_PATH)
|
| 17 |
|
| 18 |
-
|
| 19 |
def make_row_label(i):
|
| 20 |
return f"Row {i+1} (index={i})"
|
| 21 |
|
|
|
|
| 22 |
def parse_index(label):
|
| 23 |
return int(label.split("index=")[1].strip(")"))
|
| 24 |
|
| 25 |
-
|
| 26 |
-
def
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
return "✅ Prediction complete", features_md, result_md
|
| 44 |
-
except Exception as e:
|
| 45 |
-
return f"Error: {e}", "", ""
|
| 46 |
-
|
| 47 |
-
# === UI ===
|
| 48 |
row_choices = [make_row_label(i) for i in range(len(df))]
|
| 49 |
|
| 50 |
with gr.Blocks(css="""
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
.
|
| 54 |
-
|
| 55 |
-
box-shadow: 0 4px 14px rgba(0,0,0,0.05); }
|
| 56 |
-
h1 { margin-bottom: 12px; }
|
| 57 |
""") as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
gr.Markdown("# EcoGrowAI — Yield Prediction")
|
| 60 |
-
gr.Markdown("Select a row from the dataset and predict **Yield (g per pot)** using the trained XGBoost model.")
|
| 61 |
-
|
| 62 |
-
with gr.Row(elem_classes="card"):
|
| 63 |
-
with gr.Column():
|
| 64 |
-
row_dropdown = gr.Dropdown(
|
| 65 |
-
label="Select Row",
|
| 66 |
-
choices=row_choices,
|
| 67 |
-
value=row_choices[0],
|
| 68 |
-
interactive=True
|
| 69 |
-
)
|
| 70 |
-
predict_button = gr.Button("Predict", variant="primary")
|
| 71 |
-
status = gr.Markdown("")
|
| 72 |
-
with gr.Column():
|
| 73 |
-
row_display = gr.Markdown("No row selected yet.")
|
| 74 |
-
features_display = gr.Markdown("")
|
| 75 |
-
result_display = gr.Markdown("")
|
| 76 |
-
|
| 77 |
-
# --- Bind events ---
|
| 78 |
-
row_dropdown.change(
|
| 79 |
-
fn=show_row,
|
| 80 |
-
inputs=[row_dropdown],
|
| 81 |
-
outputs=[row_display]
|
| 82 |
-
)
|
| 83 |
-
|
| 84 |
-
predict_button.click(
|
| 85 |
-
fn=predict_yield,
|
| 86 |
-
inputs=[row_dropdown],
|
| 87 |
-
outputs=[status, features_display, result_display]
|
| 88 |
-
)
|
| 89 |
-
|
| 90 |
-
# === Launch ===
|
| 91 |
if __name__ == "__main__":
|
| 92 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 3 |
import gradio as gr
|
| 4 |
from xgboost import XGBRegressor
|
| 5 |
|
|
|
|
| 6 |
EXCEL_PATH = "microalgae_pot_experiment_corrected_doses.xlsx"
|
| 7 |
MODEL_PATH = "EcoGrowAI_yield_model.json"
|
| 8 |
|
|
|
|
| 9 |
df = pd.read_excel(EXCEL_PATH)
|
| 10 |
drop_cols = ["Yield_g_per_pot", "Relative_Yield_%"]
|
| 11 |
feature_cols = [c for c in df.columns if c not in drop_cols]
|
|
|
|
| 13 |
model = XGBRegressor()
|
| 14 |
model.load_model(MODEL_PATH)
|
| 15 |
|
| 16 |
+
|
| 17 |
def make_row_label(i):
|
| 18 |
return f"Row {i+1} (index={i})"
|
| 19 |
|
| 20 |
+
|
| 21 |
def parse_index(label):
|
| 22 |
return int(label.split("index=")[1].strip(")"))
|
| 23 |
|
| 24 |
+
|
| 25 |
+
def on_row_select(row_label):
|
| 26 |
+
idx = parse_index(row_label)
|
| 27 |
+
row_df = df.iloc[[idx]][feature_cols]
|
| 28 |
+
return "### Selected Row (Input Features)\n\n" + row_df.T.to_markdown()
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def on_predict(row_label):
|
| 32 |
+
idx = parse_index(row_label)
|
| 33 |
+
row = df.iloc[idx][feature_cols]
|
| 34 |
+
X_row = row.astype(float).values.reshape(1, -1)
|
| 35 |
+
pred = model.predict(X_row)[0]
|
| 36 |
+
pred_rounded = float(np.round(pred, 3))
|
| 37 |
+
features_md = "### Input Features\n\n" + row.to_frame().to_markdown()
|
| 38 |
+
result_md = f"### Predicted Yield (g per pot)\n\n**{pred_rounded}**"
|
| 39 |
+
return "✅ Prediction complete", features_md, result_md
|
| 40 |
+
|
| 41 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
row_choices = [make_row_label(i) for i in range(len(df))]
|
| 43 |
|
| 44 |
with gr.Blocks(css="""
|
| 45 |
+
body {background:#f5f7fa;color:#0b1220;font-family:Inter,sans-serif;}
|
| 46 |
+
.gradio-container{max-width:900px;margin:20px auto;}
|
| 47 |
+
.card{background:white;padding:20px;border-radius:10px;
|
| 48 |
+
box-shadow:0 4px 16px rgba(0,0,0,0.05);}
|
|
|
|
|
|
|
| 49 |
""") as demo:
|
| 50 |
+
with gr.Column(elem_classes="card"):
|
| 51 |
+
gr.Markdown("# EcoGrowAI — Yield Prediction")
|
| 52 |
+
gr.Markdown("Select any experimental row and predict **Yield (g per pot)** using the trained XGBoost model.")
|
| 53 |
+
|
| 54 |
+
row_dropdown = gr.Dropdown(
|
| 55 |
+
label="Select Row", choices=row_choices, value=row_choices[0], interactive=True
|
| 56 |
+
)
|
| 57 |
+
|
| 58 |
+
row_info = gr.Markdown("No row selected yet.")
|
| 59 |
+
predict_button = gr.Button("Predict", variant="primary")
|
| 60 |
+
status = gr.Markdown("")
|
| 61 |
+
features_md = gr.Markdown("")
|
| 62 |
+
result_md = gr.Markdown("")
|
| 63 |
+
|
| 64 |
+
row_dropdown.change(on_row_select, row_dropdown, row_info)
|
| 65 |
+
predict_button.click(on_predict, row_dropdown, [status, features_md, result_md])
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
if __name__ == "__main__":
|
| 68 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|