Spaces:
Running
Running
Download app.py from CLL-Prediction/web: direct link, hf CLI and curl.
- Browser
- Download file 7.1 kB
-
https://huggingface.co/spaces/CLL-Prediction/web/resolve/main/app.py
- Command line
-
hf download hf://spaces/CLL-Prediction/web/app.py
-
curl -L -o app.py https://huggingface.co/spaces/CLL-Prediction/web/resolve/main/app.py
7.1 kB
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from modules.pre_processing import pre_processing | |
| from modules.nested_calibration_cv import nested_cv_calibration | |
| # 設定頁面配置 | |
| st.set_page_config( | |
| page_title="CLL Prediction", | |
| layout="wide", | |
| initial_sidebar_state="expanded", | |
| ) | |
| # # CSS 客製化 sidebar 寬度 | |
| # st.markdown( | |
| # """ | |
| # <style> | |
| # section[data-testid="stSidebar"] { | |
| # width: 500px !important; | |
| # } | |
| # </style> | |
| # """, | |
| # unsafe_allow_html=True, | |
| # ) | |
| def get_calibration(): | |
| return nested_cv_calibration() | |
| # Initialize session state | |
| if "predict_clicked" not in st.session_state: | |
| st.session_state.predict_clicked = False | |
| if "input_payload" not in st.session_state: | |
| st.session_state.input_payload = None | |
| st.title("Chronic Lymphocytic Leukemia Prediction") | |
| st.caption("Please fill in the fields on the left and click \"Predict\".") | |
| with st.expander("Explanation of HL p-value / Brier Score / AUC"): | |
| col1, col2, col3 = st.columns(3) | |
| with col1: | |
| st.markdown("**HL p-value**: The p-value of the Hosmer-Lemeshow test. A p-value **higher than 0.05** indicates that the model is fit well.") | |
| with col2: | |
| st.markdown("**Brier Score**: The Brier score is a measure of the accuracy of probability predictions. A **closer to 0** indicates a better model.") | |
| with col3: | |
| st.markdown("**AUC**: The area under the receiver operating characteristic curve. A **higher AUC** indicates a better model.") | |
| st.sidebar.title(" ⚙️Input") | |
| with st.sidebar.form("prediction_form"): | |
| st.subheader("Lymphocyte count(×10⁹/L)", help="Normal range is around 1.0-4.8 ×10⁹/L.") | |
| lym_count = st.number_input( | |
| "", | |
| min_value=0.0, | |
| max_value=100.0, | |
| value=1.0, | |
| step=0.1 | |
| ) | |
| st.subheader("Sex") | |
| sex = st.radio("", ("Male", "Female")) | |
| st.subheader("Past chemotherapy treatment") | |
| past_chemo = st.radio("", ("Yes", "No"), key="past_chemo") | |
| st.subheader("Sleep duration (hours)") | |
| slp_duration = st.slider("", min_value=0, max_value=24, value=8) | |
| st.subheader("Enlarged liver and spleen") | |
| enlarge_liver_spleen = st.radio("", ("Yes", "No"), key="enlarge_liver_spleen") | |
| st.subheader("Unusual blood test results") | |
| unusual_blood_test = st.radio("", ("Yes", "No"), key="unusual_blood_test") | |
| st.subheader("History of smoking") | |
| smoking_history = st.radio("In the past, how often have you smoked tobacco?", ( | |
| "Smoked on most or all days", | |
| "Smoked occasionally", | |
| "Just tried once or twice", | |
| "I have never smoked", | |
| ), key="smoking_history") | |
| st.subheader("History of major muscle & joint conditions", help="e.g., Rheumatoid Arthritis, Lupus, Chronic Disc Herniation, or past surgeries. Do not include temporary muscle soreness.") | |
| muscle_joint_conditions = st.radio("", ("Yes", "No"),key="muscle_joint_conditions") | |
| st.subheader("Metastasis (cancer spread to lungs or digestive system)") | |
| metastasis = st.radio("", ("Yes", "No"), key="metastasis") | |
| submitted = st.form_submit_button("Predict") | |
| st.sidebar.markdown("The UK Biobank CLL Risk Prediction tool was established based on UK Biobank study cohort. The tool was developed on research purpose and cannot be used as clinical evidence.") | |
| if submitted: | |
| st.session_state.predict_clicked = True | |
| st.session_state.input_payload = { | |
| "Lymphocyte count": lym_count, | |
| "Sex": sex, | |
| "Past chemotherapy treatment": past_chemo, | |
| "Sleep duration": slp_duration, | |
| "Enlarged liver and spleen": enlarge_liver_spleen, | |
| "Unusual blood test results": unusual_blood_test, | |
| "Smoking history": smoking_history, | |
| "History of muscle & joint conditions": muscle_joint_conditions, | |
| "Metastasis": metastasis, | |
| } | |
| # numeric_columns = ['Lymphocyte count', 'Sleep duration'] | |
| # categorical_columns = ['Sex', 'Past chemotherapy treatment', 'Enlarged liver and spleen', 'Unusual blood test results', 'Smoking history', 'History of muscle & joint conditions', 'Metastasis'] | |
| if st.session_state.predict_clicked and st.session_state.input_payload is not None: | |
| msg = st.empty() # 建立一個可被清空的區塊 | |
| msg.success("Data submitted, starting prediction process.") | |
| Fix_Param = { | |
| 'learning_rate': 0.05, | |
| 'n_estimators': 200, | |
| 'num_leaves': 6, | |
| 'boosting_type' : 'gbdt', | |
| 'objective' : 'binary', | |
| 'max_bin':225, | |
| 'random_state' : 500, | |
| 'colsample_bytree' : 0.64, | |
| 'subsample' : 0.7, | |
| 'reg_alpha' : 0.8, | |
| 'reg_lambda' : 1.8, | |
| 'is_unbalance': True, # imbalanced data | |
| } | |
| input_df = pd.DataFrame([st.session_state.input_payload]) | |
| processed_df = pre_processing(input_df) # already scaled and encoded | |
| X_np_5y = np.load("./plot_reference/5y/5y_balanced_X.npy"); y_np_5y = np.load("./plot_reference/5y/5y_balanced_y.npy"); | |
| X_np_10y = np.load("./plot_reference/10y/10y_balanced_X.npy"); y_np_10y = np.load("./plot_reference/10y/10y_balanced_y.npy"); | |
| X_np_all = np.load("./plot_reference/all/all_balanced_X.npy"); y_np_all = np.load("./plot_reference/all/all_balanced_y.npy"); | |
| # get risk from model (use processed features to match training data) | |
| fig_5y = nested_cv_calibration(X_np_5y, y_np_5y, Fix_Param, "5-year CLL Risk", | |
| calibration_method="isotonic", | |
| input_df=processed_df, | |
| num_groups=4, | |
| output_name=None, | |
| feature_names=None) | |
| col_left, col_right = st.columns([5, 1]) | |
| with col_right: | |
| st.markdown("**5-year model AUC: 0.876**", unsafe_allow_html=True) | |
| st.pyplot(fig_5y) | |
| fig_10y = nested_cv_calibration(X_np_10y, y_np_10y, Fix_Param, "10-year CLL Risk", | |
| calibration_method="isotonic", | |
| input_df=processed_df, | |
| num_groups=4, | |
| output_name=None, | |
| feature_names=None) | |
| col_left, col_right = st.columns([5, 1]) | |
| with col_right: | |
| st.markdown("**10-year model AUC: 0.873**", unsafe_allow_html=True) | |
| st.pyplot(fig_10y) | |
| fig_all = nested_cv_calibration(X_np_all, y_np_all, Fix_Param, "All CLL Risk", | |
| calibration_method="isotonic", | |
| input_df=processed_df, | |
| num_groups=4, | |
| output_name=None, | |
| feature_names=None) | |
| col_left, col_right = st.columns([5, 1]) | |
| with col_right: | |
| st.markdown("**All model AUC: 0.884**", unsafe_allow_html=True) | |
| st.pyplot(fig_all) | |
| msg.empty() # 清空成功訊息 | |