Exemple #1
0
def test_custom_predict_from_tarball(client):
    """
    Custom prediction logic.
    """
    with tempfile.NamedTemporaryFile("w", delete=False) as requirements_f:
        requirements_f.write("cowsay")
    with tempfile.NamedTemporaryFile("w") as f:
        f.write("""\
# make sure we can import custom library
import cowsay

def predict(model, query):
    import main
    # make sure we got the model passed in:
    assert model == main.model
    return [sum(query["instances"][0]), {"hello": 123}]
""")
        f.flush()
        tf = tarfile.open("mymodel", "w")
        tf.add(
            os.path.join(ORIGINAL_DIR, "example_model", "model.joblib"),
            arcname="mymodel/mymodel",
        )
        tf.add(f.name, arcname="mymodel/custom_predict.py")
        tf.add(requirements_f.name, arcname="mymodel/runtime-requirements.txt")
        tf.close()

    main.setup()
    result = client.post("/v1/models/model:predict",
                         json={"instances": [[1, 2, 3, 4]]})
    assert result.json == [10, {"hello": 123}]
Exemple #2
0
def dummy_phase_estimation(phase,
                           n=3,
                           ancilla=0,
                           precision=None,
                           config=None,
                           backend="local_qiskit_simulator",
                           skip=[],
                           coupling_map=None,
                           basis_gates=None):
    from main import setup, execute, get_name, plot_circuit
    if precision:
        ancilla = int(np.ceil(np.log2(2 + 1 / 2 / precision)))
    if ancilla:
        qp, qc, qrs, cr = setup(
            n,
            additional_registers={"qr": {
                "ur": 1,
                "ar": ancilla
            }},
            login=backend == "ibmqx5")
        qr, ur, ar = qrs
        creg = [qr[i] for i in range(len(qr))]
        creg += [ar[i] for i in range(len(ar))]
    else:
        qp, qc, qrs, cr = setup(n,
                                additional_registers={"qr": {
                                    "ur": 1
                                }},
                                login=backend == "ibmqx5")
        qr, ur = qrs
        creg = [qr[i] for i in range(len(qr))]
    qc.x(ur[0])

    def cu(qc, ctl, ur, n):
        # if abs(n*phase-1) > 1e-3 and n*phase > 1e-3:
        qc.cu1(n * 2 * np.pi * phase, ctl, ur[0])
        # if n == 1:
        # qc.cz(ctl, ur[0])

    phase_estimation(qc, creg, ur, cu)
    # qc.optimize_gates()
    qc.barrier(qr)
    qc.barrier(ur)
    qc.measure(qr, cr)

    res = execute(qp,
                  meta="QPE(%d+%d)-1 U1(%f)" % (n, ancilla, phase),
                  config=config,
                  backend=backend,
                  coupling_map=coupling_map,
                  basis_gates=basis_gates)
    # print(res.get_ran_qasm(get_name()))
    # print(res.get_counts(get_name()))
    print("N: %d\tAncilla: %d" % (n, ancilla))
    hc = handle_counts(res.get_counts(get_name()), n, skip=skip)
    print_handled_counts(hc)
    return hc
