MoYoYoTech/VoiceDialogue
653
1import multiprocessing2import os3import sys4import typing5from pathlib import Path6 7if __name__ == '__main__':8 if hasattr(sys, '_voice_dialogue_started'):9 sys.exit(0)10 sys._voice_dialogue_started = True11 12 # 设置multiprocessing启动方法为spawn,避免fork问题13 if hasattr(multiprocessing, 'set_start_method'):14 try:15 multiprocessing.set_start_method('spawn', force=True)16 except RuntimeError:17 pass18 19 # Pyinstaller 多进程支持20 multiprocessing.freeze_support()21 22 # 禁用各种可能导致多进程问题的并行处理23 os.environ.update({24 "TOKENIZERS_PARALLELISM": "false",25 # "OMP_NUM_THREADS": "1",26 # "MKL_NUM_THREADS": "1", 27 # "NUMEXPR_NUM_THREADS": "1",28 # "OPENBLAS_NUM_THREADS": "1",29 # "VECLIB_MAXIMUM_THREADS": "1",30 # "BLIS_NUM_THREADS": "1",31 # # 禁用huggingface的多进程32 # "HF_HUB_DISABLE_PROGRESS_BARS": "1",33 # "TRANSFORMERS_NO_ADVISORY_WARNINGS": "1",34 # # 禁用torch的多进程35 # "TORCH_NUM_THREADS": "1",36 # "PYTORCH_JIT": "0",37 # # 禁用joblib的loky后端,使用threading38 # "JOBLIB_START_METHOD": "threading",39 # "SKLEARN_JOBLIB_START_METHOD": "threading",40 })41 42HERE = Path(__file__).parent43lib_path = HERE / "src"44if lib_path.exists() and lib_path.as_posix() not in sys.path:45 sys.path.insert(0, lib_path.as_posix())46 47from voice_dialogue.core.launcher import launch_system48from voice_dialogue.core.constants import set_debug_mode49from voice_dialogue.cli.args import create_argument_parser50from voice_dialogue.api.server import launch_api_server51 52language: typing.Literal['zh', 'en'] = 'en'53 54 55def main():56 """57 主程序入口函数58 59 根据命令行参数选择启动模式:60 - cli: 启动命令行语音对话系统61 - api: 启动HTTP API服务器62 """63 parser = create_argument_parser()64 args = parser.parse_args()65 66 # 列出音频输入设备后退出67 if getattr(args, 'list_audio_devices', False):68 from voice_dialogue.audio.devices import list_input_devices69 devices = list_input_devices()70 print(f"\n可用音频输入设备 ({len(devices)}):")71 print(f"{'索引':>4} {'通道':>4} {'采样率':>7} {'默认':>4} 名称")72 for d in devices:73 default_mark = '✓' if d['is_default'] else ''74 print(f"{d['index']:>4} {d['max_input_channels']:>4} "75 f"{d['default_sample_rate']:>7} {default_mark:>4} {d['name']}")76 print("\n使用 --input-device <索引> 选择设备。")77 sys.exit(0)78 79 set_debug_mode(args.debug)80 81 print(f"""82{"=" * 80}83VoiceDialogue - 语音对话系统84{"=" * 80}85运行模式: {args.mode.upper()}86调试模式: {'启用' if args.debug else '禁用'}87{"=" * 80}88 """)89 90 try:91 if args.mode == 'cli':92 print(f"语言设置: {args.language}")93 print(f"说话人: {args.speaker}")94 if args.input_device is not None:95 print(f"输入设备索引: {args.input_device}")96 print("正在启动命令行语音对话系统...")97 launch_system(args.language, args.speaker, args.disable_echo_cancellation, args.input_device)98 99 elif args.mode == 'api':100 launch_api_server(101 host=args.host,102 port=args.port,103 reload=args.reload104 )105 106 except KeyboardInterrupt:107 print("\n程序被用户中断")108 except Exception as e:109 print(f"程序运行出错: {e}")110 raise111 112 113if __name__ == '__main__':114 main()115 