UCS2014 commited on
Commit
22c14c0
·
verified ·
1 Parent(s): e0c0056

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +69 -19
app.py CHANGED
@@ -54,9 +54,9 @@ st.markdown(
54
  # Helpers
55
  # =========================
56
 
57
- # Modal dialog fallback for older Streamlit
58
  try:
59
- dialog = st.dialog # Streamlit ≥1.31
60
  except AttributeError:
61
  def dialog(title):
62
  def deco(fn):
@@ -216,18 +216,43 @@ def stats_table(df: pd.DataFrame, cols: list[str]) -> pd.DataFrame:
216
  return out
217
 
218
  @dialog("Preview data")
219
- def preview_modal(book: dict[str, pd.DataFrame], default_cols: list[str]):
220
  if not book:
221
  st.info("No data loaded yet.")
222
  return
223
- sheet = st.selectbox("Sheet", list(book.keys()), index=0)
224
- df = book[sheet]
225
- present = [c for c in default_cols if c in df.columns]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
226
  tab_tracks, tab_summary = st.tabs(["Tracks", "Summary"])
227
  with tab_tracks:
228
- st.pyplot(make_index_tracks(df, present), use_container_width=True)
229
  with tab_summary:
230
- st.dataframe(stats_table(df, present), use_container_width=True)
231
 
232
  # =========================
233
  # Model presence (local or optional download)
@@ -287,7 +312,7 @@ if ("Train" in st.session_state.results) or ("Test" in st.session_state.results)
287
  st.session_state.dev_ready = True
288
 
289
  # =========================
290
- # Hero header (logo + title)
291
  # =========================
292
  st.markdown(
293
  f"""
@@ -332,17 +357,27 @@ if st.session_state.app_step == "intro":
332
  # =========================
333
  if st.session_state.app_step == "dev":
334
  st.sidebar.header("Model Development Data")
335
- train_test_file = st.sidebar.file_uploader("Upload Data (Excel)", type=["xlsx","xls"], key="dev_upload")
336
-
337
- # Preview button (uses modal)
 
 
 
 
 
 
 
 
 
 
338
  preview_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=(train_test_file is None))
339
  if preview_btn and train_test_file is not None:
340
  _book = read_book(train_test_file)
341
- preview_modal(_book, FEATURES)
342
 
343
  run_btn = st.sidebar.button("Run Model", type="primary", use_container_width=True)
344
 
345
- # Proceed button BELOW run, always visible; enables immediately after first successful run
346
  st.sidebar.button(
347
  "Proceed to Prediction ▶",
348
  use_container_width=True,
@@ -352,7 +387,7 @@ if st.session_state.app_step == "dev":
352
 
353
  # Header + helper sentence under the header
354
  st.subheader("Model Development")
355
- st.write("Upload your data to train the model and review the development performance.")
356
 
357
  if run_btn and train_test_file is not None:
358
  with st.status("Processing…", expanded=False) as status:
@@ -440,13 +475,22 @@ if st.session_state.app_step == "dev":
440
  # =========================
441
  if st.session_state.app_step == "predict":
442
  st.sidebar.header("Prediction (Validation)")
443
- validation_file = st.sidebar.file_uploader("Upload Validation Excel", type=["xlsx","xls"], key="val_upload")
 
 
 
 
 
 
 
 
 
444
 
445
  # Preview button for validation data
446
  preview_val_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=(validation_file is None))
447
  if preview_val_btn and validation_file is not None:
448
  _book = read_book(validation_file)
449
- preview_modal(_book, FEATURES)
450
 
451
  predict_btn = st.sidebar.button("Predict", type="primary", use_container_width=True)
452
  st.sidebar.button("⬅ Back", on_click=lambda: st.session_state.update(app_step="dev"), use_container_width=True)
@@ -546,7 +590,13 @@ if st.session_state.app_step == "predict":
546
  st.warning(str(e))
547
 
548
  # =========================
549
- # Footer
550
  # =========================
551
  st.markdown("---")
552
- st.markdown("<div style='text-align:center; color:#6b7280;'>ST_GeoMech_UCS • © Smart Thinking</div>", unsafe_allow_html=True)
 
 
 
 
 
 
 
54
  # Helpers
55
  # =========================
56
 
57
+ # Prefer real dialog (Streamlit ≥1.31). Fallback to expander if older.
58
  try:
59
+ dialog = st.dialog
60
  except AttributeError:
61
  def dialog(title):
62
  def deco(fn):
 
216
  return out
217
 
218
  @dialog("Preview data")
219
+ def preview_modal_dev(book: dict[str, pd.DataFrame], feature_cols: list[str]):
220
  if not book:
221
  st.info("No data loaded yet.")
222
  return
223
+ # Auto detect Train/Test-like sheets
224
+ sh_train = find_sheet(book, ["Train","Training","training2","train","training"])
225
+ sh_test = find_sheet(book, ["Test","Testing","testing2","test","testing"])
226
+ tabs = []
227
+ data = []
228
+ if sh_train: tabs.append("Train"); data.append(book[sh_train])
229
+ if sh_test: tabs.append("Test"); data.append(book[sh_test])
230
+ if not tabs: # nothing matched, just show the first sheet
231
+ first_name = list(book.keys())[0]
232
+ tabs = [first_name]; data = [book[first_name]]
233
+
234
+ st.write("Use the tabs to switch between Train/Test views (if available).")
235
+ t_objs = st.tabs(tabs)
236
+ for t, df in zip(t_objs, data):
237
+ with t:
238
+ tab_tracks, tab_summary = st.tabs(["Tracks", "Summary"])
239
+ with tab_tracks:
240
+ st.pyplot(make_index_tracks(df, feature_cols), use_container_width=True)
241
+ with tab_summary:
242
+ st.dataframe(stats_table(df, feature_cols), use_container_width=True)
243
+
244
+ @dialog("Preview data")
245
+ def preview_modal_val(book: dict[str, pd.DataFrame], feature_cols: list[str]):
246
+ if not book:
247
+ st.info("No data loaded yet.")
248
+ return
249
+ vname = find_sheet(book, ["Validation","Validate","validation2","Val","val"]) or list(book.keys())[0]
250
+ df = book[vname]
251
  tab_tracks, tab_summary = st.tabs(["Tracks", "Summary"])
252
  with tab_tracks:
253
+ st.pyplot(make_index_tracks(df, feature_cols), use_container_width=True)
254
  with tab_summary:
255
+ st.dataframe(stats_table(df, feature_cols), use_container_width=True)
256
 
257
  # =========================
258
  # Model presence (local or optional download)
 
312
  st.session_state.dev_ready = True
313
 
314
  # =========================
315
+ # Hero header (logo + title) — website link moved to footer
316
  # =========================
317
  st.markdown(
318
  f"""
 
357
  # =========================
358
  if st.session_state.app_step == "dev":
359
  st.sidebar.header("Model Development Data")
360
+ dev_label = "Upload Data (Excel)"
361
+ if st.session_state.get("dev_file_name"):
362
+ dev_label = "Replace data (Excel)"
363
+ train_test_file = st.sidebar.file_uploader(dev_label, type=["xlsx","xls"], key="dev_upload")
364
+ if train_test_file is not None:
365
+ st.session_state.dev_file_name = train_test_file.name
366
+ # small status line in sidebar
367
+ _book_tmp = read_book(train_test_file)
368
+ if _book_tmp:
369
+ first_df = next(iter(_book_tmp.values()))
370
+ st.sidebar.caption(f"**Data loaded:** {train_test_file.name} • {first_df.shape[0]} rows × {first_df.shape[1]} cols")
371
+
372
+ # Preview button (modal)
373
  preview_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=(train_test_file is None))
374
  if preview_btn and train_test_file is not None:
375
  _book = read_book(train_test_file)
376
+ preview_modal_dev(_book, FEATURES)
377
 
378
  run_btn = st.sidebar.button("Run Model", type="primary", use_container_width=True)
379
 
380
+ # Proceed button BELOW run, always visible; enables after first successful run
381
  st.sidebar.button(
382
  "Proceed to Prediction ▶",
383
  use_container_width=True,
 
387
 
388
  # Header + helper sentence under the header
389
  st.subheader("Model Development")
390
+ st.write("**Upload your data to build a case, run the model, and review the development performance.**")
391
 
392
  if run_btn and train_test_file is not None:
393
  with st.status("Processing…", expanded=False) as status:
 
475
  # =========================
476
  if st.session_state.app_step == "predict":
477
  st.sidebar.header("Prediction (Validation)")
478
+ val_label = "Upload Validation Excel"
479
+ if st.session_state.get("val_file_name"):
480
+ val_label = "Replace data (Excel)"
481
+ validation_file = st.sidebar.file_uploader(val_label, type=["xlsx","xls"], key="val_upload")
482
+ if validation_file is not None:
483
+ st.session_state.val_file_name = validation_file.name
484
+ _book_tmp = read_book(validation_file)
485
+ if _book_tmp:
486
+ first_df = next(iter(_book_tmp.values()))
487
+ st.sidebar.caption(f"**Data loaded:** {validation_file.name} • {first_df.shape[0]} rows × {first_df.shape[1]} cols")
488
 
489
  # Preview button for validation data
490
  preview_val_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=(validation_file is None))
491
  if preview_val_btn and validation_file is not None:
492
  _book = read_book(validation_file)
493
+ preview_modal_val(_book, FEATURES)
494
 
495
  predict_btn = st.sidebar.button("Predict", type="primary", use_container_width=True)
496
  st.sidebar.button("⬅ Back", on_click=lambda: st.session_state.update(app_step="dev"), use_container_width=True)
 
590
  st.warning(str(e))
591
 
592
  # =========================
593
+ # Footer (with website link)
594
  # =========================
595
  st.markdown("---")
596
+ st.markdown(
597
+ "<div style='text-align:center; color:#6b7280;'>"
598
+ "ST_GeoMech_UCS • © Smart Thinking • "
599
+ "<a href='https://www.smartthinking.com.sa' target='_blank'>smartthinking.com.sa</a>"
600
+ "</div>",
601
+ unsafe_allow_html=True
602
+ )