-
Notifications
You must be signed in to change notification settings - Fork 0
/
main2.py
445 lines (352 loc) · 17 KB
/
main2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
from flask import Flask, render_template, request, redirect, url_for
import mysql.connector
app = Flask(__name__, static_url_path='/static')
import pickle
import numpy as np
import warnings
warnings.filterwarnings("ignore")
# ######################################################
# ######################################################
# ######################################################
# ######################################################
# ######################################################
# ######################################################
def Recommendations(gender, part_time_job, absence_days, extracurricular_activities,
weekly_self_study_hours, math_score, history_score, physics_score,
chemistry_score, biology_score, english_score, geography_score,
total_score,average_score):
scaler = pickle.load(open("model/scaler.pkl", 'rb'))
model = pickle.load(open("model/ourmodel.pkl", 'rb'))
class_names = ['Lawyer', 'Doctor', 'Government Officer', 'Artist', 'Unknown',
'Software Engineer', 'Teacher', 'Business Owner', 'Scientist',
'Banker', 'Writer', 'Accountant', 'Designer',
'Construction Engineer', 'Game Developer', 'Stock Investor',
'Real Estate Developer']
# Encode categorical variables
gender_encoded = 1 if gender.lower() == 'female' else 0
part_time_job_encoded = 1 if part_time_job else 0
extracurricular_activities_encoded = 1 if extracurricular_activities else 0
# Create feature array
feature_array = np.array([[gender_encoded, part_time_job_encoded, absence_days, extracurricular_activities_encoded,
weekly_self_study_hours, math_score, history_score, physics_score,
chemistry_score, biology_score, english_score, geography_score,total_score,average_score]])
# Scale features
scaled_features = scaler.transform(feature_array)
# Predict using the model
probabilities = model.predict_proba(scaled_features)
# Get top five predicted classes along with their probabilities
top_classes_idx = np.argsort(-probabilities[0])[:4]
top_classes_names_probs = [(class_names[idx], probabilities[0][idx]) for idx in top_classes_idx]
return top_classes_names_probs
# ######################################################
# ######################################################
# ######################################################
# ######################################################
# ######################################################
# ######################################################
# ######################################################
# ######################################################
app = Flask(__name__)
# MySQL database setup
db = mysql.connector.connect(
host="localhost",
user="root",
password="MySQL@123",
database="carrier_recommendations"
)
cursor = db.cursor()
cursor1 = db.cursor()
# Ensure the 'students' table exists
cursor.execute('''CREATE DATABASE IF NOT EXISTS carrier_recommendations;''')
cursor.execute('''CREATE TABLE IF NOT EXISTS admin (
admin_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
admin_name VARCHAR(50) NOT NULL,
admin_email VARCHAR(50) NOT NULL UNIQUE,
admin_password VARCHAR(50),
status TINYINT(1) DEFAULT 0
); ''')
cursor.execute('''CREATE TABLE IF NOT EXISTS students (
student_ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
student_name VARCHAR(50) NOT NULL,
student_email VARCHAR(50) NOT NULL UNIQUE,
student_password VARCHAR(50) NOT NULL,
student_mobile VARCHAR(10) NOT NULL,
student_gender VARCHAR(50) NOT NULL,
student_recommendation VARCHAR(50) DEFAULT 'Not recommended yet',
recommendation_2 VARCHAR(50) DEFAULT 'Not recommended yet',
recommendation_3 VARCHAR(50) DEFAULT 'Not recommended yet',
status TINYINT(1) DEFAULT 0
);''')
cursor.execute('''CREATE TABLE IF NOT EXISTS results (
student_ID INT NOT NULL PRIMARY KEY,
gender VARCHAR(10),
part_time_job VARCHAR(5),
absence_days INT,
extracurricular_activities VARCHAR(5),
weekly_self_study_hours INT,
math_score INT,
history_score INT,
physics_score INT,
chemistry_score INT,
biology_score INT,
english_score INT,
geography_score INT,
total_score INT,
average_score DECIMAL(10, 6)
);
''')
@app.route('/')
def index():
return render_template('start.html')
# # @app.route('/')
# # def index():
# # return redirect(url_for('login'))
# @app.route('/start')
# def start():
# return render_template('start.html')
# Redirect to the start route when the user logs out
@app.route('/logout')
def logout():
# Redirect to the start route
return render_template('start.html')
@app.route('/login')
def login():
return render_template('login.html')
@app.route('/register')
def register():
return render_template('register.html')
@app.route('/register', methods=['POST'])
def register_post():
name = request.form['name']
email = request.form['email']
password = request.form['password']
mobile = request.form['mobile']
gender = request.form['gender']
cursor.execute("SELECT COUNT(*) AS email_count FROM students WHERE student_email = %s", (email,))
count = cursor.fetchone()
if count and count[0] > 0:
message = "Email allready Exist."
return render_template('register.html', message= message)
# Insert new record into the 'students' table
cursor.execute("INSERT INTO students (student_name, student_email, student_password, student_mobile, student_gender) VALUES (%s, %s, %s, %s, %s)",
(name, email, password, mobile, gender))
db.commit()
message = "Registered Succesfully."
return render_template('register.html', message= message)
@app.route('/login', methods=['POST'])
def login_post():
email = request.form['email']
password = request.form['password']
# Check if email and password match a record in the 'students' table
cursor.execute("SELECT * FROM students WHERE student_email=%s AND student_password=%s", (email, password))
user = cursor.fetchone()
if user:
# Successful login, redirect to the home page
return redirect(url_for('home', user_id=user[0], user_name=user[1]))
else:
message = "Inavlid Email or Password"
return render_template('login.html',message = message)
@app.route('/home/<int:user_id>/<user_name>')
def home(user_id, user_name):
return render_template('home.html', user_id=user_id, user_name=user_name)
@app.route('/profile/<int:user_id>')
def profile(user_id):
# Fetch user data from the database based on user ID
cursor.execute("SELECT * FROM students WHERE student_ID=%s", (user_id,))
user = cursor.fetchone()
if user:
# Render profile template with user data
return render_template('profile.html', user=user)
else:
return "User not found"
@app.route('/fill_form/<int:user_id>/<user_name>')
def fill_form(user_id,user_name):
# Check if the user has already taken the test
cursor.execute("SELECT COUNT(student_id) FROM results WHERE student_id = %s", (user_id,))
count = cursor.fetchone()
# print(count)
if count[0]> 0:
# If the test has already been taken, display a message
message = "Test is all ready Taken"
return render_template('home.html', user_id=user_id, message=message,user_name=user_name)
else:
# If the test has not been taken, render the form for the user to fill out
return render_template('fill_form.html', user_id=user_id, user_name=user_name)
@app.route('/store_values/<int:user_id>/<user_name>', methods=['POST'])
def store_values(user_id, user_name):
if request.method == 'POST':
# Retrieve form data
student_id=user_id; # Change this accordingly
gender = request.form['gender']
part_time_job = request.form['part_time_job']
absence_days = request.form['absence_days']
extracurricular_activities = request.form['extracurricular_activities']
weekly_self_study_hours = request.form['weekly_self_study_hours']
math_score = request.form['math_score']
history_score = request.form['history_score']
physics_score = request.form['physics_score']
chemistry_score = request.form['chemistry_score']
biology_score = request.form['biology_score']
english_score = request.form['english_score']
geography_score = request.form['geography_score']
total_score = request.form['total_score']
average_score = request.form['average_score']
# Insert data into the database
insert_query = "INSERT INTO results (student_id,gender, part_time_job, absence_days, extracurricular_activities, weekly_self_study_hours, math_score, history_score, physics_score, chemistry_score, biology_score, english_score, geography_score, total_score, average_score) VALUES (%s,%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursor.execute(insert_query, (student_id,gender, part_time_job, absence_days, extracurricular_activities, weekly_self_study_hours, math_score, history_score, physics_score, chemistry_score, biology_score, english_score, geography_score, total_score, average_score))
db.commit()
#########################################################
#########################################################
#########################################################
#########################################################
if part_time_job == "No":
part_time_job=False
else:
part_time_job=True
if extracurricular_activities=="No":
extracurricular_activities=False
else:
extracurricular_activities=True
final_recommendations = Recommendations(gender=gender,
part_time_job=part_time_job,
absence_days=absence_days,
extracurricular_activities=extracurricular_activities,
weekly_self_study_hours=weekly_self_study_hours,
math_score=math_score,
history_score=history_score,
physics_score=physics_score,
chemistry_score=chemistry_score,
biology_score=biology_score,
english_score=english_score,
geography_score=geography_score,
total_score=total_score,
average_score=average_score)
print("Top recommended studies with probabilities:")
print("="*50)
ans=[]
for class_name in final_recommendations:
ans.append(class_name[0])
if "Unknown" in ans:
ans.remove("Unknown")
cursor.execute("UPDATE students SET student_recommendation = %s, recommendation_2 = %s, recommendation_3 = %s WHERE student_ID = %s",(ans[0], ans[1], ans[2], user_id))
db.commit()
#########################################################
############################################### ##########
#########################################################
#########################################################
#########################################################
# Redirect to some other page or return a response
message = "Submission successful."
return render_template('home.html', user_id=user_id, message=message, user_name=user_name)
@app.route('/update/<int:user_id>')
def update(user_id):
# Fetch user data from the database based on user ID
cursor.execute("SELECT * FROM students WHERE student_ID=%s", (user_id,))
user = cursor.fetchone()
if user:
# Render profile template with user data
return render_template('update.html', user=user,user_id=user_id)
else:
return "User not found"
# @app.route('/update_info/<int:user_id>')
# def update_info(user_id):
# # Fetch user data from the database based on user ID
# name = request.form['name']
# mobile = request.form['mobile']
# # Update user information in the database
# cursor.execute("UPDATE students SET student_name=%s, student_mobile=%s WHERE student_ID=%s", (name, mobile, user_id))
# db.commit()
# user = cursor.fetchone()
# if user:
# # Render profile template with user data
# return render_template('update.html', user=user)
# else:
# return "User not found"
from flask import request
@app.route('/update_info/<int:user_id>', methods=['POST'])
def update_info(user_id):
# Fetch user data from the form
name = request.form['name']
mobile = request.form['mobile']
# Update user information in the database
cursor.execute("UPDATE students SET student_name=%s, student_mobile=%s WHERE student_ID=%s", (name, mobile, user_id))
db.commit()
cursor.execute("SELECT * FROM students WHERE student_ID=%s", (user_id,))
user = cursor.fetchone()
message = "Succesffully updated"
if user:
# Render profile template with user data
return render_template('update.html', user=user, message= message)
else:
return render_template('update.html', user=user)
@app.route('/back/<int:user_id>/<user_name>')
def back(user_id, user_name):
# Fetch user_id from the session or database
# user_id = 1 # replace this with your actual method of getting the user_id
# Redirect to the start route
return render_template('home.html', user_id=user_id, user_name=user_name)
#######################################################################
#######################################################################
#######################################################################
#######################################################################
#######################################################################
@app.route('/admin_login')
def admin_login():
return render_template('admin_login.html')
@app.route('/admin_login', methods=['POST'])
def admin_login_post():
email = request.form['email']
password = request.form['password']
# Check if email and password match a record in the 'students' table
cursor.execute("SELECT * FROM admin WHERE admin_email=%s AND admin_password=%s", (email, password))
user = cursor.fetchone()
cursor1.execute("SELECT * FROM students")
id = cursor1.fetchall()
if user:
# Successful admin_login
return render_template('admin_home.html', admin_id=user[0],admin_name=user[1], id=id)
else:
message = "Inavlid Email or Password"
return render_template('admin_login.html',message = message)
######################################################333
@app.route('/search_student/<int:admin_id>/<admin_name>', methods=['POST'])
def search_student(admin_id,admin_name):
email = request.form['email']
# Execute a database query to search for the student by email
cursor.execute("SELECT * FROM students WHERE student_email=%s", (email,))
student = cursor.fetchone()
cursor1.execute("SELECT * FROM students")
id = cursor1.fetchall()
# Check if the student exists and render the template accordingly
if student:
return render_template('admin_home.html', student=student, id=id , admin_id=admin_id,admin_name=admin_name)
else:
message = "Data does Not Exist"
return render_template('admin_home.html' , message=message, id=id ,admin_id=admin_id,admin_name=admin_name)
@app.route('/fetch_student/<int:admin_id>/<admin_name>', methods=['POST'])
def fetch_student(admin_id,admin_name):
student_id = request.form['studentID']
# Execute a database query to fetch the student details by ID
cursor.execute("SELECT * FROM students WHERE student_ID=%s", (student_id,))
student = cursor.fetchone()
cursor1.execute("SELECT * FROM students")
id = cursor1.fetchall()
# Check if the student exists and render the template accordingly
if student:
return render_template('admin_home.html', student=student,id=id , admin_id=admin_id,admin_name=admin_name)
else:
return "Student not found"
@app.route('/delete_student/<int:admin_id>/<admin_name>', methods=['POST'])
def delete_student(admin_id,admin_name):
student_id = request.form['studentID']
cursor.execute("DELETE FROM results WHERE student_ID=%s", (student_id,))
db.commit()
# Execute a database query to delete the student record by ID
cursor.execute("DELETE FROM students WHERE student_ID=%s", (student_id,))
db.commit()
message = "Deletion successful."
cursor1.execute("SELECT * FROM students")
id = cursor1.fetchall()
return render_template('admin_home.html' , message=message, id=id, admin_id=admin_id,admin_name=admin_name)
if __name__ == '__main__':
app.run(debug=True)