Exemplo n.º 1
0
    def on_modify_last(self, token, modified_output):
        if not self.sessions.has_token(token):
            raise DeployError('No such token:\'%s\'' % token)
        session = self.sessions.get_session(token)

        if not session['turns']:
            raise DeployError('This is the first turn in this session.')

        last_turn = session['turns'][-1]
        session['turns'] = session['turns'][:-1]

        for (key, value) in modified_output.items():
            last_turn['modified_output'][key] = value

        cur_turn = self._turn(
            last_turn=session['turns'][-1] if session['turns'] else None,
            model_map=session['model_map'],
            history=ServerCtrl._history_from_turns(session['turns']),
            input_module=last_turn['input_module'],
            data=last_turn['data'],
            modified_output=last_turn['modified_output'])
        session['turns'].append(cur_turn)
        self.sessions.set_session(token, session)

        return ServerCtrl._response_from_session(session['turns'])
Exemplo n.º 2
0
    def run(self, method, cache, isfirst, params):
        res_idx = self.res_lock.res_catch()
        print('+++++ catch res ' + str(res_idx) + ' ' + self.model_id)
        try:
            # get model
            model = self.models[res_idx]
            if model is None:
                raise DeployError('Model has not started yet.',
                                  model=self.model_id)

            # load cache
            if isfirst:
                getattr(model, 'init_session')()  # first turn
            else:
                getattr(model, 'from_cache')(cache)

            # process
            ret_data = getattr(model, method)(*params)

            # save cache
            new_cache = copy.deepcopy(getattr(model, 'to_cache')())

        except Exception as e:
            if not isinstance(e, DeployError):
                raise DeployError('running error:%s' % str(e),
                                  model=self.model_id)
            else:
                raise e
        finally:
            print('----- leave res ' + str(res_idx) + ' ' + self.model_id)
            self.res_lock.res_leave(res_idx)

        return ret_data, new_cache
Exemplo n.º 3
0
 def get(self, name, local_path):
     name = name.strip()
     if not name.startswith('/'):
         name = '/' + name
     dirname, basename = os.path.split(name)
     if len(basename) == 0:
         raise DeployError('file not exists in ftp: %s' % name)
     try:
         if self.ftp.pwd() != dirname:
             self.ftp.cwd(dirname)
         callback = open(os.path.join(local_path, basename), 'wb').write
         commond = 'RETR %s' % basename
         self.ftp.retrbinary(commond, callback)
     except Exception, e:
         raise DeployError('ftp get error: %s' % str(e))
Exemplo n.º 4
0
 def sub_used_num(self, model_id: str):
     if model_id is not None:
         try:
             self.models[model_id].sub_used_num()
         except TypeError:
             raise DeployError('Unknow model id \'%s\'' % model_id,
                               module=self.module_name)
Exemplo n.º 5
0
def net_function(fun):
    params = get_params_from_request(request)
    ret = {}
    try:
        # clear expire session every time
        ctrl_server.on_clear_expire()

        if fun == 'models':
            ret = ctrl_server.on_models()
        elif fun == 'register':
            ret = ctrl_server.on_register(**params)
        elif fun == 'close':
            ret = ctrl_server.on_close(**params)
        elif fun == 'clear_expire':
            ret = ctrl_server.on_clear_expire()
        elif fun == 'response':
            ret = ctrl_server.on_response(**params)
        elif fun == 'rollback':
            ret = ctrl_server.on_rollback(**params)
        else:
            raise DeployError('Unknow funtion \'%s\'' % fun)
    except Exception as e:
        err_msg = 'There are some errors in the operation. %s' % str(e)
        if isinstance(e, DeployError):
            err_msg = str(e)
        elif isinstance(e, TypeError):
            err_msg = 'Input parameters incorrect for function \'%s\'.' % fun
        ret = {'status': 'error', 'error_msg': err_msg}
    else:
        ret.setdefault('status', 'ok')
    finally:
        ret = json.dumps(ret, ensure_ascii=False)
    return ret
