Ejemplo n.º 1
0
def _start_browser(lock=_browser_lock):
    global _browser
    _quit_browser(lock=lock)
    with lock:
        if cc.feedback_on_commands():
            feedback("Opening a new browser, just a moment...")
        _browser = Firefox(executable_path=os.path.join(get_my_directory(), "drivers", "geckodriver.exe"))
Ejemplo n.º 2
0
def feedback_for_team(team, is_multi):
    commands = param_str.replace(team['name'], '').replace(team['slug'], '').strip().split(' ')
    output = []

    # autocomplete the valid commands
    if valid_commands.count(commands[0]) == 0:
        output = ac_commands(team, is_multi, commands[0])
    # Display tasks
    elif commands[0] == 'tasks':
        tasks = api.cache_method('/me/tasks', team['id'])
        title = ' '.join(commands[1:]).strip()
        if len(commands) > 1 and len(title) > 0:
            output.append(feedback.item(title='Create: ' + title, valid=True, arg='create:' + team['id'] + ':' + title, icon=icon_path(team)))
            tasks = fuzzy.fuzzy_search(title, tasks, lambda x: '%s' % (x['title']))
        elif len(tasks) == 0:
            output.append(feedback.item(title='You have no tasks', valid=False, icon=icon_path(team)))
        for task in tasks:
            output.append(feedback.item(title="Task: " + task['title'], valid=True, arg='ship:' + team['id'] + ':' + task['id'], icon=icon_path(team)))
    elif commands[0] == 'notifications':
        members = settings.get('members', [])
        notifications = api.cache_method('/me/notifications', team['id'])
        if len(notifications) == 0:
            output.append(feedback.item(title='You have no notifications', valid=False, icon=icon_path(team)))
        for n in notifications:
            m = [member for member in members if member['id'] == n['who']]
            if len(m) >= 1:
                icon = icon_path_member(m[0])
            else:
                icon = icon_path(team)
            output.append(feedback.item(title=n['title'], subtitle=n['body'], valid=False, icon=icon))
    feedback.feedback(output)
Ejemplo n.º 3
0
def algoritme1():  # hij werkt nog niet helemaal
    pogingen = 0
    geheim = input('Geef je 4 random getallen tussen de 0-5 ')
    geheime_lijst = [int(x) for x in geheim]
    lijst = []
    for i in range(0, 6):
        for j in range(0, 6):
            for k in range(0, 6):
                for l in range(0, 6):
                    lijst.append([i, j, k, l])
    new_list = lijst[:]
    for x in lijst:
        pogingen += 1
        a = random.choice(lijst)
        mogelijkheid = feedback(a, geheime_lijst)
        checken = feedback(x, geheime_lijst)
        if checken != mogelijkheid:
            new_list.remove(x)
            print(a)
        elif lijst != 1:
            print('Computer Heeft het geraden binnen: ' + str(pogingen) +
                  ' pogingen')
            break
    if pogingen <= 10:
        print('Computer is de nieuwe mastermind! ')
    elif pogingen > 10:
        print('Computer heeft zijn dag niet ')
Ejemplo n.º 4
0
def ac_teams(user_teams, query=''):
    output = []
    for team in user_teams:
        output.append(feedback.item(title=team["name"], valid=False, autocomplete=team["name"] + ' ', icon=icon_path(team)))
    if query != '':
        output = fuzzy.fuzzy_search(query, output, lambda x: '%s' % (x['content']['title']))
    feedback.feedback(output)
 def feedback(self, has_node):
     if has_node:
         title = "Node is available!"
     else:
         title = "Node is not found, please set its path!"
     item = Item(uid="node", valid="no", title=title, icon="nodejs.png")
     feedback(item)
Ejemplo n.º 6
0
def play_video(url: str, identifier: str = None):
    '''Plays the video found at given URL in a standalone media player.
    Currently throws exceptions on invalid input.
    '''
    feedback("Starting up '", identifier, "'")
    #if "youtubetest" in urlparse(url).netloc.lower():
    #    _play_youtube_video(url=url, identifier=identifier)
    #else:
    if validators.url(url):
        _play_media(link=url)
Ejemplo n.º 7
0
def _circuit_protocol(state, m, n_m, mode, return_outcome=0):
    """
    Do the protocol in arxiv/1506.05033, Fig 5 on a state using the circuit

    Args:
        state: Qutip state, initial state
        H: Does nothing here, makes it easier to swap between the two protocols
        n_m: Binary List, bit-string of results. n_m[m] is the m-th round.
        m: current round, starting at 0
        mode: mode used for feedback, see feedback.feedback()

    Returns:
        Qutip state
    """
    phi = fb.feedback(m, n_m, mode)
    state = hadamard * state
    state = controlled_zgate(displace(N, np.sqrt(np.pi/2))) * state
    state = hadamard * phasegate_(phi) * state

    if return_outcome:
        P = expect(nq, state)
        if n_m[m-1] == 0:
            P = 1-P
        state = qubit_reset(state, n_m[m-1])
        return state.unit(), P
    else:
        state = qubit_reset(state, n_m[m-1])
        return state.unit()
Ejemplo n.º 8
0
def _circuit_protocol(state, m, n_m, mode, return_outcome=0):
    """
    Do the protocol in arxiv/1506.05033, Fig 5 on a state using the circuit

    Args:
        state: Qutip state, initial state
        H: Does nothing here, makes it easier to swap between the two protocols
        n_m: Binary List, bit-string of results. n_m[m] is the m-th round.
        m: current round, starting at 0
        mode: mode used for feedback, see feedback.feedback()

    Returns:
        Qutip state
    """
    phi = fb.feedback(m, n_m, mode)
    state = hadamard * state
    state = controlled_zgate(displace(N, np.sqrt(np.pi / 2))) * state
    state = hadamard * phasegate_(phi) * state

    if return_outcome:
        P = expect(nq, state)
        if n_m[m - 1] == 0:
            P = 1 - P
        state = qubit_reset(state, n_m[m - 1])
        return state.unit(), P
    else:
        state = qubit_reset(state, n_m[m - 1])
        return state.unit()
Ejemplo n.º 9
0
def _open_tab(url:str="", lock=_browser_lock, recurse=True):
    global _browser
    if not recurse:
        feedback("_open_tab failed on second try")
        raise EnvironmentError
    with lock:
        try:
            handles_before = _browser.window_handles
            _browser.execute_script(''.join(("window.open('", url, "')")))
            new_handle = [handle for handle in _browser.window_handles if handle not in handles_before][0]
            _browser.switch_to.window(new_handle)
            load_cookies()
        except:
            #_browser was closed by an outside force
            _start_browser(lock=_Lock_Bypass())
            _open_tab(url=url, lock=_Lock_Bypass(), recurse=False)
Ejemplo n.º 10
0
 def __init__(self):
     self.EMAIL_LOG_NAME = 'email_log.ini'
     self.cr = config_reader()
     self.email_config = self.cr.get_email_settings()
     self.email_log_dir = self.cr.get_email_log()
     self.feedback = feedback()
     self.late_submission_list = late_submit_checker().late_submitter_log()
     self.late_penalty = int(self.cr.get_late_submit_penalty())
