| | |
| | | from typing import Any |
| | | from typing import List |
| | | from typing import Tuple |
| | | from typing import Optional |
| | | import numpy as np |
| | | import torch.nn.functional as F |
| | | |
| | | from funasr.models.transformer.utils.nets_utils import make_pad_mask |
| | | from funasr.train_utils.device_funcs import force_gatherable |
| | | from funasr.train_utils.device_funcs import to_device |
| | | import torch |
| | | import torch.nn as nn |
| | | from funasr.models.ct_transformer.utils import split_to_mini_sentence, split_words |
| | | |
| | | from funasr.utils.register import register_class, registry_tables |
| | | from funasr.register import tables |
| | | |
| | | @register_class("model_classes", "CTTransformer") |
| | | @tables.register("model_classes", "CTTransformer") |
| | | class CTTransformer(nn.Module): |
| | | """ |
| | | Author: Speech Lab of DAMO Academy, Alibaba Group |
| | |
| | | def __init__( |
| | | self, |
| | | encoder: str = None, |
| | | encoder_conf: str = None, |
| | | encoder_conf: dict = None, |
| | | vocab_size: int = -1, |
| | | punc_list: list = None, |
| | | punc_weight: list = None, |
| | |
| | | ignore_id: int = -1, |
| | | sos: int = 1, |
| | | eos: int = 2, |
| | | sentence_end_id: int = 3, |
| | | **kwargs, |
| | | ): |
| | | super().__init__() |
| | |
| | | |
| | | |
| | | self.embed = nn.Embedding(vocab_size, embed_unit) |
| | | encoder_class = registry_tables.encoder_classes.get(encoder.lower()) |
| | | encoder_class = tables.encoder_classes.get(encoder.lower()) |
| | | encoder = encoder_class(**encoder_conf) |
| | | |
| | | self.decoder = nn.Linear(att_unit, punc_size) |
| | |
| | | self.ignore_id = ignore_id |
| | | self.sos = sos |
| | | self.eos = eos |
| | | self.sentence_end_id = sentence_end_id |
| | | |
| | | |
| | | |
| | | def punc_forward(self, input: torch.Tensor, text_lengths: torch.Tensor) -> Tuple[torch.Tensor, None]: |
| | | def punc_forward(self, text: torch.Tensor, text_lengths: torch.Tensor) -> Tuple[torch.Tensor, None]: |
| | | """Compute loss value from buffer sequences. |
| | | |
| | | Args: |
| | |
| | | hidden (torch.Tensor): Target ids. (batch, len) |
| | | |
| | | """ |
| | | x = self.embed(input) |
| | | x = self.embed(text) |
| | | # mask = self._target_mask(input) |
| | | h, _, _ = self.encoder(x, text_lengths) |
| | | y = self.decoder(h) |
| | |
| | | punc_lengths: torch.Tensor, |
| | | vad_indexes: Optional[torch.Tensor] = None, |
| | | vad_indexes_lengths: Optional[torch.Tensor] = None, |
| | | ) -> Tuple[torch.Tensor, Dict[str, torch.Tensor], torch.Tensor]: |
| | | ): |
| | | nll, y_lengths = self.nll(text, punc, text_lengths, punc_lengths, vad_indexes=vad_indexes) |
| | | ntokens = y_lengths.sum() |
| | | loss = nll.sum() / ntokens |
| | |
| | | return loss, stats, weight |
| | | |
| | | def generate(self, |
| | | text: torch.Tensor, |
| | | text_lengths: torch.Tensor, |
| | | vad_indexes: Optional[torch.Tensor] = None) -> Tuple[torch.Tensor, None]: |
| | | if self.with_vad(): |
| | | assert vad_indexes is not None |
| | | return self.punc_forward(text, text_lengths, vad_indexes) |
| | | else: |
| | | return self.punc_forward(text, text_lengths) |
| | | data_in, |
| | | data_lengths=None, |
| | | key: list = None, |
| | | tokenizer=None, |
| | | frontend=None, |
| | | **kwargs, |
| | | ): |
| | | assert len(data_in) == 1 |
| | | |
| | | vad_indexes = kwargs.get("vad_indexes", None) |
| | | text = data_in[0] |
| | | text_lengths = data_lengths[0] if data_lengths is not None else None |
| | | split_size = kwargs.get("split_size", 20) |
| | | |
| | | tokens = split_words(text) |
| | | tokens_int = tokenizer.encode(tokens) |
| | | |
| | | mini_sentences = split_to_mini_sentence(tokens, split_size) |
| | | mini_sentences_id = split_to_mini_sentence(tokens_int, split_size) |
| | | assert len(mini_sentences) == len(mini_sentences_id) |
| | | cache_sent = [] |
| | | cache_sent_id = torch.from_numpy(np.array([], dtype='int32')) |
| | | new_mini_sentence = "" |
| | | new_mini_sentence_punc = [] |
| | | cache_pop_trigger_limit = 200 |
| | | results = [] |
| | | meta_data = {} |
| | | for mini_sentence_i in range(len(mini_sentences)): |
| | | mini_sentence = mini_sentences[mini_sentence_i] |
| | | mini_sentence_id = mini_sentences_id[mini_sentence_i] |
| | | mini_sentence = cache_sent + mini_sentence |
| | | mini_sentence_id = np.concatenate((cache_sent_id, mini_sentence_id), axis=0) |
| | | data = { |
| | | "text": torch.unsqueeze(torch.from_numpy(mini_sentence_id), 0), |
| | | "text_lengths": torch.from_numpy(np.array([len(mini_sentence_id)], dtype='int32')), |
| | | } |
| | | data = to_device(data, kwargs["device"]) |
| | | # y, _ = self.wrapped_model(**data) |
| | | y, _ = self.punc_forward(**data) |
| | | _, indices = y.view(-1, y.shape[-1]).topk(1, dim=1) |
| | | punctuations = indices |
| | | if indices.size()[0] != 1: |
| | | punctuations = torch.squeeze(indices) |
| | | assert punctuations.size()[0] == len(mini_sentence) |
| | | |
| | | # Search for the last Period/QuestionMark as cache |
| | | if mini_sentence_i < len(mini_sentences) - 1: |
| | | sentenceEnd = -1 |
| | | last_comma_index = -1 |
| | | for i in range(len(punctuations) - 2, 1, -1): |
| | | if self.punc_list[punctuations[i]] == "。" or self.punc_list[punctuations[i]] == "?": |
| | | sentenceEnd = i |
| | | break |
| | | if last_comma_index < 0 and self.punc_list[punctuations[i]] == ",": |
| | | last_comma_index = i |
| | | |
| | | if sentenceEnd < 0 and len(mini_sentence) > cache_pop_trigger_limit and last_comma_index >= 0: |
| | | # The sentence it too long, cut off at a comma. |
| | | sentenceEnd = last_comma_index |
| | | punctuations[sentenceEnd] = self.sentence_end_id |
| | | cache_sent = mini_sentence[sentenceEnd + 1:] |
| | | cache_sent_id = mini_sentence_id[sentenceEnd + 1:] |
| | | mini_sentence = mini_sentence[0:sentenceEnd + 1] |
| | | punctuations = punctuations[0:sentenceEnd + 1] |
| | | |
| | | # if len(punctuations) == 0: |
| | | # continue |
| | | |
| | | punctuations_np = punctuations.cpu().numpy() |
| | | new_mini_sentence_punc += [int(x) for x in punctuations_np] |
| | | words_with_punc = [] |
| | | for i in range(len(mini_sentence)): |
| | | if (i==0 or self.punc_list[punctuations[i-1]] == "。" or self.punc_list[punctuations[i-1]] == "?") and len(mini_sentence[i][0].encode()) == 1: |
| | | mini_sentence[i] = mini_sentence[i].capitalize() |
| | | if i == 0: |
| | | if len(mini_sentence[i][0].encode()) == 1: |
| | | mini_sentence[i] = " " + mini_sentence[i] |
| | | if i > 0: |
| | | if len(mini_sentence[i][0].encode()) == 1 and len(mini_sentence[i - 1][0].encode()) == 1: |
| | | mini_sentence[i] = " " + mini_sentence[i] |
| | | words_with_punc.append(mini_sentence[i]) |
| | | if self.punc_list[punctuations[i]] != "_": |
| | | punc_res = self.punc_list[punctuations[i]] |
| | | if len(mini_sentence[i][0].encode()) == 1: |
| | | if punc_res == ",": |
| | | punc_res = "," |
| | | elif punc_res == "。": |
| | | punc_res = "." |
| | | elif punc_res == "?": |
| | | punc_res = "?" |
| | | words_with_punc.append(punc_res) |
| | | new_mini_sentence += "".join(words_with_punc) |
| | | # Add Period for the end of the sentence |
| | | new_mini_sentence_out = new_mini_sentence |
| | | new_mini_sentence_punc_out = new_mini_sentence_punc |
| | | if mini_sentence_i == len(mini_sentences) - 1: |
| | | if new_mini_sentence[-1] == "," or new_mini_sentence[-1] == "、": |
| | | new_mini_sentence_out = new_mini_sentence[:-1] + "。" |
| | | new_mini_sentence_punc_out = new_mini_sentence_punc[:-1] + [self.sentence_end_id] |
| | | elif new_mini_sentence[-1] == ",": |
| | | new_mini_sentence_out = new_mini_sentence[:-1] + "." |
| | | new_mini_sentence_punc_out = new_mini_sentence_punc[:-1] + [self.sentence_end_id] |
| | | elif new_mini_sentence[-1] != "。" and new_mini_sentence[-1] != "?" and len(new_mini_sentence[-1].encode())==0: |
| | | new_mini_sentence_out = new_mini_sentence + "。" |
| | | new_mini_sentence_punc_out = new_mini_sentence_punc[:-1] + [self.sentence_end_id] |
| | | elif new_mini_sentence[-1] != "." and new_mini_sentence[-1] != "?" and len(new_mini_sentence[-1].encode())==1: |
| | | new_mini_sentence_out = new_mini_sentence + "." |
| | | new_mini_sentence_punc_out = new_mini_sentence_punc[:-1] + [self.sentence_end_id] |
| | | |
| | | result_i = {"key": key[0], "text": new_mini_sentence_out} |
| | | results.append(result_i) |
| | | |
| | | return results, meta_data |
| | | |