Update src/streamlit_app.py
Browse files9 unmodified lines101112131415161713 unmodified lines313233323435343636373738404142394041424344454647484950455147525354505152535455555657585960606162626364656619 unmodified lines8687888889909192679 unmodified lines772773774774775776777778126 unmodified lines9059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529109539549559569 unmodified lines9669679689269699 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 ephemeral paths (no Hugging Face data mount)# Base directory and persistent logsBASE_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")# 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): """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") f.write(f"[{datetime.now():%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")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: {DATA_DIR}") st.sidebar.success(f" Using persistent storage | Run directory: {RUN_DIR}")else: st.sidebar.warning(f" Using ephemeral storage: {DATA_DIR}. Data will be lost on rebuild.") st.sidebar.warning(f" Using ephemeral storage | Run directory: {RUN_DIR}. Data will be lost on rebuild.")
# -------------------------19 unmodified lines variance_overrides: dict mapping feature name or substring → stddev multiplier """ np.random.seed(random_seed) os.makedirs(DATA_DIR, exist_ok=True) os.makedirs(RUN_DIR, exist_ok=True) if variance_overrides is None: variance_overrides = {}
679 unmodified lines st.pyplot(fig) # Save trained stack artifacts stack_artifact = os.path.join(DATA_DIR, f"stacked_{use_case.replace(' ', '_')}.joblib") stack_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 files files = [ 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 download zip_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.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 lines with 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) st.download_button(" Download Log", content, file_name=latest)src/streamlit_app.py+64-21