Ejemplo n.º 11
0
 def __init__(self):
     cmd2.Cmd.__init__(self)
     self.cr = config_reader()
     self.utility = utility()
     self.feedback = feedback()
     self.statistics = statistics()
     self.es = email_sender()
     self.late_submit_checker = late_submit_checker()
     os.chdir('ASSIGNMENTS')
Ejemplo n.º 12
0
def Form():
    result = request.form
    #print(result)
    obj = feedback()
    ans = obj.insertfeedback(**result)

    if ans == True:
        return render_template("EmployeeSatisfaction.html", post=True)
    else:
        return render_template("EmployeeSatisfaction.html", post=False)
Ejemplo n.º 13
0
def _open_browser(url:str=None, lock=_browser_lock):
    '''Opens a browser, or a new tab if already open.
    Goes to given URL if specified.
    '''
    global _browser
    try:
        new_url = _DEFAULT_SITE
        if url:
            new_url = url
        with lock:
            if not _browser:
                _start_browser(lock=_Lock_Bypass())
                _browser.get(new_url)
                load_cookies()
            else:
                _open_tab(url=new_url, lock=_Lock_Bypass())
            if cc.feedback_on_commands():
                feedback(''.join((_browser.title, " is open.")))
    except:
        traceback.print_exc()
    return True
Ejemplo n.º 14
0
 def store_location(self, url:str, path_name:str=None):
     '''Stores the given URL as the given name.
     If no name is given only stores the domain, and also the query pattern if it is in the url.
     '''
     if not validate_url(url):
         feedback("Url fails validation")
         raise ValueError
     parsed_url = urlparse(url)
     # store scheme
     if not self._base:
         self._base = '://'.join((parsed_url.scheme, parsed_url.netloc))
     # store domain
     if not self.domain:
         self._domain = parsed_url.netloc
     if not self.domain in parsed_url.netloc:
         feedback("Non-matching domain")
         raise ValueError
     # store query pattern
     self._store_query_pattern(parsed_url=parsed_url)
     # store path
     if not path_name:
         return
     self._paths[path_name] = parsed_url.path
Ejemplo n.º 15
0
def main():
    while True:
        if mode == 'tasklist':
            tasks.print_all_tasks()
            break
        elif mode == 'eventlist':
            events.print_all_events()
            break
        elif mode == 'add':
            scheduler.add_event_and_task_to_db(title)
            break
        elif mode == 'feedback':
            feedback.feedback()
            break
        elif mode == 'build_db':
            tasks.createTasksTable()
            break
        elif mode == 'rebuild_db':
            tasks.recreate_tasks_table()
            break
        else:
            # WASNT IMPLEMENTED YET
            print('What would you like to do?')
            break
Ejemplo n.º 16
0
def _time_evolution_protocol(state,
                             m,
                             n_m,
                             mode,
                             H,
                             noise_ops,
                             return_outcome=0):
    """
    Do the protocol in arxiv/1506.05033, Fig 5 on a state using time-evolution

    The circuit for the controlled displacement using time-evolution can be
    found in the same paper, Fig 10

    Args:
        state: Qutip state, initial state
        H: Hamiltonian used for time evolution
        n_m: Binary List, bit-string of results. n_m[m] is the m-th round.
        m: current round, starting at 0
        mode: mode used for feedback, see feedback.feedback()

    Returns:
        Qutip state
    """
    phi = fb.feedback(m, n_m, mode)
    state = hadamard * state
    state = mesolve(H,
                    state, [np.pi / (2 * chi)],
                    noise_ops, [],
                    options=Odeoptions(nsteps=5000)).states[-1]
    state = displace_(-1j * np.sqrt(np.pi / 2)) * sx * state
    state = mesolve(H,
                    state, [np.pi / (2 * chi)],
                    noise_ops, [],
                    options=Odeoptions(nsteps=5000)).states[-1]
    state = hadamard * phasegate_(phi) * sx * state
    state = state.unit()

    if return_outcome:
        P = expect(nq, state)
        if n_m[m - 1] == 0:
            P = 1 - P
        state = qubit_reset(state, n_m[m - 1])
        return state.unit(), P
    else:
        state = qubit_reset(state, n_m[m - 1])
        return state.unit()
Ejemplo n.º 17
0
def run():
    wrap_problems()
    output = {}
    for problem in get_problems():
        output[problem.__name__] = {}
        try:
            output[problem.__name__]['result'] = problem()
            output[problem.__name__]['error'] = None
            output[problem.__name__]['locals'] = problem.locals
        except Exception as ex:
            output[problem.__name__]['result'] = None
            output[problem.__name__]['locals'] = None
            output[problem.__name__]['error'] = ex
        output[problem.__name__]['feedback'] = feedback.feedback(problem, output[problem.__name__])
        show_problem(problem, output[problem.__name__])

    import game
    game.run(output)
Ejemplo n.º 18
0
def _time_evolution_protocol(state, m, n_m, mode, H, noise_ops,
                             return_outcome=0):
    """
    Do the protocol in arxiv/1506.05033, Fig 5 on a state using time-evolution

    The circuit for the controlled displacement using time-evolution can be
    found in the same paper, Fig 10

    Args:
        state: Qutip state, initial state
        H: Hamiltonian used for time evolution
        n_m: Binary List, bit-string of results. n_m[m] is the m-th round.
        m: current round, starting at 0
        mode: mode used for feedback, see feedback.feedback()

    Returns:
        Qutip state
    """
    phi = fb.feedback(m, n_m, mode)
    state = hadamard * state
    state = mesolve(H, state, [np.pi/(2*chi)], noise_ops, [],
                    options=Odeoptions(nsteps=5000)).states[-1]
    state = displace_(-1j * np.sqrt(np.pi/2)) * sx * state
    state = mesolve(H, state, [np.pi/(2*chi)], noise_ops, [],
                    options=Odeoptions(nsteps=5000)).states[-1]
    state = hadamard * phasegate_(phi) * sx * state
    state = state.unit()

    if return_outcome:
        P = expect(nq, state)
        if n_m[m-1] == 0:
            P = 1-P
        state = qubit_reset(state, n_m[m-1])
        return state.unit(), P
    else:
        state = qubit_reset(state, n_m[m-1])
        return state.unit()