Exemple #3
0
def maincall():
    global loopDelay
    GUI.log('main function start at: %s' %
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    main.setup()
    GUI.log('setup finished at: %s' %
            time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
    while True:
        main.loop(main, socketio.sleep)
        socketio.sleep(main.loopDelay)
Exemple #4
0
def test_default_predict(client):
    """The default prediction logic, loading a pickled sklearn model."""
    shutil.copy(os.path.join(ORIGINAL_DIR, "example_model", "model.joblib"),
                "mymodel")

    main.setup()
    result = client.post("/v1/models/model:predict",
                         json={"instances": [[1, 2, 3, 4]]})
    assert result.json["predictions"] == [[
        pytest.approx(0.26, 0.1),
        pytest.approx(0.23, 0.1),
        pytest.approx(0.51, 0.1)
    ]]
def main():
    codec, model, _, config = setup('../../data/test')
    path = os.path.join("../../output", "original_gpt2")
    target_path = os.path.join("../../data", "test/stories")
    highlight_path = os.path.join("../../data", "test/highlights")
    rouge = Rouge()
    nlls = 0.0
    rouges = {}
    length = len(os.listdir(path))
    for i, file in enumerate(tqdm(os.listdir(path))):
    # for file in os.listdir(path):
        with open(os.path.join(path, file), "r") as f:
            text = f.read()
            # ppl = ppl_calc(model, codec, text)
            # print(ppl)
            # ppls.append(ppl)
        with open(os.path.join(target_path, file), "r") as t, open(os.path.join(highlight_path, file), "r") as h:
            target_text = t.read()
            highlight_text = h.read()
            nlls += nll_calc(model, codec, highlight_text, target_text)
            # print(nlls)
            # print(ppl)
            # nlls.append(nll)
            rouge_score = rouge.get_scores(text, target_text, avg=True)
            if not rouges:
                for cat in rouge_score.keys():
                    rouges[cat] = Counter(rouge_score[cat])
            else:
                for cat in rouge_score.keys():
                    rouges[cat] += Counter(rouge_score[cat])
    print(np.exp(nlls / (511*length)))
    for cat in rouges.keys():
        for key in rouges[cat].keys():
            rouges[cat][key] /= length
    print(rouges)
    def test_setup_satsp(self):
        ct = 'SAT_SP'
        for a in self.agent_cases:
            for b in self.blind_cases:
                for d in ['False']:
                    for s in self.season_cases:
                        args = self.default_args + ['--agent_type', a]
                        args = args + ['--daylighting', d]
                        args = args + ['--season', s]
                        args = args + ['--blinds', b]
                        args = args + ['--control_type', ct]
                        args = args + ['--zone_blinds_multi', 'False']
                        n = self.set_network(a)
                        args = args + ['--network', n]

                        setup_result = setup(args)

                        self._check_model(setup_result[0], b, d, s, 1, ct)
                        self._check_agent(setup_result[1], setup_result[3], a)
                        self._check_forecast(setup_result[2])
                        self.assertEqual(ct, setup_result[4])
                        self._check_other_args(setup_result[5], a, n, s, b, d,
                                               ct)
                        # base = f"test/main_tests/test_results/rl_results/{a}_{n}_{s}_blinds{b}_dlighting{d}_0.1_0.2_0.3"
                        base = f"test/main_tests/test_results/rl_results/{ct}_{a}_{n}_{s}_" \
                               f"blinds{b}MultiFalse_dlighting{d}_0.1_0.2_0.3"
                        self.assertEqual(setup_result[5][2], base)
Exemple #7
0
        def test_add_element(self):
                n, A0, S = setup()

                x0 = secrets.randbelow(pow(2, 256))
                x1 = secrets.randbelow(pow(2, 256))

                # first addition
                A1 = add(A0, S, x0, n)
                nonce = S[x0]

                proof = prove_membership(A0, S, x0, n)
                self.assertEqual(len(S), 1)
                self.assertEqual(A0, proof)
                self.assertTrue(verify_membership(A1, x0, nonce, proof, n))

                # second addition
                A2 = add(A1, S, x1, n)
                nonce = S[x1]

                proof = prove_membership(A0, S, x1, n)
                self.assertEqual(len(S), 2)
                self.assertEqual(A1, proof)
                self.assertTrue(verify_membership(A2, x1, nonce, proof, n))

                # delete
                A1_new = delete(A0, A2, S, x0, n)
                proof = prove_membership(A0, S, x1, n)
                proof_none = prove_membership(A0, S, x0, n)
                self.assertEqual(len(S), 1)
                self.assertEqual(proof_none, None)
                self.assertTrue(verify_membership(A1_new, x1, nonce, proof, n))
Exemple #8
0
def test_batch_add(total_utxo_set_size_for_accumulator, add_element_size):
    print("--> initialize and fill up accumulator state")
    n, A0, S = setup()

    elements_for_accumulator = create_random_list(
        total_utxo_set_size_for_accumulator)

    tik = time.time()
    A1, proof1 = batch_add(A0, S, elements_for_accumulator, n)
    tok = time.time()
    acc_batch_add_genesis_timing.append(tok - tik)
    print("<--   Done.", acc_batch_add_genesis_timing[-1])

    add_elements = create_random_list(add_element_size)
    print("--> ADD elements to accumulator")
    tik = time.time()
    A2, proof2 = batch_add(A1, S, add_elements, n)
    tok = time.time()
    print("<-- Done", tok - tik)

    nonces_list = []
    for x in add_elements:
        _, nonce = hash_to_prime(x=x, nonce=0)
        nonces_list.append(nonce)

    print("--> verify NI_POES")
    tik = time.time()
    print(
        batch_verify_membership_with_NIPoE(proof2[0], proof2[1], A1,
                                           add_elements, nonces_list, A2, n))
    tok = time.time()
    print("<-- Done", tok - tik)
Exemple #9
0
def main():
    _, settings, _, _, _ = pycraft.setup()
    save = pycraft.saves.new_save({'name': 'test', 'seed': 'This is a test!'})

    try:
        pycraft.game(pycraft.server_interface.LocalInterface('tester', save, 0), settings)
    finally:
        pycraft.setdown()
Exemple #10
0
def test_mem_witness_up_on_delete(total_utxo_set_size_for_accumulator,
                                  delete_element_index):

    if delete_element_index >= total_utxo_set_size_for_accumulator:
        print("delete element index should smaller than total_utxo_set_size")

    print("total_utxo_set_size_for_accumulator =",
          total_utxo_set_size_for_accumulator)

    print("--> initialize and fill up accumulator state")
    n, A0, S, order = setup()
    elements_for_accumulator = create_random_list(
        total_utxo_set_size_for_accumulator)
    tik = time.time()
    A_post, _ = batch_add(A0, S, elements_for_accumulator, n)
    tok = time.time()
    print("<--   Done.", tok - tik)

    print("--> generate other element proof")
    tik = time.time()
    other_element_proofs = {}
    for i in range(len(elements_for_accumulator)):
        if i != delete_element_index:
            other_element_proofs[
                elements_for_accumulator[i]] = prove_membership(
                    A0, S, elements_for_accumulator[i], n)
    tok = time.time()
    print("<-- Done.", tok - tik)

    print("--> delete element and generate A_del accumulator value.")
    tik = time.time()
    delete_element = elements_for_accumulator[delete_element_index]
    A_del = delete(A0, A_post, S, delete_element, n)
    tok = time.time()
    print("<-- Done.", tok - tik)

    print("--> update proof on delete element")
    update_proofs = {}

    tik = time.time()
    for x, proof in other_element_proofs.items():
        update_proofs[x] = acc_del_mem_witness_update(x, proof, delete_element,
                                                      A_del, n)
    tok = time.time()
    print("<-- Done.", tok - tik)

    print("--> verify updated proof on delete element")
    tik = time.time()
    result = []
    for x, proof in update_proofs.items():
        result.append(verify_membership(A_del, x, S[x], proof, n))
    tok = time.time()
    print("<-- Done.", tok - tik)

    print("verify results:")
    print(result)
Exemple #11
0
def test_witness_update_on_del_add(total_utxo_set_size_for_accumulator,
                                   delete_element_size, add_element_size):

    if delete_element_size >= total_utxo_set_size_for_accumulator:
        print("delete element size should smaller than total_utxo_set_size")

    print("total_utxo_set_size_for_accumulator =",
          total_utxo_set_size_for_accumulator)

    print("--> initialize and fill up accumulator state")
    n, A0, S = setup()
    elements_for_accumulator = create_random_list(
        total_utxo_set_size_for_accumulator)
    tik = time.time()
    A_post, _ = batch_add(A0, S, elements_for_accumulator, n)
    tok = time.time()
    print("<--   Done.", tok - tik)

    print("--> generate the last element proof")
    tik = time.time()
    last_element = elements_for_accumulator[-1]
    last_element_proof = prove_membership(A0, S, elements_for_accumulator[-1],
                                          n)
    tok = time.time()
    print("<-- Done.", tok - tik)

    print("--> delete elements")
    delete_elements = []
    for i in range(delete_element_size):
        delete_elements.append(elements_for_accumulator[i])
    A_post_del = batch_delete(A0, S, delete_elements, n)
    print("<--  Done.")

    print("--> witness update on batch delete")
    tik = time.time()
    new_proof_del = acc_batch_del_mem_witness_update(last_element,
                                                     last_element_proof,
                                                     delete_elements,
                                                     A_post_del, n)
    tok = time.time()
    print("<-- Done.", tok - tik)

    print("--> verify new proof")
    result = verify_membership(A_post_del, last_element, S[last_element],
                               new_proof_del, n)
    print("Done")
    print(result)

    elements_for_add = create_random_list(add_element_size)
    print("--> witness update on batch add")
    tik = time.time()
    acc_batch_add_witness_update(S, last_element, new_proof_del,
                                 elements_for_add, n)
    tok = time.time()
    print("<-- Done.", tok - tik)
Exemple #12
0
def test_default_predict_from_tarball(client):
    """
    The default prediction logic, loading a pickled sklearn model from a
    tarball.
    """
    tf = tarfile.open("mymodel", "w")
    tf.add(
        os.path.join(ORIGINAL_DIR, "example_model", "model.joblib"),
        arcname="mymodel/mymodel",
    )
    tf.close()

    main.setup()
    result = client.post("/v1/models/model:predict",
                         json={"instances": [[1, 2, 3, 4]]})
    assert result.json["predictions"] == [[
        pytest.approx(0.26, 0.1),
        pytest.approx(0.23, 0.1),
        pytest.approx(0.51, 0.1)
    ]]
Exemple #13
0
        def test_batch_add(self):
                n, A0, S = setup()

                elements_list = create_list(10)

                A_post_add, nipoe = batch_add(A0, S, elements_list, n)
                self.assertEqual(len(S), 10)

                nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_list))
                is_valid = batch_verify_membership_with_NIPoE(nipoe[0], nipoe[1], A0, elements_list, nonces_list, A_post_add, n)
                self.assertTrue(is_valid)
