Beispiel #1
0
    def post(self):
        inputs = request.get_json(force=True)
        response_list = list()
        if len(inputs) > 0:
            log_info("Making labse-aligner(Resource) API call", MODULE_CONTEXT)
            log_info("Complete request input: {}".format(inputs),
                     MODULE_CONTEXT)
            try:
                for i in inputs:
                    if all(v in i for v in ["src_phrases", "tgt"]):
                        log_info("Making labse-aligner service call",
                                 MODULE_CONTEXT)
                        res = LabseAlignerService.phrase_aligner(i)
                        response_list.append(res)
                        out = CustomResponse(Status.SUCCESS.value,
                                             response_list)
                    else:
                        log_info(
                            "Missing mandatory Parameters for labse-aligner:src_phrases or tgt",
                            MODULE_CONTEXT)
                        out = CustomResponse(
                            Status.MANDATORY_PARAM_MISSING.value, [])
                        return out.getres()
            except Exception as e:
                status = Status.SYSTEM_ERR.value
                status['why'] = str(e)
                out = CustomResponse(status, [])

            return out.getres()
        else:
            log_info("null inputs in request in labse-aligner API",
                     MODULE_CONTEXT)
            out = CustomResponse(Status.INVALID_API_REQUEST.value, None)
            return out.getres()
Beispiel #2
0
    def post(self):
        body        = request.json

        parser = reqparse.RequestParser()
        parser.add_argument('dict_fallback', type=int, location='args', help='set 1 to invoke google transalte and 0 to not', required=False,default=1)
        args    = parser.parse_args()
        dict_fallback=args["dict_fallback"]
        
        log_info('received request for WordSearch', AppContext.getContext())
        if 'word' not in body or 'word_locale' not in body or 'target_locale' not in body:
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
            return res.getresjson(), 400

        if (body['word_locale'] == 'en') or (body['target_locale'] == 'en'):
            result = None
            if body['word_locale'] == 'en':
                body['word'] = body['word'].lower()
                result = wordRepo.search_english(body['word'], body['target_locale'])
            else:
                result = wordRepo.search_vernacular(body['word'], body['word_locale'])
            if result == None and DICTIONARY_FALLBACK==True:
                translate   = GoogleTranslate()
                '''
                    - call google apis to get the translation
                    - save the translation
                    - return the response
                '''
                log_info('checking google for the searched word ({})'.format(body['word']), AppContext.getContext())

                input_word, translated_word, input_locale = translate.translate_text(body['target_locale'], body['word'])
                log_info('google returned input ({}), translated ({})'.format(input_word, translated_word), AppContext.getContext())
                if translated_word == None:
                    res = CustomResponse(Status.SUCCESS.value, None)
                    return res.getres()
                else:
                    if body['word_locale'] == 'en':
                        result = wordRepo.update(body['word'], 'en', translated_word, body['target_locale'])
                    else:
                        result = wordRepo.update(translated_word, body['target_locale'], body['word'], body['word_locale'])

                    if result == None:
                        res = CustomResponse(Status.SUCCESS.value, None)
                        return res.getres()
                    else:
                        res = CustomResponse(Status.SUCCESS.value, result)
                        return res.getres()
            else:
                log_info('returning word search from local database', AppContext.getContext())
                res = CustomResponse(Status.SUCCESS.value, result)
                return res.getres()
        else:
            res = CustomResponse(Status.ERR_ENGLISH_MANDATORY.value, None)
            return res.getresjson(), 400
Beispiel #3
0
 def get(self):
     log_info("FetchModelsResource api called", MODULE_CONTEXT)
     try:
         with open(config.FETCH_MODEL_CONFG) as f:
             confs = json.load(f)
             models = confs['data']
             out = CustomResponse(Status.SUCCESS.value, models)
         return out.getres()
     except Exception as e:
         log_exception("Error in FetchModelsResource: {}".format(e),
                       MODULE_CONTEXT, e)
         status = Status.SYSTEM_ERR.value
         status['why'] = str(e)
         out = CustomResponse(status, None)
         return out.getres()