Ejemplo n.º 19
0
def main():

    #set counter to 0
    counter = 0

    # is this a category-only mission
    catmission = False

    #create stack for status histories
    statushistory = deque([], 50)

    #create emotion table
    emotions = Emotion(9)
    emotions.create_area("angry", 0, 0, 2, 2)
    emotions.create_area("frustrated", 0, 3, 2, 5)
    emotions.create_area("sad", 0, 6, 2, 8)
    emotions.create_area("bored", 3, 0, 5, 8)
    emotions.create_area("excited", 6, 0, 8, 2)
    emotions.create_area("happy", 6, 3, 8, 5)
    emotions.create_area("ok", 6, 6, 8, 8)

    # primary status, possibilities are: idle = waiting for customers, mission = on a mission, advising = telling where the book is, feedback = waiting for or reacting to feedback
    robot_status = "idle"

    print("ROBOT STATUS: " + robot_status)

    while True:
        #get robot status
        status = mir_calls.get_mir_status()

        if (counter % 1 == 0):
            statushistory.append(status)
            print("counter: " + str(counter) + " status: " + status)

        counter = counter + 1

        print("debug: checking if we are on a mission")

        ### ON A MISSION? if yes, call the related logic in travel.py, and skip cycle

        if robot_status == 'shelfmission':
            mir_status = travel.move()
            print("debug: shelf mission in progress, mir state: " + mir_status)

            if mir_status == 'Ready':
                print("debug: shelf-mission has been accomplished")

                print("debug: wait for 5 seconds")

                if catmission == True:

                    print("DEBUG: catmission true, cat " + category)

                    if category == '79000':
                        switch_flask_view('r')

                    if category == '46900':
                        switch_flask_view('r')

                    if category == '69110':
                        switch_flask_view('lr')

                    if category == '85000':
                        switch_flask_view('r')

                    if category == '99000':
                        switch_flask_view('l')

                    if category == '90000':
                        switch_flask_view('l')

                    time.sleep(10)
                    mir_calls.add_to_mission_queue(
                        "beb5b742-341b-11e9-a33f-94c691a3a93e")
                    robot_status = 'homing'
                    time.sleep(2)
                    #eyes.lookDown()
                    catmission = False
                    sierra.update_target_time()
                    continue

                # FLASK: display arrow to left or right
                side = find_side_by_category(category, "column")
                switch_flask_view(side)
                print("debug: the book could be on the " + side + " side")

                # if side == 'r':
                #eyes.lookLeft()

                #if side == 'l':
                #eyes.lookRight()

                time.sleep(5)

                # the category should be mapped to a physical oodi position recognised by the mir robot
                print("debug: move to the correct column")
                positionguid = str(
                    find_position_by_category(category, "column"))
                print(
                    "debug: position guid for category column from database is "
                    + positionguid)
                # if we have a position, we can create a mission
                mir_calls.modify_mir_mission(positionguid)
                # add the modified travel mission to the queue
                mir_calls.add_to_mission_queue(
                    "2e066786-3424-11e9-954b-94c691a3a93e")

                # set the robot status to be on a mission
                robot_status = 'columnmission'
                switch_flask_view("blank")

                # give the MiR a few seconds to react so we enter the correct state (mission executing)
                time.sleep(2)

            time.sleep(1)
            continue

        if robot_status == 'columnmission':
            mir_status = travel.move()
            print("debug: column mission in progress, mir state: " +
                  mir_status)

            if mir_status == 'Ready':
                print("debug: column-mission has been accomplished")
                print("debug: wait for 5 seconds")
                # FLASK: display arrow to left or right
                side = find_side_by_category(category, "column")
                switch_flask_view(side)
                sierra.update_target_time()

                print("debug: the book could be on the " + side + " side")
                time.sleep(10)
                #switch_flask_view("blank")

                # move forward one meter to make room for the customer
                #mir_calls.add_to_mission_queue("3ac4fc26-3f3f-11e9-9822-94c691a3a93e")
                #time.sleep(10)

                # TODO ask for feedback! maybe not?
                mir_calls.add_to_mission_queue(
                    "beb5b742-341b-11e9-a33f-94c691a3a93e")
                robot_status = 'homing'
                #eyes.lookDown()
                time.sleep(5)

            time.sleep(1)
            continue

        if robot_status == 'homing':
            mir_status = travel.move()
            print("debug: homing in progress, mir state: " + mir_status)

            switch_flask_view("home")

            if mir_status == 'Ready':
                print("debug: homing has been accomplished")
                print("debug: enter idle loop")
                switch_flask_view("home2")
                robot_status = 'idle'
                #eyes.lookLeft()
                #eyes.lookRight()
                #eyes.lookDown()
                sierra.update_home_time()

            time.sleep(1)
            continue

        print("debug: checking if we have a new mission")

        ### NEW MISSION AVAILABLE? if there's a category entry in missions table with status new, we have a new mission

        category = check_for_new_mission()

        if category is not None:

            # a quick hack to allow for the category missions
            if category == '79000':
                positionguid = 'aac480fd-44d5-11e9-b653-94c691a3a93e'
                catmission = True

            if category == '46900':
                positionguid = '48e76bd3-44d4-11e9-b653-94c691a3a93e'
                catmission = True

            if category == '69110':
                positionguid = '78795881-44d5-11e9-b653-94c691a3a93e'
                catmission = True

            if category == '85000':
                positionguid = 'b955c6f0-4bb0-11e9-98e7-94c691a3a93e'
                catmission = True

            if category == '99000':
                positionguid = 'd8585103-4bb0-11e9-98e7-94c691a3a93e'
                catmission = True

            if category == '90000':
                positionguid = 'eb62e8a2-4bb0-11e9-98e7-94c691a3a93e'
                catmission = True

            if catmission == True:
                print("DEBUG: catmission is true!")
                change_mission_status()

            # the category should be mapped to a physical oodi position recognised by the mir robot
            if catmission == False:
                print("debug: received target category is " + category)
                change_mission_status()
                positionguid = str(find_position_by_category(
                    category, "shelf"))
                print(
                    "debug: position guid for category shelf from database is "
                    + positionguid)

            # if we have a position, we can create a mission
            mir_calls.modify_mir_mission(positionguid)

            # finally add the modified shelf/column mission to the queue
            mir_calls.add_to_mission_queue(
                "2e066786-3424-11e9-954b-94c691a3a93e")

            # set the robot status to be on a mission
            robot_status = 'shelfmission'

            # give the MiR a few seconds to react so we enter the correct state (mission executing)
            time.sleep(3)

            # eye movement :D
            #eyes.topRoll()

            continue

        ### NO MISSIONS? let's call the related logic in idle.py to attract customers

        if robot_status == 'idle':
            idle.idle(statushistory, emotions)
            time.sleep(1)
            continue

        if robot_status == "advising":
            advise.advise(emotions, "atColumn", "right")
            time.sleep(1)
            continue

        if robot_status == "feedback":
            feedback.feedback(emotions, 1, "good")

        ### NO STATUS, NO NEW MISSION? let's send the robot back to the homebase

        print("debug: no status, return to home")
        mir_calls.add_to_mission_queue("beb5b742-341b-11e9-a33f-94c691a3a93e")
        robot_status = 'homing'
        time.sleep(2)
        continue