Exemple #14
0
        def test_create_all_membership_witnesses(self):
                n, A0, S = setup()

                elements_list = create_list(3)

                A1, nipoe = batch_add(A0, S, elements_list, n)
                witnesses = create_all_membership_witnesses(A0, S, n)

                elements_list = list(S.keys())  # this specific order is important
                for i, witness in enumerate(witnesses):
                        self.assertTrue(verify_membership(A1, elements_list[i], S[elements_list[i]], witness, n))
Exemple #15
0
        def test_proof_of_exponent(self):
                # first, do regular accumulation
                n, A0, S = setup()
                x0 = secrets.randbelow(pow(2, 256))
                x1 = secrets.randbelow(pow(2, 256))
                A1 = add(A0, S, x0, n)
                A2 = add(A1, S, x1, n)

                Q, l_nonce, u = prove_membership_with_NIPoE(A0, S, x0, n, A2)
                is_valid = verify_exponentiation(Q, l_nonce, u, x0, S[x0], A2, n)
                self.assertTrue(is_valid)
Exemple #16
0
def test_non_membership_witness_update_on_batch_add(
        total_utxo_set_size_for_accumulator, add_element_size):
    print("total_utxo_set_size_for_accumulator =",
          total_utxo_set_size_for_accumulator)

    print("--> initialize and fill up accumulator state")
    n, A0, S, order = setup()
    elements_for_accumulator = create_random_list(
        total_utxo_set_size_for_accumulator)
    tik = time.time()
    A, _ = batch_add(A0, S, elements_for_accumulator, n, order)
    tok = time.time()
    print("<--   Done.", tok - tik)

    test_element = random.randint(1, pow(2, 256))

    if test_element in S.keys():
        print(
            "test_element is included in accumulator, change it to other value!"
        )

    print("--> generate non-membership witness for ", test_element)
    d, b = prove_non_membership(A0, S, test_element, 0, n, order)
    print("<-- Done.")

    print("--> prove non-membership without add")
    result = verify_non_membership(A0, A, d, b, test_element, 0, n)
    print("<-- Done.")
    print("verify result ", result)

    print("--> add random elements, size:", add_element_size)
    elements_for_add = create_random_list(add_element_size)
    A_add = batch_add(A, S, elements_for_add, n, order)[0]
    print("<-- Done.")

    print("--> update non-membership witness for batch add")

    # d, b = acc_batch_add_non_membership_witness_update(A, S, test_element, d, b, elements_for_add, n)

    # add_elements_product is included in block header, not right, it is too big

    tik = time.time()
    add_elements_product = calculate_primes_product(elements_for_add, n)
    d, b = acc_batch_add_prime_non_membership_witness_update(
        A, S, test_element, d, b, add_elements_product, n, order)
    tok = time.time()
    acc_non_membership_update_result_time.append(tok - tik)
    print("<-- Done.", tok - tik)

    print("--> prove non-membership after add elements for size:",
          add_element_size)
    result = verify_non_membership(A0, A_add, d, b, test_element, 0, n)
    print("<-- Done.")
    acc_non_membership_update_result.append(result)