Exemplo n.º 6
0
    def on_close(self, token):
        if not self.sessions.has_token(token):
            raise DeployError('No such token:\'%s\'' % token)

        sess_info = self.sessions.pop_session(token)
        for module in ['nlu', 'dst', 'policy', 'nlg']:
            self.modules[module].sub_used_num(sess_info[module])
        return {'del_token': token}
Exemplo n.º 7
0
 def on_rollback(self, token, back_turns=1):
     if not self.sessions.has_token(token):
         raise DeployError('No such token:\'%s\'' % token)
     sess_info = self.sessions.get_session(token)
     sess_info['cache'] = sess_info['cache'][:-back_turns]
     turns = len(sess_info['cache'])
     self.sessions.set_session(token, sess_info)
     return {'current_turns': turns}
Exemplo n.º 8
0
    def on_close(self, token):
        if not self.sessions.has_token(token):
            raise DeployError('No such token:\'%s\'' % token)

        session = self.sessions.pop_session(token)
        for module in MODULES:
            self.modules[module].sub_used_num(session['model_map'][module])
        return {'del_token': token}
Exemplo n.º 9
0
 def run(self, model_id, cache, isfirst, params):
     try:
         ret = self.models[model_id].run(self.method, cache, isfirst,
                                         params)
     except TypeError:
         raise DeployError('Unknow model id \'%s\'' % model_id,
                           module=self.module_name)
     return ret
Exemplo n.º 10
0
    def on_rollback(self, token, back_turns=1):
        if not self.sessions.has_token(token):
            raise DeployError('No such token:\'%s\'' % token)
        session = self.sessions.get_session(token)

        session['turns'] = session['turns'][:-back_turns]
        self.sessions.set_session(token, session)

        return ServerCtrl._response_from_session(session['turns'])
Exemplo n.º 11
0
 def add_used_num(self):
     with self.opt_lock:
         if self.used_num == 0:
             print('------------implement: ' + self.model_id)
             res_idxs = self.__catch_all_res()
             try:
                 self.models = [self.__implement() for _ in range(self.max_core)]
             except Exception as e:
                 raise DeployError('Instantiation failed:%s' % str(e), model=self.model_id)
             finally:
                 self.__leave_all_res(res_idxs)
         self.used_num += 1
     return self.used_num
Exemplo n.º 12
0
 def get_model(self, cache):
     res_idx = self.res_lock.res_catch()
     model = self.models[res_idx]
     if cache is None:
         getattr(model, 'init_session')()  # first turn
     else:
         getattr(model, 'from_cache')(cache)
     self.res_lock.res_leave(res_idx)
     if model is None:
         raise DeployError('Model has not started yet.',
                           model=self.model_id)
     else:
         return model
Exemplo n.º 13
0
    def on_response(self, token, input_module, data, modified_output={}):
        if not self.sessions.has_token(token):
            raise DeployError('No such token:\'%s\'' % token)
        session = self.sessions.get_session(token)

        cur_turn = self._turn(last_turn=session['turns'][-1] if session['turns'] else None,
                              model_map=session['model_map'],
                              history=ServerCtrl._history_from_turns(session['turns']),
                              input_module=input_module,
                              data=data,
                              modified_output=modified_output)
        session['turns'].append(cur_turn)
        self.sessions.set_session(token, session)

        return ServerCtrl._response_from_session(session['turns'])
Exemplo n.º 14
0
    def on_register(self, **kwargs):
        ret = {key: 0 for key in MODULES}
        try:
            for module_name in MODULES:
                model_id = kwargs.get(module_name, None)
                if isinstance(model_id, str):
                    ret[module_name] = self.modules[module_name].add_used_num(model_id)
        except Exception as e:
            for module_name in MODULES:
                model_id = kwargs.get(module_name, None)
                if isinstance(model_id, str) and ret[module_name] != 0:
                    self.modules[module_name].sub_used_num(model_id)
            raise e

        if ret['nlu'] == 0 and ret['dst'] == 0 and ret['policy'] == 0 and ret['nlg'] == 0:
            raise DeployError('At least one model needs to be started')

        token = self.sessions.new_session(*[kwargs.get(mn, None) for mn in MODULES])

        return {'token': token}