Ejemplo n.º 20
0
def run_step(job_desc, resources):
    """
    Run step (parallel to main thread)
    :param job_desc: string, jod id + "_" + step order
    :param resources: dictionary, resources required by the step
    :return: None
    """
    global LATEST_JOB_ID, LATEST_JOB_STEP, CPU_POOL, MEMORY_POOL, DISK_POOL, RUNNING_JOBS
    items = job_desc.split('_')
    job_id = int(items[0])
    step_order = int(items[1])
    recheck = forecast_step(job_id, step_order, resources)

    if recheck is not True:
        return

    if settings['program']['feedback'] == 'yes':
        try:
            from feedback import feedback
            feedback(JOB_COMMAND[job_id][0], ' '.join(JOB_COMMAND[job_id][1:]),
                     JOB_TABLE[job_id]['steps'][step_order]['hash'])
        except:
            pass

    if settings['cluster']['type']:
        # for cluster
        import clusterSupport
        if resources['cpu'] is None:
            allocate_cpu = settings['env']['cpu']
        else:
            predict_cpu = int(round(resources['cpu']) / 100)
            if predict_cpu > settings['cluster']['cpu'] or predict_cpu == 0:
                allocate_cpu = settings['cluster']['cpu']
            else:
                allocate_cpu = predict_cpu
        if resources['mem'] is None:
            allocate_mem = ''
        else:
            if resources['mem'] > 1073741824:
                allocate_mem = str(
                    int(round(resources['mem'] / 1073741824) + 1)) + 'Gb'
            elif resources['mem']:
                allocate_mem = ''
            else:
                allocate_mem = str(
                    int(round(resources['mem'] / 1048576) + 1)) + 'Mb'

        #baseDriver.update(settings['datasets']['job_db'], job_id, 'status', step_order + 1)
        try:
            job_record = Queue.objects.get(id=job_id)
            job_record.set_status(step_order + 1)
        except:
            pass
        return_code = clusterSupport.main(settings['cluster']['type'],
                                          ' '.join(JOB_COMMAND[job_id]),
                                          job_id, step_order, allocate_cpu,
                                          allocate_mem,
                                          settings['cluster']['queue'],
                                          JOB_TABLE[job_id]['job_folder'])
        if return_code != 0:
            error_job(job_id, resources)
    else:
        # for local environment or cloud
        print "Now run %s" % job_desc
        print CPU_POOL, MEMORY_POOL, DISK_POOL
        try:
            log_file = os.path.join(settings["env"]["log"], str(job_id))
            log_file_handler = open(log_file, "a")
            RUNNING_JOBS += 1
            step_process = subprocess.Popen(
                JOB_COMMAND[job_id],
                shell=False,
                stdout=log_file_handler,
                stderr=log_file_handler,
                cwd=JOB_TABLE[job_id]['job_folder'])
            process_id = step_process.pid
            if 'trace' in resources.keys():
                learn_process = subprocess.Popen([
                    "python",
                    os.path.join(root_path, 'mlCollector.py'), "-p",
                    str(step_process.pid), "-n",
                    str(JOB_TABLE[job_id]['steps'][step_order]['hash']), "-j",
                    str(resources['trace'])
                ],
                                                 shell=False,
                                                 stdout=None,
                                                 stderr=subprocess.STDOUT)
            while step_process.poll() is None:
                if process_id in psutil.pids():
                    proc_info = psutil.Process(process_id)
                    if proc_info.is_running():
                        job = Queue.objects.get(id=job_id)
                        if job.ter:
                            job.status = -3
                            job.ter = 0
                            job.save()
                            proc_info.kill()
                            error_job(job_id, resources)
                            RUNNING_JOBS -= 1

                            return None

                time.sleep(30)
            log_file_handler.close()
            print "Now job %s finished." % job_desc
            # finish_step(job_id, step_order, resources)
            if step_process.returncode != 0:
                RUNNING_JOBS -= 1
                error_job(job_id, resources)
            else:
                RUNNING_JOBS -= 1
                finish_step(job_id, step_order, resources)
            JOB_TABLE[job_id]['resume'] = step_order
            if job_id > LATEST_JOB_ID and (step_order + 1) > LATEST_JOB_STEP:
                LATEST_JOB_ID = job_id
                LATEST_JOB_STEP = step_order
        except Exception, e:
            print e
            try:
                from feedback import feedback_error
                feedback_error(JOB_COMMAND[job_id][0],
                               ' '.join(JOB_COMMAND[job_id][1:]), str(e))
            except:
                pass
            RUNNING_JOBS -= 1
            error_job(job_id, resources)
Ejemplo n.º 21
0
 def feedback_distribution(self, t, x, a):
     return feedback.feedback(t, x, a)
