Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,8 @@ import numpy as np
|
|
| 3 |
import joblib
|
| 4 |
import gspread
|
| 5 |
from oauth2client.service_account import ServiceAccountCredentials
|
|
|
|
|
|
|
| 6 |
|
| 7 |
# ===== Load models =====
|
| 8 |
model_full = joblib.load("random_forest_model_full.pkl")
|
|
@@ -108,7 +110,7 @@ def log_to_gsheet(name, student_id, major, model_type, prediction_result, final_
|
|
| 108 |
st.warning(f"Không thể lưu dữ liệu vào Google Sheets: {e}")
|
| 109 |
|
| 110 |
# ===== HEADER =====
|
| 111 |
-
st.markdown("<h1 style='text-align: center; color: #003366;'>🎓 DỰ
|
| 112 |
st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)
|
| 113 |
st.write("---")
|
| 114 |
|
|
@@ -120,8 +122,8 @@ st.subheader("🔢 Chọn ví dụ mẫu hoặc nhập thông tin cá nhân")
|
|
| 120 |
|
| 121 |
sample_option = st.selectbox("📝 Chọn ví dụ:", ["Không ví dụ", "Dùng ví dụ mẫu ngành Công nghệ thông tin", "Dùng ví dụ mẫu ngành Kinh tế"])
|
| 122 |
|
| 123 |
-
# ===== Ví dụ mẫu =====
|
| 124 |
-
|
| 125 |
"name": "Nguyễn Văn A",
|
| 126 |
"student_id": "10117367",
|
| 127 |
"major": "Công nghệ thông tin",
|
|
@@ -135,7 +137,8 @@ sample_cntt_example = {
|
|
| 135 |
]
|
| 136 |
}
|
| 137 |
|
| 138 |
-
|
|
|
|
| 139 |
"name": "Trần Thị B",
|
| 140 |
"student_id": "11418092",
|
| 141 |
"major": "Kinh tế",
|
|
@@ -149,14 +152,44 @@ sample_kinhte_example = {
|
|
| 149 |
]
|
| 150 |
}
|
| 151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
# ===== Thông tin cá nhân =====
|
| 153 |
if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":
|
| 154 |
-
name = st.text_input("👤 Họ và tên", value=
|
| 155 |
-
student_id = st.text_input("🎓 Mã sinh viên", value=
|
| 156 |
major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=0)
|
| 157 |
elif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":
|
| 158 |
-
name = st.text_input("👤 Họ và tên", value=
|
| 159 |
-
student_id = st.text_input("🎓 Mã sinh viên", value=
|
| 160 |
major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=1)
|
| 161 |
else:
|
| 162 |
name = st.text_input("👤 Họ và tên")
|
|
@@ -165,7 +198,7 @@ else:
|
|
| 165 |
|
| 166 |
st.write("---")
|
| 167 |
|
| 168 |
-
# =====
|
| 169 |
def input_semester(semester_label, default_values=None):
|
| 170 |
with st.expander(f"📖 {semester_label}", expanded=True):
|
| 171 |
col1, col2 = st.columns(2)
|
|
@@ -204,7 +237,7 @@ semesters = ["HỌC KỲ I", "HỌC KỲ II", "HỌC KỲ III", "HỌC KỲ IV",
|
|
| 204 |
if model_type == "Dùng toàn bộ dữ liệu":
|
| 205 |
st.subheader("🔢 Nhập thông tin học kỳ chi tiết")
|
| 206 |
for idx, sem in enumerate(semesters):
|
| 207 |
-
example_data =
|
| 208 |
default_values = example_data["semester_data"][idx*9:(idx+1)*9] if example_data else None
|
| 209 |
data += input_semester(sem, default_values)
|
| 210 |
nganh = 0 if major == "Công nghệ thông tin" else 1
|
|
@@ -212,21 +245,8 @@ if model_type == "Dùng toàn bộ dữ liệu":
|
|
| 212 |
else:
|
| 213 |
st.subheader("✨ Nhập thông tin rút gọn")
|
| 214 |
for idx, sem in enumerate(semesters):
|
| 215 |
-
example_data = None
|
| 216 |
-
|
| 217 |
-
example_data = sample_cntt_example
|
| 218 |
-
elif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":
|
| 219 |
-
example_data = sample_kinhte_example
|
| 220 |
-
|
| 221 |
-
if example_data:
|
| 222 |
-
raw = example_data["semester_data"][idx*9:(idx+1)*9]
|
| 223 |
-
if len(raw) >= 8:
|
| 224 |
-
default_values = [raw[4], raw[2], raw[7]]
|
| 225 |
-
else:
|
| 226 |
-
default_values = None
|
| 227 |
-
else:
|
| 228 |
-
default_values = None
|
| 229 |
-
|
| 230 |
data += input_important_features(sem, default_values)
|
| 231 |
final_features = np.array(data).reshape(1, -1)
|
| 232 |
|
|
@@ -245,4 +265,4 @@ if st.button("🎯 DỰ BÁO"):
|
|
| 245 |
st.snow()
|
| 246 |
|
| 247 |
# Ghi log lên Google Sheets
|
| 248 |
-
log_to_gsheet(name, student_id, major, model_type, prediction[0], final_features)
|
|
|
|
| 3 |
import joblib
|
| 4 |
import gspread
|
| 5 |
from oauth2client.service_account import ServiceAccountCredentials
|
| 6 |
+
from datetime import datetime
|
| 7 |
+
import json
|
| 8 |
|
| 9 |
# ===== Load models =====
|
| 10 |
model_full = joblib.load("random_forest_model_full.pkl")
|
|
|
|
| 110 |
st.warning(f"Không thể lưu dữ liệu vào Google Sheets: {e}")
|
| 111 |
|
| 112 |
# ===== HEADER =====
|
| 113 |
+
st.markdown("<h1 style='text-align: center; color: #003366;'>🎓 DỰ ĐOÁN KHẢ NĂNG TỐT NGHIỆP ĐÚNG HẠN</h1>", unsafe_allow_html=True)
|
| 114 |
st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)
|
| 115 |
st.write("---")
|
| 116 |
|
|
|
|
| 122 |
|
| 123 |
sample_option = st.selectbox("📝 Chọn ví dụ:", ["Không ví dụ", "Dùng ví dụ mẫu ngành Công nghệ thông tin", "Dùng ví dụ mẫu ngành Kinh tế"])
|
| 124 |
|
| 125 |
+
# ===== Ví dụ mẫu cho mô hình "Dùng toàn bộ dữ liệu" =====
|
| 126 |
+
sample_cntt_example_full = {
|
| 127 |
"name": "Nguyễn Văn A",
|
| 128 |
"student_id": "10117367",
|
| 129 |
"major": "Công nghệ thông tin",
|
|
|
|
| 137 |
]
|
| 138 |
}
|
| 139 |
|
| 140 |
+
# ===== Ví dụ mẫu cho mô hình "Dùng mô hình đơn giản" =====
|
| 141 |
+
sample_kinhte_example_simple = {
|
| 142 |
"name": "Trần Thị B",
|
| 143 |
"student_id": "11418092",
|
| 144 |
"major": "Kinh tế",
|
|
|
|
| 152 |
]
|
| 153 |
}
|
| 154 |
|
| 155 |
+
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====
|
| 156 |
+
sample_cntt_example_simple = {
|
| 157 |
+
"name": "Nguyễn Văn B",
|
| 158 |
+
"student_id": "10117368",
|
| 159 |
+
"major": "Công nghệ thông tin",
|
| 160 |
+
"semester_data": [
|
| 161 |
+
0, 0, 0, 0, 15, 8, 8, 7.50, 0,
|
| 162 |
+
0, 0, 0, 0, 17, 7, 7, 8.10, 0,
|
| 163 |
+
0, 0, 0, 0, 18, 7, 7, 7.90, 0,
|
| 164 |
+
0, 0, 0, 0, 17, 7, 7, 7.80, 0,
|
| 165 |
+
0, 0, 0, 0, 19, 8, 6, 8.30, 0,
|
| 166 |
+
0, 0, 6, 3, 19, 7, 5, 7.20, 1
|
| 167 |
+
]
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho Kinh tế =====
|
| 171 |
+
sample_kinhte_example_full = {
|
| 172 |
+
"name": "Trần Thị C",
|
| 173 |
+
"student_id": "11418093",
|
| 174 |
+
"major": "Kinh tế",
|
| 175 |
+
"semester_data": [
|
| 176 |
+
0, 0, 0, 0, 16, 6, 6, 7.00, 1,
|
| 177 |
+
0, 0, 0, 0, 18, 7, 7, 8.20, 1,
|
| 178 |
+
0, 0, 0, 0, 17, 7, 7, 7.80, 1,
|
| 179 |
+
0, 0, 0, 0, 17, 7, 6, 7.90, 1,
|
| 180 |
+
0, 0, 0, 0, 19, 8, 5, 8.10, 0,
|
| 181 |
+
0, 0, 8, 2, 19, 7, 6, 7.30, 1
|
| 182 |
+
]
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
# ===== Thông tin cá nhân =====
|
| 186 |
if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":
|
| 187 |
+
name = st.text_input("👤 Họ và tên", value=sample_cntt_example_full["name"])
|
| 188 |
+
student_id = st.text_input("🎓 Mã sinh viên", value=sample_cntt_example_full["student_id"])
|
| 189 |
major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=0)
|
| 190 |
elif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":
|
| 191 |
+
name = st.text_input("👤 Họ và tên", value=sample_kinhte_example_simple["name"])
|
| 192 |
+
student_id = st.text_input("🎓 Mã sinh viên", value=sample_kinhte_example_simple["student_id"])
|
| 193 |
major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=1)
|
| 194 |
else:
|
| 195 |
name = st.text_input("👤 Họ và tên")
|
|
|
|
| 198 |
|
| 199 |
st.write("---")
|
| 200 |
|
| 201 |
+
# ===== Nhập thông tin học kỳ =====
|
| 202 |
def input_semester(semester_label, default_values=None):
|
| 203 |
with st.expander(f"📖 {semester_label}", expanded=True):
|
| 204 |
col1, col2 = st.columns(2)
|
|
|
|
| 237 |
if model_type == "Dùng toàn bộ dữ liệu":
|
| 238 |
st.subheader("🔢 Nhập thông tin học kỳ chi tiết")
|
| 239 |
for idx, sem in enumerate(semesters):
|
| 240 |
+
example_data = sample_cntt_example_full if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin" else sample_kinhte_example_full if sample_option == "Dùng ví dụ mẫu ngành Kinh tế" else None
|
| 241 |
default_values = example_data["semester_data"][idx*9:(idx+1)*9] if example_data else None
|
| 242 |
data += input_semester(sem, default_values)
|
| 243 |
nganh = 0 if major == "Công nghệ thông tin" else 1
|
|
|
|
| 245 |
else:
|
| 246 |
st.subheader("✨ Nhập thông tin rút gọn")
|
| 247 |
for idx, sem in enumerate(semesters):
|
| 248 |
+
example_data = sample_cntt_example_simple if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin" else sample_kinhte_example_simple if sample_option == "Dùng ví dụ mẫu ngành Kinh tế" else None
|
| 249 |
+
default_values = example_data["semester_data"][idx*9:(idx+1)*9] if example_data else None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 250 |
data += input_important_features(sem, default_values)
|
| 251 |
final_features = np.array(data).reshape(1, -1)
|
| 252 |
|
|
|
|
| 265 |
st.snow()
|
| 266 |
|
| 267 |
# Ghi log lên Google Sheets
|
| 268 |
+
log_to_gsheet(name, student_id, major, model_type, prediction[0], final_features, model_type, data)
|