Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -80,9 +80,9 @@ def parse_excel(data_bytes: bytes):
|
|
| 80 |
xl = pd.ExcelFile(bio)
|
| 81 |
return {sh: xl.parse(sh) for sh in xl.sheet_names}
|
| 82 |
|
| 83 |
-
def
|
| 84 |
-
if
|
| 85 |
-
try: return parse_excel(
|
| 86 |
except Exception as e:
|
| 87 |
st.error(f"Failed to read Excel: {e}"); return {}
|
| 88 |
|
|
@@ -114,7 +114,7 @@ def depth_or_index_track(df, title=None, include_actual=True):
|
|
| 114 |
ax.plot(df["UCS_Pred"], df[depth_col], '-', lw=1.8, color=COLORS["pred"], label="UCS_Pred")
|
| 115 |
if include_actual and TARGET in df.columns:
|
| 116 |
ax.plot(df[TARGET], df[depth_col], ':', lw=2.0, color=COLORS["actual"], alpha=0.95, label="UCS (actual)")
|
| 117 |
-
ax.
|
| 118 |
ax.xaxis.set_label_position('top'); ax.xaxis.tick_top(); ax.invert_yaxis()
|
| 119 |
else:
|
| 120 |
idx = np.arange(1, len(df) + 1)
|
|
@@ -275,14 +275,20 @@ else:
|
|
| 275 |
if "app_step" not in st.session_state: st.session_state.app_step = "intro"
|
| 276 |
if "results" not in st.session_state: st.session_state.results = {}
|
| 277 |
if "train_ranges" not in st.session_state: st.session_state.train_ranges = None
|
| 278 |
-
if "dev_ready" not in st.session_state: st.session_state.dev_ready = False
|
| 279 |
-
if "dev_file_loaded" not in st.session_state: st.session_state.dev_file_loaded = False
|
| 280 |
-
if "dev_previewed" not in st.session_state: st.session_state.dev_previewed = False
|
| 281 |
-
if "dev_file_signature" not in st.session_state: st.session_state.dev_file_signature = None
|
| 282 |
-
if "dev_preview_request" not in st.session_state: st.session_state.dev_preview_request = False
|
| 283 |
|
| 284 |
-
|
| 285 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 286 |
|
| 287 |
# =========================
|
| 288 |
# Hero header
|
|
@@ -308,7 +314,7 @@ if st.session_state.app_step == "intro":
|
|
| 308 |
st.markdown(
|
| 309 |
"This software is developed by *Smart Thinking AI-Solutions Team* to estimate UCS from drilling data."
|
| 310 |
)
|
| 311 |
-
st.subheader("
|
| 312 |
st.markdown(
|
| 313 |
"- Q, gpm — Flow rate (gallons per minute) \n"
|
| 314 |
"- SPP(psi) — Stand pipe pressure \n"
|
|
@@ -331,35 +337,43 @@ if st.session_state.app_step == "intro":
|
|
| 331 |
# =========================
|
| 332 |
if st.session_state.app_step == "dev":
|
| 333 |
st.sidebar.header("Model Development Data")
|
| 334 |
-
dev_label = "Upload Data (Excel)" if not st.session_state.
|
| 335 |
train_test_file = st.sidebar.file_uploader(dev_label, type=["xlsx","xls"], key="dev_upload")
|
| 336 |
|
| 337 |
-
# Detect new/changed file
|
| 338 |
if train_test_file is not None:
|
| 339 |
try:
|
| 340 |
-
|
|
|
|
| 341 |
except Exception:
|
| 342 |
-
|
|
|
|
| 343 |
sig = (train_test_file.name, size)
|
| 344 |
-
if sig != st.session_state.dev_file_signature:
|
| 345 |
-
# NEW upload => reset state
|
| 346 |
st.session_state.dev_file_signature = sig
|
| 347 |
st.session_state.dev_file_name = train_test_file.name
|
| 348 |
-
|
|
|
|
|
|
|
| 349 |
if _book_tmp:
|
| 350 |
first_df = next(iter(_book_tmp.values()))
|
| 351 |
-
st.
|
|
|
|
| 352 |
st.session_state.dev_file_loaded = True
|
| 353 |
st.session_state.dev_previewed = False
|
| 354 |
st.session_state.dev_ready = False
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 358 |
|
| 359 |
# Sidebar actions
|
| 360 |
preview_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=not st.session_state.dev_file_loaded)
|
| 361 |
-
if preview_btn and st.session_state.dev_file_loaded
|
| 362 |
-
st.session_state.dev_preview_request = True
|
| 363 |
|
| 364 |
run_btn = st.sidebar.button("Run Model", type="primary", use_container_width=True)
|
| 365 |
|
|
@@ -385,16 +399,16 @@ if st.session_state.app_step == "dev":
|
|
| 385 |
st.write("**Upload your data to build a case, then run the model to review development performance.**")
|
| 386 |
|
| 387 |
# If user clicked preview, open modal *after* helper so helper stays on top
|
| 388 |
-
if st.session_state.dev_preview_request and
|
| 389 |
-
_book =
|
| 390 |
st.session_state.dev_previewed = True
|
| 391 |
st.session_state.dev_preview_request = False
|
| 392 |
preview_modal_dev(_book, FEATURES)
|
| 393 |
|
| 394 |
-
# Run model
|
| 395 |
-
if run_btn and
|
| 396 |
with st.status("Processing…", expanded=False) as status:
|
| 397 |
-
book =
|
| 398 |
if not book: status.update(label="Failed to read workbook.", state="error"); st.stop()
|
| 399 |
status.update(label="Workbook read ✓")
|
| 400 |
sh_train = find_sheet(book, ["Train","Training","training2","train","training"])
|
|
@@ -470,21 +484,20 @@ if st.session_state.app_step == "dev":
|
|
| 470 |
st.warning(str(e))
|
| 471 |
|
| 472 |
# =========================
|
| 473 |
-
# PREDICTION (Validation)
|
| 474 |
# =========================
|
| 475 |
if st.session_state.app_step == "predict":
|
| 476 |
st.sidebar.header("Prediction (Validation)")
|
| 477 |
-
|
| 478 |
-
validation_file = st.sidebar.file_uploader(val_label, type=["xlsx","xls"], key="val_upload")
|
| 479 |
if validation_file is not None:
|
| 480 |
-
_book_tmp =
|
| 481 |
if _book_tmp:
|
| 482 |
first_df = next(iter(_book_tmp.values()))
|
| 483 |
st.sidebar.caption(f"**Data loaded:** {validation_file.name} • {first_df.shape[0]} rows × {first_df.shape[1]} cols")
|
| 484 |
|
| 485 |
preview_val_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=(validation_file is None))
|
| 486 |
if preview_val_btn and validation_file is not None:
|
| 487 |
-
_book =
|
| 488 |
preview_modal_val(_book, FEATURES)
|
| 489 |
|
| 490 |
predict_btn = st.sidebar.button("Predict", type="primary", use_container_width=True)
|
|
@@ -495,7 +508,7 @@ if st.session_state.app_step == "predict":
|
|
| 495 |
|
| 496 |
if predict_btn and validation_file is not None:
|
| 497 |
with st.status("Predicting…", expanded=False) as status:
|
| 498 |
-
vbook =
|
| 499 |
if not vbook: status.update(label="Could not read the Validation Excel.", state="error"); st.stop()
|
| 500 |
status.update(label="Workbook read ✓")
|
| 501 |
vname = find_sheet(vbook, ["Validation","Validate","validation2","Val","val"]) or list(vbook.keys())[0]
|
|
@@ -581,10 +594,11 @@ if st.session_state.app_step == "predict":
|
|
| 581 |
# =========================
|
| 582 |
st.markdown("---")
|
| 583 |
st.markdown(
|
| 584 |
-
"
|
| 585 |
-
|
| 586 |
-
|
| 587 |
-
|
| 588 |
-
|
|
|
|
| 589 |
unsafe_allow_html=True
|
| 590 |
)
|
|
|
|
| 80 |
xl = pd.ExcelFile(bio)
|
| 81 |
return {sh: xl.parse(sh) for sh in xl.sheet_names}
|
| 82 |
|
| 83 |
+
def read_book_bytes(data_bytes: bytes):
|
| 84 |
+
if not data_bytes: return {}
|
| 85 |
+
try: return parse_excel(data_bytes)
|
| 86 |
except Exception as e:
|
| 87 |
st.error(f"Failed to read Excel: {e}"); return {}
|
| 88 |
|
|
|
|
| 114 |
ax.plot(df["UCS_Pred"], df[depth_col], '-', lw=1.8, color=COLORS["pred"], label="UCS_Pred")
|
| 115 |
if include_actual and TARGET in df.columns:
|
| 116 |
ax.plot(df[TARGET], df[depth_col], ':', lw=2.0, color=COLORS["actual"], alpha=0.95, label="UCS (actual)")
|
| 117 |
+
ax.set.ylabel(depth_col); ax.set_xlabel("UCS")
|
| 118 |
ax.xaxis.set_label_position('top'); ax.xaxis.tick_top(); ax.invert_yaxis()
|
| 119 |
else:
|
| 120 |
idx = np.arange(1, len(df) + 1)
|
|
|
|
| 275 |
if "app_step" not in st.session_state: st.session_state.app_step = "intro"
|
| 276 |
if "results" not in st.session_state: st.session_state.results = {}
|
| 277 |
if "train_ranges" not in st.session_state: st.session_state.train_ranges = None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
+
# Dev page state (persist file)
|
| 280 |
+
for k, v in {
|
| 281 |
+
"dev_ready": False,
|
| 282 |
+
"dev_file_loaded": False,
|
| 283 |
+
"dev_previewed": False,
|
| 284 |
+
"dev_file_signature": None,
|
| 285 |
+
"dev_preview_request": False,
|
| 286 |
+
"dev_file_bytes": b"",
|
| 287 |
+
"dev_file_name": "",
|
| 288 |
+
"dev_file_rows": 0,
|
| 289 |
+
"dev_file_cols": 0,
|
| 290 |
+
}.items():
|
| 291 |
+
if k not in st.session_state: st.session_state[k] = v
|
| 292 |
|
| 293 |
# =========================
|
| 294 |
# Hero header
|
|
|
|
| 314 |
st.markdown(
|
| 315 |
"This software is developed by *Smart Thinking AI-Solutions Team* to estimate UCS from drilling data."
|
| 316 |
)
|
| 317 |
+
st.subheader("Expected Input Features")
|
| 318 |
st.markdown(
|
| 319 |
"- Q, gpm — Flow rate (gallons per minute) \n"
|
| 320 |
"- SPP(psi) — Stand pipe pressure \n"
|
|
|
|
| 337 |
# =========================
|
| 338 |
if st.session_state.app_step == "dev":
|
| 339 |
st.sidebar.header("Model Development Data")
|
| 340 |
+
dev_label = "Upload Data (Excel)" if not st.session_state.dev_file_name else "Replace data (Excel)"
|
| 341 |
train_test_file = st.sidebar.file_uploader(dev_label, type=["xlsx","xls"], key="dev_upload")
|
| 342 |
|
| 343 |
+
# Detect new/changed file and PERSIST BYTES
|
| 344 |
if train_test_file is not None:
|
| 345 |
try:
|
| 346 |
+
file_bytes = train_test_file.getvalue()
|
| 347 |
+
size = len(file_bytes)
|
| 348 |
except Exception:
|
| 349 |
+
file_bytes = b""
|
| 350 |
+
size = 0
|
| 351 |
sig = (train_test_file.name, size)
|
| 352 |
+
if sig != st.session_state.dev_file_signature and size > 0:
|
|
|
|
| 353 |
st.session_state.dev_file_signature = sig
|
| 354 |
st.session_state.dev_file_name = train_test_file.name
|
| 355 |
+
st.session_state.dev_file_bytes = file_bytes
|
| 356 |
+
# Inspect first sheet for rows/cols
|
| 357 |
+
_book_tmp = read_book_bytes(file_bytes)
|
| 358 |
if _book_tmp:
|
| 359 |
first_df = next(iter(_book_tmp.values()))
|
| 360 |
+
st.session_state.dev_file_rows = int(first_df.shape[0])
|
| 361 |
+
st.session_state.dev_file_cols = int(first_df.shape[1])
|
| 362 |
st.session_state.dev_file_loaded = True
|
| 363 |
st.session_state.dev_previewed = False
|
| 364 |
st.session_state.dev_ready = False
|
| 365 |
+
|
| 366 |
+
# Sidebar caption (from persisted info)
|
| 367 |
+
if st.session_state.dev_file_loaded:
|
| 368 |
+
st.sidebar.caption(
|
| 369 |
+
f"**Data loaded:** {st.session_state.dev_file_name} • "
|
| 370 |
+
f"{st.session_state.dev_file_rows} rows × {st.session_state.dev_file_cols} cols"
|
| 371 |
+
)
|
| 372 |
|
| 373 |
# Sidebar actions
|
| 374 |
preview_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=not st.session_state.dev_file_loaded)
|
| 375 |
+
if preview_btn and st.session_state.dev_file_loaded:
|
| 376 |
+
st.session_state.dev_preview_request = True
|
| 377 |
|
| 378 |
run_btn = st.sidebar.button("Run Model", type="primary", use_container_width=True)
|
| 379 |
|
|
|
|
| 399 |
st.write("**Upload your data to build a case, then run the model to review development performance.**")
|
| 400 |
|
| 401 |
# If user clicked preview, open modal *after* helper so helper stays on top
|
| 402 |
+
if st.session_state.dev_preview_request and st.session_state.dev_file_bytes:
|
| 403 |
+
_book = read_book_bytes(st.session_state.dev_file_bytes)
|
| 404 |
st.session_state.dev_previewed = True
|
| 405 |
st.session_state.dev_preview_request = False
|
| 406 |
preview_modal_dev(_book, FEATURES)
|
| 407 |
|
| 408 |
+
# Run model (from persisted bytes)
|
| 409 |
+
if run_btn and st.session_state.dev_file_bytes:
|
| 410 |
with st.status("Processing…", expanded=False) as status:
|
| 411 |
+
book = read_book_bytes(st.session_state.dev_file_bytes)
|
| 412 |
if not book: status.update(label="Failed to read workbook.", state="error"); st.stop()
|
| 413 |
status.update(label="Workbook read ✓")
|
| 414 |
sh_train = find_sheet(book, ["Train","Training","training2","train","training"])
|
|
|
|
| 484 |
st.warning(str(e))
|
| 485 |
|
| 486 |
# =========================
|
| 487 |
+
# PREDICTION (Validation) — (kept simple; uploader works fine)
|
| 488 |
# =========================
|
| 489 |
if st.session_state.app_step == "predict":
|
| 490 |
st.sidebar.header("Prediction (Validation)")
|
| 491 |
+
validation_file = st.sidebar.file_uploader("Upload Validation Excel", type=["xlsx","xls"], key="val_upload")
|
|
|
|
| 492 |
if validation_file is not None:
|
| 493 |
+
_book_tmp = read_book_bytes(validation_file.getvalue())
|
| 494 |
if _book_tmp:
|
| 495 |
first_df = next(iter(_book_tmp.values()))
|
| 496 |
st.sidebar.caption(f"**Data loaded:** {validation_file.name} • {first_df.shape[0]} rows × {first_df.shape[1]} cols")
|
| 497 |
|
| 498 |
preview_val_btn = st.sidebar.button("Preview data", use_container_width=True, disabled=(validation_file is None))
|
| 499 |
if preview_val_btn and validation_file is not None:
|
| 500 |
+
_book = read_book_bytes(validation_file.getvalue())
|
| 501 |
preview_modal_val(_book, FEATURES)
|
| 502 |
|
| 503 |
predict_btn = st.sidebar.button("Predict", type="primary", use_container_width=True)
|
|
|
|
| 508 |
|
| 509 |
if predict_btn and validation_file is not None:
|
| 510 |
with st.status("Predicting…", expanded=False) as status:
|
| 511 |
+
vbook = read_book_bytes(validation_file.getvalue())
|
| 512 |
if not vbook: status.update(label="Could not read the Validation Excel.", state="error"); st.stop()
|
| 513 |
status.update(label="Workbook read ✓")
|
| 514 |
vname = find_sheet(vbook, ["Validation","Validate","validation2","Val","val"]) or list(vbook.keys())[0]
|
|
|
|
| 594 |
# =========================
|
| 595 |
st.markdown("---")
|
| 596 |
st.markdown(
|
| 597 |
+
"""
|
| 598 |
+
<div style='text-align:center; color:#6b7280; line-height:1.6'>
|
| 599 |
+
ST_GeoMech_UCS • © Smart Thinking<br/>
|
| 600 |
+
<strong>Visit our website:</strong> <a href='https://www.smartthinking.com.sa' target='_blank'>smartthinking.com.sa</a>
|
| 601 |
+
</div>
|
| 602 |
+
""",
|
| 603 |
unsafe_allow_html=True
|
| 604 |
)
|