Ejemplo n.º 22
0
def recieve_message():
    #global re_ask
    #global re_intent
    global store
    user_input = request.get_json()
    #get user ID to response back
    if user_input.get('entry'):  #for event in user_input['entry']:
        event = user_input['entry'][0]
        if event.get('messaging'):  #for message in messaging:
            message = event['messaging'][0]
            if message.get('message'):
                #Facebook Messenger ID for user so we know where to send response back to

                text = message['message'].get('text')
                user_ID = message['sender']['id']

                sent_time = time.time()  ## current time
                if user_ID not in store.keys():
                    store[user_ID] = {
                        'input': '',
                        're_intent': '',
                        'keyword': {},
                        're_ask': 0,
                        'time': sent_time,
                        'intent_acc': 0.0,
                        'response': ''
                    }
                else:
                    break_time = sent_time - store[user_ID]['time']
                    if break_time > 120:  # longer than 2 mins
                        store[user_ID] = {
                            'input': '',
                            're_intent': '',
                            'keyword': {},
                            're_ask': 0,
                            'time': sent_time,
                            'intent_acc': 0.0,
                            'response': ''
                        }
                    else:
                        store[user_ID]['time'] = sent_time

                if store[user_ID][
                        're_ask'] == 0 or store[user_ID]['re_ask'] > 1:
                    store[user_ID]['re_ask'] = 0
                    new_text = intent_classify.preprocessing(text)
                    #new_text = TextBlob(text).correct()
                    #new_text = str(new_text)
                    intent, intent_acc = intent_classify.intent_classification(
                        new_text)  ####  need return a accuracy
                    store[user_ID]['intent_acc'] = intent_acc

                    if intent == 'Greetings':
                        greet_response = [
                            'Hi, I am here to help you!', 'Hi nice to meet u!',
                            'Hey there!', 'Yo!', 'Hey, boo!'
                        ]
                        response = random.choice(greet_response)
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            store[user_ID]['re_intent'] = intent
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                            return "Message Processed"
                        else:
                            server.send_text_message(user_ID, response)
                            return "Message Processed"
                    elif intent == 'Goodbye':
                        bye_response = [
                            'See you soon!', 'Thanks!', 'ByeBye!',
                            'Thank you for choosing us!', 'I will miss you!'
                        ]
                        response = random.choice(bye_response)
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            store[user_ID]['re_intent'] = intent
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                            return "Message Processed"
                        else:
                            server.send_text_message(user_ID, response)
                            return "Message Processed"
                    elif intent == 'Name':
                        name_response = [
                            'My name is KOBO. :)', 'Please call me KOBO ~',
                            'I am KOBO ^^'
                        ]
                        response = random.choice(name_response)
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            store[user_ID]['re_intent'] = intent
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                            return "Message Processed"
                        else:
                            server.send_text_message(user_ID, response)
                            return "Message Processed"

                    store[user_ID]['input'] = text

                else:
                    print(store[user_ID])
                    intent = store[user_ID]['re_intent']
                    text = store[user_ID]['input'] + ' ' + text
                    print('new reask text is ' + text)

                keyword = keyword_extract.keyword_extraction(intent, text)
                print('keyword is ' + str(keyword))

                response = retrieve.retrieval_func(keyword)
                print('data retrieve is ' + response)

                if response == 'Please provide valid courses code.':

                    if store[user_ID]['keyword'] != {}:
                        print(store[user_ID])
                        if store[user_ID]['keyword']['course'] != []:
                            store[user_ID]['keyword']['intent'] = intent
                            #print(store[user_ID]['keyword'])
                            keyword['course'] = store[user_ID]['keyword'][
                                'course']
                            response = retrieve.retrieval_func(keyword)
                            if store[user_ID]['intent_acc'] <= intent_bound:
                                store[user_ID]['response'] = response
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                res = response  # + ' ' + str(store[user_ID]['re_ask'])
                                server.send_button_message(
                                    user_ID, res, button)
                        else:
                            store[user_ID]['re_ask'] += 1  #True
                            store[user_ID]['re_intent'] = intent
                            store[user_ID]['keyword'] = keyword
                            if store[user_ID]['intent_acc'] <= intent_bound:
                                store[user_ID]['response'] = response
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                res = response  #+ ' ' + str(store[user_ID]['re_ask'])
                                server.send_text_message(user_ID, res)
                    else:
                        store[user_ID]['re_ask'] += 1  #True
                        store[user_ID]['re_intent'] = intent
                        store[user_ID]['keyword'] = keyword
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                        else:
                            res = response  # + ' ' + str(store[user_ID]['re_ask'])
                            server.send_text_message(user_ID, res)
                elif response[(
                        len(response) - 39
                ):] == 'Do you want query one specific stream name?':  # == 'Please provide valid stream name.':

                    if store[user_ID]['keyword'] != {}:
                        if store[user_ID]['keyword']['stream_name'] != []:
                            store[user_ID]['keyword']['intent'] = intent
                            keyword['stream_name'] = store[user_ID]['keyword'][
                                'stream_name']
                            response = retrieve.retrieval_func(keyword)
                            if store[user_ID]['intent_acc'] <= intent_bound:
                                store[user_ID]['response'] = response
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                res = response  #+ ' ' + str(store[user_ID]['re_ask'])
                                server.send_button_message(
                                    user_ID, res, button)
                        else:
                            store[user_ID]['re_ask'] += 1  #True
                            store[user_ID]['re_intent'] = intent
                            store[user_ID]['keyword'] = keyword
                            if store[user_ID]['intent_acc'] <= intent_bound:
                                store[user_ID]['response'] = response
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                res = response  #+ ' ' + str(store[user_ID]['re_ask'])
                                server.send_text_message(user_ID, res)
                    else:
                        store[user_ID]['re_ask'] += 1  #True
                        store[user_ID]['re_intent'] = intent
                        store[user_ID]['keyword'] = keyword
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                        else:
                            res = response  #+ ' ' + str(store[user_ID]['re_ask'])
                            server.send_text_message(user_ID, res)
                else:
                    store[user_ID]['re_ask'] = 0
                    store[user_ID]['keyword'] = keyword
                    store[user_ID]['re_intent'] = ''
                    if store[user_ID]['intent_acc'] <= intent_bound:
                        store[user_ID]['response'] = response
                        res = 'We think your input may lead to wrong response, do you want continue?'
                        server.send_button_message(user_ID, res,
                                                   continue_button)
                    else:
                        res = response  #+ ' ' + str(store[user_ID]['re_ask'])
                        ####send feed back
                        server.send_button_message(user_ID, res, button)

            elif message.get('postback'):
                user_ID = message['sender']['id']
                recipient_id = message["recipient"]["id"]

                payload = message["postback"]["payload"]
                if payload == 'Yes':
                    res = 'It is good to hear!'
                    feedback.feedback(user_ID, store[user_ID]['input'],
                                      store[user_ID]['keyword']['intent'])
                    server.send_text_message(user_ID, res)
                elif payload == 'No':
                    res = 'We will improve soon!'
                    sent_time = time.time()
                    store[user_ID] = {
                        'input': '',
                        're_intent': '',
                        'keyword': {},
                        're_ask': 0,
                        'time': sent_time,
                        'intent_acc': 0.0,
                        'response': ''
                    }
                    server.send_text_message(user_ID, res)
                elif payload == '1':  ### want continue
                    res = store[user_ID]['response']
                    if store[user_ID]['re_ask'] == False:
                        if store[user_ID]['re_intent'] in [
                                'Greetings', 'Goodbye', 'name'
                        ]:
                            server.send_text_message(user_ID, res)
                        else:
                            server.send_button_message(user_ID, res, button)
                    else:
                        #res = store[user_ID]['re_intent'] + ' ' + res + ' ' + str(store[user_ID]['re_ask'])
                        server.send_text_message(user_ID, res)
                elif payload == '0':
                    res = 'Please reinput with more details.'
                    sent_time = time.time()
                    store[user_ID] = {
                        'input': '',
                        're_intent': '',
                        'keyword': {},
                        're_ask': 0,
                        'time': sent_time,
                        'intent_acc': 0.0,
                        'response': ''
                    }
                    server.send_text_message(user_ID, res)

    return "Message Processed"
Ejemplo n.º 23
0
    print(division)
    print("step")
    print(division.stepnow)
    print("back")

    out = division.input(answer)
    print(out)
    stepnow = division.stepnow

    if stepnow == step:
        return "0" + str(stepnow) + out

    return "1" + str(stepnow) + out


ff = feedback.feedback(core)


@app.route('/feedback/question', methods=['get'])
def getquestion():

    out = ff.getquestion()

    return out


@app.route('/feedback/checkanswer', methods=['post'])
def getanswer():
    question_id = int(request.get_json()["question"])
    inanswer = request.get_json()["answer"]
Ejemplo n.º 24
0
def main():
    print(
        "===================================System active==================================="
    )

    found_classes = []

    while True:
        print("Listening...")
        r = sr.Recognizer()
        m = sr.Microphone()

        with m as source:
            r.adjust_for_ambient_noise(source)

        with m as source:
            audio = r.listen(source)

        try:
            value = r.recognize_google(audio)
            temp = classify(value)
            if len(found_classes) < 3:
                found_classes += temp
            else:
                found_classes = temp
            print found_classes

            rooms = ["bedroom", "living_room"]
            states = ["on", "off", "value"]
            appliances = ["light", "fan", "ac"]

            r = [x for x in found_classes if x in rooms]
            a = [x for x in found_classes if x in appliances]
            s = [x for x in found_classes if x in states]
            room = r[0] if r else None
            appliance = a[0] if a else None
            state = s[0] if s else None

            if room == rooms[0]:
                if appliance == appliances[0]:
                    if state == states[0]:
                        action(0, 1)
                    elif state == states[1]:
                        action(0, 0)
                    else:
                        say = "Forgot to tell me on or off."
                        mode = "ask"
                        feedback(mode, say)
                elif appliance == appliances[1]:
                    if state == states[0]:
                        action(1, 1)
                    elif state == states[1]:
                        action(1, 0)
                    else:
                        say = "turn it off or on"
                        mode = "ask"
                        feedback(mode, say)
                elif appliance == appliances[2]:
                    if state == states[0]:
                        action(2, 1)
                    elif state == states[1]:
                        action(2, 0)
                    elif state == state[2]:
                        action(2, 0.96)
                    else:
                        say = "Sorry didn't hear you."
                        mode = "ask"
                        feedback(mode, say)
                else:
                    say = "Which appliance?"
                    mode = "ask"
                    feedback(mode, say)
            elif room == rooms[1]:
                if appliance == appliances[0]:
                    if state == states[0]:
                        action(3, 1)
                    elif state == states[1]:
                        action(3, 0)
                    else:
                        say = "turn it off or on"
                        mode = "ask"
                        feedback(mode, say)
                elif appliance == appliances[1]:
                    if state == states[0]:
                        action(4, 1)
                    elif state == states[1]:
                        action(4, 0)
                    else:
                        say = "Sorry didn't hear you."
                        mode = "ask"
                        feedback(mode, say)
                elif appliance == appliances[2]:
                    if state == states[0]:
                        action(5, 1)
                    elif state == states[1]:
                        action(5, 0)
                    elif state == state[2]:
                        action(5, 0.48)
                    else:
                        say = "Did you forget to tell me on or off."
                        mode = "ask"
                        feedback(mode, say)
                else:
                    say = "Which appliance"
                    mode = "ask"
                    feedback(mode, say)
            else:
                say = "What's the room you say?"
                mode = "ask"
                feedback(mode, say)

        except sr.UnknownValueError:
            say = "Oops didn't catch that!"
            mode = "Error"
            feedback(mode, say)

        except sr.RequestError:
            say = "Internet seems to be down."
            mode = "Error"
            feedback(mode, say)
