Spaces:
Sleeping
Sleeping
Download app.py from SPINBDO/spinbilstm: direct link, hf CLI and curl.
- Browser
- Download file 12.5 kB
-
https://huggingface.co/spaces/SPINBDO/spinbilstm/resolve/main/app.py
- Command line
-
hf download hf://spaces/SPINBDO/spinbilstm/app.py
-
curl -L -o app.py https://huggingface.co/spaces/SPINBDO/spinbilstm/resolve/main/app.py
12.5 kB
| import os | |
| import re | |
| import numpy as np | |
| import pandas as pd | |
| import gradio as gr | |
| import tensorflow as tf | |
| import joblib | |
| import pickle | |
| # ============================= | |
| # Fixed settings | |
| # ============================= | |
| SEQ_LEN = 4 | |
| RPM_DELTAS = (-200, -150, -100, -50, 50, 100, 150, 200) | |
| RPM_RANGE = (150.0, 650.0) | |
| MODEL_PATH = "model.keras" | |
| SCALER_PATH = "scaler.pkl" | |
| BASE_STEP_H = 2.0 # rollout base step (hours) | |
| # Keras 3 compatibility for biLSTM (ValueError ๋ฐฉ์ง) | |
| class CompatibleLSTM(tf.keras.layers.LSTM): | |
| def __init__(self, *args, **kwargs): | |
| kwargs.pop('time_major', None) | |
| super().__init__(*args, **kwargs) | |
| # ============================= | |
| # Robust CSV reader | |
| # ============================= | |
| def _read_csv_any_encoding(path: str, encodings=("utf-8-sig", "utf-8", "cp949", "euc-kr", "latin1")): | |
| last_err = None | |
| for enc in encodings: | |
| try: | |
| return pd.read_csv(path, encoding=enc, low_memory=False) | |
| except Exception as e: | |
| last_err = e | |
| raise last_err | |
| def _norm(s: str) -> str: | |
| s = str(s).strip().lower() | |
| s = re.sub(r"\s+", "", s) | |
| s = s.replace("_", "").replace("-", "").replace("(", "").replace(")", "").replace("/", "") | |
| return s | |
| def _pick_col(cols, candidates): | |
| cols_norm = {_norm(c): c for c in cols} | |
| for cand in candidates: | |
| if _norm(cand) in cols_norm: | |
| return cols_norm[_norm(cand)] | |
| for cand in candidates: | |
| cn = _norm(cand) | |
| for k_norm, orig in cols_norm.items(): | |
| if cn in k_norm: | |
| return orig | |
| return None | |
| # ============================= | |
| # Load artifacts | |
| # ============================= | |
| def _load_scaler(path: str): | |
| try: | |
| return joblib.load(path) | |
| except Exception: | |
| with open(path, "rb") as f: | |
| return pickle.load(f) | |
| def _get_feature_cols_from_scaler(scaler): | |
| if hasattr(scaler, "feature_names_in_"): | |
| return list(scaler.feature_names_in_) | |
| return None | |
| def _apply_scaling(scaler, X_np: np.ndarray, feature_cols: list) -> np.ndarray: | |
| if scaler is None: | |
| return X_np.astype(np.float32) | |
| # DataFrame์ผ๋ก ๋ณํํ์ฌ ์ค์ผ์ผ๋ฌ ์ ์ฉ (ํผ์ฒ ์์ ๋ณด์ฅ) | |
| X_df = pd.DataFrame(X_np, columns=feature_cols) | |
| try: | |
| return scaler.transform(X_df).astype(np.float32) | |
| except: | |
| # fallback: ์๋ ์ค์ผ์ผ๋ง | |
| mean_ = getattr(scaler, "mean_", 0.0) | |
| scale_ = getattr(scaler, "scale_", 1.0) | |
| scale_safe = np.where(scale_ == 0, 1.0, scale_) | |
| return ((X_np.astype(np.float32) - mean_) / scale_safe).astype(np.float32) | |
| # ============================= | |
| # delta_t builder | |
| # ============================= | |
| def add_delta_t(df_dyn: pd.DataFrame, last_delta: int) -> pd.DataFrame: | |
| if "Time" not in df_dyn.columns: | |
| raise ValueError("dynamic_data CSV must contain 'Time' column.") | |
| df = df_dyn.copy() | |
| df.columns = df.columns.map(str.strip) | |
| c_time = _pick_col(df.columns, ["Time", "Time(h)", "Time (h)"]) | |
| if c_time is None: | |
| raise ValueError("dynamic_data CSV must contain 'Time' column.") | |
| df[c_time] = pd.to_numeric(df[c_time], errors="coerce") | |
| df = df.dropna(subset=[c_time]).sort_values(c_time).reset_index(drop=True) | |
| df = df.rename(columns={c_time: "Time"}) | |
| t = df["Time"].to_numpy(dtype=float) | |
| dt = np.empty(len(df), dtype=float) | |
| if len(df) >= 2: | |
| dt[:-1] = t[1:] - t[:-1] | |
| dt[-1] = float(last_delta) | |
| df["delta_t"] = dt | |
| return df | |
| # ============================= | |
| # Dynamic/Static to model window | |
| # ============================= | |
| def build_last_window(df_dyn: pd.DataFrame, df_stat: pd.DataFrame, feature_cols: list, seq_len: int): | |
| if len(df_dyn) < seq_len: | |
| raise ValueError(f"dynamic_data row ์({len(df_dyn)})๊ฐ SEQ_LEN({seq_len})๋ณด๋ค ์์ต๋๋ค.") | |
| df_dyn = df_dyn.copy().rename(columns=str.strip) | |
| df_stat = df_stat.copy().rename(columns=str.strip) | |
| alias_map = { | |
| "BDO": ["BDO", "BDO(g/L)", "BDO (g/L)"], | |
| "CarbonContents": ["CarbonContents", "CarbonContents(g/L)", "CarbonContents (g/L)"], | |
| "OD": ["OD", "OD(AU)", "OD (AU)"], | |
| "Acetoin": ["Acetoin", "Acetoin(g/L)", "Acetoin (g/L)"], | |
| "Ethanol": ["Ethanol", "Ethanol(g/L)", "Ethanol (g/L)"], | |
| "rpm": ["rpm", "RPM", "RPM (RPM)", "RPM(RPM)"], | |
| "Feed": ["Feed", "Feed(g/L)", "Feed (g/L)"], | |
| "Time": ["Time", "Time(h)", "Time (h)"], | |
| "delta_t": ["delta_t", "deltat"], | |
| } | |
| cols = list(df_dyn.columns) | |
| for canon, aliases in alias_map.items(): | |
| picked = _pick_col(cols, aliases) | |
| if picked is not None and picked != canon: | |
| df_dyn = df_dyn.rename(columns={picked: canon}) | |
| stat_row = df_stat.iloc[0].to_dict() | |
| stat_norm_map = {_norm(k): k for k in df_stat.columns} | |
| def get_static_value(feature_name: str) -> float: | |
| if feature_name in stat_row: | |
| v = stat_row[feature_name] | |
| vv = pd.to_numeric(v, errors="coerce") | |
| return float(vv) if pd.notna(vv) else 0.0 | |
| fn = _norm(feature_name) | |
| if fn in stat_norm_map: | |
| k = stat_norm_map[fn] | |
| v = stat_row.get(k, 0.0) | |
| vv = pd.to_numeric(v, errors="coerce") | |
| return float(vv) if pd.notna(vv) else 0.0 | |
| return 0.0 | |
| df_win = df_dyn.sort_values("Time").reset_index(drop=True).iloc[-seq_len:].copy() | |
| for c in [fc for fc in feature_cols if fc in df_win.columns]: | |
| df_win[c] = pd.to_numeric(df_win[c], errors="coerce") | |
| df_win = df_win.ffill().bfill().fillna(0.0) | |
| X = np.zeros((seq_len, len(feature_cols)), dtype=np.float32) | |
| for i in range(seq_len): | |
| row = df_win.iloc[i] | |
| for j, fc in enumerate(feature_cols): | |
| if fc in df_win.columns: | |
| X[i, j] = float(row[fc]) | |
| else: | |
| X[i, j] = float(get_static_value(fc)) | |
| return X, df_win | |
| def _find_feature_index(feature_cols, names): | |
| norm_to_idx = {_norm(c): i for i, c in enumerate(feature_cols)} | |
| for n in names: | |
| nn = _norm(n) | |
| if nn in norm_to_idx: | |
| return norm_to_idx[nn] | |
| return None | |
| # ============================= | |
| # Rollout Logic | |
| # ============================= | |
| def _rollout_predict_bdo(model, scaler, feature_cols, X_init, horizon_h, base_step_h=2.0, clamp_non_decrease=True, override_last=None): | |
| horizon_h = float(horizon_h) | |
| bdo_in_idx = _find_feature_index(feature_cols, ["BDO"]) | |
| if horizon_h <= 0: | |
| return float(X_init[-1, bdo_in_idx]) if bdo_in_idx is not None else 0.0 | |
| steps = int(horizon_h // float(base_step_h)) | |
| rem = horizon_h - steps * float(base_step_h) | |
| time_in_idx = _find_feature_index(feature_cols, ["Time", "Time(h)", "Time (h)"]) | |
| dt_in_idx = _find_feature_index(feature_cols, ["delta_t", "deltat"]) | |
| rpm_in_idx = _find_feature_index(feature_cols, ["rpm", "RPM"]) | |
| X = X_init.astype(np.float32).copy() | |
| if override_last is not None and rpm_in_idx is not None: | |
| X[-1, rpm_in_idx] = float( | |
| override_last.get("rpm", override_last.get("RPM", X[-1, rpm_in_idx])) | |
| ) | |
| bdo_prev = float(X[-1, bdo_in_idx]) | |
| for _ in range(steps): | |
| if dt_in_idx is not None: | |
| X[-1, dt_in_idx] = float(base_step_h) | |
| X_scaled = _apply_scaling(scaler, X, feature_cols) | |
| y = model.predict(X_scaled[None, :, :], verbose=0)[0] | |
| bdo_pred = max(bdo_prev, float(y[0])) if clamp_non_decrease else float(y[0]) | |
| new_row = X[-1, :].copy() | |
| if time_in_idx is not None: | |
| new_row[time_in_idx] += float(base_step_h) | |
| if dt_in_idx is not None: | |
| new_row[dt_in_idx] = float(base_step_h) | |
| new_row[bdo_in_idx] = bdo_pred | |
| X = np.vstack([X[1:, :], new_row[None, :]]) | |
| bdo_prev = bdo_pred | |
| if rem > 1e-9: | |
| if dt_in_idx is not None: | |
| X[-1, dt_in_idx] = float(rem) | |
| X_scaled = _apply_scaling(scaler, X, feature_cols) | |
| y_rem = model.predict(X_scaled[None, :, :], verbose=0)[0] | |
| bdo_prev = max(bdo_prev, float(y_rem[0])) if clamp_non_decrease else float(y_rem[0]) | |
| return float(bdo_prev) | |
| # ============================= | |
| # Gradio handlers | |
| # ============================= | |
| def preview_csv(file_obj): | |
| if file_obj is None: return None | |
| try: | |
| df = _read_csv_any_encoding(file_obj.name) | |
| return df.head(10) | |
| except: | |
| return None | |
| def run_inference(dynamic_file, static_file, last_delta: int): | |
| if dynamic_file is None or static_file is None: | |
| return "dynamic/static CSV ๋ ํ์ผ์ ๋ชจ๋ ์ ๋ก๋ํด์ฃผ์ธ์." | |
| try: | |
| model = tf.keras.models.load_model(MODEL_PATH, compile=False, custom_objects={"LSTM": CompatibleLSTM}) | |
| scaler = _load_scaler(SCALER_PATH) | |
| feature_cols = _get_feature_cols_from_scaler(scaler) | |
| df_dyn = _read_csv_any_encoding(dynamic_file.name).rename(columns=str.strip) | |
| df_stat = _read_csv_any_encoding(static_file.name).rename(columns=str.strip) | |
| df_dyn = add_delta_t(df_dyn, last_delta=int(last_delta)) | |
| X_win, _ = build_last_window(df_dyn, df_stat, feature_cols, SEQ_LEN) | |
| bdo_idx = _find_feature_index(feature_cols, ["BDO"]) | |
| curr_bdo = float(X_win[-1, bdo_idx]) | |
| pred_bdo = _rollout_predict_bdo(model, scaler, feature_cols, X_win, float(last_delta)) | |
| rpm_msg = "" | |
| rpm_idx = _find_feature_index(feature_cols, ["rpm", "RPM"]) | |
| if rpm_idx is not None: | |
| base_rpm = float(X_win[-1, rpm_idx]) | |
| best_rpm, best_bdo, best_delta = base_rpm, pred_bdo, 0 | |
| # ํญ์ ํ์ฌ timepoint์์ RPM ์ต์ ํ ์ํ | |
| for d in RPM_DELTAS: | |
| nr = base_rpm + d | |
| if not (RPM_RANGE[0] <= nr <= RPM_RANGE[1]): | |
| continue | |
| bdo_try = _rollout_predict_bdo( | |
| model, | |
| scaler, | |
| feature_cols, | |
| X_win, | |
| float(last_delta), | |
| override_last={"rpm": nr} | |
| ) | |
| if bdo_try > best_bdo: | |
| best_bdo, best_rpm, best_delta = bdo_try, nr, d | |
| rpm_msg = ( | |
| f"\n[Real-time RPM optimization]\n" | |
| f"- Current RPM: {base_rpm:.0f}\n" | |
| f"- Recommended RPM: {best_rpm:.0f}\n" | |
| f"- RPM delta: {best_delta:+.0f}\n" | |
| f"- Predicted BDO @ current RPM: {pred_bdo:.6f}\n" | |
| f"- Predicted BDO @ recommended RPM: {best_bdo:.6f}" | |
| ) | |
| if best_delta != 0: | |
| rpm_msg += "\n- Action: RPM change recommended." | |
| else: | |
| rpm_msg += "\n- Action: Keep current RPM." | |
| else: | |
| rpm_msg = "\n[Real-time RPM optimization] RPM column not found." | |
| return f"- Current BDO (last row): {curr_bdo:.6f}\n- Predicted BDO (+{int(last_delta)}h): {pred_bdo:.6f}{rpm_msg}" | |
| except Exception as e: | |
| return f"์๋ฌ ๋ฐ์: {str(e)}" | |
| # ============================= | |
| # Gradio UI (๊ตฌ์กฐ ์ ์ง) | |
| # ============================= | |
| with gr.Blocks(title="BDO BiLSTM (Best model, last window only)") as demo: | |
| gr.Markdown( | |
| """ | |
| # BDO BiLSTM Inference (Best ckpt, last window only) | |
| - Upload **two files**: dynamic_data CSV + static_data CSV | |
| - Output: | |
| - **Predicted next BDO** | |
| - If predicted to decrease: **best RPM** and **predicted BDO at that RPM** | |
| """.strip() | |
| ) | |
| with gr.Row(): | |
| dynamic_file = gr.File(label="dynamic_data.csv") | |
| static_file = gr.File(label="static_data.csv (embedding: AR~CDS)") | |
| with gr.Row(): | |
| dyn_preview = gr.Dataframe(label="dynamic_data preview (top 10)", interactive=False) | |
| stat_preview = gr.Dataframe(label="static_data preview (top 10)", interactive=False) | |
| # ํ์ผ ์ ๋ก๋ ์ ๋ด์ฉ ๋ฏธ๋ฆฌ๋ณด๊ธฐ ์ด๋ฒคํธ ์ฐ๊ฒฐ | |
| dynamic_file.change(fn=preview_csv, inputs=[dynamic_file], outputs=[dyn_preview]) | |
| static_file.change(fn=preview_csv, inputs=[static_file], outputs=[stat_preview]) | |
| last_delta = gr.Slider( | |
| label="Last row delta_t (1~6)", | |
| minimum=1, maximum=6, step=1, value=2 | |
| ) | |
| run_btn = gr.Button("Run", variant="primary") | |
| out_box = gr.Textbox(label="Result", lines=18) | |
| run_btn.click( | |
| fn=run_inference, | |
| inputs=[dynamic_file, static_file, last_delta], | |
| outputs=[out_box] | |
| ) | |
| if __name__ == "__main__": | |
| demo.launch(ssr_mode=False) |