Пример #1
0
def get_session():
    """
    Endpoint called for retrieving entire session, can be called by MRTD scanner and client PWA.

    Request data must contain:
    - ['session_id']: Session ID of session to be returned

    :return: selected session
    :rtype: session dictionary
    """
    data = request.get_data()
    data_json = json.loads(data)
    session_id = data_json['session_id']
    session = session_manager.get_session(session_id)

    if not session:
        logging.info("ACTIVE_SESSION:")
        logging.info(session_manager.active_sessions)

        msg = "No session to continue..."
        logging.error(msg)
        return json_response({'response': msg})

    if session['status'] == "GOT_ENCR_DATA":
        session = session_manager.change_status(session_id, "FINALIZED")

    logging.info("Returning session [{}]".format(session_id))

    return json_response({'response': session})
Пример #2
0
def accept_request():
    data = request.get_data()
    data_json = json.loads(data)
    session_id = data_json['session_id']
    username = data_json['username']
    brp_data = data_source.get_data(username)

    attribute_request = session_manager.get_session(session_id)['request']
    validator_response = validator.check(attribute_request, brp_data)

    random_color = "rgb({0},{1},{2})".format(random.randint(0, 255),
                                             random.randint(0, 255),
                                             random.randint(0, 255))

    data = {
        'request_valid': validator_response,
        'request_status': 'ACCEPTED',
        'secret': random_color
    }
    active_session = session_manager.append_session_data(session_id, data)

    # can not be ended here, since requestor needs to access data still
    # session_manager.end_session(session_id)

    return json_response({'response': active_session})
Пример #3
0
def get_session():
    data = request.get_data()
    data_json = json.loads(data)
    session_id = data_json['session_id']

    response = session_manager.get_session(session_id)
    return json_response({'response': response})
Пример #4
0
def safe_account(request, full_name):
    s = session_manager.get_session()
    root_account = s.book.get_root_account()
    account = root_account.lookup_by_full_name(full_name)
    parameters = {'full_name': full_name,
                  'name': account.GetName(),
                  'balance': account.GetBalance().to_double()}
    return render_to_response('account.html', parameters)
Пример #5
0
def safe_index(request):
    s = session_manager.get_session()
    root_account = s.book.get_root_account()
    accounts = root_account.get_children()
    accounts_list = []
    append_accounts(accounts, accounts_list)
    parameters = {'accounts_list': accounts_list, 'title': 'Accounts'}
    return render_to_response('index.html', parameters)
Пример #6
0
def main():
    import session_manager

    from net_builder import NetBuilder
    builder = NetBuilder((1, ))
    actions = builder.actions
    actions_no = len(actions)

    state_len = MAX_DEPTH * (actions_no - 1) + 1

    comm = MPI.COMM_WORLD
    my_rank = comm.Get_rank()
    status = MPI.Status()
    group = comm.Get_group()
    newgroup = group.Excl([0])
    work_comm = comm.Create(newgroup)
    np.random.seed()
    workers_no = comm.Get_size() - 1
    if my_rank == 0:
        print('Total processes: ' + str(workers_no + 1))

    sess = session_manager.get_session(workers_no + 1)

    comm.Barrier()
    weights_buff = []
    weights_size = 0
    if my_rank == 0:
        m = Master(sess, state_len, actions_no, workers_no=workers_no)
        weights_buff = m.ac_net.get_weights()
        weights_size = len(weights_buff)

    weights_size = comm.bcast(weights_size)
    if my_rank > 0:
        for i in range(weights_size):
            weights_buff.append([])
    for i in range(weights_size):
        tmp_weights = 0
        if my_rank == 0:
            tmp_weights = weights_buff[i].eval(sess)
        weights_buff[i] = comm.bcast(tmp_weights)
    worker = Worker(sess,
                    state_len,
                    actions_no,
                    max_depth=MAX_DEPTH,
                    weights=weights_buff,
                    workers_no=workers_no)
    worker.memory = comm.bcast(worker.memory)
    comm.Barrier()

    t = my_rank
    if my_rank == 0:
        import my_tools
        import os

        script_name = os.path.basename(__file__)
        script_name = script_name.replace('.py', '')
        logger = my_tools.ProgressLogger(script_name, actions)

    while (t < MAX_T):
        if my_rank == 0:
            # If it is ran inside spyder
            if (workers_no == 0):
                acc, state = worker.run()
                grads = worker.get_grads()
                eps = worker.eps
                logger.update(state, acc, t)
                logger.log(state, acc, t, eps, str(0))
                weights_buff = worker.grads
                m.ac_net.update_with_grads(weights_buff)
                t += 1

            else:

                # Gradients exchange
                for _ in range(workers_no):
                    grads = None
                    grads = comm.recv(status=status)
                    rnk = status.Get_source()
                    acc = comm.recv(status=status, source=rnk)
                    state = comm.recv(status=status, source=rnk)
                    eps = comm.recv(status=status, source=rnk)
                    logger.update(state, acc, t)
                    logger.log(state, acc, t, eps, str(rnk))
                    # Weights update
                    m.ac_net.update_with_grads(grads)

                    t += 1

        else:

            work_comm.Barrier()

            acc, state = worker.run()
            # Gradients xchange
            grads = worker.get_grads()
            comm.send(grads, dest=0)
            comm.send(acc, dest=0)
            comm.send(state, dest=0)
            comm.send(worker.eps, dest=0)
            # Weights xchange

        # Sync update
        weights_buff = []
        for i in range(weights_size):
            weights_buff.append([])

        if my_rank == 0:
            weights_buff = m.ac_net.get_weights()

            for i in range(weights_size):
                weights_buff[i] = weights_buff[i].eval(sess)

        for i in range(weights_size):
            weights_buff[i] = comm.bcast(weights_buff[i])

        worker.update_ac_weights(weights_buff)

        t = comm.bcast(t)
        t += my_rank

    if my_rank == 0:
        print('--Done--')