Ejemplo n.º 25
0
def main():

    # In the scikit-learn set below, the data is shuffled using utils.resample
    # as the first pass had an artifact in the end of the list that wasn't
    # representative of the rest of the set.

    # Dependent on input set
    temp_digits = datasets.load_digits()
    digits = utils.resample(temp_digits.data, random_state=0)
    temp_answers = utils.resample(temp_digits.target, random_state=0)
    images = utils.resample(temp_digits.images, random_state=0)
    target_values = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    num_of_training_vectors = 1000
    answers, answers_to_test, validation_answers = (
        temp_answers[:num_of_training_vectors],
        temp_answers[num_of_training_vectors : num_of_training_vectors + 500],
        temp_answers[num_of_training_vectors + 500 :],
    )
    training_set, testing_set, validation_set = (
        digits[:num_of_training_vectors],
        digits[num_of_training_vectors : num_of_training_vectors + 500],
        digits[num_of_training_vectors + 500 :],
    )

    # For all inputs
    training_vectors = [append_bias(vector) for vector in training_set]
    test_vectors = [append_bias(vector) for vector in testing_set]

    network = Network(
        images,
        target_values,
        len(training_set[0]),
        training_vectors,
        answers,
        test_vectors,
        answers_to_test,
        validation_set,
        validation_answers,
    )
    # [network.learn_run() for x in range(250)]  # Number of Epochs over set
    # network.report_results(network.run_unseen())
    # network.report_results(network.run_unseen(True), True)

    # plt.ion()
    # with open("weight_set.txt", 'w') as w:
    #     for elem in network.neurons:
    #         for x in elem.weights:
    #             y = str(x) + ', '
    #             w.write(y)

    with open("weight_set.txt", "r") as w:
        for line in w:
            weights = line.strip().split(", ")

    w_list = [float(elem.replace(",", "")) for elem in weights]
    for x in range(10):
        network.neurons[x].weights = w_list[:64]
        w_list = w_list[64:]
    print(network.neurons[1].weights)
    x = np.random.normal(128, 1, (8, 8))
    y = x.flatten()
    # y = network.test_set[0][:-1]
    network.test_set = [y]
    visualization(y, "Gaussian Noise")
    guess = network.run_unseen()
    print(guess)
    # print(y)
    # input("Hit a key to continue")
    target_values.remove(guess[0])
    for i in range(15):
        for i in range(5):
            y = feedback(y, network.neurons[guess[0]].weights, 0.001)
            for x in target_values:
                y = feedback(y, network.neurons[x].weights, -0.0001)
        visualization(y, "Dreamt a " + str(guess[0]))
        network.test_set = [y]
        guess = network.run_unseen()
        print(guess)
    y = [(neurons[4].x) % 256 for x in weights]
    visualization(y, "Neuron 4 weight set")
Ejemplo n.º 26
0
 def __init__(self):
     self.cr = config_reader()
     self.feedback_dir = self.cr.get_feedback()
     self.feedbeck = feedback()
     self.late_submission_list = late_submit_checker().late_submitter_log()
     self.late_penalty = int(self.cr.get_late_submit_penalty())
