fixes
This commit is contained in:
parent
346cb31afd
commit
9412c10460
|
@ -4,51 +4,52 @@ import random
|
|||
|
||||
class Recommender:
|
||||
def __init__(self, retriever):
|
||||
self.curr_recommendations = []
|
||||
self.recommended = []
|
||||
self.curr_recommendations = {}
|
||||
self.recommended = {}
|
||||
self.retriever = retriever
|
||||
self.rand_seed = 5
|
||||
|
||||
def _new(self, material):
|
||||
for row in self.curr_recommendations:
|
||||
print(row)
|
||||
if username not in curr_recommendations:
|
||||
return True
|
||||
for row in self.curr_recommendations[username]:
|
||||
if row['id'] == material['id'] and row['type'] == material['type']:
|
||||
return False
|
||||
return True
|
||||
|
||||
def _match_tags(self, material, interest):
|
||||
def _match_tags(self, username, material, interest):
|
||||
score = 0.7
|
||||
for tag in material['tags']:
|
||||
if cosine_similarity(np.array(self.retriever.encode([tag])),
|
||||
np.array(self.retriever.encode([interest]))) > score:
|
||||
if self._new(material):
|
||||
print('hi')
|
||||
self.curr_recommendations.append(material)
|
||||
self.recommended.append(False)
|
||||
self.curr_recommendations[username] = self.curr_recommendations[username].append(material) if username not in self.curr_recommendations else [material]
|
||||
self.recommended[username] = self.recommended[username].append(False) if username not in self.recommended else [False]
|
||||
|
||||
def generate_recommendations(self, interests, new_material):
|
||||
def generate_recommendations(self, username, interests, new_material):
|
||||
for interest in interests:
|
||||
for i, material in new_material.iterrows():
|
||||
self._match_tags(material, interest)
|
||||
self._match_tags(username, material, interest)
|
||||
|
||||
def make_recommendation(self, user):
|
||||
if len(self.curr_recommendations) == 0:
|
||||
def make_recommendation(self, username):
|
||||
if len(self.curr_recommendations[username]) == 0:
|
||||
return ""
|
||||
to_consider = [idx for idx, value in enumerate(self.recommended) if value == False]
|
||||
to_consider = [idx for idx, value in enumerate(self.recommended[username]) if value == False]
|
||||
if len(to_consider) == 0:
|
||||
return ""
|
||||
index = random.choice(list(range(0, len(to_consider))))
|
||||
index = self.recommended[index]
|
||||
index = self.recommended[username][index]
|
||||
#while self.recommended[index] == True:
|
||||
# index = random.choice(list(range(0, len(self.curr_recommendations))))
|
||||
recommendation = "Hey " + user + "! This " + self.curr_recommendations[index][
|
||||
recommendation = "Hey " + username + "! This " + self.curr_recommendations[username][index][
|
||||
'type'].lower() + " about " + ', '.join(
|
||||
self.curr_recommendations[index]['tags']).lower() + " was posted recently by " + \
|
||||
self.curr_recommendations[index][
|
||||
self.curr_recommendations[username][index]['tags']).lower() + " was posted recently by " + \
|
||||
self.curr_recommendations[username][index][
|
||||
'author'].lower() + " on the catalogue. You may wanna check it out! It is titled " + \
|
||||
self.curr_recommendations[index]['title'].lower() + ". Cheers, Janet"
|
||||
self.curr_recommendations[username][index]['title'].lower() + ". Cheers, Janet"
|
||||
# self.curr_recommendations.remove(self.curr_recommendations[index])
|
||||
self.recommended[index] = True
|
||||
self.recommended[username][index] = True
|
||||
return recommendation
|
||||
|
||||
|
||||
|
|
41
main.py
41
main.py
|
@ -38,7 +38,6 @@ conn = psycopg2.connect(
|
|||
|
||||
cur = conn.cursor()
|
||||
|
||||
dms = {}
|
||||
users = {}
|
||||
|
||||
def vre_fetch():
|
||||
|
@ -50,12 +49,20 @@ def vre_fetch():
|
|||
rg.update_index(vre.get_index())
|
||||
rg.update_db(vre.get_db())
|
||||
|
||||
def user_interest_decay():
|
||||
def user_interest_decay(user):
|
||||
while True:
|
||||
print("decaying interests after 3 minutes")
|
||||
print("decaying interests after 3 minutes for " + user.username)
|
||||
time.sleep(180)
|
||||
user.decay_interests()
|
||||
|
||||
def clear_inactive():
|
||||
while True:
|
||||
time.sleep(1)
|
||||
for user in users:
|
||||
if user['activity'] > 3600:
|
||||
del users[user['username']]
|
||||
user['activity'] += 1
|
||||
|
||||
@app.route("/health", methods=['GET'])
|
||||
def health():
|
||||
return "Success", 200
|
||||
|
@ -64,23 +71,20 @@ def health():
|
|||
def init_dm():
|
||||
username = request.get_json().get("username")
|
||||
token = '2c1e8f88-461c-42c0-8cc1-b7660771c9a3-843339462'
|
||||
i = 1
|
||||
while username in dms:
|
||||
username = username + str(i)
|
||||
i+=1
|
||||
dms[username] = DM()
|
||||
user = User(username, token)
|
||||
users[username] = user
|
||||
threading.Thread(target=user_interest_decay, name='decayinterest_'+username).start()
|
||||
message = {"answer": "your assigned name is " + username, "assignedname": username}
|
||||
if username not in users:
|
||||
users[username] = {'dm': DM(), 'activity': 0, 'user': User(username, token)}
|
||||
threading.Thread(target=user_interest_decay, args=(users[username]['user'],), name='decayinterest_'+username).start()
|
||||
message = {"answer": "your assigned name is " + username, "assignedname": username}
|
||||
else:
|
||||
message = {"answer": "welcome back " + username, "assignedname": username}
|
||||
return message
|
||||
|
||||
@app.route("/api/predict", methods=['POST'])
|
||||
def predict():
|
||||
text = request.get_json().get("message")
|
||||
username = request.get_json().get("username")
|
||||
dm = dms[username]
|
||||
user = users[username]
|
||||
dm = users[username]['dm']
|
||||
user = users[username]['user']
|
||||
message = {}
|
||||
if text == "<HELP_ON_START>":
|
||||
state = {'help': True, 'inactive': False, 'modified_query':"", 'intent':""}
|
||||
|
@ -110,7 +114,7 @@ def predict():
|
|||
new_user_interests = user.get_user_interests()
|
||||
new_vre_material = pd.concat([vre.db['paper_db'], vre.db['dataset_db']]).reset_index(drop=True)
|
||||
if (new_user_interests != old_user_interests or len(old_vre_material) != len(new_vre_material)):
|
||||
rec.generate_recommendations(new_user_interests, new_vre_material)
|
||||
rec.generate_recommendations(username, new_user_interests, new_vre_material)
|
||||
dm.update(state)
|
||||
action = dm.next_action()
|
||||
response = rg.gen_response(action=action, utterance=state['modified_query'], state=dm.get_recent_state(), consec_history=dm.get_consec_history(), chitchat_history=dm.get_chitchat_history())
|
||||
|
@ -120,8 +124,9 @@ def predict():
|
|||
new_state = {'modified_query': response, 'intent': state['intent']}
|
||||
dm.update(new_state)
|
||||
reply = jsonify(message)
|
||||
dms[username] = dm
|
||||
users[username] = user
|
||||
users[username]['dm'] = dm
|
||||
users[username]['user'] = user
|
||||
users[username]['activity'] = 0
|
||||
return reply
|
||||
|
||||
@app.route('/api/feedback', methods = ['POST'])
|
||||
|
@ -174,6 +179,8 @@ if __name__ == "__main__":
|
|||
|
||||
threading.Thread(target=vre_fetch, name='updatevre').start()
|
||||
|
||||
threading.Thread(target=clear_inactive, name='clear').start()
|
||||
|
||||
rec = Recommender(retriever)
|
||||
|
||||
rg = ResponseGenerator(index,db, rec, generators, retriever)
|
||||
|
|
Loading…
Reference in New Issue