-
Notifications
You must be signed in to change notification settings - Fork 248
/
core_client.py
120 lines (90 loc) · 2.85 KB
/
core_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# coding: utf-8
import os
import sys
import asyncio
import signal
from pathlib import Path
from platform import system
from typing import List
import typer
import colorama
import keyboard
from config import ClientConfig as Config
from util.client_cosmic import console, Cosmic
from util.client_stream import stream_open, stream_close
from util.client_shortcut_handler import bond_shortcut
from util.client_recv_result import recv_result
from util.client_show_tips import show_mic_tips, show_file_tips
from util.client_hot_update import update_hot_all, observe_hot
from util.client_transcribe import transcribe_check, transcribe_send, transcribe_recv
from util.client_adjust_srt import adjust_srt
from util.empty_working_set import empty_current_working_set
# 确保根目录位置正确,用相对路径加载模型
BASE_DIR = os.path.dirname(__file__); os.chdir(BASE_DIR)
# 确保终端能使用 ANSI 控制字符
colorama.init()
# MacOS 的权限设置
if system() == 'Darwin' and not sys.argv[1:]:
if os.getuid() != 0:
print('在 MacOS 上需要以管理员启动客户端才能监听键盘活动,请 sudo 启动')
input('按回车退出'); sys.exit()
else:
os.umask(0o000)
async def main_mic():
Cosmic.loop = asyncio.get_event_loop()
Cosmic.queue_in = asyncio.Queue()
Cosmic.queue_out = asyncio.Queue()
show_mic_tips()
# 更新热词
update_hot_all()
# 实时更新热词
observer = observe_hot()
# 打开音频流
Cosmic.stream = stream_open()
# Ctrl-C 关闭音频流,触发自动重启
signal.signal(signal.SIGINT, stream_close)
# 绑定按键
bond_shortcut()
# 清空物理内存工作集
if system() == 'Windows':
empty_current_working_set()
# 接收结果
while True:
await recv_result()
async def main_file(files: List[Path]):
show_file_tips()
for file in files:
if file.suffix in ['.txt', '.json', 'srt']:
adjust_srt(file)
else:
await transcribe_check(file)
await asyncio.gather(
transcribe_send(file),
transcribe_recv(file)
)
if Cosmic.websocket:
await Cosmic.websocket.close()
input('\n按回车退出\n')
def init_mic():
try:
asyncio.run(main_mic())
except KeyboardInterrupt:
console.print(f'再见!')
finally:
print('...')
def init_file(files: List[Path]):
"""
用 CapsWriter Server 转录音视频文件,生成 srt 字幕
"""
try:
asyncio.run(main_file(files))
except KeyboardInterrupt:
console.print(f'再见!')
sys.exit()
if __name__ == "__main__":
# 如果参数传入文件,那就转录文件
# 如果没有多余参数,就从麦克风输入
if sys.argv[1:]:
typer.run(init_file)
else:
init_mic()