From 4ba1011b42e041ee1d71448eefd7ef2e7bd61bb6 Mon Sep 17 00:00:00 2001
From: 游雁 <zhifu.gzf@alibaba-inc.com>
Date: 星期五, 31 三月 2023 15:31:26 +0800
Subject: [PATCH] export

---
 funasr/runtime/grpc/paraformer_server.cc |   77 ++++++++++++++++++++++++++------------
 1 files changed, 53 insertions(+), 24 deletions(-)

diff --git a/funasr/runtime/grpc/paraformer_server.cc b/funasr/runtime/grpc/paraformer_server.cc
index 0ae241d..69ce903 100644
--- a/funasr/runtime/grpc/paraformer_server.cc
+++ b/funasr/runtime/grpc/paraformer_server.cc
@@ -1,10 +1,36 @@
+#include <algorithm>
+#include <chrono>
+#include <cmath>
+#include <iostream>
+#include <sstream>
+#include <memory>
+#include <string>
 
+#include <grpc/grpc.h>
+#include <grpcpp/server.h>
+#include <grpcpp/server_builder.h>
+#include <grpcpp/server_context.h>
+#include <grpcpp/security/server_credentials.h>
 
 #include "paraformer.grpc.pb.h"
 #include "paraformer_server.h"
 
 
-ASRServicer::ASRServicer() {
+using grpc::Server;
+using grpc::ServerBuilder;
+using grpc::ServerContext;
+using grpc::ServerReader;
+using grpc::ServerReaderWriter;
+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);
     std::cout << "ASRServicer init" << std::endl;
     init_flag = 0;
 }
@@ -80,7 +106,8 @@
                 res.set_sentence(
                     R"({"success": true, "detail": "decoding data: " + std::to_string(tmp_data.length()) + " bytes"})"
                 );
-                std::string data_len = std::to_string(tmp_data.length());
+		int data_len_int = tmp_data.length();
+                std::string data_len = std::to_string(data_len_int);
                 std::stringstream ss;
                 ss << R"({"success": true, "detail": "decoding data: )" << data_len << R"( bytes")"  << R"("})";
                 std::string result = ss.str();
@@ -108,16 +135,8 @@
                     stream->Write(res);
                 }
                 else {
-
-		    // asr_result = onnx.infer(tmp_data)
-                    /* if (asr_result.find("text") != asr_result.end()) {
-                        asr_result = asr_result["text"];
-                    }
-                    else {
-                        asr_result = "";
-                    } */
-
-                    std::string asr_result = "浣犲ソ浣犲ソ锛屾垜鏄痑sr璇嗗埆缁撴灉銆俿tatic";
+                    RPASR_RESULT Result= RapidAsrRecogPCMBuffer(AsrHanlde, tmp_data.c_str(), data_len_int, RASR_NONE, NULL);   
+                    std::string asr_result = ((RPASR_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);
@@ -151,19 +170,29 @@
 }
 
 
-void RunServer() {
-  std::string server_address("0.0.0.0:10108");
-  ASRServicer service;
+void RunServer(const std::string& port, int thread_num, const char* model_path, bool quantize) {
+    std::string server_address;
+    server_address = "0.0.0.0:" + port;
+    ASRServicer service(model_path, thread_num, quantize);
 
-  ServerBuilder builder;
-  builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
-  builder.RegisterService(&service);
-  std::unique_ptr<Server> server(builder.BuildAndStart());
-  std::cout << "Server listening on " << server_address << std::endl;
-  server->Wait();
+    ServerBuilder builder;
+    builder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
+    builder.RegisterService(&service);
+    std::unique_ptr<Server> server(builder.BuildAndStart());
+    std::cout << "Server listening on " << server_address << std::endl;
+    server->Wait();
 }
 
-int main(int argc, char** argv) {
- RunServer();
- return 0;
+int main(int argc, char* argv[]) {
+    if (argc < 5)
+    {
+        printf("Usage: %s port thread_num /path/to/model_file quantize(true or false) \n", argv[0]);
+        exit(-1);
+    }
+
+    // is quantize
+    bool quantize = false;
+    std::istringstream(argv[4]) >> std::boolalpha >> quantize;
+    RunServer(argv[1], atoi(argv[2]), argv[3], quantize);
+    return 0;
 }

--
Gitblit v1.9.1