This commit is contained in:
ahmed531998 2023-04-20 13:34:19 +02:00
parent f2bb7e77df
commit 88e11dcc7e
1 changed files with 55 additions and 50 deletions

105
main.py
View File

@ -99,52 +99,58 @@ def predict():
#rg = users[token]['args']['rg'] #rg = users[token]['args']['rg']
#vre = users[token]['args']['vre'] #vre = users[token]['args']['vre']
message = {} message = {}
if text == "<HELP_ON_START>": try:
state = {'help': True, 'inactive': False, 'modified_query':"", 'intent':""} if text == "<HELP_ON_START>":
dm.update(state) state = {'help': True, 'inactive': False, 'modified_query':"", 'intent':""}
action = dm.next_action() dm.update(state)
response = rg.gen_response(action, vrename=vre.name, username=users[token]['username'], name=users[token]['name'].split()[0]) action = dm.next_action()
message = {"answer": response} response = rg.gen_response(action, vrename=vre.name, username=users[token]['username'], name=users[token]['name'].split()[0])
elif text == "<RECOMMEND_ON_IDLE>": message = {"answer": response}
state = {'help': False, 'inactive': True, 'modified_query':"recommed: ", 'intent':""} elif text == "<RECOMMEND_ON_IDLE>":
dm.update(state) state = {'help': False, 'inactive': True, 'modified_query':"recommed: ", 'intent':""}
action = dm.next_action() dm.update(state)
response = rg.gen_response(action, username=users[token]['username'],name=users[token]['name'].split()[0], vrename=vre.name) action = dm.next_action()
message = {"answer": response} response = rg.gen_response(action, username=users[token]['username'],name=users[token]['name'].split()[0], vrename=vre.name)
new_state = {'modified_query': response} message = {"answer": response}
dm.update(new_state) new_state = {'modified_query': response}
else: dm.update(new_state)
state = nlu.process_utterance(text, dm.get_consec_history(), dm.get_sep_history()) else:
state['help'] = False state = nlu.process_utterance(text, dm.get_consec_history(), dm.get_sep_history())
state['inactive'] = False state['help'] = False
old_user_interests = user.get_user_interests() state['inactive'] = False
old_vre_material = pd.concat([vre.db['paper_db'], vre.db['dataset_db']]).reset_index(drop=True) old_user_interests = user.get_user_interests()
user_interests = [] old_vre_material = pd.concat([vre.db['paper_db'], vre.db['dataset_db']]).reset_index(drop=True)
for entity in state['entities']: user_interests = []
if entity['entity'] == 'TOPIC': for entity in state['entities']:
user_interests.append(entity['value']) if entity['entity'] == 'TOPIC':
user.update_interests(user_interests) user_interests.append(entity['value'])
new_user_interests = user.get_user_interests() user.update_interests(user_interests)
new_vre_material = pd.concat([vre.db['paper_db'], vre.db['dataset_db']]).reset_index(drop=True) new_user_interests = user.get_user_interests()
if (new_user_interests != old_user_interests or len(old_vre_material) != len(new_vre_material)): new_vre_material = pd.concat([vre.db['paper_db'], vre.db['dataset_db']]).reset_index(drop=True)
rec.generate_recommendations(users[token]['username'], new_user_interests, new_vre_material) if (new_user_interests != old_user_interests or len(old_vre_material) != len(new_vre_material)):
dm.update(state) rec.generate_recommendations(users[token]['username'], new_user_interests, new_vre_material)
action = dm.next_action() dm.update(state)
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(), vrename=vre.name, username=users[token]['username'], name=users[token]['name'].split()[0]) action = dm.next_action()
message = {"answer": response, "query": text, "cand": "candidate", "history": dm.get_consec_history(), "modQuery": state['modified_query']} 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(), vrename=vre.name, username=users[token]['username'], name=users[token]['name'].split()[0])
if state['intent'] == "QA": message = {"answer": response, "query": text, "cand": "candidate", "history": dm.get_consec_history(), "modQuery": state['modified_query']}
split_response = response.split("_______ \n ") if state['intent'] == "QA":
if len(split_response) > 1: split_response = response.split("_______ \n ")
response = split_response[1] if len(split_response) > 1:
new_state = {'modified_query': response, 'intent': state['intent']} response = split_response[1]
dm.update(new_state) new_state = {'modified_query': response, 'intent': state['intent']}
reply = jsonify(message) dm.update(new_state)
users[token]['dm'] = dm reply = jsonify(message)
users[token]['user'] = user users[token]['dm'] = dm
users[token]['activity'] = 0 users[token]['user'] = user
#users[token]['args']['vre'] = vre users[token]['activity'] = 0
#users[token]['args']['rg'] = rg #users[token]['args']['vre'] = vre
return reply #users[token]['args']['rg'] = rg
return reply
except Exception as e:
message = {"answer": str(e), "query": "", "cand": "candidate", "history": "", "modQuery": ""}
return jsonify(message)
@app.route('/api/feedback', methods = ['POST']) @app.route('/api/feedback', methods = ['POST'])
def feedback(): def feedback():
@ -152,7 +158,6 @@ def feedback():
print(data) print(data)
try: try:
"""
conn = psycopg2.connect(host="janet-pg", database=os.getenv("POSTGRES_DB"), user=os.getenv("POSTGRES_USER"), password=os.getenv("POSTGRES_PASSWORD")) conn = psycopg2.connect(host="janet-pg", database=os.getenv("POSTGRES_DB"), user=os.getenv("POSTGRES_USER"), password=os.getenv("POSTGRES_PASSWORD"))
cur = conn.cursor() cur = conn.cursor()
cur.execute('INSERT INTO feedback_experimental (query, history, janet_modified_query, is_modified_query_correct, user_modified_query, evidence_useful, response, preferred_response, response_length_feedback, response_fluency_feedback, response_truth_feedback, response_useful_feedback, response_time_feedback, response_intent) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)', cur.execute('INSERT INTO feedback_experimental (query, history, janet_modified_query, is_modified_query_correct, user_modified_query, evidence_useful, response, preferred_response, response_length_feedback, response_fluency_feedback, response_truth_feedback, response_useful_feedback, response_time_feedback, response_intent) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)',
@ -162,7 +167,7 @@ def feedback():
data['speed'], data['intent'])) data['speed'], data['intent']))
conn.commit() conn.commit()
cur.close() cur.close()
"""
reply = jsonify({"status": "done"}) reply = jsonify({"status": "done"})
return reply return reply
@ -202,7 +207,7 @@ if __name__ == "__main__":
threading.Thread(target=vre_fetch, name='updatevre').start() threading.Thread(target=vre_fetch, name='updatevre').start()
threading.Thread(target=clear_inactive, name='clear').start() threading.Thread(target=clear_inactive, name='clear').start()
"""
conn = psycopg2.connect(host="janet-pg", database=os.getenv("POSTGRES_DB"), user=os.getenv("POSTGRES_USER"), password=os.getenv("POSTGRES_PASSWORD")) conn = psycopg2.connect(host="janet-pg", database=os.getenv("POSTGRES_DB"), user=os.getenv("POSTGRES_USER"), password=os.getenv("POSTGRES_PASSWORD"))
cur = conn.cursor() cur = conn.cursor()
@ -224,5 +229,5 @@ if __name__ == "__main__":
) )
conn.commit() conn.commit()
cur.close() cur.close()
"""
app.run(host='0.0.0.0') app.run(host='0.0.0.0')