namngo commited on
Commit
cfe24b2
·
verified ·
1 Parent(s): e607155

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -7
app.py CHANGED
@@ -1,11 +1,22 @@
1
  import streamlit as st
2
  import numpy as np
3
  import joblib
 
 
4
 
5
- # Load model
6
- model = joblib.load("TomekLinks_rf_model.pkl")
 
 
 
 
 
 
 
 
 
7
 
8
- # Setup page
9
  st.set_page_config(page_title="Dự đoán tốt nghiệp đúng hạn", page_icon="🎓", layout="wide")
10
 
11
  # Inject custom CSS
@@ -34,6 +45,9 @@ st.markdown("<h1 style='text-align: center; color: #003366;'>🎓 DỰ BÁO KH
34
  st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)
35
  st.write("---")
36
 
 
 
 
37
  # ===== Thông tin cá nhân =====
38
  with st.container():
39
  col1, col2, col3 = st.columns(3)
@@ -50,13 +64,11 @@ st.write("---")
50
  def input_semester(semester_label):
51
  with st.expander(f"📖 {semester_label}", expanded=True):
52
  col1, col2 = st.columns(2)
53
-
54
  with col1:
55
  somon0thi = st.number_input("Số môn không thi", min_value=0, value=0, key=f"somon0thi_{semester_label}")
56
  sotc0thi = st.number_input("Số tín chỉ không thi", min_value=0, value=0, key=f"sotc0thi_{semester_label}")
57
  sotcno = st.number_input("Số tín chỉ nợ", min_value=0, value=0, key=f"sotcno_{semester_label}")
58
  mhno = st.number_input("Số môn không đạt", min_value=0, value=0, key=f"mhno_{semester_label}")
59
-
60
  with col2:
61
  TCHK = st.number_input("Tổng số tín chỉ", min_value=0, value=0, key=f"TCHK_{semester_label}")
62
  smhk = st.number_input("Tổng số môn học kỳ", min_value=0, value=0, key=f"smhk_{semester_label}")
@@ -87,14 +99,19 @@ for sem in semesters:
87
  nganh = 0 if major.startswith("0") else 1
88
  final_features = np.array(data + [nganh]).reshape(1, -1)
89
 
90
- # ===== Predict Button =====
91
  st.write("")
92
  if st.button("🎯 DỰ BÁO"):
93
  prediction = model.predict(final_features)
94
 
95
  if prediction[0] == 1:
 
96
  st.success(f"🎉 Chúc mừng bạn {name} - {major.split('-')[1].strip()}! Bạn có khả năng tốt nghiệp đúng hạn!")
97
  st.balloons()
98
  else:
 
99
  st.error(f"⚠️ Bạn {name} - {major.split('-')[1].strip()} cần cố gắng hơn! Có nguy cơ trễ hạn.")
100
- st.snow()
 
 
 
 
1
  import streamlit as st
2
  import numpy as np
3
  import joblib
4
+ import gspread
5
+ from oauth2client.service_account import ServiceAccountCredentials
6
 
7
+ # ===== CẤU HÌNH GOOGLE SHEETS =====
8
+ def save_to_google_sheet(name, student_id, major, semester_data, prediction_result):
9
+ scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
10
+ creds = ServiceAccountCredentials.from_json_keyfile_name('credentials.json', scope)
11
+ client = gspread.authorize(creds)
12
+ sheet = client.open_by_url("https://docs.google.com/spreadsheets/d/1i7bDNvLVLXN93_e-FN0JLzpg1jb64Z_aEuyPjIfwbdQ/edit?usp=sharing")
13
+ worksheet = sheet.sheet1
14
+
15
+ # Tạo dòng dữ liệu
16
+ new_row = [name, student_id, major] + semester_data + [prediction_result]
17
+ worksheet.append_row(new_row)
18
 
19
+ # ===== CẤU HÌNH TRANG =====
20
  st.set_page_config(page_title="Dự đoán tốt nghiệp đúng hạn", page_icon="🎓", layout="wide")
21
 
22
  # Inject custom CSS
 
45
  st.markdown("<h4 style='text-align: center; color: #666;'>Áp dụng cho sinh viên năm 3</h4>", unsafe_allow_html=True)
46
  st.write("---")
47
 
48
+ # ===== Load Model =====
49
+ model = joblib.load("TomekLinks_rf_model.pkl")
50
+
51
  # ===== Thông tin cá nhân =====
52
  with st.container():
53
  col1, col2, col3 = st.columns(3)
 
64
  def input_semester(semester_label):
65
  with st.expander(f"📖 {semester_label}", expanded=True):
66
  col1, col2 = st.columns(2)
 
67
  with col1:
68
  somon0thi = st.number_input("Số môn không thi", min_value=0, value=0, key=f"somon0thi_{semester_label}")
69
  sotc0thi = st.number_input("Số tín chỉ không thi", min_value=0, value=0, key=f"sotc0thi_{semester_label}")
70
  sotcno = st.number_input("Số tín chỉ nợ", min_value=0, value=0, key=f"sotcno_{semester_label}")
71
  mhno = st.number_input("Số môn không đạt", min_value=0, value=0, key=f"mhno_{semester_label}")
 
72
  with col2:
73
  TCHK = st.number_input("Tổng số tín chỉ", min_value=0, value=0, key=f"TCHK_{semester_label}")
74
  smhk = st.number_input("Tổng số môn học kỳ", min_value=0, value=0, key=f"smhk_{semester_label}")
 
99
  nganh = 0 if major.startswith("0") else 1
100
  final_features = np.array(data + [nganh]).reshape(1, -1)
101
 
102
+ # ===== Predict and Save =====
103
  st.write("")
104
  if st.button("🎯 DỰ BÁO"):
105
  prediction = model.predict(final_features)
106
 
107
  if prediction[0] == 1:
108
+ result_text = "Đúng hạn"
109
  st.success(f"🎉 Chúc mừng bạn {name} - {major.split('-')[1].strip()}! Bạn có khả năng tốt nghiệp đúng hạn!")
110
  st.balloons()
111
  else:
112
+ result_text = "Trễ hạn"
113
  st.error(f"⚠️ Bạn {name} - {major.split('-')[1].strip()} cần cố gắng hơn! Có nguy cơ trễ hạn.")
114
+ st.snow()
115
+
116
+ # Gửi dữ liệu lên Google Sheets
117
+ save_to_google_sheet(name, student_id, major.split('-')[1].strip(), data, result_text)