Exemple #17
0
def main():
    _, settings, _, _, benchmarks, _, _ = pycraft.setup()
    pycraft.render_interface.setup_render_module(settings)

    save = pycraft.saves.new_save({'name': 'test', 'seed': 'This is a test!'})
    server_obj = pycraft.server_interface.LocalInterface('tester', save, 0, settings)

    try:
        pycraft.game(server_obj, settings, benchmarks)
    finally:
        pycraft.setdown()
Exemple #18
0
def main():
    _, settings, _, _, benchmarks, _, _ = pycraft.setup()
    pycraft.render_interface.setup_render_module(settings)

    save = pycraft.saves.new_save({'name': 'test', 'seed': 'This is a test!'})
    server_obj = pycraft.server_interface.LocalInterface(
        'tester', save, 0, settings)

    try:
        pycraft.game(server_obj, settings, benchmarks)
    finally:
        pycraft.setdown()
Exemple #19
0
    def setUp(self):
        self.tmp_dir = tempfile.mkdtemp(prefix="mewtest_")
        lg.info("Created testing directory in %s" % self.tmp_dir)
        self.db_path = os.path.join(self.tmp_dir, "test.db")
        os.environ["MEW_DB_PATH"] = self.db_path

        # Initialize the DB
        init_qry = open('../db/init.sql', 'r').read()
        conn = sqlite3.connect(self.db_path)
        c = conn.cursor()
        c.executescript(init_qry)
        conn.commit()
        c.close()

        lg.info("Initialized DB at %s" % self.db_path)
        conn.close()

        main.setup()

        main.app.testing = True
        main.app.config['SERVER_NAME'] = 'localhost'
        self.app = main.app.test_client()
