| | |
| | | |
| | | #include "precomp.h" |
| | | |
| | | namespace funasr { |
| | | CTTransformer::CTTransformer() |
| | | :env_(ORT_LOGGING_LEVEL_ERROR, ""),session_options{} |
| | | { |
| | |
| | | |
| | | try{ |
| | | m_session = std::make_unique<Ort::Session>(env_, punc_model.c_str(), session_options); |
| | | LOG(INFO) << "Successfully load model from " << punc_model; |
| | | } |
| | | catch (std::exception const &e) { |
| | | LOG(ERROR) << "Error when load punc onnx model: " << e.what(); |
| | |
| | | m_tokenizer.OpenYaml(punc_config.c_str()); |
| | | } |
| | | |
| | | |
| | | CTTransformer::~CTTransformer() |
| | | { |
| | | } |
| | |
| | | int nTotalBatch = ceil((float)InputData.size() / TOKEN_LEN); |
| | | int nCurBatch = -1; |
| | | int nSentEnd = -1, nLastCommaIndex = -1; |
| | | vector<int64_t> RemainIDs; // |
| | | vector<int32_t> RemainIDs; // |
| | | vector<string> RemainStr; // |
| | | vector<int> NewPunctuation; // |
| | | vector<string> NewString; // |
| | |
| | | for (size_t i = 0; i < InputData.size(); i += TOKEN_LEN) |
| | | { |
| | | nDiff = (i + TOKEN_LEN) < InputData.size() ? (0) : (i + TOKEN_LEN - InputData.size()); |
| | | vector<int64_t> InputIDs(InputData.begin() + i, InputData.begin() + i + TOKEN_LEN - nDiff); |
| | | vector<int32_t> InputIDs(InputData.begin() + i, InputData.begin() + i + TOKEN_LEN - nDiff); |
| | | vector<string> InputStr(strOut.begin() + i, strOut.begin() + i + TOKEN_LEN - nDiff); |
| | | InputIDs.insert(InputIDs.begin(), RemainIDs.begin(), RemainIDs.end()); // RemainIDs+InputIDs; |
| | | InputStr.insert(InputStr.begin(), RemainStr.begin(), RemainStr.end()); // RemainStr+InputStr; |
| | |
| | | vector<string> WordWithPunc; |
| | | for (int i = 0; i < InputStr.size(); i++) |
| | | { |
| | | if (i > 0 && !(InputStr[i][0] & 0x80) && (i + 1) <InputStr.size() && !(InputStr[i+1][0] & 0x80))// �м��Ӣ�ģ� |
| | | // if (i > 0 && !(InputStr[i][0] & 0x80) && (i + 1) <InputStr.size() && !(InputStr[i+1][0] & 0x80))// �м��Ӣ�ģ� |
| | | if (i > 0 && !(InputStr[i-1][0] & 0x80) && !(InputStr[i][0] & 0x80)) |
| | | { |
| | | InputStr[i] = InputStr[i]+ " "; |
| | | InputStr[i] = " " + InputStr[i]; |
| | | } |
| | | WordWithPunc.push_back(InputStr[i]); |
| | | |
| | |
| | | NewPuncOut.assign(NewPunctuation.begin(), NewPunctuation.end() - 1); |
| | | NewPuncOut.push_back(PERIOD_INDEX); |
| | | } |
| | | else if (NewString[NewString.size() - 1] == m_tokenizer.Id2Punc(PERIOD_INDEX) && NewString[NewString.size() - 1] == m_tokenizer.Id2Punc(QUESTION_INDEX)) |
| | | else if (NewString[NewString.size() - 1] != m_tokenizer.Id2Punc(PERIOD_INDEX) && NewString[NewString.size() - 1] != m_tokenizer.Id2Punc(QUESTION_INDEX)) |
| | | { |
| | | NewSentenceOut = NewString; |
| | | NewSentenceOut.push_back(m_tokenizer.Id2Punc(PERIOD_INDEX)); |
| | |
| | | return strResult; |
| | | } |
| | | |
| | | vector<int> CTTransformer::Infer(vector<int64_t> input_data) |
| | | vector<int> CTTransformer::Infer(vector<int32_t> input_data) |
| | | { |
| | | Ort::MemoryInfo m_memoryInfo = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); |
| | | vector<int> punction; |
| | | std::array<int64_t, 2> input_shape_{ 1, (int64_t)input_data.size()}; |
| | | Ort::Value onnx_input = Ort::Value::CreateTensor<int64_t>(m_memoryInfo, |
| | | Ort::Value onnx_input = Ort::Value::CreateTensor<int32_t>( |
| | | m_memoryInfo, |
| | | input_data.data(), |
| | | input_data.size(), |
| | | input_shape_.data(), |
| | |
| | | } |
| | | catch (std::exception const &e) |
| | | { |
| | | printf(e.what()); |
| | | LOG(ERROR) << "Error when run punc onnx forword: " << (e.what()); |
| | | exit(0); |
| | | } |
| | | return punction; |
| | | } |
| | | |
| | | |
| | | |
| | | } // namespace funasr |