test_translator.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. # Copyright (c) Meta Platforms, Inc. and affiliates
  2. # All rights reserved.
  3. #
  4. # This source code is licensed under the license found in the
  5. # LICENSE file in the root directory of this source tree.
  6. from typing import Final
  7. from seamless_communication.inference import Translator
  8. from tests.common import device, get_default_dtype
  9. # fmt: off
  10. ENG_SENTENCE: Final = "On Monday, scientists from the Stanford University School of Medicine announced the invention of a new diagnostic tool that can sort cells by type: a tiny printable chip that can be manufactured using standard inkjet printers for possibly about one U.S. cent each."
  11. DEU_SENTENCE: Final = "Am Montag kündigten Wissenschaftler der Stanford University School of Medicine die Erfindung eines neuen Diagnosewerkzeugs an, das Zellen nach Typ sortieren kann: ein winziger druckbarer Chip, der mit Standard-Tintenstrahldruckern für etwa einen US-Cent hergestellt werden kann."
  12. DEU_SENTENCE_V2: Final = "Am Montag kündigten Wissenschaftler der Stanford University School of Medicine die Erfindung eines neuen diagnostischen Werkzeugs an, das Zellen nach Typ sortieren kann: ein winziger druckbarer Chip, der mit Standard-Tintenstrahldrucker für möglicherweise etwa einen US-Cent pro Stück hergestellt werden kann."
  13. # fmt: on
  14. def test_seamless_m4t_large_t2tt() -> None:
  15. model_name = "seamlessM4T_large"
  16. src_lang = "eng"
  17. tgt_lang = "deu"
  18. dtype = get_default_dtype()
  19. translator = Translator(model_name, "vocoder_36langs", device, dtype=dtype)
  20. text_output, _ = translator.predict(
  21. ENG_SENTENCE,
  22. "t2tt",
  23. tgt_lang,
  24. src_lang=src_lang,
  25. )
  26. assert text_output[0] == DEU_SENTENCE, f"'{text_output[0]}' is not '{DEU_SENTENCE}'"
  27. def test_seamless_m4t_v2_large_t2tt() -> None:
  28. model_name = "seamlessM4T_v2_large"
  29. src_lang = "eng"
  30. tgt_lang = "deu"
  31. dtype = get_default_dtype()
  32. translator = Translator(model_name, "vocoder_v2", device, dtype=dtype)
  33. text_output, _ = translator.predict(
  34. ENG_SENTENCE,
  35. "t2tt",
  36. tgt_lang,
  37. src_lang=src_lang,
  38. )
  39. assert (
  40. text_output[0] == DEU_SENTENCE_V2
  41. ), f"'{text_output[0]}' is not '{DEU_SENTENCE_V2}'"
  42. def test_seamless_m4t_v2_large_multiple_tasks() -> None:
  43. model_name = "seamlessM4T_v2_large"
  44. english_text = "Hello! I hope you're all doing well."
  45. ref_spanish_text = "Hola, espero que todos estéis haciendo bien."
  46. ref_spanish_asr_text = "Hola, espero que todos estéis haciendo bien."
  47. dtype = get_default_dtype()
  48. translator = Translator(model_name, "vocoder_v2", device, dtype=dtype)
  49. # Generate english speech for the english text.
  50. _, english_speech_output = translator.predict(
  51. english_text,
  52. "t2st",
  53. "eng",
  54. src_lang="eng",
  55. )
  56. assert english_speech_output is not None
  57. # Translate english speech to spanish speech.
  58. spanish_text_output, spanish_speech_output = translator.predict(
  59. english_speech_output.audio_wavs[0][0],
  60. "s2st",
  61. "spa",
  62. )
  63. assert spanish_speech_output is not None
  64. assert (
  65. spanish_text_output[0] == ref_spanish_text
  66. ), f"'{spanish_text_output[0]}' is not '{ref_spanish_text}'"
  67. # Run ASR on the spanish speech.
  68. spanish_asr_text_output, _ = translator.predict(
  69. spanish_speech_output.audio_wavs[0][0],
  70. "asr",
  71. "spa",
  72. )
  73. assert (
  74. spanish_asr_text_output[0] == ref_spanish_asr_text
  75. ), f"{spanish_asr_text_output[0]} is not {ref_spanish_asr_text}'"