Beispiel #4
0
    def post(self):
        body = request.get_json()
        user_id = request.headers.get('userid')
        if user_id == None:
            user_id = request.headers.get('x-user-id')

        pages = body['pages']
        file_locale = ''

        if 'file_locale' in body:
            file_locale = body['file_locale']

        job_id = ''
        if 'job_id' in body:
            job_id = body['job_id']

        record_id = None
        if 'record_id' in body:
            record_id = body['record_id']

        src_lang = None
        if 'src_lang' in body:
            src_lang = body['src_lang']
        tgt_lang = None
        if 'tgt_lang' in body:
            tgt_lang = body['tgt_lang']

        if 'pages' not in body or user_id is None or record_id == None or src_lang == None or tgt_lang == None:
            AppContext.addRecordID(record_id)
            log_info(
                'Missing params in FileContentSaveResource {}, user_id:{}'.
                format(body, user_id), AppContext.getContext())
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        AppContext.addRecordID(record_id)
        log_info(
            "FileContentSaveResource record_id ({}) for user ({})".format(
                record_id, user_id), AppContext.getContext())

        try:
            if fileContentRepo.store(user_id, file_locale, record_id, pages,
                                     src_lang, tgt_lang) == False:
                res = CustomResponse(
                    Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
                return res.getresjson(), 400
            AppContext.addRecordID(record_id)
            log_info(
                "FileContentSaveResource record_id ({}) for user ({}) saved".
                format(record_id, user_id), AppContext.getContext())
            res = CustomResponse(Status.SUCCESS.value, None)
            return res.getres()
        except Exception as e:
            AppContext.addRecordID(record_id)
            log_exception("FileContentSaveResource ", AppContext.getContext(),
                          e)
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400
Beispiel #5
0
    def post(self):
        body = request.get_json()

        if "keys" not in body or not body["keys"]:
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        keys = body["keys"]

        log_info("Fetching sentences from redis store",
                 AppContext.getContext())

        try:
            result = sentenceRepo.get_sentences_from_store(keys)
            if result == None:
                res = CustomResponse(
                    Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
                return res.getresjson(), 400

            res = CustomResponse(Status.SUCCESS.value, result)
            return res.getres()
        except Exception as e:
            log_exception(
                "Exception while fetching sentences from redis store ",
                AppContext.getContext(), e)
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400
Beispiel #6
0
    def post(self):
        body = request.json

        log_info('received request for WordSaveResource',
                 AppContext.getContext())
        if body == None:
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        if 'words' not in body:
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        for word in body['words']:
            if word['locale'] != 'en':
                res = CustomResponse(
                    Status.ERR_ENGLISH_MANDATORY_WHILE_SAVING.value, None)
                return res.getresjson(), 400

        result = wordRepo.store(body['words'])

        if result == False:
            res = CustomResponse(Status.ERR_SCHEMA_VALIDATION.value, None)
            return res.getresjson(), 400

        res = CustomResponse(Status.SUCCESS.value, None)
        return res.getres()
Beispiel #7
0
    def post(self):
        userID = request.headers.get('userID')
        if userID == None:
            userID = request.headers.get('x-user-id')
        body = request.get_json()

        if 'words' not in body and not body['words']:
            return post_error("Data Missing", "words are required", None), 400

        words = body['words']
        AppContext.adduserID(userID)
        log_info(
            "DigitalDocumentUpdateWordResource for user {}, number words to update {} request {}"
            .format(userID, len(words), body), AppContext.getContext())

        try:
            result = digitalRepo.update_words(userID, words)
            if result == True:
                res = CustomResponse(Status.SUCCESS.value, words)
                return res.getres()
            # return post_error("Data Missing","Failed to update word since data is missing",None), 400
            return result, 400

        except Exception as e:
            log_exception(
                "Exception in DigitalDocumentUpdateWordResource |{}".format(
                    str(e)), AppContext.getContext(), e)
            return post_error("Data Missing",
                              "Failed to update word since data is missing",
                              None), 400
Beispiel #8
0
    def post(self):
        body = request.get_json()

        if 'files' not in body or not body['files']:
            return post_error("Data Missing", "files is required", None), 400

        if 'recordID' not in body or not body['recordID']:
            return post_error("Data Missing", "recordID is required",
                              None), 400

        # if 'jobID' not in body or not body['jobID']:
        #     return post_error("Data Missing","jobID is required",None), 400

        files = body['files']
        userID = body['metadata']['userID']
        recordID = body['recordID']

        if not userID:
            return post_error("Data Missing", "userID is required", None), 400

            AppContext.addRecordID(recordID)
            log_info(
                'Missing params in DigitalDocumentSaveResource {}, user_id:{}, record_id:{}'
                .format(body, userID, recordID), AppContext.getContext())

        try:
            AppContext.addRecordID(recordID)
            log_info(
                'DigitalDocumentSaveResource request received, user_id:{}, record_id:{}'
                .format(userID, recordID), AppContext.getContext())

            result = digitalRepo.store(userID, recordID, files)
            if result == False:
                log_info(
                    'Missing params in DigitalDocumentSaveResource {}, user_id:{}, record_id:{}'
                    .format(body, userID, recordID), AppContext.getContext())
                return post_error("Data Missing",
                                  "Failed to store doc since data is missing",
                                  None), 400
            elif result is None:
                AppContext.addRecordID(recordID)
                log_info(
                    'DigitalDocumentSaveResource request completed, user_id:{}, record_id:{}'
                    .format(userID, recordID), AppContext.getContext())
                res = CustomResponse(Status.SUCCESS.value, None)
                return res.getres()
            else:
                log_info(
                    'Missing params in DigitalDocumentSaveResource {}, user_id:{}, record_id:{}'
                    .format(body, userID, recordID), AppContext.getContext())
                return result, 400
        except Exception as e:
            AppContext.addRecordID(recordID)
            log_exception(
                "Exception on save document | DigitalDocumentSaveResource :{}".
                format(str(e)), AppContext.getContext(), e)
            return post_error("Data Missing",
                              "Failed to store doc since data is missing",
                              None), 400
Beispiel #9
0
    def get(self):

        parser = reqparse.RequestParser()
        parser.add_argument(
            'start_page',
            type=int,
            location='args',
            help=
            'start_page can be 0, set start_page & end_page as 0 to get entire document',
            required=True)
        parser.add_argument(
            'end_page',
            type=int,
            location='args',
            help=
            'end_page can be 0, set start_page & end_page as 0 to get entire document',
            required=True)
        parser.add_argument('ad-userid',
                            location='headers',
                            type=str,
                            help='userid cannot be empty',
                            required=True)
        parser.add_argument('job_id',
                            type=str,
                            location='args',
                            help='Job Id is required',
                            required=False)
        parser.add_argument('record_id',
                            type=str,
                            location='args',
                            help='record_id is required',
                            required=True)

        args = parser.parse_args()
        AppContext.addRecordID(args['record_id'])
        log_info(
            "FileContentGetResource record_id {} for user {}".format(
                args['record_id'], args['ad-userid']), AppContext.getContext())

        try:
            result = fileContentRepo.get(args['ad-userid'], args['record_id'],
                                         args['start_page'], args['end_page'])
            if result == False:
                res = CustomResponse(
                    Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
                return res.getresjson(), 400
            log_info(
                "FileContentGetResource record_id {} for user {} has {} pages".
                format(args['record_id'], args['ad-userid'], result['total']),
                AppContext.getContext())
            res = CustomResponse(Status.SUCCESS.value, result['pages'],
                                 result['total'])
            return res.getres()
        except Exception as e:
            log_exception("FileContentGetResource ", AppContext.getContext(),
                          e)
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400
Beispiel #10
0
    def get(self):

        parser = reqparse.RequestParser()
        parser.add_argument(
            'start_page',
            type=int,
            location='args',
            help=
            'start_page can be 0, set start_page & end_page as 0 to get entire document',
            required=True)
        parser.add_argument(
            'end_page',
            type=int,
            location='args',
            help=
            'end_page can be 0, set start_page & end_page as 0 to get entire document',
            required=True)
        parser.add_argument('recordID',
                            type=str,
                            location='args',
                            help='record_id is required',
                            required=True)

        args = parser.parse_args()
        AppContext.addRecordID(args['recordID'])
        log_info(
            "DigitalDocumentGetResource record_id {} ".format(
                args['recordID']), AppContext.getContext())

        try:
            result = digitalRepo.get_pages(args['recordID'],
                                           args['start_page'],
                                           args['end_page'])
            if result == False:
                return post_error("Data Missing",
                                  "Failed to get pages since data is missing",
                                  None), 400

            AppContext.addRecordID(args['recordID'])
            log_info(
                "DigitalDocumentGetResource record_id {} has {} pages".format(
                    args['recordID'], result['total']),
                AppContext.getContext())
            res = CustomResponse(Status.SUCCESS.value, result['pages'],
                                 result['total'])
            return res.getres()

        except Exception as e:
            AppContext.addRecordID(args['recordID'])
            log_exception(
                "Exception in DigitalDocumentGetResource |{}".format(str(e)),
                AppContext.getContext(), e)
            return post_error("Data Missing",
                              "Failed to get pages since data is missing",
                              None), 400
Beispiel #11
0
    def post(self):
        body = request.get_json()
        user_id = request.headers.get('userid')
        if user_id == None:
            user_id = request.headers.get('x-user-id')

        if 'sentences' not in body or user_id is None or 'workflowCode' not in body:
            log_info(
                'Missing params in SaveSentenceResource {}, user_id:{}'.format(
                    body, user_id), AppContext.getContext())
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        sentences = body['sentences']
        workflowCode = body['workflowCode']

        AppContext.addRecordID(None)
        log_info(
            "SaveSentenceResource for user {}, number sentences to update {} request {}"
            .format(user_id, len(sentences), body), AppContext.getContext())

        try:
            result = sentenceRepo.update_sentences(user_id, sentences,
                                                   workflowCode)
            if result == False:
                res = CustomResponse(
                    Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
                return res.getresjson(), 400

            if USER_TRANSLATION_ENABLED:
                try:
                    result = sentenceRepo.save_sentences(user_id, sentences)
                except Exception as e:
                    log_exception("SaveSentenceResource",
                                  AppContext.getContext(), e)

            # sentence_ids = []
            # for sentence in sentences:
            #     sentence_ids.append(sentence['s_id'])

            # result  = sentenceRepo.get_sentence(user_id, sentence_ids)
            # if result == False:
            #     res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
            #     return res.getresjson(), 400
            # else:
            res = CustomResponse(Status.SUCCESS.value, sentences)
            return res.getres()

        except Exception as e:
            log_exception("SaveSentenceResource ", AppContext.getContext(), e)
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400
Beispiel #12
0
 def post(self):
     inputs = request.get_json(force=True)
     if len(inputs) > 0:
         log_info("Making interactive-model-convert API call",
                  MODULE_CONTEXT)
         out = ModelConvertService.model_conversion(inputs)
         return out.getres()
     else:
         log_info("null inputs in request in interactive-translation API",
                  MODULE_CONTEXT)
         out = CustomResponse(Status.INVALID_API_REQUEST.value, None)
         return out.getres()
Beispiel #13
0
 def post(self):
     inputs = request.get_json(force=True)
     if len(inputs)>0:
         log_info("Making v3/translate-anuvaad API call",MODULE_CONTEXT)
         log_info("inputs---{}".format(inputs),MODULE_CONTEXT)
         out = OpenNMTTranslateService.translate_func(inputs)
         log_info("Final output from v3/translate-anuvaad API: {}".format(out.getresjson()),MODULE_CONTEXT)
         return out.getres()
     else:
         log_info("null inputs in request in translate-anuvaad API",MODULE_CONTEXT)
         out = CustomResponse(Status.INVALID_API_REQUEST.value,None)
         return out.getres()             
Beispiel #14
0
    def post(self):
        body = request.get_json()
        user_id = request.headers.get('userid')
        if user_id == None:
            user_id = request.headers.get('x-user-id')

        modifiedSentences = None
        if 'modifiedSentences' in body:
            modifiedSentences = body['modifiedSentences']

        workflowCode = None
        record_id = None
        if 'blocks' not in body or user_id is None:
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        if 'workflowCode' in body:
            workflowCode = body['workflowCode']
        if 'record_id' in body:
            record_id = body['record_id']

        blocks = body['blocks']
        AppContext.addRecordID(record_id)
        log_info(
            "FileContentUpdateResource for user ({}), to update ({}) blocks".
            format(user_id, len(blocks)), AppContext.getContext())
        log_info(str(body), AppContext.getContext())
        try:
            result, updated_blocks = fileContentRepo.update(
                record_id, user_id, blocks, workflowCode, modifiedSentences)

            if result == False:
                res = CustomResponse(
                    Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
                return res.getresjson(), 400

            log_info(
                "FileContentUpdateResource for user ({}) updated".format(
                    user_id), AppContext.getContext())
            response = {'blocks': updated_blocks, 'workflowCode': workflowCode}
            res = CustomResponse(Status.SUCCESS.value, response,
                                 len(updated_blocks))
            return res.getres()
        except Exception as e:
            log_exception("FileContentUpdateResource ",
                          AppContext.getContext(), e)
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400
Beispiel #15
0
 def post(self):
     inputs = request.get_json(force=True)
     if len(inputs)>0:
         log_info("Making v2/interactive-translation API call",MODULE_CONTEXT)
         log_info("inputs---{}".format(inputs),MODULE_CONTEXT)
         # log_info(entry_exit_log(LOG_TAGS["input"],inputs))
         out = TranslateService.interactive_translation(inputs)
         log_info("out from v2/interactive-translation done: {}".format(out.getresjson()),MODULE_CONTEXT)
         # log_info(entry_exit_log(LOG_TAGS["output"],out))
         return out.getres()
     else:
         log_info("null inputs in request in v2/interactive-translation API",MODULE_CONTEXT)
         out = CustomResponse(Status.INVALID_API_REQUEST.value,None)
         return out.getres()        
Beispiel #16
0
 def post(self):
     inputs = request.get_json(force=True)
     if len(inputs)>0:
         log_info("Making interactive-translation API call",MODULE_CONTEXT)
         log_info("inputs---{}".format(inputs),MODULE_CONTEXT)
         # log_info(entry_exit_log(LOG_TAGS["input"],inputs))
         out = TranslateService.interactive_translation(inputs)
         out = out.getresjson()
         complete_response = out['response_body']
         out['response_body'] = [{"tgt": complete_response[i]['tgt'][0],"tagged_tgt":complete_response[i]['tagged_tgt'][0],
                                 "tagged_src":complete_response[i]['tagged_src'],"s_id":complete_response[i]['s_id'],
                                 "src":complete_response[i]["src"]}
                 for i in range(len(complete_response))]
         log_info("out from interactive-translation done: {}".format(out),MODULE_CONTEXT)
         # log_info(entry_exit_log(LOG_TAGS["output"],out))
         return CustomResponse.jsonify(out)
     else:
         log_info("null inputs in request in interactive-translation API",MODULE_CONTEXT)
         out = CustomResponse(Status.INVALID_API_REQUEST.value,None)
         return out.getres()
Beispiel #17
0
    def post(self):
        body = request.get_json()
        user_id = request.headers.get('userid')
        if user_id == None:
            user_id = request.headers.get('x-user-id')

        if 'record_ids' not in body or user_id is None:
            log_info(
                'Missing params in SentenceStatisticsCount {}, user_id:{}'.
                format(body, user_id), AppContext.getContext())
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        record_ids = body['record_ids']

        bleu_return = None
        if 'bleu_score' in body:
            bleu_return = body['bleu_score']
        else:
            bleu_return = False
        AppContext.addRecordID(None)
        log_info(
            "SentenceStatisticsCount for user {}, sentence count for record_ids {}"
            .format(user_id, record_ids), AppContext.getContext())

        try:
            result = sentenceRepo.get_sentences_counts(record_ids, bleu_return)
            if result == False:
                res = CustomResponse(
                    Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
                return res.getresjson(), 400

            res = CustomResponse(Status.SUCCESS.value, result)
            return res.getres()
        except Exception as e:
            log_exception("SentenceStatisticsCount ", AppContext.getContext(),
                          e)
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400
Beispiel #18
0
    def post(self):
        body = request.get_json()
        user_id = request.headers.get('userid')
        if user_id == None:
            user_id = request.headers.get('x-user-id')

        s_ids = None
        if 'sentences' in body:
            s_ids = body['sentences']

        if user_id is None or s_ids is None:
            log_info(
                'Missing params in FetchSentenceResource {}, user_id:{}'.
                format(body, user_id), AppContext.getContext())
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        AppContext.addRecordID(None)
        log_info(
            "FetchSentenceResource s_ids {} for user {}".format(
                len(s_ids), user_id), AppContext.getContext())

        try:
            result = sentenceRepo.get_sentence(user_id, s_ids)
            if result == False:
                res = CustomResponse(
                    Status.ERR_GLOBAL_MISSING_PARAMETERS.value, None)
                return res.getresjson(), 400

            res = CustomResponse(Status.SUCCESS.value, result)
            return res.getres()
        except Exception as e:
            log_exception("FetchSentenceResource ", AppContext.getContext(), e)
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400
Beispiel #19
0
 def get(self):
     log_info("NMT Health api called", MODULE_CONTEXT)
     out = CustomResponse(Status.SUCCESS.value, [])
     return out.getres()
Beispiel #20
0
    def post(self):
        body = request.json

        log_info('received request for WordSearch', AppContext.getContext())
        if 'word' not in body or 'word_locale' not in body or 'target_locale' not in body:
            res = CustomResponse(Status.ERR_GLOBAL_MISSING_PARAMETERS.value,
                                 None)
            return res.getresjson(), 400

        if (body['word_locale'] == 'en') or (body['target_locale'] == 'en'):
            result = None
            if body['word_locale'] == 'en':
                body['word'] = body['word'].lower()
                result = wordRepo.search_english(body['word'],
                                                 body['target_locale'])
            else:
                result = wordRepo.search_vernacular(body['word'],
                                                    body['word_locale'])

            if result == None:
                '''
                    - call google apis to get the translation
                    - save the translation
                    - return the response
                '''
                log_info(
                    'checking google for the searched word ({})'.format(
                        body['word']), AppContext.getContext())

                input_word, translated_word, input_locale = translate.translate_text(
                    body['target_locale'], body['word'])
                log_info(
                    'google returned input ({}), translated ({})'.format(
                        input_word, translated_word), AppContext.getContext())
                if translated_word == None:
                    res = CustomResponse(Status.SUCCESS.value, None)
                    return res.getres()
                else:
                    if body['word_locale'] == 'en':
                        result = wordRepo.update(body['word'], 'en',
                                                 translated_word,
                                                 body['target_locale'])
                    else:
                        result = wordRepo.update(translated_word,
                                                 body['target_locale'],
                                                 body['word'],
                                                 body['word_locale'])

                    if result == None:
                        res = CustomResponse(Status.SUCCESS.value, None)
                        return res.getres()
                    else:
                        res = CustomResponse(Status.SUCCESS.value, result)
                        return res.getres()
            else:
                log_info('returning word search from local database',
                         AppContext.getContext())
                res = CustomResponse(Status.SUCCESS.value, result)
                return res.getres()
        else:
            res = CustomResponse(Status.ERR_ENGLISH_MANDATORY.value, None)
            return res.getresjson(), 400
Beispiel #21
0
    def post(self):
        inputs = request.get_json(force=True)
        if len(inputs) > 0:
            log_info("Making performance check API call", MODULE_CONTEXT)
            try:
                for i in inputs:
                    if i['mode'] == 0:
                        avg_words_per_sec, target_array = BatchNMTPerformanceService.find_performance(i['input_txt_file'],\
                            i['model_id'],i['batch_size'])

                        output_file_name = os.path.basename(i['input_txt_file']).split(".")[0] + "_" + str(i['model_id']) + \
                            "_" + str(i['batch_size']) + "_" + "output" +".txt"
                        with open(
                                os.path.join(str(Path.home()),
                                             output_file_name), 'w') as f:
                            for sentence in target_array:
                                f.write("%s\n" % sentence)

                        out = {}
                        out['response_body'] = {
                            "words_per_sec": avg_words_per_sec
                        }
                        out = CustomResponse(Status.SUCCESS.value,
                                             out['response_body'])
                        log_info(
                            "out from performance check done: {}".format(
                                out.getresjson()), MODULE_CONTEXT)

                        return out.getres()

                    elif i['mode'] == 1:
                        time_taken_array = BatchNMTPerformanceService.find_performance_pipeline(i['input_txt_file'],\
                            i['model_id'],i['batch_size'],i['max_batch_size'],i['batch_type'])

                        out = {}
                        out['response_body'] = {"avg_time_loading_per_word": time_taken_array[0] ,\
                                                "avg_time_preprocessing_per_word": time_taken_array[1] ,\
                                                "avg_time_tokenizing_per_word": time_taken_array[2] ,\
                                                "avg_time_encoding_per_word": time_taken_array[3] ,\
                                                "avg_time_translating_per_word": time_taken_array[4] ,\
                                                "avg_time_decoding_per_word": time_taken_array[5] ,\
                                                "avg_time_detokenizing_per_word": time_taken_array[6] ,\
                                                "avg_time_postprocessing_per_word": time_taken_array[7] }
                        out = CustomResponse(Status.SUCCESS.value,
                                             out['response_body'])
                        log_info(
                            "out from performance check done: {}".format(
                                out.getresjson()), MODULE_CONTEXT)

                        return out.getres()

            except Exception as e:
                status = Status.SYSTEM_ERR.value
                status['why'] = str(e)
                out = CustomResponse(status, [])

                return out.getres()
        else:
            log_info("null inputs in request in /v0/performance API",
                     MODULE_CONTEXT)
            out = CustomResponse(Status.INVALID_API_REQUEST.value, None)

            return out.getres()