def check_len():
    print('=========  check! =========')
    local_len = get_local_len()
    _, page, _ = setup(skip_page_data=True)
    web_len = sanitizer.get_syllabus_count(page['text'])
    is_equal = local_len == web_len
    message = 'same.' if is_equal else 'should update.'
    print(f'''
    local_len, {local_len}
    web_len, {web_len}
    {message}
    ''')
    return is_equal
Exemple #21
0
        def test_batch_proof_of_membership_with_NIPoE(self):
                n, A0, S = setup()

                elements_list = create_list(10)

                A = A0
                for x in elements_list:
                        A = add(A, S, x, n)
                A_final = A

                elements_to_prove_list = [elements_list[4], elements_list[7], elements_list[8]]
                Q, l_nonce, u = batch_prove_membership_with_NIPoE(A0, S, elements_to_prove_list, n, A_final)
                nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_to_prove_list))
                is_valid = batch_verify_membership_with_NIPoE(Q, l_nonce, u, elements_to_prove_list, nonces_list, A_final, n)
                self.assertTrue(is_valid)
Exemple #22
0
        def test_batch_proof_of_membership(self):
                n, A0, S = setup()

                elements_list = create_list(10)

                A = A0
                for x in elements_list:
                        A = add(A, S, x, n)
                A_final = A

                elements_to_prove_list = [elements_list[4], elements_list[7], elements_list[8]]
                A_intermediate = batch_prove_membership(A0, S, elements_to_prove_list, n=n)
                nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_to_prove_list))
                is_valid = batch_verify_membership(A_final, elements_to_prove_list, nonces_list, A_intermediate, n)
                self.assertTrue(is_valid)
Exemple #23
0
        def test_agg_mem_witnesses(self):
                n, A0, S = setup()

                elements_list = create_list(3)

                A1, nipoe = batch_add(A0, S, elements_list, n)
                witnesses = create_all_membership_witnesses(A0, S, n)

                elements_list = list(S.keys())  # this specific order is important
                for i, witness in enumerate(witnesses):
                        self.assertTrue(verify_membership(A1, elements_list[i], S[elements_list[i]], witness, n))

                nonces_list = [S[x] for x in elements_list]
                agg_wit, nipoe = aggregate_membership_witnesses(A1, witnesses, elements_list, nonces_list, n)

                is_valid = batch_verify_membership_with_NIPoE(nipoe[0], nipoe[1], agg_wit, elements_list, nonces_list, A1, n)
                self.assertTrue(is_valid)
