-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
184 lines (153 loc) · 5.95 KB
/
Copy pathmain.py
File metadata and controls
184 lines (153 loc) · 5.95 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
main.py
-------
Entry point for Interview Assistant.
Wires together:
AudioCapture ──(raw audio)──▶ Transcriber ──(text)──▶ GUI
"""
from __future__ import annotations
import sys
import threading
import numpy as np
from audio_capture import AudioCapture
from transcriber import Transcriber
from gui import InterviewAssistantGUI
def _get_hw_info() -> str:
"""Detect and return a human-readable CPU or GPU description."""
try:
import torch
if torch.cuda.is_available():
name = torch.cuda.get_device_name(0)
mem = torch.cuda.get_device_properties(0).total_memory / 1024 ** 3
return f"GPU: {name} ({mem:.1f} GB VRAM)"
except Exception:
pass
# CPU fallback
try:
import platform
cpu = platform.processor() or platform.machine()
try:
with open("/proc/cpuinfo") as fh:
for line in fh:
if "model name" in line:
cpu = line.split(":", 1)[1].strip()
break
except OSError:
pass
return f"CPU: {cpu}"
except Exception:
return "硬件信息不可用"
def _check_deps() -> None:
"""Print a friendly error if required packages are missing."""
missing = []
try:
import sounddevice # noqa: F401
except ImportError:
missing.append("sounddevice")
try:
import funasr # noqa: F401
except ImportError:
missing.append("funasr")
try:
import modelscope # noqa: F401
except ImportError:
missing.append("modelscope")
if missing:
print(
"\n❌ 缺少依赖包,请先运行安装脚本:\n"
" Linux/macOS: bash setup.sh\n"
" Windows: setup_windows.bat\n\n"
f" 缺少的包: {', '.join(missing)}\n"
)
sys.exit(1)
class InterviewAssistant:
"""Top-level orchestrator."""
def __init__(self) -> None:
self._audio = AudioCapture()
self._transcriber = Transcriber(language="zh")
self._gui = InterviewAssistantGUI()
self._listening = False
# Wire GUI callbacks
self._gui.on_start = self._start_listening
self._gui.on_stop = self._stop_listening
self._gui.on_reload_model = self._reload_model
# Populate device list
devices = AudioCapture.list_devices()
if not devices:
self._gui.set_status("⚠️ 未找到音频输入设备")
else:
self._gui.set_devices(devices)
# Detect hardware info in background (torch import can be slow)
threading.Thread(
target=lambda: self._gui.set_hw_info(_get_hw_info()),
daemon=True,
).start()
# ------------------------------------------------------------------
# Model loading
# ------------------------------------------------------------------
def _load_model_bg(self) -> None:
"""Load FunASR Paraformer in a background thread."""
try:
self._transcriber.load_model(
progress_callback=self._gui.set_status
)
except Exception as exc:
self._gui.set_status(f"❌ 模型加载失败: {exc}")
def _reload_model(self) -> None:
"""Rebuild Transcriber with current GUI settings and reload the model."""
if self._listening:
self._stop_listening()
lang = self._gui.get_language()
device = self._gui.get_compute_device()
self._gui.set_status(f"⏳ 正在重新加载模型({lang} / {device})…")
self._transcriber = Transcriber(language=lang, device=device)
threading.Thread(target=self._load_model_bg, daemon=True).start()
# ------------------------------------------------------------------
# Listen control
# ------------------------------------------------------------------
def _start_listening(self) -> None:
if not self._transcriber.is_loaded:
self._gui.set_status("⏳ 模型尚未加载完成,请稍候…")
return
# Apply current language and VAD settings
self._transcriber.set_language(self._gui.get_language())
self._transcriber.set_vad_params(
self._gui.get_vad_threshold(),
self._gui.get_vad_silence(),
)
self._transcriber.reset_vad()
device_id = self._gui.get_selected_device_id()
self._listening = True
self._gui.set_listening(True)
def _audio_cb(chunk: np.ndarray) -> None:
rms = AudioCapture.rms(chunk)
self._gui.update_volume(rms)
self._transcriber.process_chunk(chunk, self._on_transcript)
try:
self._audio.start(device_id=device_id, callback=_audio_cb)
except Exception as exc:
self._gui.set_listening(False)
self._gui.set_status(f"❌ 无法打开音频设备: {exc}")
def _stop_listening(self) -> None:
self._audio.stop()
self._transcriber.reset_vad()
self._listening = False
self._gui.set_listening(False)
self._gui.update_volume(0.0)
# ------------------------------------------------------------------
# Transcription callback (background thread → GUI queue)
# ------------------------------------------------------------------
def _on_transcript(self, text: str) -> None:
self._gui.append_transcription(text)
# ------------------------------------------------------------------
# Run
# ------------------------------------------------------------------
def run(self) -> None:
# Load model in background so GUI is immediately responsive
threading.Thread(target=self._load_model_bg, daemon=True).start()
self._gui.run()
# ---------------------------------------------------------------------------
if __name__ == "__main__":
_check_deps()
app = InterviewAssistant()
app.run()