(self, inputs)
return results, scores, self.opt.n_best, timer.times
Translate `inputs` using this model

            Args:
                inputs: [{"src": "..."},{"src": ...}]

            Returns:
                result: (list) translations
                times: (dict) containing times
Translate `inputs` using this model
Translate inputs using this model
def run(self, inputs):
        """Translate `inputs` using this model

            Args:
                inputs: [{"src": "..."},{"src": ...}]

            Returns:
                result: (list) translations
                times: (dict) containing times
        """
        self.stop_unload_timer()

        timer = Timer()
        timer.start()
        self.logger.info("Running translation using %d" % self.model_id)

        if not self.loading_lock.is_set():
            self.logger.info(
                "Model #%d is being loaded by another thread, waiting"
                % self.model_id)
            if not self.loading_lock.wait(timeout=30):
                raise ServerModelError("Model %d loading timeout"
                                       % self.model_id)

        else:
            if not self.loaded:
                self.load()
                timer.tick(name="load")
            elif self.opt.cuda:
                self.to_gpu()
                timer.tick(name="to_gpu")

        texts = []
        head_spaces = []
        tail_spaces = []
        sslength = []
        for i, inp in enumerate(inputs):
            src = inp['src']
            if src.strip() == "":
                head_spaces.append(src)
                texts.append("")
                tail_spaces.append("")
            else:
                whitespaces_before, whitespaces_after = "", ""
                match_before = re.search(r'^\s+', src)
                match_after = re.search(r'\s+$', src)
                if match_before is not None:
                    whitespaces_before = match_before.group(0)
                if match_after is not None:
                    whitespaces_after = match_after.group(0)
                head_spaces.append(whitespaces_before)
                tok = self.maybe_tokenize(src.strip())
                texts.append(tok)
                sslength.append(len(tok.split()))
                tail_spaces.append(whitespaces_after)

        empty_indices = [i for i, x in enumerate(texts) if x == ""]
        texts_to_translate = [x for x in texts if x != ""]

        scores = []
        predictions = []
        if len(texts_to_translate) > 0:
            try:
                scores, predictions = self.translator.translate(
                    src_data_iter=texts_to_translate,
                    batch_size=self.opt.batch_size)
            except RuntimeError as e:
                raise ServerModelError("Runtime Error: %s" % str(e))

        timer.tick(name="translation")
        self.logger.info("""Using model #%d\t%d inputs
               \ttranslation time: %f""" % (self.model_id, len(texts),
                                            timer.times['translation']))
        self.reset_unload_timer()

        # NOTE: translator returns lists of `n_best` list
        #       we can ignore that (i.e. flatten lists) only because
        #       we restrict `n_best=1`
        def flatten_list(_list): return sum(_list, [])
        results = flatten_list(predictions)
        scores = [score_tensor.item()
                  for score_tensor in flatten_list(scores)]

        results = [self.maybe_detokenize(item)
                   for item in results]

        # build back results with empty texts
        for i in empty_indices:
            results.insert(i, "")
            scores.insert(i, 0)

        results = ["".join(items)
                   for items in zip(head_spaces, results, tail_spaces)]

        self.logger.info("Translation Results: %d", len(results))

        return results, scores, self.opt.n_best, timer.times
def run ( self , inputs ) : self . stop_unload_timer ( ) timer = Timer ( ) timer . start ( ) self . logger . info ( "Running translation using %d" % self . model_id ) if not self . loading_lock . is_set ( ) : self . logger . info ( "Model #%d is being loaded by another thread, waiting" % self . model_id ) if not self . loading_lock . wait ( timeout = 30 ) : raise ServerModelError ( "Model %d loading timeout" % self . model_id ) else : if not self . loaded : self . load ( ) timer . tick ( name = "load" ) elif self . opt . cuda : self . to_gpu ( ) timer . tick ( name = "to_gpu" ) texts = [ ] head_spaces = [ ] tail_spaces = [ ] sslength = [ ] for i , inp in enumerate ( inputs ) : src = inp [ 'src' ] if src . strip ( ) == "" : head_spaces . append ( src ) texts . append ( "" ) tail_spaces . append ( "" ) else : whitespaces_before , whitespaces_after = "" , "" match_before = re . search ( r'^\s+' , src ) match_after = re . search ( r'\s+$' , src ) if match_before is not None : whitespaces_before = match_before . group ( 0 ) if match_after is not None : whitespaces_after = match_after . group ( 0 ) head_spaces . append ( whitespaces_before ) tok = self . maybe_tokenize ( src . strip ( ) ) texts . append ( tok ) sslength . append ( len ( tok . split ( ) ) ) tail_spaces . append ( whitespaces_after ) empty_indices = [ i for i , x in enumerate ( texts ) if x == "" ] texts_to_translate = [ x for x in texts if x != "" ] scores = [ ] predictions = [ ] if len ( texts_to_translate ) > 0 : try : scores , predictions = self . translator . translate ( src_data_iter = texts_to_translate , batch_size = self . opt . batch_size ) except RuntimeError as e : raise ServerModelError ( "Runtime Error: %s" % str ( e ) ) timer . tick ( name = "translation" ) self . logger . info ( """Using model #%d\t%d inputs
               \ttranslation time: %f""" % ( self . model_id , len ( texts ) , timer . times [ 'translation' ] ) ) self . reset_unload_timer ( ) # NOTE: translator returns lists of `n_best` list #       we can ignore that (i.e. flatten lists) only because #       we restrict `n_best=1` def flatten_list ( _list ) : return sum ( _list , [ ] ) results = flatten_list ( predictions ) scores = [ score_tensor . item ( ) for score_tensor in flatten_list ( scores ) ] results = [ self . maybe_detokenize ( item ) for item in results ] # build back results with empty texts for i in empty_indices : results . insert ( i , "" ) scores . insert ( i , 0 ) results = [ "" . join ( items ) for items in zip ( head_spaces , results , tail_spaces ) ] self . logger . info ( "Translation Results: %d" , len ( results ) ) return results , scores , self . opt . n_best , timer . times