websocket_client.py 3.3 KB
Newer Older
1
#!/usr/bin/python
X
xiongxinlei 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
15
# calc avg RTF(NOT Accurate): grep -rn RTF log.txt | awk '{print $NF}' | awk -F "=" '{sum += $NF} END {print "all time",sum, "audio num", NR,  "RTF", sum/NR}'
H
Hui Zhang 已提交
16 17
# python3 websocket_client.py --server_ip 127.0.0.1 --port 8290 --punc.server_ip 127.0.0.1 --punc.port 8190 --wavfile ./zh.wav
# python3 websocket_client.py --server_ip 127.0.0.1 --port 8290 --wavfile ./zh.wav
X
xiongxinlei 已提交
18 19 20 21 22 23 24
import argparse
import asyncio
import codecs
import logging
import os

from paddlespeech.cli.log import logger
25
from paddlespeech.server.utils.audio_handler import ASRWsAudioHandler
X
xiongxinlei 已提交
26 27 28 29


def main(args):
    logger.info("asr websocket client start")
30 31 32
    handler = ASRWsAudioHandler(
        args.server_ip,
        args.port,
X
xiongxinlei 已提交
33
        endpoint=args.endpoint,
34 35
        punc_server_ip=args.punc_server_ip,
        punc_server_port=args.punc_server_port)
X
xiongxinlei 已提交
36 37 38 39 40 41
    loop = asyncio.get_event_loop()

    # support to process single audio file
    if args.wavfile and os.path.exists(args.wavfile):
        logger.info(f"start to process the wavscp: {args.wavfile}")
        result = loop.run_until_complete(handler.run(args.wavfile))
42
        result = result["result"]
X
xiongxinlei 已提交
43 44
        logger.info(f"asr websocket client finished : {result}")

H
huangyuxin 已提交
45
    # support to process batch audios from wav.scp
X
xiongxinlei 已提交
46 47 48 49 50 51 52
    if args.wavscp and os.path.exists(args.wavscp):
        logging.info(f"start to process the wavscp: {args.wavscp}")
        with codecs.open(args.wavscp, 'r', encoding='utf-8') as f,\
             codecs.open("result.txt", 'w', encoding='utf-8') as w:
            for line in f:
                utt_name, utt_path = line.strip().split()
                result = loop.run_until_complete(handler.run(utt_path))
53
                result = result["result"]
X
xiongxinlei 已提交
54 55 56 57 58 59
                w.write(f"{utt_name} {result}\n")


if __name__ == "__main__":
    logger.info("Start to do streaming asr client")
    parser = argparse.ArgumentParser()
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    parser.add_argument(
        '--server_ip', type=str, default='127.0.0.1', help='server ip')
    parser.add_argument('--port', type=int, default=8090, help='server port')
    parser.add_argument(
        '--punc.server_ip',
        type=str,
        default=None,
        dest="punc_server_ip",
        help='Punctuation server ip')
    parser.add_argument(
        '--punc.port',
        type=int,
        default=8091,
        dest="punc_server_port",
        help='Punctuation server port')
X
xiongxinlei 已提交
75 76 77 78 79
    parser.add_argument(
        "--endpoint",
        type=str,
        default="/paddlespeech/asr/streaming",
        help="ASR websocket endpoint")
X
xiongxinlei 已提交
80 81 82 83 84 85 86 87 88 89
    parser.add_argument(
        "--wavfile",
        action="store",
        help="wav file path ",
        default="./16_audio.wav")
    parser.add_argument(
        "--wavscp", type=str, default=None, help="The batch audios dict text")
    args = parser.parse_args()

    main(args)