Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,99 +3,90 @@ import numpy as np
|
|
| 3 |
import gradio as gr
|
| 4 |
from xgboost import XGBRegressor
|
| 5 |
|
| 6 |
-
#
|
| 7 |
EXCEL_PATH = "microalgae_pot_experiment_corrected_doses.xlsx"
|
| 8 |
MODEL_PATH = "EcoGrowAI_yield_model.json"
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
except Exception as e:
|
| 14 |
-
raise RuntimeError(f"Failed to load Excel file: {e}")
|
| 15 |
-
|
| 16 |
-
# Drop targets for prediction
|
| 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 |
-
# Load model
|
| 21 |
model = XGBRegressor()
|
| 22 |
model.load_model(MODEL_PATH)
|
| 23 |
|
| 24 |
-
#
|
| 25 |
def make_row_label(i):
|
| 26 |
return f"Row {i+1} (index={i})"
|
| 27 |
|
| 28 |
def parse_index(label):
|
| 29 |
-
"""Extract numeric index from dropdown label."""
|
| 30 |
return int(label.split("index=")[1].strip(")"))
|
| 31 |
|
| 32 |
-
#
|
| 33 |
-
def
|
| 34 |
try:
|
| 35 |
idx = parse_index(row_label)
|
|
|
|
|
|
|
| 36 |
except Exception:
|
| 37 |
return "Invalid row label."
|
| 38 |
|
| 39 |
-
|
| 40 |
-
return "### Selected Row (Input Features)\n\n" + row_df.T.to_markdown()
|
| 41 |
-
|
| 42 |
-
def on_predict(row_label):
|
| 43 |
try:
|
| 44 |
idx = parse_index(row_label)
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
# --- UI ---
|
| 58 |
row_choices = [make_row_label(i) for i in range(len(df))]
|
| 59 |
|
| 60 |
with gr.Blocks(css="""
|
| 61 |
-
/*
|
| 62 |
body { background: #f5f7fa; color: #0b1220; font-family: Inter, sans-serif; }
|
| 63 |
.gradio-container { max-width: 900px; margin: 20px auto; }
|
| 64 |
-
.card { background: white; padding:
|
| 65 |
-
|
|
|
|
| 66 |
""") as demo:
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
#
|
| 87 |
row_dropdown.change(
|
| 88 |
-
fn=
|
| 89 |
inputs=[row_dropdown],
|
| 90 |
-
outputs=[
|
| 91 |
)
|
| 92 |
|
| 93 |
-
# When Predict is clicked
|
| 94 |
predict_button.click(
|
| 95 |
-
fn=
|
| 96 |
inputs=[row_dropdown],
|
| 97 |
-
outputs=[status,
|
| 98 |
)
|
| 99 |
|
|
|
|
| 100 |
if __name__ == "__main__":
|
| 101 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 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]
|
| 14 |
|
|
|
|
| 15 |
model = XGBRegressor()
|
| 16 |
model.load_model(MODEL_PATH)
|
| 17 |
|
| 18 |
+
# === Helpers ===
|
| 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 |
+
# === Logic ===
|
| 26 |
+
def show_row(row_label):
|
| 27 |
try:
|
| 28 |
idx = parse_index(row_label)
|
| 29 |
+
row_df = df.iloc[idx][feature_cols].to_frame().T
|
| 30 |
+
return "### Selected Row\n\n" + row_df.T.to_markdown()
|
| 31 |
except Exception:
|
| 32 |
return "Invalid row label."
|
| 33 |
|
| 34 |
+
def predict_yield(row_label):
|
|
|
|
|
|
|
|
|
|
| 35 |
try:
|
| 36 |
idx = parse_index(row_label)
|
| 37 |
+
row = df.iloc[idx][feature_cols]
|
| 38 |
+
X_row = row.astype(float).values.reshape(1, -1)
|
| 39 |
+
pred = model.predict(X_row)[0]
|
| 40 |
+
pred_rounded = float(np.round(pred, 3))
|
| 41 |
+
features_md = "### Input Features\n\n" + row.to_frame().to_markdown()
|
| 42 |
+
result_md = f"### Predicted Yield (g per pot)\n\n**{pred_rounded}**"
|
| 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 |
+
/* Professional minimal styling */
|
| 52 |
body { background: #f5f7fa; color: #0b1220; font-family: Inter, sans-serif; }
|
| 53 |
.gradio-container { max-width: 900px; margin: 20px auto; }
|
| 54 |
+
.card { background: white; padding: 22px; border-radius: 10px;
|
| 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)
|