| | |
| | | from concurrent import futures |
| | | import grpc |
| | | import json |
| | | import paraformer_pb2 |
| | | import paraformer_pb2_grpc |
| | | import time |
| | | |
| | | import paraformer_pb2_grpc |
| | | from paraformer_pb2 import Response |
| | | from modelscope.pipelines import pipeline |
| | | from modelscope.utils.constant import Tasks |
| | | |
| | | |
| | | class ASRServicer(paraformer_pb2_grpc.ASRServicer): |
| | | def __init__(self, user_allowed, model, sample_rate): |
| | | def __init__(self, user_allowed, model, sample_rate, backend, onnx_dir, vad_model='', punc_model=''): |
| | | print("ASRServicer init") |
| | | self.backend = backend |
| | | self.init_flag = 0 |
| | | self.client_buffers = {} |
| | | self.client_transcription = {} |
| | | self.auth_user = user_allowed.split("|") |
| | | self.inference_16k_pipeline = pipeline(task=Tasks.auto_speech_recognition, model=model) |
| | | if self.backend == "pipeline": |
| | | try: |
| | | from modelscope.pipelines import pipeline |
| | | from modelscope.utils.constant import Tasks |
| | | except ImportError: |
| | | raise ImportError(f"Please install modelscope") |
| | | self.inference_16k_pipeline = pipeline(task=Tasks.auto_speech_recognition, model=model, vad_model=vad_model, punc_model=punc_model) |
| | | elif self.backend == "onnxruntime": |
| | | try: |
| | | from funasr_onnx import Paraformer |
| | | except ImportError: |
| | | raise ImportError(f"Please install onnxruntime environment") |
| | | self.inference_16k_pipeline = Paraformer(model_dir=onnx_dir) |
| | | self.sample_rate = sample_rate |
| | | |
| | | def clear_states(self, user): |
| | |
| | | if req.user not in self.client_buffers: |
| | | result = {} |
| | | result["success"] = True |
| | | result["detail"] = "waiting_for_voice" |
| | | result["detail"] = "waiting_for_more_voice" |
| | | result["text"] = "" |
| | | yield Response(sentence=json.dumps(result), user=req.user, action="waiting", language=req.language) |
| | | else: |
| | |
| | | delay_str = str(end_time - begin_time) |
| | | result = {} |
| | | result["success"] = True |
| | | result["detail"] = "finish_sentence_data_is_not_long_enough" |
| | | result["detail"] = "waiting_for_more_voice" |
| | | result["server_delay_ms"] = delay_str |
| | | result["text"] = "" |
| | | print ("user: %s , delay(ms): %s, error: %s " % (req.user, delay_str, "data_is_not_long_enough")) |
| | | yield Response(sentence=json.dumps(result), user=req.user, action="finish", language=req.language) |
| | | else: |
| | | asr_result = self.inference_16k_pipeline(audio_in=tmp_data, audio_fs = self.sample_rate) |
| | | if "text" in asr_result: |
| | | asr_result = asr_result['text'] |
| | | else: |
| | | asr_result = "" |
| | | print ("user: %s , delay(ms): %s, info: %s " % (req.user, delay_str, "waiting_for_more_voice")) |
| | | yield Response(sentence=json.dumps(result), user=req.user, action="waiting", language=req.language) |
| | | else: |
| | | if self.backend == "pipeline": |
| | | asr_result = self.inference_16k_pipeline(audio_in=tmp_data, audio_fs = self.sample_rate) |
| | | if "text" in asr_result: |
| | | asr_result = asr_result['text'] |
| | | else: |
| | | asr_result = "" |
| | | elif self.backend == "onnxruntime": |
| | | from funasr_onnx.utils.frontend import load_bytes |
| | | array = load_bytes(tmp_data) |
| | | asr_result = self.inference_16k_pipeline(array)[0] |
| | | end_time = int(round(time.time() * 1000)) |
| | | delay_str = str(end_time - begin_time) |
| | | print ("user: %s , delay(ms): %s, text: %s " % (req.user, delay_str, asr_result)) |