def val_step(input_ids, target_ids_, step, write_output_seq): dec_padding_mask = create_padding_mask(input_ids) (draft_predictions, _, refine_predictions, _) = Model.predict(input_ids, dec_padding_mask, False) if config.use_refine_decoder: predictions = refine_predictions else: predictions = draft_predictions rouge, bert = tf_write_output_sequence(target_ids_[:, 1:], predictions[:, 1:], step, write_output_seq) return (rouge, bert)
def translate(): en_input = input('Enter the english sentence-> ') en_input = preprocess(en_input) input_CLS_ID = source_tokenizer.vocab_size input_SEP_ID = source_tokenizer.vocab_size + 1 target_CLS_ID = target_tokenizer.vocab_size target_SEP_ID = target_tokenizer.vocab_size + 1 input_ids = tf.convert_to_tensor( [[input_CLS_ID] + source_tokenizer.encode(en_input) + [input_SEP_ID]]) dec_padding_mask = create_padding_mask(input_ids) start = time.time() preds_draft_summary, _, _, _ = Model.predict(input_ids, dec_padding_mask) translated_sequence = target_tokenizer.decode([ i for i in tf.squeeze(preds_draft_summary) if i not in [target_CLS_ID, target_SEP_ID, config.PAD_ID] ]) print( f'Translated output --> {translated_sequence if translated_sequence else "EMPTY"}' ) print(f'Time taken --> {round(time.time()-start)} seconds')
def generate(): en_input = input('Enter the sentence-> ') en_input = preprocess(en_input) input_ids = tf.constant(source_tokenizer.encode(en_input))[None, :] dec_padding_mask = create_padding_mask(input_ids) start = time.time() (preds_draft_summary, _, preds_refine_summary, _, _) = Model.predict(input_ids, batch_size=1, draft_decoder_type='topktopp', beam_size=10, length_penalty=0.6, temperature=1, top_p=0.9, top_k=25) generated_sequence = target_tokenizer.decode( tf.squeeze(preds_refine_summary), skip_special_tokens=True) print( f'Translated output--> {generated_sequence if generated_sequence else "EMPTY"}' ) print(f'Time to process --> {round(time.time()-start)} seconds')