king4168888/side_effect_model
0
1import json2 3import joblib4import pandas as pd5import streamlit as st6from huggingface_hub import hf_hub_download7 8# 下載 & 載入模型9xgb_model_path = hf_hub_download(10 repo_id="king4168888/side_effect_model", filename="xgb_model.pkl"11)12encoders_path = hf_hub_download(13 repo_id="king4168888/side_effect_model", filename="label_encoders.pkl"14)15mlb_path = hf_hub_download(16 repo_id="king4168888/side_effect_model", filename="mlb.pkl"17) # ← 這行新增18 19mlp_path = hf_hub_download(repo_id="king4168888/side_effect_model", filename="mlp.pkl")20 21xgb_model = joblib.load(xgb_model_path)22encoders = joblib.load(encoders_path)23mlb = joblib.load(mlb_path) # ← 這行新增24mlp = joblib.load(mlp_path)25 26# === Streamlit UI ===27st.set_page_config(page_title="即時副作用預測", page_icon="⚡")28st.title("⚡ 即時副作用預測")29st.markdown("輸入藥品名稱與 CID,立即預測可能副作用")30 31drug_name = st.text_input("🔤 藥品名稱")32cid = st.text_input("🔢 CID")33 34# 選擇模型35model_choice = st.selectbox("🔍 選擇預測模型", ["XGBoost", "MLP"])36 37# 下載並載入模型38if model_choice == "XGBoost":39 model_path = xgb_model40else:41 model_path = mlp42 43if st.button("🚀 預測副作用"):44 try:45 dn_code = encoders["drug_name"].transform([drug_name])[0]46 cid_code = encoders["cid"].transform([cid])[0]47 X = pd.DataFrame(48 [[dn_code, cid_code]], columns=["drug_name_encoded", "cid_encoded"]49 )50 51 y_pred = model_path.predict(X)52 53 predicted_labels = mlb.inverse_transform(y_pred)[0]54 55 st.success("✅ 預測完成")56 st.json(predicted_labels)57 except Exception as e:58 st.error(f"預測失敗:{e}")59 