from typing import Sequence


def wordpiece_tokens_to_normalized_text(wordpiece_tokens: Sequence[str]) -> str:
  """Concatenates wordpiece tokens to a normalized text and cleans up.

  The wordpiece tokens are results from BERT tokenization. They may contain
  symbols of '##' or ' ##' and some extra whitespaces. The function first
  concatenate the tokens and then removes those extrac symbols and whitespaces.

  Args:
    wordpiece_tokens: A sequence of wordpiece tokens from BERT tokenization.

  Returns:
    The text by concatenating the wordpiece tokens and cleaning up.
  """
  text = " ".join(wordpiece_tokens)

  # De-tokenize WordPieces that have been split off.
  text = text.replace(" ##", "")
  text = text.replace("##", "")

  # Clean whitespace
  text = text.strip()
  text = " ".join(text.split())
  return text