Exemple #24
0
        def test_prove_non_membership(self):
                n, A0, S = setup()

                elements_list = create_list(3)

                A1 = add(A0, S, elements_list[0], n)
                A2 = add(A1, S, elements_list[1], n)
                A3 = add(A2, S, elements_list[2], n)

                proof = prove_non_membership(A0, S, elements_list[0], S[elements_list[0]], n)
                self.assertIsNone(proof)

                x = create_list(1)[0]
                prime, x_nonce = hash_to_prime(x)
                proof = prove_non_membership(A0, S, x, x_nonce, n)
                is_valid = verify_non_membership(A0, A3, proof[0], proof[1], x, x_nonce, n)
                self.assertTrue(is_valid)
Exemple #25
0
        def test_shamir_trick_2(self):
                n, A0, S = setup()

                elements_list = create_list(2)

                A1 = add(A0, S, elements_list[0], n)
                A2 = add(A1, S, elements_list[1], n)

                prime0 = hash_to_prime(elements_list[0], nonce=S[elements_list[0]])[0]
                prime1 = hash_to_prime(elements_list[1], nonce=S[elements_list[1]])[0]

                proof0 = prove_membership(A0, S, elements_list[0], n)
                proof1 = prove_membership(A0, S, elements_list[1], n)

                agg_proof = shamir_trick(proof0, proof1, prime0, prime1, n)

                is_valid = pow(agg_proof, prime0 * prime1, n) == A2
                self.assertTrue(is_valid)
Exemple #26
0
        def test_batch_delete(self):
                n, A0, S = setup()

                elements_list = create_list(5)

                A = A0
                for i in range(len(elements_list)):
                        A = add(A, S, elements_list[i], n)
                A_pre_delete = A

                elements_to_delete_list = [elements_list[0], elements_list[2], elements_list[4]]
                nonces_list = list(map(lambda e: hash_to_prime(e)[1], elements_to_delete_list))

                proofs = list(map(lambda x: prove_membership(A0, S, x, n), elements_to_delete_list))

                A_post_delete, nipoe = batch_delete_using_membership_proofs(A_pre_delete, S, elements_to_delete_list, proofs, n)

                is_valid = batch_verify_membership_with_NIPoE(nipoe[0], nipoe[1], A_post_delete, elements_to_delete_list, nonces_list, A_pre_delete, n)
                self.assertTrue(is_valid)
Exemple #27
0
def test_non_membership_witness_update_on_add(
        total_utxo_set_size_for_accumulator):
    print("total_utxo_set_size_for_accumulator =",
          total_utxo_set_size_for_accumulator)

    print("--> initialize and fill up accumulator state")
    n, A0, S, order = setup()
    elements_for_accumulator = create_random_list(
        total_utxo_set_size_for_accumulator)
    tik = time.time()
    A, _ = batch_add(A0, S, elements_for_accumulator, n)
    tok = time.time()
    print("<--   Done.", tok - tik)

    test_element = random.randint(1, pow(2, 256))

    if test_element in S.keys():
        print(
            "test_element is included in accumulator, change it to other value!"
        )

    print("--> generate non-membership witness for ", test_element)
    d, b = prove_non_membership(A0, S, test_element, 0, n)
    print("<-- Done.")

    print("--> prove non-membership without add")
    result = verify_non_membership(A0, A, d, b, test_element, 0, n)
    print("<-- Done.")
    print("verify result ", result)

    print("--> add random element and update non-membership witness for",
          test_element)
    element_add = random.randint(1, pow(2, 256))
    A_add = add(A, S, element_add, n)
    d, b = acc_add_non_membership_witness_update(A, S, test_element, d, b,
                                                 element_add, n)
    print("<-- Done.")

    print("--> prove non-membership after add", element_add)
    result = verify_non_membership(A0, A_add, d, b, test_element, 0, n)
    print("<-- Done.")
    acc_non_membership_update_result.append(result)
