funasr-onnx-online-punc.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /**
  2. * Copyright FunASR (https://github.com/alibaba-damo-academy/FunASR). All Rights Reserved.
  3. * MIT License (https://opensource.org/licenses/MIT)
  4. */
  5. #ifndef _WIN32
  6. #include <sys/time.h>
  7. #else
  8. #include <win_func.h>
  9. #endif
  10. #include <iostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <map>
  14. #include <glog/logging.h>
  15. #include "funasrruntime.h"
  16. #include "tclap/CmdLine.h"
  17. #include "com-define.h"
  18. using namespace std;
  19. void GetValue(TCLAP::ValueArg<std::string>& value_arg, string key, std::map<std::string, std::string>& model_path)
  20. {
  21. if (value_arg.isSet()){
  22. model_path.insert({key, value_arg.getValue()});
  23. LOG(INFO)<< key << " : " << value_arg.getValue();
  24. }
  25. }
  26. void splitString(vector<string>& strings, const string& org_string, const string& seq) {
  27. string::size_type p1 = 0;
  28. string::size_type p2 = org_string.find(seq);
  29. while (p2 != string::npos) {
  30. if (p2 == p1) {
  31. ++p1;
  32. p2 = org_string.find(seq, p1);
  33. continue;
  34. }
  35. strings.push_back(org_string.substr(p1, p2 - p1));
  36. p1 = p2 + seq.size();
  37. p2 = org_string.find(seq, p1);
  38. }
  39. if (p1 != org_string.size()) {
  40. strings.push_back(org_string.substr(p1));
  41. }
  42. }
  43. int main(int argc, char *argv[])
  44. {
  45. google::InitGoogleLogging(argv[0]);
  46. FLAGS_logtostderr = true;
  47. TCLAP::CmdLine cmd("funasr-onnx-online-punc", ' ', "1.0");
  48. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the punc model path, which contains model.onnx, punc.yaml", true, "", "string");
  49. TCLAP::ValueArg<std::string> quantize("", QUANTIZE, "false (Default), load the model of model.onnx in model_dir. If set true, load the model of model_quant.onnx in model_dir", false, "false", "string");
  50. TCLAP::ValueArg<std::string> txt_path("", TXT_PATH, "txt file path, one sentence per line", true, "", "string");
  51. cmd.add(model_dir);
  52. cmd.add(quantize);
  53. cmd.add(txt_path);
  54. cmd.parse(argc, argv);
  55. std::map<std::string, std::string> model_path;
  56. GetValue(model_dir, MODEL_DIR, model_path);
  57. GetValue(quantize, QUANTIZE, model_path);
  58. GetValue(txt_path, TXT_PATH, model_path);
  59. struct timeval start, end;
  60. gettimeofday(&start, NULL);
  61. int thread_num = 1;
  62. FUNASR_HANDLE punc_hanlde=CTTransformerInit(model_path, thread_num, PUNC_ONLINE);
  63. if (!punc_hanlde)
  64. {
  65. LOG(ERROR) << "FunASR init failed";
  66. exit(-1);
  67. }
  68. gettimeofday(&end, NULL);
  69. long seconds = (end.tv_sec - start.tv_sec);
  70. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  71. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  72. // read txt_path
  73. vector<string> txt_list;
  74. if(model_path.find(TXT_PATH)!=model_path.end()){
  75. ifstream in(model_path.at(TXT_PATH));
  76. if (!in.is_open()) {
  77. LOG(ERROR) << "Failed to open file: " << model_path.at(TXT_PATH) ;
  78. return 0;
  79. }
  80. string line;
  81. while(getline(in, line))
  82. {
  83. txt_list.emplace_back(line);
  84. }
  85. in.close();
  86. }
  87. long taking_micros = 0;
  88. for(auto& txt_str : txt_list){
  89. vector<string> vad_strs;
  90. splitString(vad_strs, txt_str, "|");
  91. string str_out;
  92. FUNASR_RESULT result = nullptr;
  93. gettimeofday(&start, NULL);
  94. for(auto& vad_str:vad_strs){
  95. result=CTTransformerInfer(punc_hanlde, vad_str.c_str(), RASR_NONE, NULL, PUNC_ONLINE, result);
  96. if(result){
  97. string msg = CTTransformerGetResult(result, 0);
  98. str_out += msg;
  99. LOG(INFO)<<"Online result: "<<msg;
  100. }
  101. }
  102. gettimeofday(&end, NULL);
  103. seconds = (end.tv_sec - start.tv_sec);
  104. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  105. LOG(INFO)<<"Results: "<<str_out;
  106. CTTransformerFreeResult(result);
  107. }
  108. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  109. CTTransformerUninit(punc_hanlde);
  110. return 0;
  111. }