Пример #7
0
def main(dataset, trainable):

    import session_manager

    actions_no = ACTIONS_NO
    state_len = len(STARTING_STATE)
    actions_bounds = ACTIONS_BOUNDS.copy()

    comm = MPI.COMM_WORLD
    my_rank = comm.Get_rank()
    status = MPI.Status()
    group = comm.Get_group()
    newgroup = group.Excl([0])
    work_comm = comm.Create(newgroup)
    np.random.seed()
    workers_no = comm.Get_size() - 1
    if my_rank == 0:
        print('Total processes: ' + str(workers_no + 1))
    sess = session_manager.get_session(workers_no + 1)

    comm.Barrier()
    weights_buff = []
    weights_size = 0
    if my_rank == 0:
        m = Master(sess,
                   state_len,
                   actions_no,
                   actions_bounds,
                   workers_no=workers_no)
        weights_buff = m.ac_net.get_weights()
        print('AC Net generated')
        weights_size = len(weights_buff)

    weights_size = comm.bcast(weights_size)
    if my_rank > 0:
        for i in range(weights_size):
            weights_buff.append([])
    for i in range(weights_size):
        tmp = 0
        if my_rank == 0:
            tmp = weights_buff[i].eval(sess)
        weights_buff[i] = comm.bcast(tmp)
    worker = Worker(sess, state_len, actions_no, actions_bounds, MAX_DEPTH,
                    weights_buff, workers_no, dataset, trainable)

    comm.Barrier()

    t = my_rank
    if my_rank == 0:
        import my_tools
        import os

        script_name = os.path.basename(__file__)
        script_name = script_name.replace('.py', '')
        trainable_tag = 'Trainable' if trainable else 'Nontrainable'
        log_tag = dataset + '_' + trainable_tag
        logger = my_tools.ProgressLogger(script_name,
                                         log_tag,
                                         None,
                                         representation=None,
                                         live_plot=LIVE_PLOT)

    while (t < MAX_T):
        if my_rank == 0:

            # If it is ran inside spyder
            if (workers_no == 0):
                rnk = 0
                acc, state = worker.run()
                grads = worker.get_grads()
                EPS = worker.eps
                logger.update(state, acc, t)
                logger.log(state, acc, t, EPS, str(0))
                t += 1

            else:
                # Gradients xchange
                avg_grads = dict()
                avg_grads['p'] = []
                avg_grads['v'] = []
                for _ in range(workers_no):
                    grads = comm.recv(status=status)
                    rnk = status.Get_source()
                    acc = comm.recv(status=status, source=rnk)
                    state = comm.recv(status=status, source=rnk)
                    EPS = comm.recv(status=status, source=rnk)
                    logger.update(state, acc, t)
                    logger.log(state, acc, t, EPS, str(rnk))
                    t += 1
                    if len(avg_grads['p']) == 0:
                        for i in range(len(grads['p'])):
                            avg_grads['p'].append(grads['p'][i])
                        for i in range(len(grads['v'])):
                            avg_grads['v'].append(grads['v'][i])
                    else:
                        for i in range(len(grads['p'])):
                            avg_grads['p'][i] += grads['p'][i]
                        for i in range(len(grads['v'])):
                            avg_grads['v'][i] += grads['v'][i]
                    # Weights update

                for i in range(len(avg_grads['v'])):
                    avg_grads['v'][i] /= workers_no
                for i in range(len(avg_grads['p'])):
                    avg_grads['p'][i] /= workers_no

                m.ac_net.update_with_grads(avg_grads)

        else:

            work_comm.Barrier()
            acc, state = worker.run()
            # Gradients xchange
            grads = worker.get_grads()
            comm.send(grads, dest=0)
            comm.send(acc, dest=0)
            comm.send(state, dest=0)
            comm.send(worker.eps, dest=0)
            # Weights xchange

        if workers_no > 0:
            # Sync update
            weights_buff = []
            for i in range(weights_size):
                weights_buff.append([])

            if my_rank == 0:
                weights_buff = m.ac_net.get_weights()

                for i in range(weights_size):
                    weights_buff[i] = weights_buff[i].eval(sess)

            for i in range(weights_size):
                weights_buff[i] = comm.bcast(weights_buff[i])

            worker.update_ac_weights(weights_buff)
            t = comm.bcast(t)

    if my_rank == 0:
        print('--DONE--')