Exemple #28
0
def test_create_all_membership_witnesses(total_utxo_set_size_for_accumulator):
    print("total_utxo_set_size_for_accumulator =",
          total_utxo_set_size_for_accumulator)

    print("--> initialize and fill up accumulator state")
    n, A0, S, order = setup()
    elements_for_accumulator = create_random_list(
        total_utxo_set_size_for_accumulator)

    tik = time.time()
    batch_add(A0, S, elements_for_accumulator, n)
    tok = time.time()
    print("<--   Done.", tok - tik)

    print("--> create all membership witness")
    tik = time.time()
    create_all_membership_witnesses(A0, S, n)
    tok = time.time()
    acc_create_all_membership_witness.append(tok - tik)
    print("<--   Done.", tok - tik)
Exemple #29
0
def main():
    codec, model, _, config = setup('../../data/test.csv')

    path = os.path.join("../../outputs", "gpt2_generated.csv")
    # path = os.path.join("../../outputs", "gpt2_with_prompt.csv")

    rouge = Rouge()
    nlls = 0.0
    ppls = 0.0
    rouges = {}
    length = 0

    with open(path, 'r') as file:
        csv_file = csv.DictReader(file)
        for row in csv_file:
            row = dict(row)
            text = row['generated']
            highlight_text = row['highlight']
            target_text = row['reference']
            ppl = nll_calc(model, codec, highlight_text, target_text)
            ppls += ppl
            rouge_score = rouge.get_scores(text, target_text, avg=True)
            if not rouges:
                for cat in rouge_score.keys():
                    rouges[cat] = Counter(rouge_score[cat])
            else:
                for cat in rouge_score.keys():
                    rouges[cat] += Counter(rouge_score[cat])
            length += 1
            print(length, '/', 4595)
    # print(np.exp(nlls / (511*length)))
    print("perplexity =", ppls / length)
    for cat in rouges.keys():
        for key in rouges[cat].keys():
            rouges[cat][key] /= length
    print("rouges =", rouges)
Exemple #30
0
                                   'r').read()
                self.wfile.write(bytes(filecontent, 'UTF-8'))


print("Running setup...")

#thing="Hola que tal"
#path=os.path.dirname(os.path.abspath(__file__))+"/speech/"
#tts=googletts.googleTTS(text=''+thing,lang='en', debug=True)
#tts.save(path+"speech.mp3")
#song = pyglet.media.load(path+"speech.mp3")
#song.play()
#quit()

myInMoov = inmoov.InMoov()
if not main.setup(myInMoov):
    print("Error during setup. Exiting...")
    quit()
    # Ver los exiting codes of quit a ver que significan

print("Starting server...")
myServer = HTTPServer((hostName, hostPort), MyServer)
myServer.socket = ssl.wrap_socket(myServer.socket,
                                  certfile='localhost.pem',
                                  server_side=True)
print("Server Starts - %s:%s" % (hostName, hostPort))

try:
    myServer.serve_forever()
except KeyboardInterrupt:
    pass
Exemple #31
0
 def reconnect(self):
     from main import setup
     setup()
     if hdwf.value != hdwfNone.value:
         self.manager.current = 'menu'
Exemple #32
0
'''
Created on Feb 26, 2016

@author: francica
'''

from unittest import TestCase
import main
import json
from metro import Metros
from route import Routes
#initilized
main.setup()


class testParse(TestCase):

    def test_listLength(self):
        self.assertEqual(48, len(main.metros))
        self.assertEqual(94, len(main.routes))
        
    def test_parseMetro(self):
        self.assertEqual("Lima", main.metros[1].name)
        self.assertEqual("Buenos Aires", main.metros[4].name)
        self.assertEqual({u'S': 35, u'W': 58}, main.metros[4].coordinates)
        self.assertEqual("Sao Paulo", main.metros[5].name)
        self.assertEqual("Toronto", main.metros[47].name)
        self.assertEqual("New York", main.metros[46].name)
        self.assertEqual("Washington", main.metros[45].name)

    def test_editRoute(self):
 def test_setup_should_report_pid(self):
     main.proctl = Mock()
     app_globals.OPTIONS["report_pid"] = True
     self.assertRaises(SystemExit, lambda: main.setup([]))
     self.assertTrue(main.proctl.report_pid.called)