Ejemplo n.º 27
0
def recieve_message():
    global store
    user_input = request.get_json()

    if user_input.get('entry'):
        event = user_input['entry'][0]
        if event.get('messaging'):
            message = event['messaging'][0]
            # server get text type message
            if message.get('message'):

                text = message['message'].get(
                    'text')  #user input on Facebook Messenger
                user_ID = message['sender'][
                    'id']  #Facebook Messenger ID for user so we know where to send response back to

                sent_time = time.time()  # current time

                if user_ID not in store.keys(
                ):  #new user, create a new record for this ID
                    store[user_ID] = {
                        'input': '',
                        're_intent': '',
                        'keyword': {},
                        're_ask': 0,
                        'time': sent_time,
                        'intent_acc': 0.0,
                        'response': ''
                    }
                else:  #old user, update record of this ID
                    break_time = sent_time - store[user_ID]['time']
                    if break_time > 120:  # longer than 2 mins, renew the record
                        store[user_ID] = {
                            'input': '',
                            're_intent': '',
                            'keyword': {},
                            're_ask': 0,
                            'time': sent_time,
                            'intent_acc': 0.0,
                            'response': ''
                        }
                    else:
                        store[user_ID]['time'] = sent_time

                if store[user_ID]['re_ask'] == 0 or store[user_ID][
                        're_ask'] > 1:  ## the server do not need re-ask or server had re-asked more than twice
                    store[user_ID]['re_ask'] = 0
                    new_text = intent_classify.preprocessing(
                        text)  # preprocessing the user input
                    intent, intent_acc = intent_classify.intent_classification(
                        new_text)  # get intent and the accuracy
                    store[user_ID]['intent_acc'] = intent_acc

                    # Corresponding actions for intents of 'Greetings','Goodbye','Name'
                    if intent == 'Greetings':
                        greet_response = [
                            'Hi, I am here to help you!', 'Hi nice to meet u!',
                            'Hey there!', 'Yo!', 'Hey, boo!'
                        ]
                        response = random.choice(
                            greet_response)  #random chooser one response
                        if store[user_ID][
                                'intent_acc'] <= intent_bound:  # intent accuracy lower than the bound
                            # store response and intent into record for further continue sending
                            store[user_ID]['response'] = response
                            store[user_ID]['re_intent'] = intent
                            # send ask-continue button type message to user
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                            return "Message Processed"
                        else:  # send the reponse back to user
                            server.send_text_message(user_ID, response)
                            return "Message Processed"

                    elif intent == 'Goodbye':
                        bye_response = [
                            'See you soon!', 'Thanks!', 'ByeBye!',
                            'Thank you for choosing us!', 'I will miss you!'
                        ]
                        response = random.choice(bye_response)
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            store[user_ID]['re_intent'] = intent
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                            return "Message Processed"
                        else:
                            server.send_text_message(user_ID, response)
                            return "Message Processed"

                    elif intent == 'Name':
                        name_response = [
                            'My name is KOBO. :)', 'Please call me KOBO ~',
                            'I am KOBO ^^'
                        ]
                        response = random.choice(name_response)
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            store[user_ID]['re_intent'] = intent
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                            return "Message Processed"
                        else:
                            server.send_text_message(user_ID, response)
                            return "Message Processed"

                    store[user_ID]['input'] = text  #record the input of user

                else:  #server is re-asking for course code or stream name
                    print(store[user_ID])
                    intent = store[user_ID]['re_intent']  #get recorded intent
                    text = store[user_ID][
                        'input'] + ' ' + text  # combine the new input with recorded input for further actions
                    print('new reask text is ' + text)

                keyword = keyword_extract.keyword_extraction(
                    intent, text)  # extract keyword fron input
                print('keyword is ' + str(keyword))

                response = retrieve.retrieval_func(
                    keyword)  # get response from data-retrieving
                print('data retrieve is ' + response)

                if response == 'Please provide valid courses code.':  # can not retrieve without valid course codes
                    # search record for valid course code
                    if store[user_ID]['keyword'] != {}:
                        print(store[user_ID])
                        if store[user_ID]['keyword'][
                                'course'] != []:  # recorded valid course code
                            store[user_ID]['keyword']['intent'] = intent
                            print(store[user_ID]['keyword'])
                            keyword['course'] = store[user_ID]['keyword'][
                                'course']  #conbine keyword with recorded course codes
                            response = retrieve.retrieval_func(
                                keyword
                            )  # re-data_retrieving for response with new keyword
                            if store[user_ID][
                                    'intent_acc'] <= intent_bound:  # intent accuracy lower than the bound
                                store[user_ID][
                                    'response'] = response  # recorded the response for further sending
                                # send ask-continue button type message to user
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                # send response back to user
                                res = response
                                server.send_button_message(
                                    user_ID, res, button)
                        else:  # no valid course code in recorded keyword
                            store[user_ID][
                                're_ask'] += 1  # add one more time to re-ask label of record,server need re-ask
                            # record intent and keyword
                            store[user_ID]['re_intent'] = intent
                            store[user_ID]['keyword'] = keyword
                            if store[user_ID]['intent_acc'] <= intent_bound:
                                store[user_ID]['response'] = response
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                res = response
                                server.send_text_message(user_ID, res)
                    else:  # not record keyword
                        store[user_ID][
                            're_ask'] += 1  # add one more time to re-ask label of record
                        # record intent and keyword
                        store[user_ID]['re_intent'] = intent
                        store[user_ID]['keyword'] = keyword
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                        else:
                            res = response
                            server.send_text_message(user_ID, res)

                elif response[(
                        len(response) - 43
                ):] == 'Do you want query one specific stream name?':  # can not retrieve without an valid stream name
                    # search record for valid stream name
                    if store[user_ID]['keyword'] != {}:
                        print(store[user_ID])
                        if store[user_ID]['keyword'][
                                'stream_name'] != []:  # recorded valid stream name
                            store[user_ID]['keyword']['intent'] = intent
                            print(store[user_ID]['keyword'])
                            keyword['stream_name'] = store[user_ID]['keyword'][
                                'stream_name']  #conbine keyword with recorded course codes
                            response = retrieve.retrieval_func(
                                keyword
                            )  # re-data_retrieving for response with new keyword
                            if store[user_ID][
                                    'intent_acc'] <= intent_bound:  # intent accuracy lower than the bound
                                store[user_ID][
                                    'response'] = response  # recorded the response for further sending
                                # send ask-continue button type message to user
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                # send response back to user
                                res = response
                                server.send_button_message(
                                    user_ID, res, button)
                        else:  # no valid stream name in recorded keyword
                            store[user_ID][
                                're_ask'] += 1  # add one more time to re-ask label of record, server need re-ask
                            # record intent and keyword
                            store[user_ID]['re_intent'] = intent
                            store[user_ID]['keyword'] = keyword
                            if store[user_ID]['intent_acc'] <= intent_bound:
                                store[user_ID]['response'] = response
                                res = 'We think your input may lead to wrong response, do you want continue?'
                                server.send_button_message(
                                    user_ID, res, continue_button)
                            else:
                                res = response
                                server.send_text_message(user_ID, res)
                    else:  # not record keyword
                        store[user_ID][
                            're_ask'] += 1  # add one more time to re-ask label of record,server need re-ask
                        store[user_ID]['re_intent'] = intent
                        store[user_ID]['keyword'] = keyword
                        if store[user_ID]['intent_acc'] <= intent_bound:
                            store[user_ID]['response'] = response
                            res = 'We think your input may lead to wrong response, do you want continue?'
                            server.send_button_message(user_ID, res,
                                                       continue_button)
                        else:
                            res = response  #+ ' ' + str(store[user_ID]['re_ask'])
                            server.send_text_message(user_ID, res)

                else:  # getting an response from retrieving data
                    store[user_ID][
                        're_ask'] = 0  # set re-ask label as 0 in record, server did not re-ask
                    store[user_ID]['keyword'] = keyword  # record the keyword
                    store[user_ID]['re_intent'] = ''
                    if store[user_ID][
                            'intent_acc'] <= intent_bound:  # intent accuracy lower than the bound
                        store[user_ID][
                            'response'] = response  # recorded the response for further sending
                        # send ask-continue button type message to user
                        res = 'We think your input may lead to wrong response, do you want continue?'
                        server.send_button_message(user_ID, res,
                                                   continue_button)
                    else:
                        # send response back to user
                        res = response
                        server.send_button_message(user_ID, res, button)

            # server get message from button
            elif message.get('postback'):
                user_ID = message['sender'][
                    'id']  #Facebook Messenger ID for user so we know where to send response back to
                recipient_id = message["recipient"]["id"]

                payload = message["postback"]["payload"]  # get button text
                if payload == 'Yes':  # user feedback that response is helpful
                    res = 'It is good to hear!'
                    #  upload user input into AWS dataset for further model training
                    feedback.feedback(user_ID, store[user_ID]['input'],
                                      store[user_ID]['keyword']['intent'])
                    server.send_text_message(user_ID, res)
                elif payload == 'No':  # user feedback that response is helpless
                    res = 'We will improve soon!'
                    sent_time = time.time()
                    # renew the record of user for new query
                    store[user_ID] = {
                        'input': '',
                        're_intent': '',
                        'keyword': {},
                        're_ask': 0,
                        'time': sent_time,
                        'intent_acc': 0.0,
                        'response': ''
                    }
                    server.send_text_message(user_ID, res)
                elif payload == '1':  # user feedback that wanna continue
                    res = store[user_ID]['response']
                    if store[user_ID]['re_ask'] == False:  # server not re-ask
                        # send stored response back to user
                        if store[user_ID]['re_intent'] in [
                                'Greetings', 'Goodbye', 'name'
                        ]:  # send text type message for 3 types of intent
                            server.send_text_message(user_ID, res)
                        else:  # send button type message for other kinds of intent
                            server.send_button_message(user_ID, res, button)
                    else:  #server is re-asking
                        # send stored response back to user
                        server.send_text_message(user_ID, res)
                elif payload == '0':  # user feedback that wanna not continue
                    res = 'Please reinput with more details.'
                    sent_time = time.time()
                    # renew the record of user for new query
                    store[user_ID] = {
                        'input': '',
                        're_intent': '',
                        'keyword': {},
                        're_ask': 0,
                        'time': sent_time,
                        'intent_acc': 0.0,
                        'response': ''
                    }
                    server.send_text_message(user_ID, res)

    return "Message Processed"
