Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -16,7 +16,7 @@ st.set_page_config(page_title="Dự đoán tốt nghiệp đúng hạn", page_ic
|
|
| 16 |
# ===== Custom CSS =====
|
| 17 |
st.markdown("""
|
| 18 |
<style>
|
| 19 |
-
html, body, [class*="css"]
|
| 20 |
font-family: 'Poppins', sans-serif;
|
| 21 |
}
|
| 22 |
.block-container {
|
|
@@ -71,8 +71,8 @@ def save_to_gsheet(name, student_id, major, prediction, semester_data, sheet_nam
|
|
| 71 |
# Get all values from the sheet to check if the first row is empty (i.e., headers not created yet)
|
| 72 |
all_values = sheet.get_all_values()
|
| 73 |
|
| 74 |
-
#
|
| 75 |
-
if
|
| 76 |
headers = ["Họ và tên", "MSV", "Khoa", "Dự báo", "Thời gian"] + [
|
| 77 |
f"Số môn không thi - HK{i+1}" for i in range(6)] + [
|
| 78 |
f"Số tín chỉ không thi - HK{i+1}" for i in range(6)] + [
|
|
@@ -84,20 +84,24 @@ def save_to_gsheet(name, student_id, major, prediction, semester_data, sheet_nam
|
|
| 84 |
f"Số môn đạt - HK{i+1}" for i in range(6)] + [
|
| 85 |
f"GPA - HK{i+1}" for i in range(6)] + [
|
| 86 |
f"Xếp loại - HK{i+1}" for i in range(6)]
|
| 87 |
-
|
| 88 |
-
elif sheet_name == "Trang tính2": # Important features model
|
| 89 |
-
headers = ["Họ và tên", "MSV", "Khoa", "Dự báo", "Thời gian"] + [
|
| 90 |
-
f"Số tín chỉ đạt - HK{i+1}" for i in range(6)] + [
|
| 91 |
-
f"Số tín chỉ nợ - HK{i+1}" for i in range(6)] + [
|
| 92 |
-
f"Điểm trung bình - HK{i+1}" for i in range(6)]
|
| 93 |
-
|
| 94 |
-
# If the sheet is empty or if headers are missing, insert headers
|
| 95 |
-
if not all_values or len(all_values[0]) != len(headers): # Ensure number of columns matches expected headers
|
| 96 |
sheet.append_row(headers) # Create headers if missing
|
| 97 |
|
| 98 |
# Append data to the sheet
|
| 99 |
sheet.append_row(data_row)
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
# ===== HEADER =====
|
| 102 |
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)
|
| 103 |
st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)
|
|
@@ -111,7 +115,7 @@ st.subheader("🔢 Chọn ví dụ mẫu hoặc nhập thông tin cá nhân")
|
|
| 111 |
|
| 112 |
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ế"])
|
| 113 |
|
| 114 |
-
# Ví dụ mẫu cho mô hình "Dùng toàn bộ dữ liệu"
|
| 115 |
sample_cntt_example_full = {
|
| 116 |
"name": "Nguyễn Văn A",
|
| 117 |
"student_id": "10117367",
|
|
@@ -126,7 +130,8 @@ sample_cntt_example_full = {
|
|
| 126 |
]
|
| 127 |
}
|
| 128 |
|
| 129 |
-
# Ví dụ mẫu cho mô hình "Dùng mô hình đơn giản"
|
|
|
|
| 130 |
sample_kinhte_example_simple = {
|
| 131 |
"name": "Trần Thị B",
|
| 132 |
"student_id": "11418092",
|
|
@@ -142,6 +147,7 @@ sample_kinhte_example_simple = {
|
|
| 142 |
}
|
| 143 |
|
| 144 |
|
|
|
|
| 145 |
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====
|
| 146 |
sample_cntt_example_simple = {
|
| 147 |
"name": "Nguyễn Văn B",
|
|
@@ -174,22 +180,12 @@ sample_kinhte_example_full = {
|
|
| 174 |
|
| 175 |
# ===== Thông tin cá nhân =====
|
| 176 |
if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
sample_data_personal = sample_cntt_example_full
|
| 180 |
-
else: # Dùng đặc trưng quan trọng
|
| 181 |
-
sample_data_personal = sample_cntt_example_simple
|
| 182 |
-
name = st.text_input("👤 Họ và tên", value=sample_data_personal["name"])
|
| 183 |
-
student_id = st.text_input("🎓 Mã sinh viên", value=sample_data_personal["student_id"])
|
| 184 |
major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=0)
|
| 185 |
elif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
sample_data_personal = sample_kinhte_example_full
|
| 189 |
-
else: # Dùng đặc trưng quan trọng
|
| 190 |
-
sample_data_personal = sample_kinhte_example_simple
|
| 191 |
-
name = st.text_input("👤 Họ và tên", value=sample_data_personal["name"])
|
| 192 |
-
student_id = st.text_input("🎓 Mã sinh viên", value=sample_data_personal["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")
|
|
@@ -207,14 +203,28 @@ def input_semester(semester_label, default_values=None):
|
|
| 207 |
sotc0thi = st.number_input("Số tín chỉ không thi", 0, value=default_values[1] if default_values else 0, key=f"tc0_{semester_label}")
|
| 208 |
sotcno = st.number_input("Số tín chỉ nợ", 0, value=default_values[2] if default_values else 0, key=f"tcno_{semester_label}")
|
| 209 |
mhno = st.number_input("Số môn không đạt", 0, value=default_values[3] if default_values else 0, key=f"mhno_{semester_label}")
|
| 210 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 211 |
with col2:
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
mhpass = st.number_input("Số môn đạt", 0, value=default_values[7] if default_values else 0, key=f"mhpass_{semester_label}")
|
| 215 |
-
TBCHK = st.number_input("GPA", 0.0, 10.0, value=default_values[8] if default_values else 0.0, step=0.01, key=f"gpa_{semester_label}")
|
| 216 |
-
xep_loai_selected = st.selectbox("Xếp loại", list(range(7)), index=default_values[9] if default_values else 0, key=f"xeploai_{semester_label}")
|
| 217 |
-
return [somon0thi, sotc0thi, sotcno, mhno, sotc_qua, TCHK, smhk, mhpass, TBCHK, xep_loai_selected]
|
| 218 |
|
| 219 |
# ===== Giao diện theo mô hình =====
|
| 220 |
data = []
|
|
@@ -225,26 +235,42 @@ if model_type == "Dùng toàn bộ đặc trưng":
|
|
| 225 |
st.subheader("🔢 Nhập thông tin học kỳ chi tiết")
|
| 226 |
for idx, sem in enumerate(semesters):
|
| 227 |
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
|
| 228 |
-
default_values = example_data["semester_data"][idx*
|
| 229 |
-
data += input_semester(sem, default_values)
|
| 230 |
nganh = 0 if major == "Công nghệ thông tin" else 1
|
| 231 |
final_features = np.array(data + [nganh]).reshape(1, -1)
|
| 232 |
|
| 233 |
-
# Mô hình 2: "Dùng
|
| 234 |
-
else:
|
| 235 |
st.subheader("✨ Nhập thông tin rút gọn")
|
|
|
|
|
|
|
| 236 |
for idx, sem in enumerate(semesters):
|
| 237 |
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
|
| 238 |
-
default_values = example_data["semester_data"][idx*3:(idx+1)*3] if example_data else None
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
final_features = np.array(data).reshape(1, -1)
|
| 249 |
|
| 250 |
# ===== Predict =====
|
|
@@ -253,9 +279,10 @@ if st.button("🎯 DỰ BÁO"):
|
|
| 253 |
prediction = model_full.predict(final_features)
|
| 254 |
sheet_name = "Trang tính1" # For full data model, save to Trang tính1
|
| 255 |
|
| 256 |
-
else:
|
| 257 |
prediction = model_important.predict(final_features)
|
| 258 |
-
sheet_name = "Trang tính2" # For
|
|
|
|
| 259 |
|
| 260 |
if prediction[0] == 1:
|
| 261 |
st.success(f"🎉 Chúc mừng bạn {name} - {major}! Bạn có khả năng tốt nghiệp đúng hạn!")
|
|
@@ -264,4 +291,10 @@ if st.button("🎯 DỰ BÁO"):
|
|
| 264 |
st.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")
|
| 265 |
st.snow()
|
| 266 |
|
| 267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# ===== Custom CSS =====
|
| 17 |
st.markdown("""
|
| 18 |
<style>
|
| 19 |
+
html, body, [class*="css"] {
|
| 20 |
font-family: 'Poppins', sans-serif;
|
| 21 |
}
|
| 22 |
.block-container {
|
|
|
|
| 71 |
# Get all values from the sheet to check if the first row is empty (i.e., headers not created yet)
|
| 72 |
all_values = sheet.get_all_values()
|
| 73 |
|
| 74 |
+
# If the sheet is empty or if headers are missing, insert headers
|
| 75 |
+
if not all_values or len(all_values[0]) != (len(semester_data_flat) + 5): # Ensure number of columns matches
|
| 76 |
headers = ["Họ và tên", "MSV", "Khoa", "Dự báo", "Thời gian"] + [
|
| 77 |
f"Số môn không thi - HK{i+1}" for i in range(6)] + [
|
| 78 |
f"Số tín chỉ không thi - HK{i+1}" for i in range(6)] + [
|
|
|
|
| 84 |
f"Số môn đạt - HK{i+1}" for i in range(6)] + [
|
| 85 |
f"GPA - HK{i+1}" for i in range(6)] + [
|
| 86 |
f"Xếp loại - HK{i+1}" for i in range(6)]
|
| 87 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 88 |
sheet.append_row(headers) # Create headers if missing
|
| 89 |
|
| 90 |
# Append data to the sheet
|
| 91 |
sheet.append_row(data_row)
|
| 92 |
|
| 93 |
+
|
| 94 |
+
# ===== List all sheet names =====
|
| 95 |
+
# def list_sheet_names(sheet_id):
|
| 96 |
+
# client = get_gsheet_client()
|
| 97 |
+
# sheet = client.open_by_key(sheet_id)
|
| 98 |
+
|
| 99 |
+
# # List all sheet names
|
| 100 |
+
# sheet_names = [worksheet.title for worksheet in sheet.worksheets()]
|
| 101 |
+
# return sheet_names
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
|
| 105 |
# ===== HEADER =====
|
| 106 |
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)
|
| 107 |
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 |
|
| 116 |
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ế"])
|
| 117 |
|
| 118 |
+
# ===== Ví dụ mẫu cho mô hình "Dùng toàn bộ dữ liệu" =====
|
| 119 |
sample_cntt_example_full = {
|
| 120 |
"name": "Nguyễn Văn A",
|
| 121 |
"student_id": "10117367",
|
|
|
|
| 130 |
]
|
| 131 |
}
|
| 132 |
|
| 133 |
+
# ===== Ví dụ mẫu cho mô hình "Dùng mô hình đơn giản" =====
|
| 134 |
+
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho Kinh tế =====
|
| 135 |
sample_kinhte_example_simple = {
|
| 136 |
"name": "Trần Thị B",
|
| 137 |
"student_id": "11418092",
|
|
|
|
| 147 |
}
|
| 148 |
|
| 149 |
|
| 150 |
+
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====
|
| 151 |
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====
|
| 152 |
sample_cntt_example_simple = {
|
| 153 |
"name": "Nguyễn Văn B",
|
|
|
|
| 180 |
|
| 181 |
# ===== Thông tin cá nhân =====
|
| 182 |
if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":
|
| 183 |
+
name = st.text_input("👤 Họ và tên", value=sample_cntt_example_full["name"])
|
| 184 |
+
student_id = st.text_input("🎓 Mã sinh viên", value=sample_cntt_example_full["student_id"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=0)
|
| 186 |
elif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":
|
| 187 |
+
name = st.text_input("👤 Họ và tên", value=sample_kinhte_example_simple["name"])
|
| 188 |
+
student_id = st.text_input("🎓 Mã sinh viên", value=sample_kinhte_example_simple["student_id"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 189 |
major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=1)
|
| 190 |
else:
|
| 191 |
name = st.text_input("👤 Họ và tên")
|
|
|
|
| 203 |
sotc0thi = st.number_input("Số tín chỉ không thi", 0, value=default_values[1] if default_values else 0, key=f"tc0_{semester_label}")
|
| 204 |
sotcno = st.number_input("Số tín chỉ nợ", 0, value=default_values[2] if default_values else 0, key=f"tcno_{semester_label}")
|
| 205 |
mhno = st.number_input("Số môn không đạt", 0, value=default_values[3] if default_values else 0, key=f"mhno_{semester_label}")
|
| 206 |
+
try:
|
| 207 |
+
default_tc_qua = default_values[4] - default_values[2] if default_values and len(default_values) >= 5 else 0
|
| 208 |
+
except:
|
| 209 |
+
default_tc_qua = 0
|
| 210 |
+
sotc_qua = st.number_input("Số tín chỉ qua môn", 0, value=default_tc_qua, key=f"tcqua_{semester_label}")
|
| 211 |
+
with col2:
|
| 212 |
+
TCHK = st.number_input("Tổng tín chỉ học kỳ", 0, value=default_values[4] if default_values else 0, key=f"tchk_{semester_label}")
|
| 213 |
+
smhk = st.number_input("Số môn học kỳ", 0, value=default_values[5] if default_values else 0, key=f"smhk_{semester_label}")
|
| 214 |
+
mhpass = st.number_input("Số môn đạt", 0, value=default_values[6] if default_values else 0, key=f"mhpass_{semester_label}")
|
| 215 |
+
TBCHK = st.number_input("GPA", 0.0, 10.0, value=default_values[7] if default_values else 0.0, step=0.01, key=f"gpa_{semester_label}")
|
| 216 |
+
xep_loai_selected = st.selectbox("Xếp loại", list(range(7)), index=default_values[8] if default_values else 0, key=f"xeploai_{semester_label}")
|
| 217 |
+
return [somon0thi, sotc0thi, sotcno, mhno, TCHK, smhk, mhpass, TBCHK, xep_loai_selected, sotc_qua]
|
| 218 |
+
|
| 219 |
+
def input_important_features(semester_label, default_values=None):
|
| 220 |
+
with st.expander(f"📘 {semester_label}", expanded=True):
|
| 221 |
+
col1, col2 = st.columns(2)
|
| 222 |
+
with col1:
|
| 223 |
+
sotc_qua = st.number_input("Số tín chỉ đạt", 0, value=default_values[0] if default_values else 0, key=f"tcqua_imp_{semester_label}")
|
| 224 |
+
sotcno = st.number_input("Số tín chỉ nợ", 0, value=default_values[1] if default_values else 0, key=f"tcno_imp_{semester_label}")
|
| 225 |
with col2:
|
| 226 |
+
TBCHK = st.number_input("Điểm trung bình", 0.0, 10.0, value=default_values[2] if default_values else 0.0, step=0.01, key=f"gpa_imp_{semester_label}")
|
| 227 |
+
return [sotc_qua, sotcno, TBCHK]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
# ===== Giao diện theo mô hình =====
|
| 230 |
data = []
|
|
|
|
| 235 |
st.subheader("🔢 Nhập thông tin học kỳ chi tiết")
|
| 236 |
for idx, sem in enumerate(semesters):
|
| 237 |
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
|
| 238 |
+
default_values = example_data["semester_data"][idx*9:(idx+1)*9] if example_data else None
|
| 239 |
+
data += input_semester(sem, default_values)
|
| 240 |
nganh = 0 if major == "Công nghệ thông tin" else 1
|
| 241 |
final_features = np.array(data + [nganh]).reshape(1, -1)
|
| 242 |
|
| 243 |
+
# Mô hình 2: "Dùng mô hình đơn giản"
|
| 244 |
+
else:
|
| 245 |
st.subheader("✨ Nhập thông tin rút gọn")
|
| 246 |
+
|
| 247 |
+
# Allow selecting an example
|
| 248 |
for idx, sem in enumerate(semesters):
|
| 249 |
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
|
| 250 |
+
default_values = example_data["semester_data"][idx*3:(idx+1)*3] if example_data else None # Fix this line to slice 3 values per semester (Số tín chỉ đạt, Số tín chỉ nợ, GPA)
|
| 251 |
+
|
| 252 |
+
# Use expander to group each semester's fields in a collapsible box
|
| 253 |
+
if idx < 3: # First row (3 boxes)
|
| 254 |
+
with st.expander(f"📘 {sem}", expanded=True):
|
| 255 |
+
col1, col2, col3 = st.columns(3) # 3 columns for the first row
|
| 256 |
+
with col1:
|
| 257 |
+
sotc_qua = st.number_input(f"Số tín chỉ đạt - {sem}", 0, value=default_values[0] if default_values else 0, key=f"tcqua_imp_{sem}")
|
| 258 |
+
with col2:
|
| 259 |
+
sotcno = st.number_input(f"Số tín chỉ nợ - {sem}", 0, value=default_values[1] if default_values else 0, key=f"tcno_imp_{sem}")
|
| 260 |
+
with col3:
|
| 261 |
+
TBCHK = st.number_input(f"Điểm trung bình - {sem}", 0.0, 10.0, value=default_values[2] if default_values else 0.0, step=0.01, key=f"gpa_imp_{sem}")
|
| 262 |
+
else: # Second row (2 boxes)
|
| 263 |
+
with st.expander(f"📘 {sem}", expanded=True):
|
| 264 |
+
col1, col2 = st.columns(2) # 2 columns for the second row
|
| 265 |
+
with col1:
|
| 266 |
+
sotc_qua = st.number_input(f"Số tín chỉ đạt - {sem}", 0, value=default_values[0] if default_values else 0, key=f"tcqua_imp_{sem}")
|
| 267 |
+
sotcno = st.number_input(f"Số tín chỉ nợ - {sem}", 0, value=default_values[1] if default_values else 0, key=f"tcno_imp_{sem}")
|
| 268 |
+
with col2:
|
| 269 |
+
TBCHK = st.number_input(f"Điểm trung bình - {sem}", 0.0, 10.0, value=default_values[2] if default_values else 0.0, step=0.01, key=f"gpa_imp_{sem}")
|
| 270 |
+
|
| 271 |
+
# Collect semester data
|
| 272 |
+
data += [sotc_qua, sotcno, TBCHK]
|
| 273 |
+
|
| 274 |
final_features = np.array(data).reshape(1, -1)
|
| 275 |
|
| 276 |
# ===== Predict =====
|
|
|
|
| 279 |
prediction = model_full.predict(final_features)
|
| 280 |
sheet_name = "Trang tính1" # For full data model, save to Trang tính1
|
| 281 |
|
| 282 |
+
else:
|
| 283 |
prediction = model_important.predict(final_features)
|
| 284 |
+
sheet_name = "Trang tính2" # For full data model, save to Trang tính1
|
| 285 |
+
|
| 286 |
|
| 287 |
if prediction[0] == 1:
|
| 288 |
st.success(f"🎉 Chúc mừng bạn {name} - {major}! Bạn có khả năng tốt nghiệp đúng hạn!")
|
|
|
|
| 291 |
st.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")
|
| 292 |
st.snow()
|
| 293 |
|
| 294 |
+
# # Debugging: List all sheet names
|
| 295 |
+
# sheet_id = "1i7bDNvLVLXN93_e-FN0JLzpg1jb64Z_aEuyPjIfwbdQ" # Use your actual Spreadsheet ID
|
| 296 |
+
# sheet_names = list_sheet_names(sheet_id)
|
| 297 |
+
# st.write("Available sheet names:", sheet_names)
|
| 298 |
+
|
| 299 |
+
# Ghi log lên Google Sheets
|
| 300 |
+
save_to_gsheet(name, student_id, major, prediction, data,sheet_name) # Pass data to save function
|