Exemple #34
0
 def connect(self):
   main.setup()
   self.update()
   self.msg_str.set("Connected.")
 def test_setup_should_ensure_singleton(self):
     main.proctl = Mock()
     app_globals.OPTIONS["report_pid"] = False
     main.setup(["--user=a", "--password=b"])
     self.assertTrue(main.proctl.ensure_singleton_process.called)
Exemple #36
0
import sys
from helpfunctions import hash_to_prime, xgcd
import random
from main import setup
import time


def create_random_list(size):
    result = []
    for index in range(0, size):
        random_element = random.randint(1, pow(2, 256))
        result.append(random_element)
    return result


n, A0, S = setup()
cache = {}
elememts = create_random_list(1000)
product = 1
for ele in elememts:
    product *= ele

tik = time.time()
ser = pow(A0, product, n)
tok = time.time()

print("ser:", tok - tik)
print("size:", sys.getsizeof(product))
Exemple #37
0
            else:
                filecontent=open(application_path+"/"+cmd+"/"+url,'r').read()
                self.wfile.write(bytes(filecontent,'UTF-8'))

print ("Running setup...")

#thing="Hola que tal"
#path=os.path.dirname(os.path.abspath(__file__))+"/speech/"
#tts=googletts.googleTTS(text=''+thing,lang='en', debug=True)
#tts.save(path+"speech.mp3")
#song = pyglet.media.load(path+"speech.mp3")
#song.play()
#quit()

myInMoov=inmoov.InMoov()
if not main.setup(myInMoov):
    print ("Error during setup. Exiting...")
    quit()
    # Ver los exiting codes of quit a ver que significan

print ("Starting server...")
myServer = HTTPServer((hostName, hostPort), MyServer)
myServer.socket = ssl.wrap_socket (myServer.socket, certfile='localhost.pem', server_side=True)
print("Server Starts - %s:%s" % (hostName, hostPort))

try:
    myServer.serve_forever()
except KeyboardInterrupt:
    pass

main.end(myInMoov)
Exemple #38
0
    def start(self, detected_callback=play_audio_file,
              interrupt_check=lambda: False,
              sleep_time=0.03):

        """
        Start the voice detector. For every `sleep_time` second it checks the
        audio buffer for triggering keywords. If detected, then call
        corresponding function in `detected_callback`, which can be a single
        function (single model) or a list of callback functions (multiple
        models). Every loop it also calls `interrupt_check` -- if it returns
        True, then breaks from the loop and return.

        :param detected_callback: a function or list of functions. The number of
                                  items must match the number of models in
                                  `decoder_model`.
        :param interrupt_check: a function that returns True if the main loop
                                needs to stop.
        :param float sleep_time: how much time in second every loop waits.
        :return: None
        """
        if interrupt_check():
            print "check"
            logger.debug("detect voice return")
            return

        tc = type(detected_callback)
        if tc is not list:
            detected_callback = [detected_callback]
        if len(detected_callback) == 1 and self.num_hotwords > 1:
            detected_callback *= self.num_hotwords

        assert self.num_hotwords == len(detected_callback), \
            "Error: hotwords in your models (%d) do not match the number of " \
            "callbacks (%d)" % (self.num_hotwords, len(detected_callback))

        logger.debug("detecting...")
        # self.check_kill_process("main.py")
        while True:
            #this keeps running
            if interrupt_check():
                print "interrupt check"
                logger.debug("detect voice break")
                break
            data = self.ring_buffer.get()

            if len(data) == 0:
                time.sleep(sleep_time)
                continue

            ans = self.detector.RunDetection(data)
            if ans == -1:
                logger.warning("Error initializing streams or reading audio data")
            elif ans > 0:
                if ax != "":
                    print "zero"
                    ax = returnabcdefgh()
                    ax.stop()

                message = "Keyword " + str(ans) + " detected at time: "
                message += time.strftime("%Y-%m-%d %H:%M:%S",
                                         time.localtime(time.time()))
                logger.info(message)
                callback = detected_callback[ans-1]
                global ax
                import main
                main.setup()
                main.start()

                ax = returnabcdefgh()

        logger.debug("finished.")