Exemplo n.º 15
0
    def on_response(self, token, input_module, data):
        if not self.sessions.has_token(token):
            raise DeployError('No such token:\'%s\'' % token)
        sess_info = self.sessions.get_session(token)
        isfirst = not sess_info['cache']
        history = []
        if isfirst:
            cache_nlu, cache_dst, cache_plc, cache_nlg = None, None, None, None
        else:
            last_cache = sess_info['cache'][-1]
            cache_nlu = last_cache.get('nlu', None)
            cache_dst = last_cache.get('dst', None)
            cache_plc = last_cache.get('policy', None)
            cache_nlg = last_cache.get('nlg', None)
            for cache in sess_info['cache']:
                history.append(['user', cache.get('usr', '')])
                history.append(['system', cache.get('sys', '')])

        ret_nlu, ret_dst, ret_plc, ret_nlg = None, None, None, None
        new_cache_nlu, new_cache_dst, new_cache_plc, new_cache_nlg = None, None, None, None

        # NLU
        if input_module == 'nlu':
            in_nlu = data
            if sess_info['nlu'] is not None:
                (ret_nlu, new_cache_nlu) = self.modules['nlu'].run(
                    sess_info['nlu'], cache_nlu, isfirst, [in_nlu, history])
                out_nlu = ret_nlu
            else:
                out_nlu = in_nlu

        # DST
        if input_module in ['nlu', 'dst']:
            in_dst = out_nlu if input_module == 'nlu' else data
            if sess_info['dst'] is not None:
                (ret_dst, new_cache_dst) = self.modules['dst'].run(
                    sess_info['dst'], cache_dst, isfirst, [in_dst])
                out_dst = ret_dst
            else:
                out_dst = in_dst

        # POLICY
        if input_module in ['nlu', 'dst', 'policy']:
            in_plc = out_dst if input_module in ['nlu', 'dst'] else data
            if sess_info['policy'] is not None:
                (ret_plc, new_cache_plc) = self.modules['policy'].run(
                    sess_info['policy'], cache_plc, isfirst, [in_plc])
                out_plc = ret_plc
            else:
                out_plc = None

        # NLG
        in_nlg = out_plc if input_module in ['nlu', 'dst', 'policy'] else data
        if sess_info['nlg'] is not None and in_nlg is not None:
            (ret_nlg,
             new_cache_nlg) = self.modules['nlg'].run(sess_info['nlg'],
                                                      cache_nlg, isfirst,
                                                      [in_nlg])

        # save cache
        new_cache = {
            'nlu': new_cache_nlu,
            'dst': new_cache_dst,
            'policy': new_cache_plc,
            'nlg': new_cache_nlg,
            'usr':
            data if isinstance(data, str) and input_module == 'nlu' else '',
            'sys': ret_nlg if isinstance(ret_nlg, str) else ''
        }
        sess_info['cache'].append(copy.deepcopy(new_cache))
        self.sessions.set_session(token, sess_info)

        history.append(['user', new_cache.get('usr', '')])
        history.append(['system', new_cache.get('sys', '')])

        return {
            'nlu': ret_nlu,
            'dst': ret_dst,
            'policy': ret_plc,
            'nlg': ret_nlg,
            'history': history
        }
Exemplo n.º 16
0
 def add_used_num(self, model_id: str):
     try:
         self.models[model_id].add_used_num()
     except TypeError:
         raise DeployError('Unknow model id \'%s\'' % model_id,
                           module=self.module_name)
Exemplo n.º 17
0
 def __init__(self, host='ftp.yihaodian.com.cn', user='******', passwd=''):
     try:
         self.ftp = FTP(host, user, passwd)
     except Exception, e:
         raise DeployError('ftp connect error: %s' % str(e))
Exemplo n.º 18
0
 def lists(self, path='/'):
     try:
         lists = self.ftp.nlst(path)
     except Exception, e:
         raise DeployError('ftp list error: %s' % str(e))