Skip to content

Commit b99cd78

Browse files
committed
added counter logic but commented the call. While testing uncomment it and run
1 parent 84d91aa commit b99cd78

File tree

1 file changed

+105
-56
lines changed

1 file changed

+105
-56
lines changed

Backend/app.py

Lines changed: 105 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
# DateTime
1111
from datetime import datetime
1212
from dateutil import parser
13+
from os.path import exists
1314

1415
# Bloom filter - for matching
1516
from bloom_filter2 import BloomFilter
@@ -30,10 +31,6 @@
3031
# import the tables
3132
# =============================================================
3233

33-
# add a counter for retraining
34-
# COUNTER_FOR_RETRANING = 0
35-
# =============================================================
36-
3734
# initialize the app
3835
app = Flask(__name__)
3936
api = Api(app, version='3.18.3', title='Sample API',
@@ -212,7 +209,105 @@ class Social_URLs(db.Model):
212209

213210
def __repr__(self) -> str:
214211
return f"{self.Social_URL_ID}"
212+
#==========================================================================================
213+
214+
#re-training logic
215+
# @app.route("/get_all_users_skills", methods=['GET'])
216+
def user_and_skills_for_retraining():
217+
users = Student.query.all()
218+
users_and_their_skills = dict()
219+
for user in users:
220+
if user.Name == "Dummy_a" or user.Name == "Dummy_ab":
221+
continue
222+
user_skills = list()
223+
db_user_skills = user.skills
224+
for skill in db_user_skills:
225+
user_skills.append(skill.Skill_name)
226+
users_and_their_skills[user.Name] = user_skills
227+
return users_and_their_skills
228+
# return users_and_their_skills, 200
229+
230+
231+
def domain_and_skills_for_retraining():
232+
'''
233+
For direct API calls through request
234+
'''
235+
# For post
236+
# data = request.get_json(force=True)
237+
# prediction = model.predict([np.array(list(data.values()))])
238+
# output = prediction[0]
239+
ids_domains = Domains.query.all()
240+
241+
res_ids_domains_skills = list()
242+
for domain_obj in ids_domains:
243+
curr_id_domain = dict()
244+
curr_id_domain['domain_id'] = domain_obj.Domain_ID
245+
curr_id_domain['domain_name'] = domain_obj.Domain_name
246+
curr_id_domain['skills'] = list()
247+
for s in domain_obj.see_skills:
248+
skill_in_domain = dict()
249+
skill_in_domain['skill_id'] = s.Skill_ID
250+
skill_in_domain['skill_name'] = s.Skill_name
251+
curr_id_domain['skills'].append(skill_in_domain)
252+
res_ids_domains_skills.append(curr_id_domain)
253+
return res_ids_domains_skills
254+
# output is list of domains dictionaries which has domian id,name and skills list with skill dictionaries with skill id,name
255+
256+
257+
def call_to_retraining_function():
258+
users_and_their_skills = user_and_skills_for_retraining()
259+
domain_and_skills = domain_and_skills_for_retraining()
260+
pipelining.update_models(domain_and_skills, users_and_their_skills)
261+
print('Retraining performed successfully')
262+
215263

264+
#counter logic
265+
#==========================================================================================
266+
counter_filename = 'counter.txt'
267+
target_count_value = 25
268+
269+
def check_file_exists(filename):
270+
if(exists(filename)):
271+
pass
272+
else:
273+
with open(filename, 'w') as f:
274+
f.write('0')
275+
f.close()
276+
277+
def check_counter(filename):
278+
check_file_exists(filename)
279+
with open(filename, 'r') as f:
280+
count = int(f.read())
281+
f.close()
282+
global target_count_value
283+
if(count > target_count_value):
284+
return True
285+
return False
286+
287+
def update_counter(filename):
288+
with open(filename, 'r') as f:
289+
count = int(f.read())
290+
with open(filename, 'w') as f:
291+
f.write(str(count + 1))
292+
293+
def reset_counter(filename):
294+
with open(filename, 'w') as f:
295+
f.write('0')
296+
297+
def should_call_retrain():
298+
global counter_filename
299+
status = check_counter(counter_filename)
300+
if(status):
301+
call_to_retraining_function()
302+
reset_counter(counter_filename)
303+
else:
304+
update_counter(counter_filename)
305+
306+
#==========================================================================================
307+
308+
309+
# Routes
310+
#==========================================================================================
216311

217312
# Homepage
218313
@app.route("/", methods=['GET', 'POST'])
@@ -226,6 +321,7 @@ def hello_world():
226321

227322
@app.route("/signup_and_login", methods=["POST"])
228323
def signup_and_login():
324+
# should_call_retrain()
229325
username = str(request.json["username"])
230326
new_user = False
231327
student = Student.query.filter_by(
@@ -269,6 +365,7 @@ def protected():
269365
@app.route("/get_recommendations", methods=['POST'])
270366
@jwt_required()
271367
def get_recommendations():
368+
# should_call_retrain()
272369
filter_skill_arr = request.json['filter_skills']
273370
filter_skill_arr = [x.strip().lower() for x in filter_skill_arr]
274371
# print(filter_skill_arr)
@@ -573,6 +670,7 @@ def get_student_profile():
573670
@app.route("/update_student_profile", methods=['POST'])
574671
@jwt_required()
575672
def update_student_profile():
673+
# should_call_retrain()
576674
if request.method == "POST":
577675
id = get_jwt_identity()
578676
student = Student.query.filter_by(Student_ID=id).first()
@@ -720,6 +818,7 @@ def get_student_languages():
720818
@app.route("/add_student_skills", methods=['POST'])
721819
@jwt_required()
722820
def add_student_skills():
821+
# should_call_retrain()
723822
if request.method == "POST":
724823
id = get_jwt_identity()
725824
skills_ids = list(request.json['Skills'])
@@ -739,6 +838,7 @@ def add_student_skills():
739838
@app.route("/update_student_skills", methods=['POST'])
740839
@jwt_required()
741840
def update_student_skills():
841+
# should_call_retrain()
742842
if request.method == "POST":
743843
id = get_jwt_identity()
744844
skills_ids = list(request.json['Skills'])
@@ -955,58 +1055,7 @@ def get_chats_after_last_cached():
9551055
# }
9561056
# or "DateTime": "Tue, 26 Oct 2021 13:10:38 GMT"
9571057

958-
959-
# @app.route("/get_all_users_skills", methods=['GET'])
960-
def user_and_skills_for_retraining():
961-
users = Student.query.all()
962-
users_and_their_skills = dict()
963-
for user in users:
964-
if user.Name == "Dummy_a" or user.Name == "Dummy_ab":
965-
continue
966-
user_skills = list()
967-
db_user_skills = user.skills
968-
for skill in db_user_skills:
969-
user_skills.append(skill.Skill_name)
970-
users_and_their_skills[user.Name] = user_skills
971-
return users_and_their_skills
972-
# return users_and_their_skills, 200
973-
974-
975-
def domain_and_skills_for_retraining():
976-
'''
977-
For direct API calls through request
978-
'''
979-
# For post
980-
# data = request.get_json(force=True)
981-
# prediction = model.predict([np.array(list(data.values()))])
982-
# output = prediction[0]
983-
ids_domains = Domains.query.all()
984-
985-
res_ids_domains_skills = list()
986-
for domain_obj in ids_domains:
987-
curr_id_domain = dict()
988-
curr_id_domain['domain_id'] = domain_obj.Domain_ID
989-
curr_id_domain['domain_name'] = domain_obj.Domain_name
990-
curr_id_domain['skills'] = list()
991-
for s in domain_obj.see_skills:
992-
skill_in_domain = dict()
993-
skill_in_domain['skill_id'] = s.Skill_ID
994-
skill_in_domain['skill_name'] = s.Skill_name
995-
curr_id_domain['skills'].append(skill_in_domain)
996-
res_ids_domains_skills.append(curr_id_domain)
997-
return res_ids_domains_skills
998-
# output is list of domains dictionaries which has domian id,name and skills list with skill dictionaries with skill id,name
999-
1000-
1001-
def call_to_retraining_function():
1002-
users_and_their_skills = user_and_skills_for_retraining()
1003-
domain_and_skills = domain_and_skills_for_retraining()
1004-
pipelining.update_models(domain_and_skills, users_and_their_skills)
1005-
print('Retraining performed successfully')
1006-
1007-
1008-
# call to retrain the model
1009-
# call_to_retraining_function()
1058+
#==============================================================================
10101059

10111060
if __name__ == "__main__":
10121061
app.run(debug=True)

0 commit comments

Comments
 (0)