Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,6 +5,7 @@ import google.generativeai as genai
|
|
| 5 |
import os
|
| 6 |
from deep_translator import GoogleTranslator
|
| 7 |
|
|
|
|
| 8 |
|
| 9 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 10 |
|
|
@@ -54,69 +55,62 @@ with st.sidebar:
|
|
| 54 |
|
| 55 |
# Label dictionary for multilingual UI
|
| 56 |
LABELS = {
|
|
|
|
|
|
|
| 57 |
"age": {"English": "Patient's Age", "中文": "年齡", "Malay": "Umur"},
|
| 58 |
"height": {"English": "Patient's Height (cm)", "中文": "身高 (cm)", "Malay": "Tinggi (cm)"},
|
| 59 |
"weight": {"English": "Patient's Weight (kg)", "中文": "體重 (kg)", "Malay": "Berat (kg)"},
|
| 60 |
"systolic": {"English": "Patient's Systolic BP", "中文": "收縮壓", "Malay": "Tekanan Sistolik"},
|
| 61 |
-
"diastolic": {"English": "Patient's Diastolic BP", "中文": "舒
|
| 62 |
"cholesterol": {"English": "Patient's Cholesterol", "中文": "膽固醇", "Malay": "Kolesterol"},
|
| 63 |
"gluc": {"English": "Patient's Glucose", "中文": "血糖", "Malay": "Glukosa"},
|
| 64 |
"gender": {"English": "Patient's Gender", "中文": "性別", "Malay": "Jantina"},
|
| 65 |
-
"smoke": {"English": "Do patient smoke?", "中文": "是否吸
|
| 66 |
"alco": {"English": "Do patient drink alcohol?", "中文": "是否喝酒?", "Malay": "Adakah minum alkohol?"},
|
| 67 |
-
"active": {"English": "Are patient physically active?", "中文": "是否
|
| 68 |
"predict": {"English": "Predict CVD Risk", "中文": "預測心血管風險", "Malay": "Ramalkan Risiko CVD"}
|
| 69 |
}
|
| 70 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
# User input
|
| 73 |
with st.sidebar:
|
| 74 |
selected = option_menu(
|
| 75 |
menu_title="Navigation",
|
| 76 |
-
options=[
|
| 77 |
icons=["activity", "book"],
|
| 78 |
menu_icon="cast",
|
| 79 |
default_index=0,
|
| 80 |
)
|
| 81 |
|
| 82 |
-
if selected ==
|
| 83 |
-
|
| 84 |
with st.sidebar:
|
| 85 |
-
st.header("
|
| 86 |
-
user_type = st.selectbox("
|
| 87 |
-
age = st.number_input("
|
| 88 |
-
height = st.number_input("
|
| 89 |
-
weight = st.number_input("
|
| 90 |
-
systolic = st.number_input("
|
| 91 |
-
diastolic = st.number_input("
|
| 92 |
-
cholesterol = st.selectbox("
|
| 93 |
-
gluc = st.selectbox("
|
| 94 |
-
gender = st.selectbox("
|
| 95 |
-
smoke = st.selectbox("
|
| 96 |
-
alco = st.selectbox("
|
| 97 |
-
active = st.selectbox("
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
"height": height,
|
| 102 |
-
"weight": weight,
|
| 103 |
-
"ap_hi": systolic,
|
| 104 |
-
"ap_lo": diastolic,
|
| 105 |
-
"cholesterol": cholesterol,
|
| 106 |
-
"gluc": gluc,
|
| 107 |
-
"gender": gender,
|
| 108 |
-
"smoke": smoke,
|
| 109 |
-
"alco": alco,
|
| 110 |
-
"active": active
|
| 111 |
-
}])
|
| 112 |
-
predict_clicked = st.button("Predict CVD Risk")
|
| 113 |
-
|
| 114 |
if predict_clicked:
|
| 115 |
input_data["bmi"] = input_data["weight"] / ((input_data["height"]/100)**2)
|
| 116 |
input_data["pulse_pressure"] = input_data["ap_hi"] - input_data["ap_lo"]
|
| 117 |
input_data["hypertension"] = ((input_data["ap_hi"] > 140) | (input_data["ap_lo"] > 90)).astype(int)
|
| 118 |
|
| 119 |
-
|
| 120 |
|
| 121 |
preds = model.predict(input_data)
|
| 122 |
proba = preds["cardio_1_probability"].iloc[0]
|
|
@@ -158,11 +152,11 @@ if selected == "Prediction Tool":
|
|
| 158 |
|
| 159 |
if lang == "中文":
|
| 160 |
translated = translator.translate(original_text, dest="zh-tw").text
|
| 161 |
-
st.markdown("### 🔹 AI 健康建議
|
| 162 |
st.write(translated)
|
| 163 |
elif lang == "Malay":
|
| 164 |
translated = translator.translate(original_text, dest="ms").text
|
| 165 |
-
st.markdown("### 🔹 Nasihat Kesihatan AI
|
| 166 |
st.write(translated)
|
| 167 |
else:
|
| 168 |
st.markdown("### 🔹 AI Health Advice")
|
|
|
|
| 5 |
import os
|
| 6 |
from deep_translator import GoogleTranslator
|
| 7 |
|
| 8 |
+
translator = GoogleTranslator()
|
| 9 |
|
| 10 |
genai.configure(api_key=os.getenv("GEMINI_API_KEY"))
|
| 11 |
|
|
|
|
| 55 |
|
| 56 |
# Label dictionary for multilingual UI
|
| 57 |
LABELS = {
|
| 58 |
+
"user_type": {"English": "I am a...", "中文": "我的身份是...", "Malay": "Saya seorang..."},
|
| 59 |
+
"input_header": {"English": "Enter Health Info", "中文": "輸入健康資料", "Malay": "Masukkan Maklumat Kesihatan"},
|
| 60 |
"age": {"English": "Patient's Age", "中文": "年齡", "Malay": "Umur"},
|
| 61 |
"height": {"English": "Patient's Height (cm)", "中文": "身高 (cm)", "Malay": "Tinggi (cm)"},
|
| 62 |
"weight": {"English": "Patient's Weight (kg)", "中文": "體重 (kg)", "Malay": "Berat (kg)"},
|
| 63 |
"systolic": {"English": "Patient's Systolic BP", "中文": "收縮壓", "Malay": "Tekanan Sistolik"},
|
| 64 |
+
"diastolic": {"English": "Patient's Diastolic BP", "中文": "舒張壓", "Malay": "Tekanan Diastolik"},
|
| 65 |
"cholesterol": {"English": "Patient's Cholesterol", "中文": "膽固醇", "Malay": "Kolesterol"},
|
| 66 |
"gluc": {"English": "Patient's Glucose", "中文": "血糖", "Malay": "Glukosa"},
|
| 67 |
"gender": {"English": "Patient's Gender", "中文": "性別", "Malay": "Jantina"},
|
| 68 |
+
"smoke": {"English": "Do patient smoke?", "中文": "是否吸菸?", "Malay": "Adakah merokok?"},
|
| 69 |
"alco": {"English": "Do patient drink alcohol?", "中文": "是否喝酒?", "Malay": "Adakah minum alkohol?"},
|
| 70 |
+
"active": {"English": "Are patient physically active?", "中文": "是否有運動習慣?", "Malay": "Adakah aktif secara fizikal?"},
|
| 71 |
"predict": {"English": "Predict CVD Risk", "中文": "預測心血管風險", "Malay": "Ramalkan Risiko CVD"}
|
| 72 |
}
|
| 73 |
|
| 74 |
+
MENU = {
|
| 75 |
+
"English": ["Prediction Tool", "Let's know more about CVD!"],
|
| 76 |
+
"中文": ["預測工具", "了解更多心血管資訊"],
|
| 77 |
+
"Malay": ["Alat Ramalan", "Ketahui lebih lanjut tentang CVD"]
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
|
| 81 |
# User input
|
| 82 |
with st.sidebar:
|
| 83 |
selected = option_menu(
|
| 84 |
menu_title="Navigation",
|
| 85 |
+
options=MENU[lang],
|
| 86 |
icons=["activity", "book"],
|
| 87 |
menu_icon="cast",
|
| 88 |
default_index=0,
|
| 89 |
)
|
| 90 |
|
| 91 |
+
if selected == MENU[lang][0]:
|
|
|
|
| 92 |
with st.sidebar:
|
| 93 |
+
st.header(LABELS["input_header"][lang])
|
| 94 |
+
user_type = st.selectbox(LABELS["user_type"][lang], ["Patient", "Medical Professional"])
|
| 95 |
+
age = st.number_input(LABELS["age"][lang], min_value=18, max_value=100, value=50)
|
| 96 |
+
height = st.number_input(LABELS["height"][lang], min_value=100, max_value=250, value=170)
|
| 97 |
+
weight = st.number_input(LABELS["weight"][lang], min_value=30, max_value=200, value=70)
|
| 98 |
+
systolic = st.number_input(LABELS["systolic"][lang], min_value=80, max_value=250, value=120)
|
| 99 |
+
diastolic = st.number_input(LABELS["diastolic"][lang], min_value=40, max_value=150, value=80)
|
| 100 |
+
cholesterol = st.selectbox(LABELS["cholesterol"][lang], [0, 1, 2], format_func=lambda x: {0: "Normal", 1: "High", 2: "Very High"}[x])
|
| 101 |
+
gluc = st.selectbox(LABELS["gluc"][lang], [0, 1, 2], format_func=lambda x: {0: "Normal", 1: "High", 2: "Very High"}[x])
|
| 102 |
+
gender = st.selectbox(LABELS["gender"][lang], [0, 1], format_func=lambda x: {0: "Female", 1: "Male"}[x])
|
| 103 |
+
smoke = st.selectbox(LABELS["smoke"][lang], [0, 1], format_func=lambda x: "Yes" if x else "No")
|
| 104 |
+
alco = st.selectbox(LABELS["alco"][lang], [0, 1], format_func=lambda x: "Yes" if x else "No")
|
| 105 |
+
active = st.selectbox(LABELS["active"][lang], [0, 1], format_func=lambda x: "Yes" if x else "No")
|
| 106 |
+
input_data = pd.DataFrame([{ "age": age * 365, "height": height, "weight": weight, "ap_hi": systolic, "ap_lo": diastolic, "cholesterol": cholesterol, "gluc": gluc, "gender": gender, "smoke": smoke, "alco": alco, "active": active }])
|
| 107 |
+
predict_clicked = st.button(LABELS["predict"][lang])
|
| 108 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 109 |
if predict_clicked:
|
| 110 |
input_data["bmi"] = input_data["weight"] / ((input_data["height"]/100)**2)
|
| 111 |
input_data["pulse_pressure"] = input_data["ap_hi"] - input_data["ap_lo"]
|
| 112 |
input_data["hypertension"] = ((input_data["ap_hi"] > 140) | (input_data["ap_lo"] > 90)).astype(int)
|
| 113 |
|
|
|
|
| 114 |
|
| 115 |
preds = model.predict(input_data)
|
| 116 |
proba = preds["cardio_1_probability"].iloc[0]
|
|
|
|
| 152 |
|
| 153 |
if lang == "中文":
|
| 154 |
translated = translator.translate(original_text, dest="zh-tw").text
|
| 155 |
+
st.markdown("### 🔹 AI 健康建議")
|
| 156 |
st.write(translated)
|
| 157 |
elif lang == "Malay":
|
| 158 |
translated = translator.translate(original_text, dest="ms").text
|
| 159 |
+
st.markdown("### 🔹 Nasihat Kesihatan AI")
|
| 160 |
st.write(translated)
|
| 161 |
else:
|
| 162 |
st.markdown("### 🔹 AI Health Advice")
|