Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

mutox_speech.py 3.7 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
  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. # MIT_LICENSE file in the root directory of this source tree.
  6. import argparse
  7. import torch
  8. from tqdm import tqdm
  9. from pathlib import Path
  10. from sonar.inference_pipelines.speech import (
  11. SpeechInferenceParams,
  12. )
  13. from seamless_communication.toxicity.mutox.speech_pipeline import (
  14. MutoxSpeechClassifierPipeline,
  15. )
  16. import logging
  17. logging.basicConfig(
  18. level=logging.INFO,
  19. format="%(asctime)s %(levelname)s -- %(name)s: %(message)s",
  20. )
  21. logger = logging.getLogger(__name__)
  22. def main() -> None:
  23. parser = argparse.ArgumentParser(
  24. description="Mutox speech will compute a toxicity score for each speech segment it is provided."
  25. )
  26. parser.add_argument(
  27. "data_file",
  28. type=Path,
  29. help="Path to the input TSV manifest that list the audio files.",
  30. )
  31. parser.add_argument(
  32. "output_file",
  33. type=Path,
  34. help="Path to a TSV file where to save the results.",
  35. )
  36. parser.add_argument(
  37. "--lang",
  38. type=str,
  39. help="Language, language of the speech being passed as input, three letter code",
  40. required=True,
  41. )
  42. parser.add_argument(
  43. "--audio_root_dir",
  44. type=str,
  45. help="Root directory for the audio filenames in the data file.",
  46. )
  47. parser.add_argument(
  48. "--audio_path_index",
  49. type=int,
  50. help="Index of the column where the audiofile is listed in the input tsv.",
  51. default="audio",
  52. )
  53. parser.add_argument(
  54. "--batch_size",
  55. type=int,
  56. help="Inference batch size.",
  57. default=4,
  58. )
  59. parser.add_argument(
  60. "--n_parallel",
  61. type=int,
  62. help="Number of data loading in parallel.",
  63. default=4,
  64. )
  65. parser.add_argument(
  66. "--device",
  67. type=str,
  68. help="name of the device to use with torch.",
  69. required=False,
  70. )
  71. args, _unknown = parser.parse_known_args()
  72. if args.device is not None:
  73. device = torch.device(args.device)
  74. dtype = torch.float32
  75. if device.type == "cuda":
  76. dtype = torch.float16
  77. elif torch.cuda.is_available():
  78. device = torch.device("cuda:0")
  79. dtype = torch.float16
  80. logger.info("using cuda:0, %s", dtype)
  81. else:
  82. device = torch.device("cpu")
  83. dtype = torch.float32
  84. logger.info("no gpu, using cpu")
  85. logger.info("loading models.")
  86. pipeline_builder = MutoxSpeechClassifierPipeline.load_model_from_name(
  87. mutox_classifier_name="mutox",
  88. encoder_name=f"sonar_speech_encoder_{args.lang}",
  89. device=device,
  90. )
  91. pipeline = pipeline_builder.build_pipeline(
  92. SpeechInferenceParams(
  93. data_file=args.data_file,
  94. audio_root_dir=args.audio_root_dir,
  95. audio_path_index=args.audio_path_index,
  96. target_lang=args.lang,
  97. batch_size=args.batch_size,
  98. pad_idx=0,
  99. device=device,
  100. fbank_dtype=torch.float32,
  101. n_parallel=args.n_parallel,
  102. )
  103. )
  104. logger.info("processing.")
  105. with open(args.output_file, "w", encoding="utf-8") as outf:
  106. print(
  107. "input_audio_path",
  108. "score",
  109. sep="\t",
  110. file=outf,
  111. )
  112. for example in tqdm(pipeline):
  113. ex = example["audio"]
  114. for idx, path in enumerate(ex["path"]):
  115. print(
  116. str(path),
  117. ex["data"][idx].item(),
  118. sep="\t",
  119. file=outf,
  120. )
  121. logger.info(f"Done, outputs are in {args.output_file}.")
  122. if __name__ == "__main__":
  123. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...