Ayesha-Majeed commited on
Commit
ddfa7c6
·
verified ·
1 Parent(s): 8c5c5f4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -16
app.py CHANGED
@@ -1,4 +1,3 @@
1
- # app.py
2
  import pandas as pd
3
  import numpy as np
4
  import gradio as gr
@@ -14,31 +13,32 @@ try:
14
  except Exception as e:
15
  raise RuntimeError(f"Failed to load Excel file: {e}")
16
 
17
- # Drop targets for prediction
18
  drop_cols = ['Yield_g_per_pot', 'Relative_Yield_%']
19
  feature_cols = [c for c in df.columns if c not in drop_cols]
20
 
21
- # Load model
22
  model = XGBRegressor()
23
  model.load_model(MODEL_PATH)
24
 
25
- # --- Functions ---
26
  def make_row_label(i):
27
  return f"Row {i+1} (index={i})"
28
 
 
 
 
 
29
  def on_row_select(row_label):
30
  try:
31
- idx = int(row_label.split("index=")[1].strip(")"))
32
  except Exception:
33
- return "Invalid row label.", gr.update(visible=False), None
34
 
35
  row_df = df.iloc[idx][feature_cols].to_frame().T
36
- md = "### Selected Row (Input Features)\n\n" + row_df.T.to_markdown()
37
- return md, gr.update(visible=True), idx
38
 
39
  def on_predict(row_label):
40
  try:
41
- idx = int(row_label.split("index=")[1].strip(")"))
42
  except Exception:
43
  return "Invalid row label.", "", ""
44
 
@@ -55,19 +55,19 @@ def on_predict(row_label):
55
  row_choices = [make_row_label(i) for i in range(len(df))]
56
 
57
  with gr.Blocks(css="""
58
- /* Minimal, professional design */
59
  body { background: #f5f7fa; color: #0b1220; font-family: Inter, sans-serif; }
60
  .gradio-container { max-width: 900px; margin: 20px auto; }
61
  .card { background: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 16px rgba(0,0,0,0.05); }
62
  h1 { margin-bottom: 10px; }
63
  """) as demo:
64
-
65
  with gr.Column(elem_classes="card"):
66
  gr.Markdown("# EcoGrowAI — Yield Prediction")
67
- gr.Markdown("Select any experimental row below and get the predicted **Yield (g per pot)** based on the trained XGBoost model.")
68
 
69
  row_dropdown = gr.Dropdown(
70
- label="Select row",
71
  choices=row_choices,
72
  value=row_choices[0],
73
  interactive=True
@@ -75,23 +75,24 @@ h1 { margin-bottom: 10px; }
75
 
76
  row_info = gr.Markdown("No row selected yet.")
77
  predict_button = gr.Button("Predict", variant="primary")
 
78
  status = gr.Markdown("")
79
  features_md = gr.Markdown("")
80
  result_md = gr.Markdown("")
81
 
82
- # Events
83
  row_dropdown.change(
84
  fn=on_row_select,
85
  inputs=[row_dropdown],
86
- outputs=[row_info, predict_button, gr.State()]
87
  )
88
 
 
89
  predict_button.click(
90
  fn=on_predict,
91
  inputs=[row_dropdown],
92
  outputs=[status, features_md, result_md]
93
  )
94
 
95
- # --- Run ---
96
  if __name__ == "__main__":
97
  demo.launch(server_name="0.0.0.0", server_port=7860)
 
 
1
  import pandas as pd
2
  import numpy as np
3
  import gradio as gr
 
13
  except Exception as e:
14
  raise RuntimeError(f"Failed to load Excel file: {e}")
15
 
 
16
  drop_cols = ['Yield_g_per_pot', 'Relative_Yield_%']
17
  feature_cols = [c for c in df.columns if c not in drop_cols]
18
 
 
19
  model = XGBRegressor()
20
  model.load_model(MODEL_PATH)
21
 
22
+ # --- Helpers ---
23
  def make_row_label(i):
24
  return f"Row {i+1} (index={i})"
25
 
26
+ def parse_index(label):
27
+ return int(label.split("index=")[1].strip(" return int(label.split("index=")[1].strip(")"))
28
+
29
+ # --- Callbacks ---
30
  def on_row_select(row_label):
31
  try:
32
+ idx = parse_index(row_label)
33
  except Exception:
34
+ return "Invalid row label."
35
 
36
  row_df = df.iloc[idx][feature_cols].to_frame().T
37
+ return "### Selected Row (Input Features)\n\n" + row_df.T.to_markdown()
 
38
 
39
  def on_predict(row_label):
40
  try:
41
+ idx = parse_index(row_label)
42
  except Exception:
43
  return "Invalid row label.", "", ""
44
 
 
55
  row_choices = [make_row_label(i) for i in range(len(df))]
56
 
57
  with gr.Blocks(css="""
58
+ /* Minimal professional styling */
59
  body { background: #f5f7fa; color: #0b1220; font-family: Inter, sans-serif; }
60
  .gradio-container { max-width: 900px; margin: 20px auto; }
61
  .card { background: white; padding: 20px; border-radius: 10px; box-shadow: 0 4px 16px rgba(0,0,0,0.05); }
62
  h1 { margin-bottom: 10px; }
63
  """) as demo:
64
+
65
  with gr.Column(elem_classes="card"):
66
  gr.Markdown("# EcoGrowAI — Yield Prediction")
67
+ gr.Markdown("Select any experimental row and predict **Yield (g per pot)** using the trained XGBoost model.")
68
 
69
  row_dropdown = gr.Dropdown(
70
+ label="Select Row",
71
  choices=row_choices,
72
  value=row_choices[0],
73
  interactive=True
 
75
 
76
  row_info = gr.Markdown("No row selected yet.")
77
  predict_button = gr.Button("Predict", variant="primary")
78
+
79
  status = gr.Markdown("")
80
  features_md = gr.Markdown("")
81
  result_md = gr.Markdown("")
82
 
83
+ # When row changes
84
  row_dropdown.change(
85
  fn=on_row_select,
86
  inputs=[row_dropdown],
87
+ outputs=[row_info],
88
  )
89
 
90
+ # When Predict is clicked
91
  predict_button.click(
92
  fn=on_predict,
93
  inputs=[row_dropdown],
94
  outputs=[status, features_md, result_md]
95
  )
96
 
 
97
  if __name__ == "__main__":
98
  demo.launch(server_name="0.0.0.0", server_port=7860)