minor changes
Browse files
app.py
CHANGED
|
@@ -13,7 +13,6 @@ from google.genai import types
|
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
-
# keep a reference to the real number_input
|
| 17 |
_original_number_input = st.number_input
|
| 18 |
|
| 19 |
def safe_number_input(label, **kwargs):
|
|
@@ -24,7 +23,6 @@ def safe_number_input(label, **kwargs):
|
|
| 24 |
min_value = kwargs.get("min_value", float("-inf"))
|
| 25 |
max_value = kwargs.get("max_value", float("inf"))
|
| 26 |
value = kwargs.get("value", min_value)
|
| 27 |
-
# clamp
|
| 28 |
clamped = min(max(value, min_value), max_value)
|
| 29 |
if clamped != value:
|
| 30 |
st.warning(
|
|
@@ -34,21 +32,18 @@ def safe_number_input(label, **kwargs):
|
|
| 34 |
kwargs["value"] = clamped
|
| 35 |
return _original_number_input(label, **kwargs)
|
| 36 |
|
| 37 |
-
# override streamlit's number_input globally
|
| 38 |
st.number_input = safe_number_input
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
-
# directly use your Gemini key
|
| 43 |
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 44 |
if not gemini_key:
|
| 45 |
-
st.error("
|
| 46 |
st.stop()
|
| 47 |
|
| 48 |
client = genai.Client(api_key=gemini_key)
|
| 49 |
|
| 50 |
|
| 51 |
-
# βββ Scaffold Quality Function βββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 52 |
def scaffold_quality_combined(printability, cell_response,
|
| 53 |
weight_printability=0.3, weight_cell_response=0.7):
|
| 54 |
if printability == 0:
|
|
@@ -64,7 +59,6 @@ def scaffold_quality_combined(printability, cell_response,
|
|
| 64 |
mc = (norm_p**weight_printability) * (norm_c**weight_cell_response)
|
| 65 |
return 100 * ((hm + mc) / 2.0)
|
| 66 |
|
| 67 |
-
# βββ Constants ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 68 |
BIOMATERIAL_OPTIONS = [
|
| 69 |
"Alginate (%w/v)",
|
| 70 |
"PVA-HA (%w/v)",
|
|
@@ -378,7 +372,6 @@ PRINT_PARAM_NAMES = [
|
|
| 378 |
"Substrate Temperature (Β°C)",
|
| 379 |
]
|
| 380 |
|
| 381 |
-
# βββ Load Encoder, Scalers, Models ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 382 |
@st.cache_resource
|
| 383 |
def load_encoder():
|
| 384 |
return joblib.load('cell_line_encoder.joblib')
|
|
@@ -403,7 +396,6 @@ model_print, model_cell = load_models()
|
|
| 403 |
feature_order_print = list(scaler_print.feature_names_in_)
|
| 404 |
feature_order_cell = list(scaler_cell.feature_names_in_)
|
| 405 |
|
| 406 |
-
# βββ Session State Initialization ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 407 |
if 'bio_rows' not in st.session_state:
|
| 408 |
st.session_state.bio_rows = [{
|
| 409 |
'mat': BIOMATERIAL_OPTIONS[0],
|
|
@@ -419,7 +411,6 @@ if 'pp_ranges' not in st.session_state:
|
|
| 419 |
for name in PRINT_PARAM_NAMES
|
| 420 |
}
|
| 421 |
|
| 422 |
-
# βββ App Layout ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 423 |
st.title("𧬠MLATE: Machine Learning Applications in Tissue Engieering")
|
| 424 |
st.markdown(
|
| 425 |
"<p style='font-size:1.2em; color:grey;'>"
|
|
@@ -430,7 +421,6 @@ st.markdown(
|
|
| 430 |
unsafe_allow_html=True
|
| 431 |
)
|
| 432 |
|
| 433 |
-
# βββ Biomaterials Section ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 434 |
st.subheader("Biomaterials (enter range for each)")
|
| 435 |
if st.button("β Add Biomaterial"):
|
| 436 |
used = {r['mat'] for r in st.session_state.bio_rows}
|
|
@@ -459,7 +449,6 @@ for i, row in enumerate(st.session_state.bio_rows):
|
|
| 459 |
"Min", min_value=0.0, max_value=row['max'],
|
| 460 |
value=row['min'], step=row['step'], key=f"bio_min_{i}"
|
| 461 |
)
|
| 462 |
-
# β min_value for Max is now the step; default value at least step
|
| 463 |
mx = c3.number_input(
|
| 464 |
"Max", min_value=row['step'], max_value=100.0,
|
| 465 |
value=max(row['max'], row['step']), step=row['step'],
|
|
@@ -479,7 +468,6 @@ for i, row in enumerate(st.session_state.bio_rows):
|
|
| 479 |
|
| 480 |
st.markdown("---")
|
| 481 |
|
| 482 |
-
# βββ Cell Line & Density Section ββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 483 |
st.subheader("Cell Line & Density (10^6 cells/ml)")
|
| 484 |
col1, col2, col3, col4 = st.columns([2,1,1,1])
|
| 485 |
cell_line = col1.selectbox("Cell Line", CELL_LINE_OPTIONS)
|
|
@@ -489,7 +477,6 @@ dmin = col2.number_input(
|
|
| 489 |
"Min Density", min_value=0.0, max_value=dr['max'],
|
| 490 |
value=dr['min'], step=dr['step'], key="cd_min"
|
| 491 |
)
|
| 492 |
-
# β ensure Max Density cannot go below the step
|
| 493 |
dmax = col3.number_input(
|
| 494 |
"Max Density", min_value=dr['step'], max_value=1000.0,
|
| 495 |
value=max(dr['max'], dr['step']), step=dr['step'], key="cd_max"
|
|
@@ -504,7 +491,6 @@ dr['step'] = col4.number_input(
|
|
| 504 |
|
| 505 |
st.markdown("---")
|
| 506 |
|
| 507 |
-
# βββ Printing Parameters Section βββββββββββββββββββββββββββββββοΏ½οΏ½ββββββββββββββββ
|
| 508 |
st.subheader("Printing Parameters (enter range)")
|
| 509 |
for name in PRINT_PARAM_NAMES:
|
| 510 |
pmin = st.session_state.pp_ranges[name]['min']
|
|
@@ -517,7 +503,6 @@ for name in PRINT_PARAM_NAMES:
|
|
| 517 |
"Min", min_value=0.0, max_value=pmax,
|
| 518 |
value=pmin, step=pstep, key=f"pp_min_{name}"
|
| 519 |
)
|
| 520 |
-
# β enforce Max β₯ step
|
| 521 |
pmax = c3.number_input(
|
| 522 |
"Max", min_value=pstep, max_value=10000.0,
|
| 523 |
value=max(pmax, pstep), step=pstep, key=f"pp_max_{name}"
|
|
@@ -532,7 +517,6 @@ for name in PRINT_PARAM_NAMES:
|
|
| 532 |
|
| 533 |
st.markdown("---")
|
| 534 |
|
| 535 |
-
# βββ Optuna Optimize & Display ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 536 |
if st.button("π οΈ Optimize WSSQ"):
|
| 537 |
with st.spinner("Running Optunaβ¦"):
|
| 538 |
def objective(trial):
|
|
@@ -588,7 +572,6 @@ if st.button("π οΈ Optimize WSSQ"):
|
|
| 588 |
.to_frame()
|
| 589 |
st.table(best_df)
|
| 590 |
|
| 591 |
-
# βββ Fabrication Procedure via GPT βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 592 |
st.markdown("## π§ͺ Fabrication Procedure")
|
| 593 |
with st.spinner("Generating fabrication procedureβ¦"):
|
| 594 |
density = best.params.get("cell_density", 0.0)
|
|
|
|
| 13 |
|
| 14 |
|
| 15 |
|
|
|
|
| 16 |
_original_number_input = st.number_input
|
| 17 |
|
| 18 |
def safe_number_input(label, **kwargs):
|
|
|
|
| 23 |
min_value = kwargs.get("min_value", float("-inf"))
|
| 24 |
max_value = kwargs.get("max_value", float("inf"))
|
| 25 |
value = kwargs.get("value", min_value)
|
|
|
|
| 26 |
clamped = min(max(value, min_value), max_value)
|
| 27 |
if clamped != value:
|
| 28 |
st.warning(
|
|
|
|
| 32 |
kwargs["value"] = clamped
|
| 33 |
return _original_number_input(label, **kwargs)
|
| 34 |
|
|
|
|
| 35 |
st.number_input = safe_number_input
|
| 36 |
|
| 37 |
|
| 38 |
|
|
|
|
| 39 |
gemini_key = os.getenv("GEMINI_API_KEY")
|
| 40 |
if not gemini_key:
|
| 41 |
+
st.error("Gemini API key not found. Please set GEMINI_API_KEY in your Space settings.")
|
| 42 |
st.stop()
|
| 43 |
|
| 44 |
client = genai.Client(api_key=gemini_key)
|
| 45 |
|
| 46 |
|
|
|
|
| 47 |
def scaffold_quality_combined(printability, cell_response,
|
| 48 |
weight_printability=0.3, weight_cell_response=0.7):
|
| 49 |
if printability == 0:
|
|
|
|
| 59 |
mc = (norm_p**weight_printability) * (norm_c**weight_cell_response)
|
| 60 |
return 100 * ((hm + mc) / 2.0)
|
| 61 |
|
|
|
|
| 62 |
BIOMATERIAL_OPTIONS = [
|
| 63 |
"Alginate (%w/v)",
|
| 64 |
"PVA-HA (%w/v)",
|
|
|
|
| 372 |
"Substrate Temperature (Β°C)",
|
| 373 |
]
|
| 374 |
|
|
|
|
| 375 |
@st.cache_resource
|
| 376 |
def load_encoder():
|
| 377 |
return joblib.load('cell_line_encoder.joblib')
|
|
|
|
| 396 |
feature_order_print = list(scaler_print.feature_names_in_)
|
| 397 |
feature_order_cell = list(scaler_cell.feature_names_in_)
|
| 398 |
|
|
|
|
| 399 |
if 'bio_rows' not in st.session_state:
|
| 400 |
st.session_state.bio_rows = [{
|
| 401 |
'mat': BIOMATERIAL_OPTIONS[0],
|
|
|
|
| 411 |
for name in PRINT_PARAM_NAMES
|
| 412 |
}
|
| 413 |
|
|
|
|
| 414 |
st.title("𧬠MLATE: Machine Learning Applications in Tissue Engieering")
|
| 415 |
st.markdown(
|
| 416 |
"<p style='font-size:1.2em; color:grey;'>"
|
|
|
|
| 421 |
unsafe_allow_html=True
|
| 422 |
)
|
| 423 |
|
|
|
|
| 424 |
st.subheader("Biomaterials (enter range for each)")
|
| 425 |
if st.button("β Add Biomaterial"):
|
| 426 |
used = {r['mat'] for r in st.session_state.bio_rows}
|
|
|
|
| 449 |
"Min", min_value=0.0, max_value=row['max'],
|
| 450 |
value=row['min'], step=row['step'], key=f"bio_min_{i}"
|
| 451 |
)
|
|
|
|
| 452 |
mx = c3.number_input(
|
| 453 |
"Max", min_value=row['step'], max_value=100.0,
|
| 454 |
value=max(row['max'], row['step']), step=row['step'],
|
|
|
|
| 468 |
|
| 469 |
st.markdown("---")
|
| 470 |
|
|
|
|
| 471 |
st.subheader("Cell Line & Density (10^6 cells/ml)")
|
| 472 |
col1, col2, col3, col4 = st.columns([2,1,1,1])
|
| 473 |
cell_line = col1.selectbox("Cell Line", CELL_LINE_OPTIONS)
|
|
|
|
| 477 |
"Min Density", min_value=0.0, max_value=dr['max'],
|
| 478 |
value=dr['min'], step=dr['step'], key="cd_min"
|
| 479 |
)
|
|
|
|
| 480 |
dmax = col3.number_input(
|
| 481 |
"Max Density", min_value=dr['step'], max_value=1000.0,
|
| 482 |
value=max(dr['max'], dr['step']), step=dr['step'], key="cd_max"
|
|
|
|
| 491 |
|
| 492 |
st.markdown("---")
|
| 493 |
|
|
|
|
| 494 |
st.subheader("Printing Parameters (enter range)")
|
| 495 |
for name in PRINT_PARAM_NAMES:
|
| 496 |
pmin = st.session_state.pp_ranges[name]['min']
|
|
|
|
| 503 |
"Min", min_value=0.0, max_value=pmax,
|
| 504 |
value=pmin, step=pstep, key=f"pp_min_{name}"
|
| 505 |
)
|
|
|
|
| 506 |
pmax = c3.number_input(
|
| 507 |
"Max", min_value=pstep, max_value=10000.0,
|
| 508 |
value=max(pmax, pstep), step=pstep, key=f"pp_max_{name}"
|
|
|
|
| 517 |
|
| 518 |
st.markdown("---")
|
| 519 |
|
|
|
|
| 520 |
if st.button("π οΈ Optimize WSSQ"):
|
| 521 |
with st.spinner("Running Optunaβ¦"):
|
| 522 |
def objective(trial):
|
|
|
|
| 572 |
.to_frame()
|
| 573 |
st.table(best_df)
|
| 574 |
|
|
|
|
| 575 |
st.markdown("## π§ͺ Fabrication Procedure")
|
| 576 |
with st.spinner("Generating fabrication procedureβ¦"):
|
| 577 |
density = best.params.get("cell_density", 0.0)
|