Spaces:
Sleeping
Sleeping
Update app.py
Browse files15 unmodified lines1617181920212248 unmodified lines71727374757677785 unmodified lines848586878889909192939495969798991001011021037 unmodified lines1111121131141151161178 unmodified lines1261271281291301311329 unmodified lines14214314414514614726 unmodified lines17417517617717817918018118218318418518618718818919019119219319419511 unmodified lines2072082092102112122132142152162172182192204 unmodified lines2252262272282292302312322332342352362372382392402412422432442452462472482492502 unmodified lines2532542552562572582592602612 unmodified lines26426526626715 unmodified lines# ===== Custom CSS =====st.markdown("""<style>html, body, [class*="css"] {font-family: 'Poppins', sans-serif;}.block-container {48 unmodified lines# Get all values from the sheet to check if the first row is empty (i.e., headers not created yet)all_values = sheet.get_all_values()# Define headers based on the model type for accurate sheet savingif sheet_name == "Trang tính1": # Full features modelheaders = ["Họ và tên", "MSV", "Khoa", "Dự báo", "Thời gian"] + [f"Số môn không thi - HK{i+1}" for i in range(6)] + [f"Số tín chỉ không thi - HK{i+1}" for i in range(6)] + [5 unmodified linesf"Số môn đạt - HK{i+1}" for i in range(6)] + [f"GPA - HK{i+1}" for i in range(6)] + [f"Xếp loại - HK{i+1}" for i in range(6)]elif sheet_name == "Trang tính2": # Important features modelheaders = ["Họ và tên", "MSV", "Khoa", "Dự báo", "Thời gian"] + [f"Số tín chỉ đạt - HK{i+1}" for i in range(6)] + [f"Số tín chỉ nợ - HK{i+1}" for i in range(6)] + [f"Điểm trung bình - HK{i+1}" for i in range(6)]# If the sheet is empty or if headers are missing, insert headersif not all_values or len(all_values[0]) != len(headers): # Ensure number of columns matches expected headerssheet.append_row(headers) # Create headers if missing# Append data to the sheetsheet.append_row(data_row)# ===== HEADER =====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)st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)7 unmodified linessample_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ế"])# Ví dụ mẫu cho mô hình "Dùng toàn bộ dữ liệu"sample_cntt_example_full = {"name": "Nguyễn Văn A","student_id": "10117367",8 unmodified lines]}# Ví dụ mẫu cho mô hình "Dùng mô hình đơn giản"sample_kinhte_example_simple = {"name": "Trần Thị B","student_id": "11418092",9 unmodified lines}# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====sample_cntt_example_simple = {"name": "Nguyễn Văn B",26 unmodified lines# ===== Thông tin cá nhân =====if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":# Check model type to load appropriate example dataif model_type == "Dùng toàn bộ đặc trưng":sample_data_personal = sample_cntt_example_fullelse: # Dùng đặc trưng quan trọngsample_data_personal = sample_cntt_example_simplename = st.text_input("👤 Họ và tên", value=sample_data_personal["name"])student_id = st.text_input("🎓 Mã sinh viên", value=sample_data_personal["student_id"])major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=0)elif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":# Check model type to load appropriate example dataif model_type == "Dùng toàn bộ đặc trưng":sample_data_personal = sample_kinhte_example_fullelse: # Dùng đặc trưng quan trọngsample_data_personal = sample_kinhte_example_simplename = st.text_input("👤 Họ và tên", value=sample_data_personal["name"])student_id = st.text_input("🎓 Mã sinh viên", value=sample_data_personal["student_id"])major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=1)else:name = st.text_input("👤 Họ và tên")11 unmodified linessotc0thi = 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}")sotcno = st.number_input("Số tín chỉ nợ", 0, value=default_values[2] if default_values else 0, key=f"tcno_{semester_label}")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}")sotc_qua = st.number_input("Số tín chỉ qua môn", 0, value=default_values[4] if default_values else 0, key=f"tcqua_{semester_label}")with col2:TCHK = st.number_input("Tổng tín chỉ học kỳ", 0, value=default_values[5] if default_values else 0, key=f"tchk_{semester_label}")smhk = st.number_input("Số môn học kỳ", 0, value=default_values[6] if default_values else 0, key=f"smhk_{semester_label}")mhpass = st.number_input("Số môn đạt", 0, value=default_values[7] if default_values else 0, key=f"mhpass_{semester_label}")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}")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}")return [somon0thi, sotc0thi, sotcno, mhno, sotc_qua, TCHK, smhk, mhpass, TBCHK, xep_loai_selected]# ===== Giao diện theo mô hình =====data = []4 unmodified linesst.subheader("🔢 Nhập thông tin học kỳ chi tiết")for idx, sem in enumerate(semesters):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 Nonedefault_values = example_data["semester_data"][idx*10:(idx+1)*10] if example_data else Nonedata += input_semester(sem, default_values) # Use the corrected functionnganh = 0 if major == "Công nghệ thông tin" else 1final_features = np.array(data + [nganh]).reshape(1, -1)# Mô hình 2: "Dùng đặc trưng quan trọng"else: # model_type == "Dùng đặc trưng quan trọng"st.subheader("✨ Nhập thông tin rút gọn")for idx, sem in enumerate(semesters):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 Nonedefault_values = example_data["semester_data"][idx*3:(idx+1)*3] if example_data else Nonewith st.expander(f"📘 {sem}", expanded=True):col1, col2, col3 = st.columns(3)with col1:sotc_qua = st.number_input("Số tín chỉ đạt", 0, value=default_values[0] if default_values else 0, key=f"tcqua_imp_{sem}")with col2:sotcno = st.number_input("Số tín chỉ nợ", 0, value=default_values[1] if default_values else 0, key=f"tcno_imp_{sem}")with col3:TBCHK = st.number_input("Điểm trung bình hệ 10", 0.0, 10.0, value=default_values[2] if default_values else 0.0, step=0.01, key=f"gpa_imp_{sem}")data += [sotc_qua, sotcno, TBCHK]final_features = np.array(data).reshape(1, -1)# ===== Predict =====2 unmodified linesprediction = model_full.predict(final_features)sheet_name = "Trang tính1" # For full data model, save to Trang tính1else: # model_type == "Dùng đặc trưng quan trọng"prediction = model_important.predict(final_features)sheet_name = "Trang tính2" # For important data model, save to Trang tính2if prediction[0] == 1:st.success(f"🎉 Chúc mừng bạn {name} - {major}! Bạn có khả năng tốt nghiệp đúng hạn!")2 unmodified linesst.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")st.snow()save_to_gsheet(name, student_id, major, prediction, data, sheet_name) # Pass data to save function15 unmodified lines1617181920212248 unmodified lines71727374757677785 unmodified lines848586878889909192939495969798991001011021031041051061077 unmodified lines1151161171181191201218 unmodified lines1301311321331341351361379 unmodified lines14714814915015115215326 unmodified lines18018118218318418518618718818919019111 unmodified lines2032042052062072082092102112122132142152162172182192202212222232242252262272282292304 unmodified lines2352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762 unmodified lines2792802812822832842852862872882 unmodified lines29129229329429529629729829930015 unmodified lines# ===== Custom CSS =====st.markdown("""<style>html, body, [class*="css"] {font-family: 'Poppins', sans-serif;}.block-container {48 unmodified lines# Get all values from the sheet to check if the first row is empty (i.e., headers not created yet)all_values = sheet.get_all_values()# If the sheet is empty or if headers are missing, insert headersif not all_values or len(all_values[0]) != (len(semester_data_flat) + 5): # Ensure number of columns matchesheaders = ["Họ và tên", "MSV", "Khoa", "Dự báo", "Thời gian"] + [f"Số môn không thi - HK{i+1}" for i in range(6)] + [f"Số tín chỉ không thi - HK{i+1}" for i in range(6)] + [5 unmodified linesf"Số môn đạt - HK{i+1}" for i in range(6)] + [f"GPA - HK{i+1}" for i in range(6)] + [f"Xếp loại - HK{i+1}" for i in range(6)]sheet.append_row(headers) # Create headers if missing# Append data to the sheetsheet.append_row(data_row)# ===== List all sheet names =====# def list_sheet_names(sheet_id):# client = get_gsheet_client()# sheet = client.open_by_key(sheet_id)# # List all sheet names# sheet_names = [worksheet.title for worksheet in sheet.worksheets()]# return sheet_names# ===== HEADER =====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)st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)7 unmodified linessample_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ế"])# ===== Ví dụ mẫu cho mô hình "Dùng toàn bộ dữ liệu" =====sample_cntt_example_full = {"name": "Nguyễn Văn A","student_id": "10117367",8 unmodified lines]}# ===== Ví dụ mẫu cho mô hình "Dùng mô hình đơn giản" =====# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho Kinh tế =====sample_kinhte_example_simple = {"name": "Trần Thị B","student_id": "11418092",9 unmodified lines}# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho CNTT =====sample_cntt_example_simple = {"name": "Nguyễn Văn B",26 unmodified lines# ===== Thông tin cá nhân =====if sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":name = st.text_input("👤 Họ và tên", value=sample_cntt_example_full["name"])student_id = st.text_input("🎓 Mã sinh viên", value=sample_cntt_example_full["student_id"])major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=0)elif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":name = st.text_input("👤 Họ và tên", value=sample_kinhte_example_simple["name"])student_id = st.text_input("🎓 Mã sinh viên", value=sample_kinhte_example_simple["student_id"])major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"], index=1)else:name = st.text_input("👤 Họ và tên")11 unmodified linessotc0thi = 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}")sotcno = st.number_input("Số tín chỉ nợ", 0, value=default_values[2] if default_values else 0, key=f"tcno_{semester_label}")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}")try:default_tc_qua = default_values[4] - default_values[2] if default_values and len(default_values) >= 5 else 0except:default_tc_qua = 0sotc_qua = st.number_input("Số tín chỉ qua môn", 0, value=default_tc_qua, key=f"tcqua_{semester_label}")with col2: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}")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}")mhpass = st.number_input("Số môn đạt", 0, value=default_values[6] if default_values else 0, key=f"mhpass_{semester_label}")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}")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}")return [somon0thi, sotc0thi, sotcno, mhno, TCHK, smhk, mhpass, TBCHK, xep_loai_selected, sotc_qua]def input_important_features(semester_label, default_values=None):with st.expander(f"📘 {semester_label}", expanded=True):col1, col2 = st.columns(2)with col1: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}")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}")with col2: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}")return [sotc_qua, sotcno, TBCHK]# ===== Giao diện theo mô hình =====data = []4 unmodified linesst.subheader("🔢 Nhập thông tin học kỳ chi tiết")for idx, sem in enumerate(semesters):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 Nonedefault_values = example_data["semester_data"][idx*9:(idx+1)*9] if example_data else Nonedata += input_semester(sem, default_values)nganh = 0 if major == "Công nghệ thông tin" else 1final_features = np.array(data + [nganh]).reshape(1, -1)# Mô hình 2: "Dùng mô hình đơn giản"else:st.subheader("✨ Nhập thông tin rút gọn")# Allow selecting an examplefor idx, sem in enumerate(semesters):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 Nonedefault_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)# Use expander to group each semester's fields in a collapsible boxif idx < 3: # First row (3 boxes)with st.expander(f"📘 {sem}", expanded=True):col1, col2, col3 = st.columns(3) # 3 columns for the first rowwith col1: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}")with col2: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}")with col3: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}")else: # Second row (2 boxes)with st.expander(f"📘 {sem}", expanded=True):col1, col2 = st.columns(2) # 2 columns for the second rowwith col1: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}")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}")with col2: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}")# Collect semester datadata += [sotc_qua, sotcno, TBCHK]final_features = np.array(data).reshape(1, -1)# ===== Predict =====2 unmodified linesprediction = model_full.predict(final_features)sheet_name = "Trang tính1" # For full data model, save to Trang tính1else:prediction = model_important.predict(final_features)sheet_name = "Trang tính2" # For full data model, save to Trang tính1if prediction[0] == 1:st.success(f"🎉 Chúc mừng bạn {name} - {major}! Bạn có khả năng tốt nghiệp đúng hạn!")2 unmodified linesst.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")st.snow()# # Debugging: List all sheet names# sheet_id = "1i7bDNvLVLXN93_e-FN0JLzpg1jb64Z_aEuyPjIfwbdQ" # Use your actual Spreadsheet ID# sheet_names = list_sheet_names(sheet_id)# st.write("Available sheet names:", sheet_names)# Ghi log lên Google Sheetssave_to_gsheet(name, student_id, major, prediction, data,sheet_name) # Pass data to save function
app.py+85-52