Spaces:
Sleeping
Sleeping
Update app.py
Browse files6 unmodified lines78910111213142 unmodified lines171819202122239 unmodified lines333435363738394041424344454647487 unmodified lines5657585960616263646566676869707172737475767778798081822 unmodified lines8586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231247 unmodified lines1321331341351361371381 unmodified line1401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982996 unmodified linesimport json# ===== Load models =====model_full = joblib.load("random_forest_model_full.pkl")model_important = joblib.load("random_forest_model_importainfeature.pkl")# ===== Setup page =====st.set_page_config(page_title="Dự đoán tốt nghiệp đúng hạn", page_icon="🎓", layout="wide")2 unmodified linesst.markdown("""<style>html, body, [class*="css"] {font-family: 'Poppins', sans-serif;}.block-container {padding: 2rem 2rem 2rem 2rem;9 unmodified lines}/* Make number_input, selectbox, and text_input shorter using data-testid */div[data-testid="stNumberInput"] {max-width: 200px; /* Adjust width as needed */}div[data-testid="stSelectbox"] {max-width: 300px; /* Adjust width as needed */}div[data-testid="stTextInput"] {max-width: 350px; /* Adjust width as needed */}/* Ensure inputs inside columns don't overflow */div[data-testid="stNumberInput"] input[type="number"],div[data-testid="stSelectbox"] select,7 unmodified lines# ===== GSheet integration =====@st.cache_resourcedef get_gsheet_client():scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)client = gspread.authorize(creds)return client# ===== Save to Google Sheets =====def save_to_gsheet(name, student_id, major, prediction, semester_data, sheet_name="Trang tính1"):# Get the current date and timenow = datetime.now()current_time = now.strftime("%Y-%m-%d %H:%M:%S")# Ensure prediction is in the expected format (0 or 1)if isinstance(prediction, np.ndarray):prediction = int(prediction[0]) # Convert to int if it's ndarrayelif isinstance(prediction, np.int64):prediction = int(prediction) # Convert np.int64 to int# Flatten semester data to individual columns for each semestersemester_data_flat = [str(val) for val in semester_data] # Convert all semester data values to strings# Create a data row to insert into the sheetdata_row = [name, student_id, major, prediction, current_time] + semester_data_flat2 unmodified lines# Use the Spreadsheet ID (replace with your actual ID)sheet_id = "1i7bDNvLVLXN93_e-FN0JLzpg1jb64Z_aEuyPjIfwbdQ" # Use your actual ID heresheet = client.open_by_key(sheet_id).worksheet(sheet_name)# 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)] + [f"Số tín chỉ nợ - HK{i+1}" for i in range(6)] + [f"Số môn không đạt - HK{i+1}" for i in range(6)] + [f"Số tín chỉ qua môn - HK{i+1}" for i in range(6)] + [f"Tổng tín chỉ học kỳ - HK{i+1}" for i in range(6)] + [f"Số môn học kỳ - HK{i+1}" for i in range(6)] + [f"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 =====7 unmodified lines# ===== Giao diện nhập ví dụ =====st.subheader("🔢 Chọn ví dụ mẫu hoặc nhập thông tin cá nhân")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ế"])# ===== Ví dụ mẫu cho mô hình "Dùng toàn bộ dữ liệu" =====sample_cntt_example_full = {1 unmodified line"student_id": "10117367","major": "Công nghệ thông tin","semester_data": [0, 0, 0, 0, 17, 7, 7, 8.73, 0,0, 0, 0, 0, 17, 7, 7, 8.19, 0,0, 0, 0, 0, 17, 7, 7, 7.90, 0,0, 0, 0, 0, 17, 7, 7, 8.19, 0,0, 0, 0, 0, 19, 7, 6, 8.18, 0,0, 0, 5, 2, 19, 7, 5, 7.10, 1]}# ===== 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","major": "Kinh tế","semester_data": [17, 7, 7.64, # Học kỳ 1: Số tín chỉ đạt, Số tín chỉ nợ, GPA17, 7, 7.19, # Học kỳ 2: Số tín chỉ đạt, Số tín chỉ nợ, GPA17, 7, 6.94, # Học kỳ 3: Số tín chỉ đạt, Số tín chỉ nợ, GPA17, 7, 7.19, # Học kỳ 4: Số tín chỉ đạt, Số tín chỉ nợ, GPA19, 7, 7.65, # Học kỳ 5: Số tín chỉ đạt, Số tín chỉ nợ, GPA19, 7, 7.49 # Học kỳ 6: Số tín chỉ đạt, Số tín chỉ nợ, GPA]}# ===== 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","student_id": "10117368","major": "Công nghệ thông tin","semester_data": [15, 8, 7.50, # Học kỳ 1: Số tín chỉ đạt, Số tín chỉ nợ, GPA17, 7, 8.10, # Học kỳ 2: Số tín chỉ đạt, Số tín chỉ nợ, GPA18, 7, 7.90, # Học kỳ 3: Số tín chỉ đạt, Số tín chỉ nợ, GPA17, 7, 7.80, # Học kỳ 4: Số tín chỉ đạt, Số tín chỉ nợ, GPA19, 8, 8.30, # Học kỳ 5: Số tín chỉ đạt, Số tín chỉ nợ, GPA19, 7, 7.20 # Học kỳ 6: Số tín chỉ đạt, Số tín chỉ nợ, GPA]}# ===== Ví dụ cho mô hình "Dùng mô hình đơn giản" cho Kinh tế =====sample_kinhte_example_full = {"name": "Trần Thị C","student_id": "11418093","major": "Kinh tế","semester_data": [0, 0, 0, 0, 16, 6, 6, 7.00, 1,0, 0, 0, 0, 18, 7, 7, 8.20, 1,0, 0, 0, 0, 17, 7, 7, 7.80, 1,0, 0, 0, 0, 17, 7, 6, 7.90, 1,0, 0, 0, 0, 19, 8, 5, 8.10, 0,0, 0, 8, 2, 19, 7, 6, 7.30, 1]}# ===== 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")student_id = st.text_input("🎓 Mã sinh viên")major = st.selectbox("📚 Ngành học", ["Công nghệ thông tin", "Kinh tế"])st.write("---")# ===== Nhập thông tin học kỳ =====def input_semester(semester_label, default_values=None):with st.expander(f"📖 {semester_label}", expanded=True):col1, col2 = st.columns(2)with col1: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}")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}")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 = []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"]# Mô hình 1: "Dùng toàn bộ dữ liệu"if model_type == "Dùng toàn bộ đặc trưng":st.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:# Ensure the example defaults to "Không ví dụ" when model type is 2st.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)data += input_important_features(sem, default_values)final_features = np.array(data).reshape(1, -1)# ===== Predict =====if st.button("🎯 DỰ BÁO"):if model_type == "Dùng toàn bộ đặc trưng":prediction = 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!")st.balloons()else:st.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 function6 unmodified lines78910111213141516171819202 unmodified lines232425262728299 unmodified lines394041424344454647484950515253545556575859606162636465666768697071727374757677787980817 unmodified lines89909192939495969798991001011021031041051061071081091101111121131141151161171181191201211222 unmodified lines1251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731747 unmodified lines1821831841851861871881891901911921931 unmodified line1951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903916 unmodified linesimport json# ===== Load models =====# Ensure these file paths are correct relative to your scripttry:model_full = joblib.load("random_forest_model_full.pkl")model_important = joblib.load("random_forest_model_importainfeature.pkl")except FileNotFoundError:st.error("Error loading model files. Make sure 'random_forest_model_full.pkl' and 'random_forest_model_importainfeature.pkl' are in the same directory.")st.stop()# ===== Setup page =====st.set_page_config(page_title="Dự đoán tốt nghiệp đúng hạn", page_icon="🎓", layout="wide")2 unmodified linesst.markdown("""<style>html, body, [class*="css"] {font-family: 'Arial', sans-serif; /* Changed font for better compatibility */}.block-container {padding: 2rem 2rem 2rem 2rem;9 unmodified lines}/* Make number_input, selectbox, and text_input shorter using data-testid */div[data-testid="stNumberInput"] label {font-weight: bold; /* Make labels bold */}div[data-testid="stSelectbox"] label {font-weight: bold; /* Make labels bold */}div[data-testid="stTextInput"] label {font-weight: bold; /* Make labels bold */}/* Style the expander header */.streamlit-expander {border: 1px solid #ccc; /* Add border */border-radius: 5px; /* Add rounded corners */margin-bottom: 10px; /* Add space below */}.streamlit-expander > div:first-child {background-color: #f0f2f6; /* Light background color for header */padding: 10px; /* Add padding */border-radius: 5px 5px 0 0; /* Rounded top corners */}/* Style the expander content */.streamlit-expander > div:last-child {padding: 10px; /* Add padding to content */}/* Adjust width for inputs inside expanders */.streamlit-expander div[data-testid="stNumberInput"],.streamlit-expander div[data-testid="stSelectbox"],.streamlit-expander div[data-testid="stTextInput"] {max-width: 95%; /* Limit width within expander */}/* Ensure inputs inside columns don't overflow */div[data-testid="stNumberInput"] input[type="number"],div[data-testid="stSelectbox"] select,7 unmodified lines# ===== GSheet integration =====@st.cache_resourcedef get_gsheet_client():try:scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)client = gspread.authorize(creds)return clientexcept FileNotFoundError:st.error("Error loading Google Sheets credentials. Make sure 'credentials.json' is in the same directory.")st.stop()except Exception as e:st.error(f"Error connecting to Google Sheets: {e}")st.stop()# ===== Save to Google Sheets =====def save_to_gsheet(name, student_id, major, prediction, semester_data, sheet_name="Trang tính1"):# Get the current date and timenow = datetime.now()current_time = now.strftime("%Y-%m-%d %H:%M:%S")# Ensure prediction is in the expected format (0 or 1)if isinstance(prediction, np.ndarray):prediction = int(prediction[0]) # Convert to int if it's ndarrayelif isinstance(prediction, np.int64):prediction = int(prediction) # Convert np.int64 to int# Flatten semester data to individual columns for each semestersemester_data_flat = [str(val) for val in semester_data] # Convert all semester data values to strings# Create a data row to insert into the sheetdata_row = [name, student_id, major, prediction, current_time] + semester_data_flat2 unmodified lines# Use the Spreadsheet ID (replace with your actual ID)sheet_id = "1i7bDNvLVLXN93_e-FN0JLzpg1jb64Z_aEuyPjIfwbdQ" # Use your actual ID heretry:sheet = client.open_by_key(sheet_id).worksheet(sheet_name)except gspread.WorksheetNotFound:st.error(f"Google Sheet '{sheet_name}' not found. Please create a sheet with this name.")returnexcept Exception as e:st.error(f"Error accessing Google Sheet: {e}")return# 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 sheet name (model type)if sheet_name == "Trang tính1": # Full model headersheaders = ["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)] + [f"Số tín chỉ nợ - HK{i+1}" for i in range(6)] + [f"Số môn không đạt - HK{i+1}" for i in range(6)] + [f"Số tín chỉ qua môn - HK{i+1}" for i in range(6)] + [f"Tổng tín chỉ học kỳ - HK{i+1}" for i in range(6)] + [f"Số môn học kỳ - HK{i+1}" for i in range(6)] + [f"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 model headersheaders = ["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ỉ không đạt - HK{i+1}" for i in range(6)] + [f"Điểm trung bình hệ 10 - HK{i+1}" for i in range(6)]else: # Default headers if sheet_name is unexpectedheaders = ["Họ và tên", "MSV", "Khoa", "Dự báo", "Thời gian"] + [f"Data{i+1}" for i in range(len(semester_data_flat))]# If the sheet is empty or if headers are missing, insert headersif not all_values or all_values[0] != headers: # Check if the first row matches the expected headerssheet.append_row(headers) # Create headers if missing or incorrect# Append data to the sheettry:sheet.append_row(data_row)except Exception as e:st.error(f"Error appending data to Google Sheet: {e}")# ===== HEADER =====7 unmodified lines# ===== Giao diện nhập ví dụ =====st.subheader("🔢 Chọn ví dụ mẫu hoặc nhập thông tin cá nhân")# Update sample options based on selected model typeif model_type == "Dùng toàn bộ đặc trưng":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ế"])else: # "Dùng đặc trưng quan trọng"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ế"]) # Keep options for consistency# ===== Ví dụ mẫu cho mô hình "Dùng toàn bộ dữ liệu" =====sample_cntt_example_full = {1 unmodified line"student_id": "10117367","major": "Công nghệ thông tin","semester_data": [0, 0, 0, 0, 17, 7, 7, 8.73, 0, # HK1: sm0, tc0, tcno, mhno, TCHK, smhk, mhpass, TBCHK, xeploai, sotc_qua (this last one was added in the code logic, need to check the original data structure)0, 0, 0, 0, 17, 7, 7, 8.19, 0, # HK20, 0, 0, 0, 17, 7, 7, 7.90, 0, # HK30, 0, 0, 0, 17, 7, 7, 8.19, 0, # HK40, 0, 0, 0, 19, 7, 6, 8.18, 0, # HK50, 0, 5, 2, 19, 7, 5, 7.10, 1 # HK6]}# Correcting sample data structure for the full model (input_semester returns 10 values)# Let's re-examine the input_semester function's return:# return [somon0thi, sotc0thi, sotcno, mhno, TCHK, smhk, mhpass, TBCHK, xep_loai_selected, sotc_qua] - This is 10 values.# The sample data has 9 values per semester. This is a mismatch.# Let's adjust the sample data to match the 10 expected values per semester by adding a placeholder (e.g., 0) for 'sotc_qua' in the sample data, assuming it was derived or not a direct input feature in the original data.sample_cntt_example_full = {"name": "Nguyễn Văn A","student_id": "10117367","major": "Công nghệ thông tin","semester_data": [0, 0, 0, 0, 17, 7, 7, 8.73, 0, 17, # HK1: sm0, tc0, tcno, mhno, TCHK, smhk, mhpass, TBCHK, xeploai, sotc_qua0, 0, 0, 0, 17, 7, 7, 8.19, 0, 17, # HK20, 0, 0, 0, 17, 7, 7, 7.90, 0, 17, # HK30, 0, 0, 0, 17, 7, 7, 8.19, 0, 17, # HK40, 0, 0, 0, 19, 7, 6, 8.18, 0, 19, # HK50, 0, 5, 2, 19, 7, 5, 7.10, 1, 14 # HK6 (tc_qua = TCHK - tcno = 19 - 5 = 14)]}sample_kinhte_example_full = {"name": "Trần Thị C","student_id": "11418093","major": "Kinh tế","semester_data": [0, 0, 0, 0, 16, 6, 6, 7.00, 1, 16, # HK10, 0, 0, 0, 18, 7, 7, 8.20, 1, 18, # HK20, 0, 0, 0, 17, 7, 7, 7.80, 1, 17, # HK30, 0, 0, 0, 17, 7, 6, 7.90, 1, 17, # HK40, 0, 0, 0, 19, 8, 5, 8.10, 0, 19, # HK50, 0, 8, 2, 19, 7, 6, 7.30, 1, 11 # HK6 (tc_qua = TCHK - tcno = 19 - 8 = 11)]}# ===== Ví dụ cho mô hình "Dùng đặc trưng quan trọng" (matching image layout) =====# Features: Số tín chỉ đạt, Số tín chỉ không đạt, Điểm trung bình hệ 10sample_cntt_example_simple = {"name": "Nguyễn Văn B","student_id": "10117368","major": "Công nghệ thông tin","semester_data": [15, 0, 7.50, # Học kỳ 1: Số tín chỉ đạt, Số tín chỉ không đạt, Điểm trung bình hệ 1017, 0, 8.10, # Học kỳ 218, 0, 7.90, # Học kỳ 317, 0, 7.80, # Học kỳ 419, 0, 8.30, # Học kỳ 519, 0, 7.20 # Học kỳ 6]}sample_kinhte_example_simple = {"name": "Trần Thị B","student_id": "11418092","major": "Kinh tế","semester_data": [17, 0, 7.64, # Học kỳ 117, 0, 7.19, # Học kỳ 217, 0, 6.94, # Học kỳ 317, 0, 7.19, # Học kỳ 419, 0, 7.65, # Học kỳ 519, 0, 7.49 # Học kỳ 6]}# ===== Thông tin cá nhân =====# Determine which example data to use based on sample_option and model_typecurrent_sample_data = Noneif sample_option == "Dùng ví dụ mẫu ngành Công nghệ thông tin":current_sample_data = sample_cntt_example_full if model_type == "Dùng toàn bộ đặc trưng" else sample_cntt_example_simpleelif sample_option == "Dùng ví dụ mẫu ngành Kinh tế":current_sample_data = sample_kinhte_example_full if model_type == "Dùng toàn bộ đặc trưng" else sample_kinhte_example_simplename = st.text_input("👤 Họ và tên", value=current_sample_data["name"] if current_sample_data else "")student_id = st.text_input("🎓 Mã sinh viên", value=current_sample_data["student_id"] if current_sample_data else "")# Find the correct index for the default majormajor_options = ["Công nghệ thông tin", "Kinh tế"]default_major_index = major_options.index(current_sample_data["major"]) if current_sample_data and current_sample_data["major"] in major_options else 0major = st.selectbox("📚 Ngành học", major_options, index=default_major_index)st.write("---")# ===== Nhập thông tin học kỳ functions =====def input_semester_full(semester_label, default_values=None):# Ensure default_values has enough elements, pad with None if necessarypadded_defaults = (default_values + [None] * 10)[:10] if default_values else [None] * 10with st.expander(f"📖 {semester_label}", expanded=True):col1, col2 = st.columns(2)with col1:somon0thi = st.number_input("Số môn không thi", min_value=0, value=int(padded_defaults[0]) if padded_defaults[0] is not None else 0, key=f"sm0_{semester_label}")sotc0thi = st.number_input("Số tín chỉ không thi", min_value=0, value=int(padded_defaults[1]) if padded_defaults[1] is not None else 0, key=f"tc0_{semester_label}")sotcno = st.number_input("Số tín chỉ nợ", min_value=0, value=int(padded_defaults[2]) if padded_defaults[2] is not None else 0, key=f"tcno_{semester_label}")mhno = st.number_input("Số môn không đạt", min_value=0, value=int(padded_defaults[3]) if padded_defaults[3] is not None else 0, key=f"mhno_{semester_label}")# Calculate default_tc_qua based on TCHK and sotcno if TCHK is availabledefault_tc_qua_calc = (int(padded_defaults[4]) - int(padded_defaults[2])) if padded_defaults[4] is not None and padded_defaults[2] is not None else 0# Use the provided default_tc_qua if available, otherwise use the calculated onesotc_qua = st.number_input("Số tín chỉ qua môn", min_value=0, value=int(padded_defaults[9]) if padded_defaults[9] is not None else default_tc_qua_calc, key=f"tcqua_{semester_label}")with col2:TCHK = st.number_input("Tổng tín chỉ học kỳ", min_value=0, value=int(padded_defaults[4]) if padded_defaults[4] is not None else 0, key=f"tchk_{semester_label}")smhk = st.number_input("Số môn học kỳ", min_value=0, value=int(padded_defaults[5]) if padded_defaults[5] is not None else 0, key=f"smhk_{semester_label}")mhpass = st.number_input("Số môn đạt", min_value=0, value=int(padded_defaults[6]) if padded_defaults[6] is not None else 0, key=f"mhpass_{semester_label}")TBCHK = st.number_input("GPA", min_value=0.0, max_value=10.0, value=float(padded_defaults[7]) if padded_defaults[7] is not None else 0.0, step=0.01, key=f"gpa_{semester_label}")xep_loai_selected = st.selectbox("Xếp loại", list(range(7)), index=int(padded_defaults[8]) if padded_defaults[8] is not None and int(padded_defaults[8]) < 7 else 0, key=f"xeploai_{semester_label}")# Return the 10 values in the expected order for the full modelreturn [somon0thi, sotc0thi, sotcno, mhno, TCHK, smhk, mhpass, TBCHK, xep_loai_selected, sotc_qua]def input_important_features(semester_label, default_values=None):# Ensure default_values has enough elements, pad with None if necessary (expecting 3 values)padded_defaults = (default_values + [None] * 3)[:3] if default_values else [None] * 3with st.expander(f"📘 {semester_label}", expanded=True):# Removed columns to match the image layout (vertical inputs)sotc_qua = st.number_input("Số tín chỉ đạt", min_value=0, value=int(padded_defaults[0]) if padded_defaults[0] is not None else 0, key=f"tcqua_imp_{semester_label}")sotc0thi = st.number_input("Số tín chỉ không đạt", min_value=0, value=int(padded_defaults[1]) if padded_defaults[1] is not None else 0, key=f"tc0thi_imp_{semester_label}")TBCHK = st.number_input("Điểm trung bình hệ 10", min_value=0.0, max_value=10.0, value=float(padded_defaults[2]) if padded_defaults[2] is not None else 0.0, step=0.01, key=f"gpa_imp_{semester_label}")# Return the 3 values in the expected order for the simple modelreturn [sotc_qua, sotc0thi, TBCHK]# ===== Giao diện theo mô hình =====data = []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"] # Assuming 6 semesters are still needed# Mô hình 1: "Dùng toàn bộ đặc trưng"if model_type == "Dùng toàn bộ đặc trưng":st.subheader("🔢 Nhập thông tin học kỳ chi tiết")for idx, sem in enumerate(semesters):example_data_for_sem = current_sample_data["semester_data"][idx*10:(idx+1)*10] if current_sample_data else None # Slicing 10 valuesdata += input_semester_full(sem, example_data_for_sem)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 đặc trưng quan trọng"else:st.subheader("✨ Nhập thông tin rút gọn")for idx, sem in enumerate(semesters):example_data_for_sem = current_sample_data["semester_data"][idx*3:(idx+1)*3] if current_sample_data else None # Slicing 3 valuesdata += input_important_features(sem, example_data_for_sem)# For the important features model, the 'major' feature is not included in the input vectorfinal_features = np.array(data).reshape(1, -1)# ===== Predict =====if st.button("🎯 DỰ BÁO"):if model_type == "Dùng toàn bộ đặc trưng":# Ensure the number of features matches the model's expectation (6 semesters * 10 features/sem + 1 major = 61)if final_features.shape[1] != 61:st.error(f"Mismatch in number of features for 'Dùng toàn bộ đặc trưng' model. Expected 61, got {final_features.shape[1]}. Please check the input fields.")else:prediction = model_full.predict(final_features)sheet_name = "Trang tính1" # 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!")st.balloons()else:st.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")st.snow()# Ghi log lên Google Sheetssave_to_gsheet(name, student_id, major, prediction, data,sheet_name) # Pass data to save functionelse: # model_type == "Dùng đặc trưng quan trọng"# Ensure the number of features matches the model's expectation (6 semesters * 3 features/sem = 18)if final_features.shape[1] != 18:st.error(f"Mismatch in number of features for 'Dùng đặc trưng quan trọng' model. Expected 18, got {final_features.shape[1]}. Please check the input fields.")else:prediction = model_important.predict(final_features)sheet_name = "Trang tính2" # For important features 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!")st.balloons()else:st.error(f"⚠️ Bạn {name} - {major} cần cố gắng hơn! Có nguy cơ trễ hạn.")st.snow()# Ghi log lên Google Sheetssave_to_gsheet(name, student_id, major, prediction, data,sheet_name) # Pass data to save function
app.py+240-148