From c954b81cea89502ba8899451ea2eb13f05f7f279 Mon Sep 17 00:00:00 2001 From: yumoqing Date: Mon, 21 Oct 2024 11:04:46 +0800 Subject: [PATCH] first commit --- README.md | 35 +++++++++++++++++++++++++++++ conf/config.json | 6 +++++ run.sh | 5 +++++ zmq_client.py | 42 +++++++++++++++++++++++++++++++++++ zmqfw.py | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 README.md create mode 100644 conf/config.json create mode 100755 run.sh create mode 100644 zmq_client.py create mode 100644 zmqfw.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..24c3dce --- /dev/null +++ b/README.md @@ -0,0 +1,35 @@ +# faster-whisper +这个比openai-whisper要快 + +## 创建虚拟环境 +``` +mkdir ve +python3 -m venv ~/ve/fw +``` + +## 安装 + +``` +source ~/ve/fw/bin/activate +pip install nvidia-cublas-cu12 nvidia-cudnn-cu12 + +export LD_LIBRARY_PATH=`python -c 'import os; import nvidia.cublas.lib; import nvidia.cudnn.lib; print(os.path.dirname(nvidia.cublas.lib.__file__) + ":" + os.path.dirname(nvidia.cudnn.lib.__file__))'` +pip install faster-whisper +``` + +## 运行 +创建一个run.sh +``` +#!/bin/sh + +r=$HOME/ve/fw/bin/python +export LD_LIBRARY_PATH=`$r -c 'import os; import nvidia.cublas.lib; import nvidia.cudnn.lib; print(os.path.dirname(nvidia.cublas.lib.__file__) + ":" + os.path.dirname(nvidia.cudnn.lib.__file__))'` +$r $* +``` + +## 执行服务 +./run.sh zmqfw.py + +## 执行客户端 +./run.shzmq_client.py + diff --git a/conf/config.json b/conf/config.json new file mode 100644 index 0000000..e9f2136 --- /dev/null +++ b/conf/config.json @@ -0,0 +1,6 @@ +{ + "zmq_url" : "tcp://127.0.0.1:10002", + "model_dir" : "large-v3", + "device" : "cuda", + "conpute_type": "float16" +} diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..a8b615e --- /dev/null +++ b/run.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +export LD_LIBRARY_PATH=/data/ymq/ve/fw/lib/python3.12/site-packages/nvidia/cublas/lib:/data/ymq/ve/fw/lib/python3.12/site-packages/nvidia/cudnn/lib +r=$HOME/ve/fw/bin/python +$r $* diff --git a/zmq_client.py b/zmq_client.py new file mode 100644 index 0000000..0f3f334 --- /dev/null +++ b/zmq_client.py @@ -0,0 +1,42 @@ +import json +import os + +from appPublic.dictObject import DictObject +from appPublic.zmq_reqrep import ZmqRequester +from appPublic.jsonConfig import getConfig + +zmq_url = "tcp://127.0.0.1:9999" +from time import time + +class ASRClient: + def __init__(self, zmq_url): + self.zmq_url = zmq_url + self.requester = ZmqRequester(self.zmq_url) + + def generate(self, audio_file): + d = { + "audio_file":audio_file + } + msg = json.dumps(d) + print(f'send={msg}') + resp = self.requester.send(msg) + if resp != None: + ret = json.loads(resp) + print(f'response={ret}') + else: + print(f'response is None') + + def run(self): + print(f'running {self.zmq_url}') + while True: + print('input audio_file:') + af = input() + if len(af) > 0: + self.generate(af) + print('ended ...') + +if __name__ == '__main__': + workdir = os.getcwd() + config = getConfig(workdir) + asr = ASRClient(config.zmq_url or zmq_url) + asr.run() diff --git a/zmqfw.py b/zmqfw.py new file mode 100644 index 0000000..b101b47 --- /dev/null +++ b/zmqfw.py @@ -0,0 +1,57 @@ +import json +import os + +from appPublic.dictObject import DictObject +from appPublic.zmq_reqrep import ZmqReplier +from appPublic.jsonConfig import getConfig + +from time import time +from faster_whisper import WhisperModel + +class ZmqASR: + def __init__(self): + config = getConfig() + self.model_dir = config.model_dir or "large-v3" + self.device = config.device or "cpu" + self.zmq_url = config.zmq_url + self.compute_type=config.compute_type or "float16" + self.model = WhisperModel(self.model_dir, + device=self.device, + compute_type=self.compute_type) + self.replier = ZmqReplier(self.zmq_url, self.generate) + + def run(self): + print(f'running {self.zmq_url}') + self.replier._run() + print('ended ...') + + def generate(self, d): + msg= d.decode('utf-8') + data = DictObject(**json.loads(msg)) + t1 = time() + segs, info = self.model.transcribe(data.audio_file) + t2 = time() + d = DictObject(**{ + 'segments':[], + 'language':info.language + }) + d.content = '' + for s in segs: + seg = { + "start":s.start, + "end":s.end, + "text":s.text + } + d.segments.append(seg) + d.content += s.text + d.time_cost = t2 - t1 + print(f'{d}') + return json.dumps(d) + +if __name__ == '__main__': + workdir = os.getcwd() + config = getConfig(workdir) + print(f'{config=}') + asr = ZmqASR() + print('here') + asr.run()