Ejemplo n.º 28
0
def run_step(job_desc, resources):
    """
    Run step (parallel to main thread)
    :param job_desc: string, jod id + "_" + step order
    :param resources: dictionary, resources required by the step
    :return: None
    """
    global LATEST_JOB_ID, LATEST_JOB_STEP, CPU_POOL, MEMORY_POOL, DISK_POOL, RUNNING_JOBS
    items = job_desc.split('_')
    job_id = int(items[0])
    step_order = int(items[1])
    user_id = JOB_TABLE[job_id]['user_id']
    recheck = forecast_step(job_id, step_order, resources)

    log_file = os.path.join(settings["env"]["log"], str(job_id))

    if recheck is not True:
        return
    if 'feedback' in settings['env'].keys(
    ) and settings['env']['feedback'] == 'yes':
        try:
            from feedback import feedback
            feedback(JOB_COMMAND[job_id][0], ' '.join(JOB_COMMAND[job_id][1:]),
                     get_user_mail(JOB_TABLE[job_id]['user_id']))
        except:
            pass

    if settings['cluster']['type']:
        # for cluster
        import clusterSupport
        if resources['cpu'] is None:
            allocate_cpu = settings['cluster']['cpu']
        else:
            from math import ceil
            predict_cpu = int(ceil(round(resources['cpu']) / 100))
            if predict_cpu > settings['cluster']['cpu'] or predict_cpu == 0:
                allocate_cpu = settings['cluster']['cpu']
            else:
                allocate_cpu = predict_cpu
        if resources['mem'] is None:
            allocate_mem = settings['cluster']['mem']
        else:
            allocate_mem = bytes_to_readable(resources['mem'])
        if resources['vrt_mem'] is None:
            allocate_vrt = settings['cluster']['vrt']
        else:
            allocate_vrt = bytes_to_readable(resources['vrt_mem'])
        # baseDriver.update(settings['datasets']['job_db'], job_id, 'status', step_order + 1)
        try:
            job_record = Queue.objects.get(id=job_id)
            job_record.set_status(step_order + 1)
        except:
            pass

        if 'trace' in resources.keys():
            # learn
            return_code = clusterSupport.main(
                settings['cluster']['type'], ' '.join(JOB_COMMAND[job_id]),
                job_id, step_order, allocate_cpu, allocate_mem, allocate_vrt,
                settings['cluster']['queue'], JOB_TABLE[job_id]['job_folder'],
                log_file, settings['cluster']['walltime'], 1,
                resources['trace'])
        else:
            return_code = clusterSupport.main(
                settings['cluster']['type'], ' '.join(JOB_COMMAND[job_id]),
                job_id, step_order, allocate_cpu, allocate_mem, allocate_vrt,
                settings['cluster']['queue'], JOB_TABLE[job_id]['job_folder'],
                log_file, settings['cluster']['walltime'])

        if return_code != 0:
            try:
                from feedback import feedback_error, get_error_log
                feedback_error(JOB_COMMAND[job_id][0],
                               ' '.join(JOB_COMMAND[job_id][1:]),
                               get_error_log(log_file), get_user_mail(user_id))
            except:
                pass
            error_job(job_id, resources)
        else:
            # RUNNING_JOBS -= 1
            finish_step(job_id, step_order, resources)
    else:
        # for local environment or cloud
        print("Now run %s" % job_desc)
        print(CPU_POOL, MEMORY_POOL, DISK_POOL)
        try:
            log_file_handler = open(log_file, "a")
            RUNNING_JOBS += 1
            true_shell = 0
            redirect_tags = ('>', '<', '|')

            for rt in redirect_tags:
                if rt in JOB_COMMAND[job_id]:
                    true_shell = 1
                    break

            if true_shell:
                step_process = subprocess.Popen(
                    ' '.join(JOB_COMMAND[job_id]),
                    shell=True,
                    cwd=JOB_TABLE[job_id]['job_folder'])
            else:
                step_process = subprocess.Popen(
                    JOB_COMMAND[job_id],
                    shell=False,
                    stdout=log_file_handler,
                    stderr=log_file_handler,
                    cwd=JOB_TABLE[job_id]['job_folder'])
            # step_process = subprocess.Popen(JOB_COMMAND[job_id], shell=False, stdout=log_file_handler,
            #                                stderr=log_file_handler, cwd=JOB_TABLE[job_id]['job_folder'])
            process_id = step_process.pid
            if 'trace' in resources.keys():
                learn_process = subprocess.Popen([
                    "python",
                    os.path.join(root_path, 'mlCollector.py'), "-p",
                    str(step_process.pid), "-n",
                    str(JOB_TABLE[job_id]['steps'][step_order]['hash']), "-j",
                    str(resources['trace'])
                ],
                                                 shell=False,
                                                 stdout=None,
                                                 stderr=subprocess.STDOUT)
            while step_process.poll() is None:
                if process_id in psutil.pids():
                    proc_info = psutil.Process(process_id)
                    if proc_info.is_running():
                        job = Queue.objects.get(id=job_id)
                        if job.ter:
                            job.status = -3
                            job.ter = 0
                            job.save()
                            # proc_info.kill()
                            kill_proc(proc_info)
                            # step_process.kill()
                            error_job(job_id, resources)
                            RUNNING_JOBS -= 1

                            return None

                time.sleep(30)
            log_file_handler.close()
            print("Now job %s finished." % job_desc)
            # finish_step(job_id, step_order, resources)
            if step_process.returncode != 0:
                RUNNING_JOBS -= 1
                error_job(job_id, resources)
            else:
                RUNNING_JOBS -= 1
                finish_step(job_id, step_order, resources)
            JOB_TABLE[job_id]['resume'] = step_order
            if job_id > LATEST_JOB_ID and (step_order + 1) > LATEST_JOB_STEP:
                LATEST_JOB_ID = job_id
                LATEST_JOB_STEP = step_order
        except Exception as e:
            print(e)
            try:
                from feedback import feedback_error, get_error_log
                feedback_error(JOB_COMMAND[job_id][0],
                               ' '.join(JOB_COMMAND[job_id][1:]),
                               get_error_log(log_file), get_user_mail(user_id))
            except:
                pass
            RUNNING_JOBS -= 1
            error_job(job_id, resources)
Ejemplo n.º 29
0
        members = settings.get('members', [])
        notifications = api.cache_method('/me/notifications', team['id'])
        if len(notifications) == 0:
            output.append(feedback.item(title='You have no notifications', valid=False, icon=icon_path(team)))
        for n in notifications:
            m = [member for member in members if member['id'] == n['who']]
            if len(m) >= 1:
                icon = icon_path_member(m[0])
            else:
                icon = icon_path(team)
            output.append(feedback.item(title=n['title'], subtitle=n['body'], valid=False, icon=icon))
    feedback.feedback(output)

# Do nothing until there is an access_token
if (token is None or bool(re.search("^[a-z0-9]{128}$", token)) == False) or params[0] == 'token':
    feedback.feedback(feedback.item(title='Get a token', valid=True, arg='token:' + str(is_first_time)))
    sys.exit()

# Check that the user has a team
user_teams = settings.get('teams', [])
if len(user_teams) == 0 or params[0] == 'teams':
    title = ''
    if len(user_teams) == 0:
        title = 'No'
    else:
        title = 'Update'
    feedback.feedback(feedback.item(title=title + ' teams', autocomplete='teams', subtitle='Use `teams` to update your teams', arg='teams', valid=True))
    sys.exit()

if params[0] == 'members':
    feedback.feedback(feedback.item(title='Update members', autocomplete='members', subtitle='Use `members` to update your team members', arg='members', valid=True))
Ejemplo n.º 30
0
 def __init__(self, policynames, policyranges):
     self.f = feedback.feedback() 
     self.pNames = policynames
     self.pRange = policyranges