Update src/streamlit_app.py
Browse files9 unmodified lines10111213141513 unmodified lines2930313233343536373839404142434445464748495051525354555657585960616263646519 unmodified lines85868788899091679 unmodified lines771772773774775776777126 unmodified lines9049059069079089099109119129139 unmodified lines9239249259269 unmodified linesimport matplotlib.pyplot as pltimport seaborn as snsimport joblib# ML importsfrom sklearn.model_selection import train_test_split13 unmodified lines# -------------------------st.set_page_config(page_title="Steel Authority of India Limited (MODEX)", layout="wide")# Base ephemeral paths (no Hugging Face data mount)BASE_DIR = "./"LOG_DIR = os.path.join(BASE_DIR, "logs")DATA_DIR = os.path.join(LOG_DIR, "data_ephemeral")os.makedirs(DATA_DIR, exist_ok=True)os.makedirs(LOG_DIR, exist_ok=True)# Timestamped log filetimestamp = datetime.now().strftime("%Y%m%d_%H%M%S")LOG_PATH = os.path.join(LOG_DIR, f"run_{timestamp}.log")def log(msg: str):"""Log message with timestamp to /logs/ for ephemeral HF runs."""with open(LOG_PATH, "a", encoding="utf-8") as f:f.write(f"[{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}] {msg}\n")print(msg)log(" Streamlit session started.")log(f"Python PID={os.getpid()} | Time={datetime.now().isoformat()}")log(f"Data Dir = {DATA_DIR} | Log Dir = {LOG_DIR}")CSV_PATH = os.path.join(DATA_DIR, "flatfile_universe_advanced.csv")META_PATH = os.path.join(DATA_DIR, "feature_metadata_advanced.json")ENSEMBLE_ARTIFACT = os.path.join(DATA_DIR, "ensemble_models.joblib")# Confirm storage mountif os.path.exists("/data"):st.sidebar.success(f" Using persistent storage: {DATA_DIR}")else:st.sidebar.warning(f" Using ephemeral storage: {DATA_DIR}. Data will be lost on rebuild.")# -------------------------19 unmodified linesvariance_overrides: dict mapping feature name or substring → stddev multiplier"""np.random.seed(random_seed)os.makedirs(DATA_DIR, exist_ok=True)if variance_overrides is None:variance_overrides = {}679 unmodified linesst.pyplot(fig)# Save trained stack artifactsstack_artifact = os.path.join(DATA_DIR, f"stacked_{use_case.replace(' ', '_')}.joblib")to_save = {"base_models": {bm["family"]: bm["model"] for bm in base_models if bm["family"] in selected},"meta": meta,126 unmodified lines# -------------------------st.markdown("---")st.markdown("**Notes:** This dataset is synthetic and for demo/prototyping. Real plant integration requires NDA, data on-boarding, sensor mapping, and plant safety checks before any control actions.")# ----- Logs tabtabs.append("View Logs")with tabs[-1]:st.subheader("📜 Session & Model Logs")st.markdown("Each run creates a timestamped log file in `/logs/` inside this Space. Use this panel to review run progress and debug output.")log_files = sorted(9 unmodified lineswith open(path, "r", encoding="utf-8") as f:content = f.read()st.text_area("Log Output", content, height=400)st.download_button("⬇️ Download Log", content, file_name=latest)9 unmodified lines101112131415161713 unmodified lines31323334353637383940414243444546474849505152535455565758596061626364656619 unmodified lines86878889909192679 unmodified lines772773774775776777778126 unmodified lines9059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569 unmodified lines9669679689699 unmodified linesimport matplotlib.pyplot as pltimport seaborn as snsimport joblibimport zipfileimport io# ML importsfrom sklearn.model_selection import train_test_split13 unmodified lines# -------------------------st.set_page_config(page_title="Steel Authority of India Limited (MODEX)", layout="wide")# Base directory and persistent logsBASE_DIR = "./"LOG_DIR = os.path.join(BASE_DIR, "logs")os.makedirs(LOG_DIR, exist_ok=True)# Timestamped run subfolderrun_id = datetime.now().strftime("%Y%m%d_%H%M%S")RUN_DIR = os.path.join(LOG_DIR, f"run_{run_id}")os.makedirs(RUN_DIR, exist_ok=True)# File paths for this runCSV_PATH = os.path.join(RUN_DIR, "flatfile_universe_advanced.csv")META_PATH = os.path.join(RUN_DIR, "feature_metadata_advanced.json")ENSEMBLE_ARTIFACT = os.path.join(RUN_DIR, "ensemble_models.joblib")LOG_PATH = os.path.join(RUN_DIR, "run.log")def log(msg: str):with open(LOG_PATH, "a", encoding="utf-8") as f:f.write(f"[{datetime.now():%Y-%m-%d %H:%M:%S}] {msg}\n")print(msg)log(f" Streamlit session started | run_id={run_id}")log(f"Run directory: {RUN_DIR}")# Confirm storage mountif os.path.exists("/data"):st.sidebar.success(f" Using persistent storage | Run directory: {RUN_DIR}")else:st.sidebar.warning(f" Using ephemeral storage | Run directory: {RUN_DIR}. Data will be lost on rebuild.")# -------------------------19 unmodified linesvariance_overrides: dict mapping feature name or substring → stddev multiplier"""np.random.seed(random_seed)os.makedirs(RUN_DIR, exist_ok=True)if variance_overrides is None:variance_overrides = {}679 unmodified linesst.pyplot(fig)# Save trained stack artifactsstack_artifact = os.path.join(RUN_DIR, f"stacked_{use_case.replace(' ', '_')}.joblib")to_save = {"base_models": {bm["family"]: bm["model"] for bm in base_models if bm["family"] in selected},"meta": meta,126 unmodified lines# -------------------------st.markdown("---")st.markdown("**Notes:** This dataset is synthetic and for demo/prototyping. Real plant integration requires NDA, data on-boarding, sensor mapping, and plant safety checks before any control actions.")# ----- Download tabtabs.append("Download Saved Runs")with tabs[-1]:st.subheader("Reproducibility & Run Exports")run_folders = sorted([f for f in os.listdir(LOG_DIR) if f.startswith("run_")],reverse=True)if not run_folders:st.info("No completed runs found yet.")else:selected_run = st.selectbox("Select run folder", run_folders, index=0)selected_path = os.path.join(LOG_DIR, selected_run)# Show contained filesfiles = [f for f in os.listdir(selected_path)if os.path.isfile(os.path.join(selected_path, f))]st.write(f"Files in `{selected_run}`:")st.write(", ".join(files))# Zip the folder in-memory for downloadzip_buffer = io.BytesIO()with zipfile.ZipFile(zip_buffer, "w", zipfile.ZIP_DEFLATED) as zipf:for root, _, filenames in os.walk(selected_path):for fname in filenames:file_path = os.path.join(root, fname)zipf.write(file_path, arcname=os.path.relpath(file_path, selected_path))zip_buffer.seek(0)st.download_button(label=f"Download full run ({selected_run}.zip)",data=zip_buffer,file_name=f"{selected_run}.zip",mime="application/zip")# ----- Logs tabtabs.append("View Logs")with tabs[-1]:st.subheader(" Session & Model Logs")st.markdown("Each run creates a timestamped log file in `/logs/` inside this Space. Use this panel to review run progress and debug output.")log_files = sorted(9 unmodified lineswith open(path, "r", encoding="utf-8") as f:content = f.read()st.text_area("Log Output", content, height=400)st.download_button(" Download Log", content, file_name=latest)
src/streamlit_app.py+64-21