test_translator.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. import torch
  8. from fairseq2.typing import Device
  9. from seamless_communication.inference import Translator
  10. from tests.common import device, get_default_dtype
  11. # fmt: off
  12. 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."
  13. 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."
  14. 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."
  15. # fmt: on
  16. def test_seamless_m4t_large_t2tt() -> None:
  17. model_name = "seamlessM4T_large"
  18. src_lang = "eng"
  19. tgt_lang = "deu"
  20. dtype = get_default_dtype()
  21. translator = Translator(model_name, "vocoder_36langs", device, dtype=dtype)
  22. text_output, _ = translator.predict(
  23. ENG_SENTENCE,
  24. "t2tt",
  25. tgt_lang,
  26. src_lang=src_lang,
  27. )
  28. assert text_output[0] == DEU_SENTENCE, f"'{text_output[0]}' is not '{DEU_SENTENCE}'"
  29. def test_seamless_m4t_v2_large_t2tt() -> None:
  30. model_name = "seamlessM4T_v2_large"
  31. src_lang = "eng"
  32. tgt_lang = "deu"
  33. dtype = get_default_dtype()
  34. translator = Translator(model_name, "vocoder_v2", device, dtype=dtype)
  35. text_output, _ = translator.predict(
  36. ENG_SENTENCE,
  37. "t2tt",
  38. tgt_lang,
  39. src_lang=src_lang,
  40. )
  41. assert (
  42. text_output[0] == DEU_SENTENCE_V2
  43. ), f"'{text_output[0]}' is not '{DEU_SENTENCE_V2}'"
  44. def test_seamless_m4t_v2_large_multiple_tasks() -> None:
  45. model_name = "seamlessM4T_v2_large"
  46. english_text = "Hello! I hope you're all doing well."
  47. ref_spanish_text = "Hola, espero que todos estéis haciendo bien."
  48. ref_spanish_asr_text = "Hola, espero que todos estéis haciendo bien."
  49. dtype = get_default_dtype()
  50. translator = Translator(model_name, "vocoder_v2", device, dtype=dtype)
  51. # Generate english speech for the english text.
  52. _, english_speech_output = translator.predict(
  53. english_text,
  54. "t2st",
  55. "eng",
  56. src_lang="eng",
  57. )
  58. assert english_speech_output is not None
  59. # Translate english speech to spanish speech.
  60. spanish_text_output, spanish_speech_output = translator.predict(
  61. english_speech_output.audio_wavs[0][0],
  62. "s2st",
  63. "spa",
  64. )
  65. assert spanish_speech_output is not None
  66. assert (
  67. spanish_text_output[0] == ref_spanish_text
  68. ), f"'{spanish_text_output[0]}' is not '{ref_spanish_text}'"
  69. # Run ASR on the spanish speech.
  70. spanish_asr_text_output, _ = translator.predict(
  71. spanish_speech_output.audio_wavs[0][0],
  72. "asr",
  73. "spa",
  74. )
  75. assert (
  76. spanish_asr_text_output[0] == ref_spanish_asr_text
  77. ), f"{spanish_asr_text_output[0]} is not {ref_spanish_asr_text}'"