Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -31,60 +31,15 @@ st.markdown("""
|
|
| 31 |
font-size: 18px;
|
| 32 |
font-weight: bold;
|
| 33 |
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
padding: 15px;
|
| 39 |
-
border-radius: 8px;
|
| 40 |
-
margin-top: 10px;
|
| 41 |
-
width: 45%; /* Adjust width of the box */
|
| 42 |
-
display: inline-block;
|
| 43 |
-
margin-right: 20px; /* Space between boxes */
|
| 44 |
-
margin-bottom: 20px;
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
/* Styling for the label */
|
| 48 |
-
.input-label {
|
| 49 |
-
font-weight: bold;
|
| 50 |
-
font-size: 14px;
|
| 51 |
-
margin-bottom: 5px;
|
| 52 |
-
display: block;
|
| 53 |
-
}
|
| 54 |
-
|
| 55 |
-
/* Styling for the input field */
|
| 56 |
-
.input-field {
|
| 57 |
-
border: 2px solid #003366;
|
| 58 |
-
border-radius: 8px;
|
| 59 |
-
padding: 8px;
|
| 60 |
-
font-size: 14px;
|
| 61 |
-
width: 100%; /* Ensure inputs are responsive to box size */
|
| 62 |
-
margin-bottom: 10px;
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
/* Styling for the whole semester input section */
|
| 66 |
-
.semester-section {
|
| 67 |
-
display: flex;
|
| 68 |
-
flex-wrap: wrap;
|
| 69 |
-
justify-content: space-between;
|
| 70 |
-
margin-bottom: 20px;
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
/* Input container for the label and field alignment */
|
| 74 |
-
.input-container {
|
| 75 |
-
display: flex;
|
| 76 |
-
flex-direction: column;
|
| 77 |
-
margin-bottom: 10px;
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
/* Ensure space between the label and field */
|
| 81 |
-
.semester-box-inner {
|
| 82 |
-
margin-bottom: 15px;
|
| 83 |
}
|
| 84 |
-
|
| 85 |
</style>
|
| 86 |
""", unsafe_allow_html=True)
|
| 87 |
|
|
|
|
| 88 |
# ===== GSheet integration =====
|
| 89 |
@st.cache_resource
|
| 90 |
def get_gsheet_client():
|
|
@@ -141,6 +96,18 @@ def save_to_gsheet(name, student_id, major, prediction, semester_data, sheet_nam
|
|
| 141 |
# Append data to the sheet
|
| 142 |
sheet.append_row(data_row)
|
| 143 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 144 |
# ===== HEADER =====
|
| 145 |
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)
|
| 146 |
st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)
|
|
@@ -170,6 +137,7 @@ sample_cntt_example_full = {
|
|
| 170 |
}
|
| 171 |
|
| 172 |
# ===== Ví dụ mẫu cho mô hình "Dùng mô hình đơn giản" =====
|
|
|
|
| 173 |
sample_kinhte_example_simple = {
|
| 174 |
"name": "Trần Thị B",
|
| 175 |
"student_id": "11418092",
|
|
@@ -184,6 +152,8 @@ sample_kinhte_example_simple = {
|
|
| 184 |
]
|
| 185 |
}
|
| 186 |
|
|
|
|
|
|
|
| 187 |
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====
|
| 188 |
sample_cntt_example_simple = {
|
| 189 |
"name": "Nguyễn Văn B",
|
|
@@ -199,6 +169,21 @@ sample_cntt_example_simple = {
|
|
| 199 |
]
|
| 200 |
}
|
| 201 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 202 |
# ===== Thông tin cá nhân =====
|
| 203 |
if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":
|
| 204 |
name = st.text_input("👤 Họ và tên", value=sample_cntt_example_full["name"])
|
|
@@ -216,6 +201,38 @@ else:
|
|
| 216 |
st.write("---")
|
| 217 |
|
| 218 |
# ===== Nhập thông tin học kỳ =====
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 219 |
data = []
|
| 220 |
semesters = ["HỌC KỲ I", "HỌC KỲ II", "HỌC KỲ III", "HỌC KỲ IV", "HỌC KỲ V", "HỌC KỲ VI"]
|
| 221 |
|
|
@@ -231,32 +248,15 @@ if model_type == "Dùng toàn bộ đặc trưng":
|
|
| 231 |
|
| 232 |
# Mô hình 2: "Dùng mô hình đơn giản"
|
| 233 |
else:
|
| 234 |
-
|
|
|
|
| 235 |
|
|
|
|
| 236 |
# Allow selecting an example
|
| 237 |
for idx, sem in enumerate(semesters):
|
| 238 |
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
|
| 239 |
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)
|
| 240 |
-
|
| 241 |
-
with st.expander(f"📘 {sem}", expanded=True):
|
| 242 |
-
# Create a box for each semester with 3 rows of label and input box
|
| 243 |
-
col1 = st.columns(1)[0] # Ensure single column for each input box
|
| 244 |
-
|
| 245 |
-
# Row 1
|
| 246 |
-
st.write("Số tín chỉ đạt:")
|
| 247 |
-
sotc_qua = st.number_input(f"Điền Số tín chỉ đạt - {sem}", 0, value=default_values[0] if default_values else 0, key=f"tcqua_imp_{sem}")
|
| 248 |
-
|
| 249 |
-
# Row 2
|
| 250 |
-
st.write("Số tín chỉ nợ:")
|
| 251 |
-
sotcno = st.number_input(f"Điền Số tín chỉ nợ - {sem}", 0, value=default_values[1] if default_values else 0, key=f"tcno_imp_{sem}")
|
| 252 |
-
|
| 253 |
-
# Row 3
|
| 254 |
-
st.write("Điểm trung bình:")
|
| 255 |
-
TBCHK = st.number_input(f"Điền Đ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}")
|
| 256 |
-
|
| 257 |
-
# Collect semester data
|
| 258 |
-
data += [sotc_qua, sotcno, TBCHK]
|
| 259 |
-
|
| 260 |
final_features = np.array(data).reshape(1, -1)
|
| 261 |
|
| 262 |
# ===== Predict =====
|
|
@@ -269,6 +269,7 @@ if st.button("🎯 DỰ BÁO"):
|
|
| 269 |
prediction = model_important.predict(final_features)
|
| 270 |
sheet_name = "Trang tính2" # For full data model, save to Trang tính1
|
| 271 |
|
|
|
|
| 272 |
if prediction[0] == 1:
|
| 273 |
st.success(f"🎉 Chúc mừng bạn {name} - {major}! Bạn có khả năng tốt nghiệp đúng hạn!")
|
| 274 |
st.balloons()
|
|
@@ -276,5 +277,10 @@ if st.button("🎯 DỰ BÁO"):
|
|
| 276 |
st.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")
|
| 277 |
st.snow()
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
# Ghi log lên Google Sheets
|
| 280 |
-
save_to_gsheet(name, student_id, major, prediction, data,sheet_name) # Pass data to save function
|
|
|
|
| 31 |
font-size: 18px;
|
| 32 |
font-weight: bold;
|
| 33 |
}
|
| 34 |
+
/* Shrink number_input and selectbox */
|
| 35 |
+
.stNumberInput input, .stSelectbox select {
|
| 36 |
+
font-size: 14px; /* Smaller font size */
|
| 37 |
+
padding: 5px; /* Reduce padding */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
}
|
|
|
|
| 39 |
</style>
|
| 40 |
""", unsafe_allow_html=True)
|
| 41 |
|
| 42 |
+
|
| 43 |
# ===== GSheet integration =====
|
| 44 |
@st.cache_resource
|
| 45 |
def get_gsheet_client():
|
|
|
|
| 96 |
# Append data to the sheet
|
| 97 |
sheet.append_row(data_row)
|
| 98 |
|
| 99 |
+
|
| 100 |
+
# ===== List all sheet names =====
|
| 101 |
+
# def list_sheet_names(sheet_id):
|
| 102 |
+
# client = get_gsheet_client()
|
| 103 |
+
# sheet = client.open_by_key(sheet_id)
|
| 104 |
+
|
| 105 |
+
# # List all sheet names
|
| 106 |
+
# sheet_names = [worksheet.title for worksheet in sheet.worksheets()]
|
| 107 |
+
# return sheet_names
|
| 108 |
+
|
| 109 |
+
|
| 110 |
+
|
| 111 |
# ===== HEADER =====
|
| 112 |
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)
|
| 113 |
st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)
|
|
|
|
| 137 |
}
|
| 138 |
|
| 139 |
# ===== Ví dụ mẫu cho mô hình "Dùng mô hình đơn giản" =====
|
| 140 |
+
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho Kinh tế =====
|
| 141 |
sample_kinhte_example_simple = {
|
| 142 |
"name": "Trần Thị B",
|
| 143 |
"student_id": "11418092",
|
|
|
|
| 152 |
]
|
| 153 |
}
|
| 154 |
|
| 155 |
+
|
| 156 |
+
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====
|
| 157 |
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====
|
| 158 |
sample_cntt_example_simple = {
|
| 159 |
"name": "Nguyễn Văn B",
|
|
|
|
| 169 |
]
|
| 170 |
}
|
| 171 |
|
| 172 |
+
# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho Kinh tế =====
|
| 173 |
+
sample_kinhte_example_full = {
|
| 174 |
+
"name": "Trần Thị C",
|
| 175 |
+
"student_id": "11418093",
|
| 176 |
+
"major": "Kinh tế",
|
| 177 |
+
"semester_data": [
|
| 178 |
+
0, 0, 0, 0, 16, 6, 6, 7.00, 1,
|
| 179 |
+
0, 0, 0, 0, 18, 7, 7, 8.20, 1,
|
| 180 |
+
0, 0, 0, 0, 17, 7, 7, 7.80, 1,
|
| 181 |
+
0, 0, 0, 0, 17, 7, 6, 7.90, 1,
|
| 182 |
+
0, 0, 0, 0, 19, 8, 5, 8.10, 0,
|
| 183 |
+
0, 0, 8, 2, 19, 7, 6, 7.30, 1
|
| 184 |
+
]
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
# ===== Thông tin cá nhân =====
|
| 188 |
if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":
|
| 189 |
name = st.text_input("👤 Họ và tên", value=sample_cntt_example_full["name"])
|
|
|
|
| 201 |
st.write("---")
|
| 202 |
|
| 203 |
# ===== Nhập thông tin học kỳ =====
|
| 204 |
+
def input_semester(semester_label, default_values=None):
|
| 205 |
+
with st.expander(f"📖 {semester_label}", expanded=True):
|
| 206 |
+
col1, col2 = st.columns(2)
|
| 207 |
+
with col1:
|
| 208 |
+
somon0thi = st.number_input("Số môn không thi", 0, value=default_values[0] if default_values else 0, key=f"sm0_{semester_label}")
|
| 209 |
+
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}")
|
| 210 |
+
sotcno = st.number_input("Số tín chỉ nợ", 0, value=default_values[2] if default_values else 0, key=f"tcno_{semester_label}")
|
| 211 |
+
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}")
|
| 212 |
+
try:
|
| 213 |
+
default_tc_qua = default_values[4] - default_values[2] if default_values and len(default_values) >= 5 else 0
|
| 214 |
+
except:
|
| 215 |
+
default_tc_qua = 0
|
| 216 |
+
sotc_qua = st.number_input("Số tín chỉ qua môn", 0, value=default_tc_qua, key=f"tcqua_{semester_label}")
|
| 217 |
+
with col2:
|
| 218 |
+
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}")
|
| 219 |
+
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}")
|
| 220 |
+
mhpass = st.number_input("Số môn đạt", 0, value=default_values[6] if default_values else 0, key=f"mhpass_{semester_label}")
|
| 221 |
+
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}")
|
| 222 |
+
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}")
|
| 223 |
+
return [somon0thi, sotc0thi, sotcno, mhno, TCHK, smhk, mhpass, TBCHK, xep_loai_selected, sotc_qua]
|
| 224 |
+
|
| 225 |
+
def input_important_features(semester_label, default_values=None):
|
| 226 |
+
with st.expander(f"📘 {semester_label}", expanded=True):
|
| 227 |
+
col1, col2 = st.columns(2)
|
| 228 |
+
with col1:
|
| 229 |
+
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}")
|
| 230 |
+
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}")
|
| 231 |
+
with col2:
|
| 232 |
+
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}")
|
| 233 |
+
return [sotc_qua, sotcno, TBCHK]
|
| 234 |
+
|
| 235 |
+
# ===== Giao diện theo mô hình =====
|
| 236 |
data = []
|
| 237 |
semesters = ["HỌC KỲ I", "HỌC KỲ II", "HỌC KỲ III", "HỌC KỲ IV", "HỌC KỲ V", "HỌC KỲ VI"]
|
| 238 |
|
|
|
|
| 248 |
|
| 249 |
# Mô hình 2: "Dùng mô hình đơn giản"
|
| 250 |
else:
|
| 251 |
+
|
| 252 |
+
# Ensure the example defaults to "Không ví dụ" when model type is 2
|
| 253 |
|
| 254 |
+
st.subheader("✨ Nhập thông tin rút gọn")
|
| 255 |
# Allow selecting an example
|
| 256 |
for idx, sem in enumerate(semesters):
|
| 257 |
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
|
| 258 |
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)
|
| 259 |
+
data += input_important_features(sem, default_values)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
final_features = np.array(data).reshape(1, -1)
|
| 261 |
|
| 262 |
# ===== Predict =====
|
|
|
|
| 269 |
prediction = model_important.predict(final_features)
|
| 270 |
sheet_name = "Trang tính2" # For full data model, save to Trang tính1
|
| 271 |
|
| 272 |
+
|
| 273 |
if prediction[0] == 1:
|
| 274 |
st.success(f"🎉 Chúc mừng bạn {name} - {major}! Bạn có khả năng tốt nghiệp đúng hạn!")
|
| 275 |
st.balloons()
|
|
|
|
| 277 |
st.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")
|
| 278 |
st.snow()
|
| 279 |
|
| 280 |
+
# # Debugging: List all sheet names
|
| 281 |
+
# sheet_id = "1i7bDNvLVLXN93_e-FN0JLzpg1jb64Z_aEuyPjIfwbdQ" # Use your actual Spreadsheet ID
|
| 282 |
+
# sheet_names = list_sheet_names(sheet_id)
|
| 283 |
+
# st.write("Available sheet names:", sheet_names)
|
| 284 |
+
|
| 285 |
# Ghi log lên Google Sheets
|
| 286 |
+
save_to_gsheet(name, student_id, major, prediction, data,sheet_name) # Pass data to save function
|