Download app.py from Yu-Lieng/predict1: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/Yu-Lieng/predict1/resolve/main/app.py
- Command line
-
hf download hf://spaces/Yu-Lieng/predict1/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Yu-Lieng/predict1/resolve/main/app.py
1.63 kB
| import gradio as gr | |
| import pandas as pd | |
| from sklearn.tree import DecisionTreeClassifier | |
| # 載入完整資料 | |
| df = pd.read_csv("bankloan.csv") | |
| df = df.drop(columns=["ID", "ZIP.Code"]) | |
| X = df.drop(columns=["Personal.Loan"]) | |
| y = df["Personal.Loan"] | |
| # 建立模型 | |
| model = DecisionTreeClassifier(random_state=42) | |
| model.fit(X, y) | |
| def predict(age, exp, income, family, ccavg, edu, mort, sec, cd, online, credit): | |
| input_data = pd.DataFrame([{ | |
| "Age": age, | |
| "Experience": exp, | |
| "Income": income, | |
| "Family": family, | |
| "CCAvg": ccavg, | |
| "Education": edu, | |
| "Mortgage": mort, | |
| "Securities.Account": sec, | |
| "CD.Account": cd, | |
| "Online": online, | |
| "CreditCard": credit | |
| }]) | |
| pred = model.predict(input_data)[0] | |
| return "核貸 ✅" if pred == 1 else "不核貸 ❌" | |
| demo = gr.Interface( | |
| fn=predict, | |
| inputs=[ | |
| gr.Number(label="年齡 Age"), | |
| gr.Number(label="工作年資 Experience"), | |
| gr.Number(label="收入 Income"), | |
| gr.Number(label="家庭人數 Family"), | |
| gr.Number(label="信用卡平均月花費 CCAvg"), | |
| gr.Number(label="教育程度 Education"), | |
| gr.Number(label="房貸金額 Mortgage"), | |
| gr.Number(label="是否有證券帳戶 (0/1)"), | |
| gr.Number(label="是否有定存帳戶 (0/1)"), | |
| gr.Number(label="是否使用線上服務 (0/1)"), | |
| gr.Number(label="是否持有信用卡 (0/1)") | |
| ], | |
| outputs="text", | |
| title="Loan Approval Predictor", | |
| description="使用 bankloan.csv 資料訓練的決策樹模型,預測是否核貸" | |
| ) | |
| demo.launch() | |