unity.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. #include "ggml/ggml.h"
  2. #include "ggml/ggml-alloc.h"
  3. #include "math.h"
  4. #include "model_loader.h"
  5. #include "fairseq2.h"
  6. #include "lib/unity_lib.h"
  7. #include <sndfile.h>
  8. #include <cstdlib>
  9. #include "ggml-alloc.h"
  10. #include <numeric>
  11. #include <algorithm>
  12. struct unity_params {
  13. int32_t n_threads = std::min(4, (int32_t) std::thread::hardware_concurrency());
  14. std::string model = "seamlessM4T_medium.ggml"; // model path
  15. bool text = false;
  16. SequenceGeneratorOptions opts = {
  17. /*beam_size*/ 5,
  18. /*min_seq_len*/ 1,
  19. /*soft_max_seq_len_a*/ 1,
  20. /*soft_max_seq_len_b*/ 200,
  21. /*hard_max_seq_len*/ 1000,
  22. /*len_penalty*/ 1.0,
  23. /*unk_penalty*/ 0.0,
  24. /*normalize_scores*/ true,
  25. /*mem_mb*/ 512
  26. };
  27. int32_t max_audio_s = 30;
  28. bool verbose = false;
  29. };
  30. void unity_print_usage(int /*argc*/, char ** argv, const unity_params & params) {
  31. fprintf(stderr, "usage: %s [options] file1 file2 ...\n", argv[0]);
  32. fprintf(stderr, "\n");
  33. fprintf(stderr, "options:\n");
  34. fprintf(stderr, " -h, --help show this help message and exit\n");
  35. fprintf(stderr, " -i, --input Input text for the text-2-text translation\n");
  36. fprintf(stderr, " -l, --tgt-lang Target translation lang (default: %s\n", params.tgt_lang);
  37. fprintf(stderr, " -t N, --threads N number of threads to use during computation (default: %d)\n", params.n_threads);
  38. fprintf(stderr, " -v, --verbose Print out word level confidence score and LID score (default: off)");
  39. fprintf(stderr, " -m FNAME, --model FNAME\n");
  40. fprintf(stderr, " model path (default: %s)\n", params.model.c_str());
  41. fprintf(stderr, " --text text-to-text translation (default is speech-to-text without this option on)\n");
  42. fprintf(stderr, " --beam-size beam size (default: %d)\n", params.opts.beam_size);
  43. fprintf(stderr, " -M, --mem memory buffer, increase for long inputs (default: %d)\n", params.opts.mem_mb);
  44. fprintf(stderr, " --max-audio max duration of audio in seconds (default: %d)\n", params.max_audio_s);
  45. fprintf(stderr, "\n");
  46. }
  47. std::string get_next_arg(int& i, int argc, char** argv, const std::string& flag, unity_params& params) {
  48. if (i + 1 < argc && argv[i + 1][0] != '-') {
  49. return argv[++i];
  50. } else {
  51. fprintf(stderr, "error: %s requires one argument.\n", flag.c_str());
  52. unity_print_usage(argc, argv, params);
  53. exit(0);
  54. }
  55. }
  56. bool unity_params_parse(int argc, char ** argv, unity_params & params) {
  57. for (int i = 1; i < argc; i++) {
  58. std::string arg = argv[i];
  59. if (arg == "-h" || arg == "--help") {
  60. unity_print_usage(argc, argv, params);
  61. } else if (arg == "-t" || arg == "--threads") {
  62. params.n_threads = std::stoi(get_next_arg(i, argc, argv, arg, params));
  63. } else if (arg == "-m" || arg == "--model") {
  64. params.model = get_next_arg(i, argc, argv, arg, params);
  65. } else if (arg == "--text") {
  66. params.text = true;
  67. } else if (arg == "-b" || arg == "--beam-size") {
  68. params.opts.beam_size = std::stoi(get_next_arg(i, argc, argv, arg, params));
  69. } else if (arg == "-v" || arg == "--verbose") {
  70. params.verbose = true;
  71. } else if (arg == "-M" || arg == "--mem") {
  72. params.opts.mem_mb = std::stoi(get_next_arg(i, argc, argv, arg, params));
  73. } else if (arg == "--max-audio") {
  74. params.max_audio_s = std::stoi(get_next_arg(i, argc, argv, arg, params));
  75. }
  76. }
  77. return true;
  78. }
  79. int main(int argc, char ** argv) {
  80. unity_params params;
  81. if (unity_params_parse(argc, argv, params) == false) {
  82. return 1;
  83. }
  84. fairseq2_model model;
  85. // load the model
  86. if (load_fairseq2_ggml_file(model, params.model.c_str())) {
  87. fprintf(stderr, "%s: failed to load model from '%s'\n", __func__, params.model.c_str());
  88. return 1;
  89. }
  90. // The ctx_size_mb mostly depends of input length and model dim.
  91. int ctx_size_mb = params.opts.mem_mb;
  92. auto encoder_buf = std::vector<uint8_t>(8 * 1024 * 1024); // Only tensor metadata goes in there
  93. auto encoder_fwd_buf = std::vector<uint8_t>(ctx_size_mb * 1024 * 1024 / 2);
  94. while (true) {
  95. // S2ST
  96. if (!params.text) {
  97. std::string input;
  98. std::cout << "\nEnter audio_path and tgt_lang, separated by space (or 'exit' to quit):\n";
  99. std::getline(std::cin, input);
  100. if (input == "exit") {
  101. break;
  102. }
  103. std::istringstream iss(input);
  104. std::string audio_path;
  105. std::string tgt_lang;
  106. iss >> audio_path >> tgt_lang;
  107. if (audio_path == "-") {
  108. audio_path = "/proc/self/fd/0";
  109. }
  110. std::cerr << "Translating (Transcribing) " << audio_path << " to " << tgt_lang << "\n";
  111. SF_INFO info;
  112. SNDFILE* sndfile = sf_open(audio_path.c_str(), SFM_READ, &info);
  113. if (!sndfile) {
  114. std::cerr << "Could not open file\n";
  115. continue;
  116. }
  117. // Load audio input
  118. GGML_ASSERT(info.samplerate == 16000);
  119. GGML_ASSERT(info.channels == 1);
  120. // Truncate audio input. Ideally we should chunk it, but this will prevent most obvious OOM.
  121. int n_frames = std::min(info.samplerate * params.max_audio_s, (int)info.frames);
  122. std::vector<float> data(n_frames * info.channels);
  123. sf_readf_float(sndfile, data.data(), n_frames);
  124. Result result = unity_eval_speech(model, data, params.opts, tgt_lang, params.n_threads);
  125. std::string concat_transcription = std::accumulate(std::next(result.transcription.begin()), result.transcription.end(), result.transcription[0],
  126. [](const std::string& a, const std::string& b) {
  127. return a + " " + b;
  128. }
  129. );
  130. if (params.verbose) {
  131. std::cout << "Final transcription: " << concat_transcription << std::endl;
  132. std::cout << std::endl;
  133. std::cout << "Word level confidence score:" << std::endl;
  134. for (size_t i = 0; i < result.transcription.size(); ++i) {
  135. std::cout << "Word: " << result.transcription[i] << " | Score: " << result.word_confidence_scores[i] << std::endl;
  136. }
  137. std::cout << std::endl;
  138. std::cout << "LID scores: " << std::endl;
  139. for (const auto& kv : result.lid_scores) {
  140. std::cout << "Language: " << kv.first << "| Score: " << kv.second << std::endl;
  141. }
  142. } else {
  143. std::cout << concat_transcription << std::endl;
  144. }
  145. // T2TT
  146. } else {
  147. std::string line;
  148. std::string input_text;
  149. std::string tgt_lang;
  150. std::cout << "\nEnter input_text and tgt_lang, separated by space (or 'exit' to quit):\n";
  151. if (std::getline(std::cin, line)) {
  152. std::size_t last_space = line.find_last_of(' ');
  153. if (last_space != std::string::npos) {
  154. input_text = line.substr(0, last_space);
  155. tgt_lang = line.substr(last_space + 1);
  156. std::cerr << "Translating \"" << input_text << "\" to " << tgt_lang << "\n";
  157. } else {
  158. std::cout << "No spaces found in the input. \n";
  159. }
  160. }
  161. // tokenize the input text
  162. Result result = unity_eval_text(model, input_text, params.opts, tgt_lang, params.n_threads);
  163. std::string concat_translation = std::accumulate(std::next(result.transcription.begin()), result.transcription.end(), result.transcription[0],
  164. [](const std::string& a, const std::string& b) {
  165. return a + " " + b;
  166. }
  167. );
  168. std::cout << "Translation: " << concat_translation << std::endl;
  169. }
  170. }
  171. return 0;
  172. }