Ejemplo n.º 1
0
def cleanup(flask_app):
    db_path = flask_app.config['user_params']['db']['location']
    transcript_path = os.path.join(flask_app.config['user_params']['logging']['chat_dir'], 'transcripts.json')
    conn = sqlite3.connect(db_path)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()
    DatabaseReader.dump_chats(cursor, flask_app.config['scenario_db'], transcript_path)
    if flask_app.config['user_params']['end_survey'] == 1:
        surveys_path = os.path.join(flask_app.config['user_params']['logging']['chat_dir'], 'surveys.json')
        DatabaseReader.dump_surveys(cursor, surveys_path)
    conn.close()
Ejemplo n.º 2
0
def visualize():
    backend = get_backend()
    db_path = app.config['user_params']['db']['location']
    conn = sqlite3.connect(db_path)
    conn.row_factory = sqlite3.Row
    cursor = conn.cursor()

    # create table review if not exists
    if not cursor.execute(
            '''SELECT name FROM sqlite_master WHERE type='table' AND name='review' '''
    ).fetchone():
        cursor.execute(
            '''CREATE TABLE review (chat_id text, accept integer, message text)'''
        )
        conn.commit()
        print("created new table review")

    if not request.args.get('chat_id'):
        cursor.execute('SELECT DISTINCT chat_id FROM event')
        ids = [x[0] for x in cursor.fetchall()]
        cursor.execute('SELECT DISTINCT chat_id FROM review')
        reviewed_ids = [x[0] for x in cursor.fetchall()]
        if 'incomplete' in request.args:
            # list only chat_ids which are incomplete and not reviewed yet
            ids = [
                chat_id for chat_id in ids
                if (chat_id not in reviewed_ids) and (
                    not DatabaseReader.check_completed_info(cursor, chat_id))
            ]
        elif 'completed' in request.args:
            # list all chat_ids which are completed
            ids = [
                chat_id for chat_id in ids
                if DatabaseReader.check_completed_info(cursor, chat_id)
            ]
        else:
            # list only chat_ids which are completed but not reviewed yet
            ids = [
                chat_id for chat_id in ids if chat_id not in reviewed_ids
                and DatabaseReader.check_completed_info(cursor, chat_id)
            ]
        outcomes = []
        dialogues = []
        num_success = 0
        num_fail = 0
        num_incomplete = 0
        for chat_id in ids:
            outcome = DatabaseReader.get_chat_outcome(cursor, chat_id)
            outcomes.append(outcome)
            if outcome['reward'] == 1:
                num_success += 1
            else:
                num_fail += 1
            chat_info = DatabaseReader.get_chat_example(
                cursor, chat_id, app.config['scenario_db'],
                include_meta=True).to_dict()
            chat_text = ""
            select_id = {}
            for chat_event in chat_info['events']:
                if chat_event['action'] == 'message':
                    chat_text += "{}: {}\n".format(
                        chat_event['agent'],
                        chat_event['data'].encode('ascii', 'ignore'))
            dialogues.append(chat_text)

        num_ids = len(ids)

        return render_template('chat_list.html',
                               num_chats=num_ids,
                               chat_ids=ids,
                               chat_outcomes=outcomes,
                               reviewed=["" for i in range(num_ids)],
                               base_url=request.url + '?chat_id=',
                               num_success=num_success,
                               num_fail=num_fail,
                               num_incomplete=num_incomplete,
                               num_accept=0,
                               num_reject=0,
                               dialogues=dialogues)
    else:
        chat_id = request.args.get('chat_id')
        chat_info = DatabaseReader.get_chat_example(
            cursor, chat_id, app.config['scenario_db'],
            include_meta=True).to_dict()
        chat_text = ""
        select_id = {}
        for chat_event in chat_info['events']:
            if chat_event['action'] == 'message':
                chat_text += "{}: {}\n".format(
                    chat_event['agent'],
                    chat_event['data'].encode('ascii', 'ignore'))
            elif chat_event['action'] == 'select':
                chat_text += "<{} selected {}>\n".format(
                    chat_event['agent'], chat_event['data'])
                select_id[chat_event['agent']] = chat_event['data']

        cursor.execute(
            '''SELECT accept, message FROM review where chat_id=?''',
            (chat_id, ))
        res = cursor.fetchone()
        if res:
            review_status = res[0]
            message = res[1]
        else:
            review_status = -1
            message = ""

        select = {}
        for agent in [0, 1]:
            select[agent] = None
            if agent in select_id:
                for obj in chat_info['scenario']['kbs'][agent]:
                    if obj['id'] == select_id[agent]:
                        select[agent] = obj
                        break

        cursor.execute('''SELECT * FROM survey where chat_id=?''', (chat_id, ))
        res = cursor.fetchone()
        if res:
            survey = True
            cooperative = res[3]
            humanlike = res[4]
            comments = res[5]
        else:
            survey = False
            cooperative = None
            humanlike = None
            comments = None

        return render_template('visualize.html',
                               chat_id=chat_id,
                               chat_text=chat_text,
                               kb_0=chat_info['scenario']['kbs'][0],
                               kb_1=chat_info['scenario']['kbs'][1],
                               select_0=select[0],
                               select_1=select[1],
                               review_status=review_status,
                               message=message,
                               agent_0=chat_info['agents']['0'],
                               agent_1=chat_info['agents']['1'],
                               survey=survey,
                               cooperative=cooperative,
                               humanlike=humanlike,
                               comments=comments)
Ejemplo n.º 3
0
def clean_incomplete_chats(cursor):
    cursor.execute('SELECT DISTINCT chat_id FROM event')
    ids = [x[0] for x in cursor.fetchall()]
    for chat_id in ids:
        if not DatabaseReader.check_completed_info(cursor, chat_id):
            cursor.execute('DELETE FROM event where chat_id=?', (chat_id, ))