funasr-onnx-offline-punc.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. int main(int argc, char *argv[])
  27. {
  28. google::InitGoogleLogging(argv[0]);
  29. FLAGS_logtostderr = true;
  30. TCLAP::CmdLine cmd("funasr-onnx-offline-punc", ' ', "1.0");
  31. TCLAP::ValueArg<std::string> model_dir("", MODEL_DIR, "the punc model path, which contains model.onnx, punc.yaml", true, "", "string");
  32. 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, "true", "string");
  33. TCLAP::ValueArg<std::string> txt_path("", TXT_PATH, "txt file path, one sentence per line", true, "", "string");
  34. cmd.add(model_dir);
  35. cmd.add(quantize);
  36. cmd.add(txt_path);
  37. cmd.parse(argc, argv);
  38. std::map<std::string, std::string> model_path;
  39. GetValue(model_dir, MODEL_DIR, model_path);
  40. GetValue(quantize, QUANTIZE, model_path);
  41. GetValue(txt_path, TXT_PATH, model_path);
  42. struct timeval start, end;
  43. gettimeofday(&start, NULL);
  44. int thread_num = 1;
  45. FUNASR_HANDLE punc_hanlde=CTTransformerInit(model_path, thread_num);
  46. if (!punc_hanlde)
  47. {
  48. LOG(ERROR) << "FunASR init failed";
  49. exit(-1);
  50. }
  51. gettimeofday(&end, NULL);
  52. long seconds = (end.tv_sec - start.tv_sec);
  53. long modle_init_micros = ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  54. LOG(INFO) << "Model initialization takes " << (double)modle_init_micros / 1000000 << " s";
  55. // read txt_path
  56. vector<string> txt_list;
  57. if(model_path.find(TXT_PATH)!=model_path.end()){
  58. ifstream in(model_path.at(TXT_PATH));
  59. if (!in.is_open()) {
  60. LOG(ERROR) << "Failed to open file: " << model_path.at(TXT_PATH) ;
  61. return 0;
  62. }
  63. string line;
  64. while(getline(in, line))
  65. {
  66. txt_list.emplace_back(line);
  67. }
  68. in.close();
  69. }
  70. long taking_micros = 0;
  71. for(auto& txt_str : txt_list){
  72. gettimeofday(&start, NULL);
  73. FUNASR_RESULT result=CTTransformerInfer(punc_hanlde, txt_str.c_str(), RASR_NONE, NULL);
  74. gettimeofday(&end, NULL);
  75. seconds = (end.tv_sec - start.tv_sec);
  76. taking_micros += ((seconds * 1000000) + end.tv_usec) - (start.tv_usec);
  77. string msg = FunASRGetResult(result, 0);
  78. LOG(INFO)<<"Results: "<<msg;
  79. CTTransformerFreeResult(result);
  80. }
  81. LOG(INFO) << "Model inference takes: " << (double)taking_micros / 1000000 <<" s";
  82. CTTransformerUninit(punc_hanlde);
  83. return 0;
  84. }