import streamlit as st, pandas as pd, numpy as np, os, joblib st.set_page_config(page_title="GelPredict — MVP", layout="centered") st.title("🧪 GelPredict — ECM Gelation Recommender (MVP)") clf_path = "models/gel_classifier.joblib" reg_path = "models/thalf_regressor.joblib" clf, reg = None, None if os.path.exists(clf_path): clf = joblib.load(clf_path) if os.path.exists(reg_path): reg = joblib.load(reg_path) if clf is None: st.warning("No trained classifier found. Train first with your CSV.") else: st.success("Classifier loaded.") with st.form("inputs"): c1, c2 = st.columns(2) ecm_total_protein_mgml = c1.number_input("Total protein (mg/mL)", 0.0, 50.0, 7.0, 0.1) pre_pH = c2.number_input("Pre-neutralization pH", 1.0, 7.0, 4.0, 0.1) pre_cond = c1.number_input("Pre-neutralization conductivity (mS/cm)", 0.0, 50.0, 3.0, 0.1) temp_inc = c2.number_input("Incubation temp (°C)", 4.0, 45.0, 37.0, 0.5) uv_ratio = c1.number_input("UV A220/A280", 0.0, 20.0, 5.0, 0.1) sds_smear = c2.number_input("SDS smear index (0-1)", 0.0, 1.0, 0.4, 0.01) sds_alpha = c1.number_input("SDS α-band ratio", 0.0, 5.0, 0.9, 0.05) dmem_pct = c2.number_input("DMEM % added (test)", 0.0, 50.0, 0.0, 0.5) pbs_10x = c1.number_input("PBS 10x µL per mL (test)", 0.0, 200.0, 0.0, 1.0) nacl_mM = c2.number_input("NaCl mM added (test)", 0.0, 300.0, 120.0, 5.0) cacl2_mM = c1.number_input("CaCl2 mM (if relevant)", 0.0, 100.0, 0.0, 1.0) target_pH = c2.number_input("Target pH (test)", 6.6, 8.0, 7.2, 0.05) submitted = st.form_submit_button("🔮 Recommend Recipe") if submitted and clf is not None: base = dict( ecm_total_protein_mgml=float(ecm_total_protein_mgml), pre_pH=float(pre_pH), pre_conductivity_mS=float(pre_cond), temp_incubation_C=float(temp_inc), uv_ratio_A220_A280=float(uv_ratio), sds_smear_index=float(sds_smear), sds_alpha_ratio=float(sds_alpha), DMEM_percent=float(dmem_pct), PBS_10x_uL_per_mL=float(pbs_10x), NaCl_mM_added=float(nacl_mM), CaCl2_mM_added=float(cacl2_mM), target_pH=float(target_pH) ) PH_GRID = np.linspace(7.0, 7.6, 13) IONIC_GRID = np.linspace(100, 170, 15) TEMP_GRID = np.array([25.0, 37.0]) def score(x): X = np.array([[x.get(f, np.nan) for f in clf["features"]]]) pgel = clf["model"].predict_proba(X)[0,1] thalf = 30.0 if reg is not None: Xr = np.array([[x.get(f, np.nan) for f in reg["features"]]]) try: thalf = float(reg["model"].predict(Xr)[0]) except Exception: thalf = 30.0 util = pgel - 0.25*(thalf/60.0) return util, pgel, thalf best = None for pH in PH_GRID: for mM in IONIC_GRID: for tC in TEMP_GRID: x = base.copy() x["target_pH"] = float(pH) x["NaCl_mM_added"] = float(mM) x["temp_incubation_C"] = float(tC) util, pgel, thalf = score(x) if (best is None) or (util > best[0]): best = (util, pgel, thalf, x) util, pgel, thalf, x = best suggested_pbs_10x = max(0.0, (x["NaCl_mM_added"] - 100) * 0.5) st.subheader("Recommendation") st.json({ "recipe": { "target_pH": round(x["target_pH"], 2), "PBS_10x_uL_per_mL": round(suggested_pbs_10x, 1), "temp_incubation_C": x["temp_incubation_C"] }, "predictions": { "P_gel": round(pgel, 3), "t_half_min": round(thalf, 1), "utility": round(util, 3) } }) st.caption("PBS mapping is a placeholder; calibrate using your batch logs.")