| | |
| | | #include "paraformer.grpc.pb.h" |
| | | #include "paraformer_server.h" |
| | | |
| | | |
| | | using grpc::Server; |
| | | using grpc::ServerBuilder; |
| | | using grpc::ServerContext; |
| | |
| | | using grpc::ServerWriter; |
| | | using grpc::Status; |
| | | |
| | | |
| | | using paraformer::Request; |
| | | using paraformer::Response; |
| | | using paraformer::ASR; |
| | | |
| | | ASRServicer::ASRServicer(const char* model_path, int thread_num, bool quantize) { |
| | | AsrHanlde=RapidAsrInit(model_path, thread_num, quantize); |
| | | AsrHanlde=FunASRInit(model_path, thread_num, quantize); |
| | | std::cout << "ASRServicer init" << std::endl; |
| | | init_flag = 0; |
| | | } |
| | | |
| | | void ASRServicer::clear_states(const std::string& user) { |
| | | clear_buffers(user); |
| | | clear_transcriptions(user); |
| | | } |
| | | |
| | | void ASRServicer::clear_buffers(const std::string& user) { |
| | | if (client_buffers.count(user)) { |
| | | client_buffers.erase(user); |
| | | } |
| | | } |
| | | |
| | | void ASRServicer::clear_transcriptions(const std::string& user) { |
| | | if (client_transcription.count(user)) { |
| | | client_transcription.erase(user); |
| | | } |
| | | } |
| | | |
| | | void ASRServicer::disconnect(const std::string& user) { |
| | | clear_states(user); |
| | | std::cout << "Disconnecting user: " << user << std::endl; |
| | | } |
| | | |
| | | grpc::Status ASRServicer::Recognize( |
| | |
| | | grpc::ServerReaderWriter<Response, Request>* stream) { |
| | | |
| | | Request req; |
| | | std::unordered_map<std::string, std::string> client_buffers; |
| | | std::unordered_map<std::string, std::string> client_transcription; |
| | | |
| | | while (stream->Read(&req)) { |
| | | if (req.isend()) { |
| | | std::cout << "asr end" << std::endl; |
| | | disconnect(req.user()); |
| | | // disconnect |
| | | if (client_buffers.count(req.user())) { |
| | | client_buffers.erase(req.user()); |
| | | } |
| | | if (client_transcription.count(req.user())) { |
| | | client_transcription.erase(req.user()); |
| | | } |
| | | |
| | | Response res; |
| | | res.set_sentence( |
| | | R"({"success": true, "detail": "asr end"})" |
| | |
| | | auto& buf = client_buffers[req.user()]; |
| | | buf.insert(buf.end(), req.audio_data().begin(), req.audio_data().end()); |
| | | } |
| | | std::string tmp_data = this->client_buffers[req.user()]; |
| | | this->clear_states(req.user()); |
| | | std::string tmp_data = client_buffers[req.user()]; |
| | | // clear_states |
| | | if (client_buffers.count(req.user())) { |
| | | client_buffers.erase(req.user()); |
| | | } |
| | | if (client_transcription.count(req.user())) { |
| | | client_transcription.erase(req.user()); |
| | | } |
| | | |
| | | Response res; |
| | | res.set_sentence( |
| | |
| | | res.set_user(req.user()); |
| | | res.set_action("finish"); |
| | | res.set_language(req.language()); |
| | | |
| | | |
| | | |
| | | stream->Write(res); |
| | | } |
| | | else { |
| | | RPASR_RESULT Result= RapidAsrRecogPCMBuffer(AsrHanlde, tmp_data.c_str(), data_len_int, RASR_NONE, NULL); |
| | | std::string asr_result = ((RPASR_RECOG_RESULT*)Result)->msg; |
| | | FUNASR_RESULT Result= FunASRRecogPCMBuffer(AsrHanlde, tmp_data.c_str(), data_len_int, RASR_NONE, NULL); |
| | | std::string asr_result = ((FUNASR_RECOG_RESULT*)Result)->msg; |
| | | |
| | | auto end_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count(); |
| | | std::string delay_str = std::to_string(end_time - begin_time); |
| | |
| | | res.set_user(req.user()); |
| | | res.set_action("finish"); |
| | | res.set_language(req.language()); |
| | | |
| | | |
| | | stream->Write(res); |
| | | } |
| | |
| | | } |
| | | return Status::OK; |
| | | } |
| | | |
| | | |
| | | void RunServer(const std::string& port, int thread_num, const char* model_path, bool quantize) { |
| | | std::string server_address; |