Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -176,7 +176,7 @@ def parse_excel(data_bytes: bytes):
|
|
| 176 |
xl = pd.ExcelFile(bio)
|
| 177 |
return {sh: xl.parse(sh) for sh in xl.sheet_names}
|
| 178 |
|
| 179 |
-
def read_book_bytes(b: bytes):
|
| 180 |
return parse_excel(b) if b else {}
|
| 181 |
|
| 182 |
def normalize_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
@@ -774,8 +774,8 @@ if st.session_state.app_step == "validate":
|
|
| 774 |
book = read_book_bytes(up.getvalue())
|
| 775 |
name = find_sheet(book, ["Validation","Validate","validation2","Val","val"]) or list(book.keys())[0]
|
| 776 |
df = normalize_df(book[name].copy())
|
| 777 |
-
if not ensure_cols(df, FEATURES):
|
| 778 |
-
st.markdown('<div class="st-message-box st-error">Missing required feature columns.</div>', unsafe_allow_html=True)
|
| 779 |
st.stop()
|
| 780 |
|
| 781 |
pred_raw = model.predict(df[FEATURES])
|
|
@@ -783,7 +783,7 @@ if st.session_state.app_step == "validate":
|
|
| 783 |
try:
|
| 784 |
df["GR_Actual"] = to_actual_series(df, TARGET, ACTUAL_COL, TARGET_TRANSFORM)
|
| 785 |
except Exception:
|
| 786 |
-
st.markdown('<div class="st-message-box st-error">Validation sheet must include actual GR (or a target column that can be inverse-transformed).</div>', unsafe_allow_html=True)
|
| 787 |
st.stop()
|
| 788 |
|
| 789 |
st.session_state.results["Validate"]=df
|
|
@@ -858,15 +858,15 @@ if st.session_state.app_step == "predict":
|
|
| 858 |
if up is not None:
|
| 859 |
book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]
|
| 860 |
df = normalize_df(book[name].copy())
|
| 861 |
-
if not ensure_cols(df, FEATURES):
|
| 862 |
-
st.markdown('<div class="st-message-box st-error">Missing required feature columns.</div>', unsafe_allow_html=True)
|
| 863 |
st.stop()
|
| 864 |
pred_raw = model.predict(df[FEATURES])
|
| 865 |
df["GR_Pred"] = inverse_target(np.asarray(pred_raw, dtype=float), TARGET_TRANSFORM)
|
| 866 |
st.session_state.results["PredictOnly"]=df
|
| 867 |
ranges = st.session_state.train_ranges
|
| 868 |
oor_pct = 0.0
|
| 869 |
-
oor_tbl = None
|
| 870 |
if ranges:
|
| 871 |
any_viol = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).any(axis=1)
|
| 872 |
oor_pct = float(any_viol.mean()*100.0)
|
|
@@ -885,7 +885,7 @@ if st.session_state.app_step == "predict":
|
|
| 885 |
}
|
| 886 |
st.session_state.results["oor_tbl_pred"] = oor_tbl
|
| 887 |
|
| 888 |
-
if st.sidebar.button("⬅ Back to Case Building", use_container_width=True):
|
| 889 |
st.session_state.app_step="dev"
|
| 890 |
st.rerun()
|
| 891 |
|
|
@@ -899,16 +899,16 @@ if st.session_state.app_step == "predict":
|
|
| 899 |
table = pd.DataFrame({
|
| 900 |
"Metric": ["# points","Pred min","Pred max","Pred mean","Pred std","OOR %"],
|
| 901 |
"Value": [sv["n"],
|
| 902 |
-
|
| 903 |
-
|
| 904 |
-
|
| 905 |
-
|
| 906 |
-
|
| 907 |
})
|
| 908 |
st.markdown('<div class="st-message-box st-success">Predictions ready ✓</div>', unsafe_allow_html=True)
|
| 909 |
df_centered_rounded(table, hide_index=True)
|
| 910 |
st.caption("**★ OOR** = % of rows whose input features fall outside the training min–max range.")
|
| 911 |
-
|
| 912 |
if st.session_state.results.get("oor_tbl_pred") is not None:
|
| 913 |
st.markdown('<div class="st-message-box st-warning">Some inputs fall outside **training min–max** ranges.</div>', unsafe_allow_html=True)
|
| 914 |
st.write("*Out-of-range rows (vs. Training min–max):*")
|
|
@@ -975,4 +975,6 @@ st.markdown("""
|
|
| 975 |
<hr>
|
| 976 |
<div style='text-align:center;color:#6b7280;font-size:1.0em;'>
|
| 977 |
© 2025 Smart Thinking AI-Solutions Team. All rights reserved.<br>
|
| 978 |
-
Contact: <a href="mailto:smartthinking@smartthinking.com.sa">smartthinking@smartthinking
|
|
|
|
|
|
|
|
|
| 176 |
xl = pd.ExcelFile(bio)
|
| 177 |
return {sh: xl.parse(sh) for sh in xl.sheet_names}
|
| 178 |
|
| 179 |
+
def read_book_bytes(b: bytes):
|
| 180 |
return parse_excel(b) if b else {}
|
| 181 |
|
| 182 |
def normalize_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
|
| 774 |
book = read_book_bytes(up.getvalue())
|
| 775 |
name = find_sheet(book, ["Validation","Validate","validation2","Val","val"]) or list(book.keys())[0]
|
| 776 |
df = normalize_df(book[name].copy())
|
| 777 |
+
if not ensure_cols(df, FEATURES):
|
| 778 |
+
st.markdown('<div class="st-message-box st-error">Missing required feature columns.</div>', unsafe_allow_html=True)
|
| 779 |
st.stop()
|
| 780 |
|
| 781 |
pred_raw = model.predict(df[FEATURES])
|
|
|
|
| 783 |
try:
|
| 784 |
df["GR_Actual"] = to_actual_series(df, TARGET, ACTUAL_COL, TARGET_TRANSFORM)
|
| 785 |
except Exception:
|
| 786 |
+
st.markdown('<div class="st-message-box st-error">Validation sheet must include actual GR (or a target column that can be inverse-transformed).</div>', unsafe_allow_html=True)
|
| 787 |
st.stop()
|
| 788 |
|
| 789 |
st.session_state.results["Validate"]=df
|
|
|
|
| 858 |
if up is not None:
|
| 859 |
book = read_book_bytes(up.getvalue()); name = list(book.keys())[0]
|
| 860 |
df = normalize_df(book[name].copy())
|
| 861 |
+
if not ensure_cols(df, FEATURES):
|
| 862 |
+
st.markdown('<div class="st-message-box st-error">Missing required feature columns.</div>', unsafe_allow_html=True)
|
| 863 |
st.stop()
|
| 864 |
pred_raw = model.predict(df[FEATURES])
|
| 865 |
df["GR_Pred"] = inverse_target(np.asarray(pred_raw, dtype=float), TARGET_TRANSFORM)
|
| 866 |
st.session_state.results["PredictOnly"]=df
|
| 867 |
ranges = st.session_state.train_ranges
|
| 868 |
oor_pct = 0.0
|
| 869 |
+
oor_tbl = None
|
| 870 |
if ranges:
|
| 871 |
any_viol = pd.DataFrame({f:(df[f]<ranges[f][0])|(df[f]>ranges[f][1]) for f in FEATURES}).any(axis=1)
|
| 872 |
oor_pct = float(any_viol.mean()*100.0)
|
|
|
|
| 885 |
}
|
| 886 |
st.session_state.results["oor_tbl_pred"] = oor_tbl
|
| 887 |
|
| 888 |
+
if st.sidebar.button("⬅ Back to Case Building", use_container_width=True):
|
| 889 |
st.session_state.app_step="dev"
|
| 890 |
st.rerun()
|
| 891 |
|
|
|
|
| 899 |
table = pd.DataFrame({
|
| 900 |
"Metric": ["# points","Pred min","Pred max","Pred mean","Pred std","OOR %"],
|
| 901 |
"Value": [sv["n"],
|
| 902 |
+
round(sv["pred_min"],2),
|
| 903 |
+
round(sv["pred_max"],2),
|
| 904 |
+
round(sv["pred_mean"],2),
|
| 905 |
+
round(sv["pred_std"],2),
|
| 906 |
+
f'{sv["oor"]:.1f}%']
|
| 907 |
})
|
| 908 |
st.markdown('<div class="st-message-box st-success">Predictions ready ✓</div>', unsafe_allow_html=True)
|
| 909 |
df_centered_rounded(table, hide_index=True)
|
| 910 |
st.caption("**★ OOR** = % of rows whose input features fall outside the training min–max range.")
|
| 911 |
+
|
| 912 |
if st.session_state.results.get("oor_tbl_pred") is not None:
|
| 913 |
st.markdown('<div class="st-message-box st-warning">Some inputs fall outside **training min–max** ranges.</div>', unsafe_allow_html=True)
|
| 914 |
st.write("*Out-of-range rows (vs. Training min–max):*")
|
|
|
|
| 975 |
<hr>
|
| 976 |
<div style='text-align:center;color:#6b7280;font-size:1.0em;'>
|
| 977 |
© 2025 Smart Thinking AI-Solutions Team. All rights reserved.<br>
|
| 978 |
+
Contact: <a href="mailto:smartthinking@smartthinking.com.sa">smartthinking@smartthinking.com.sa</a>
|
| 979 |
+
</div>
|
| 980 |
+
""", unsafe_allow_html=True)
|