Commit a9fd4496 by 程裕兵

fix(luna-tts): load models in background thread so host responds to Chrome immediately

parent 07cae1b7
......@@ -89,22 +89,28 @@ def _write_status(status, detail=""):
def main():
_log_stderr("[host] Starting Native Messaging host...")
_write_status("starting", "Loading our references...")
_write_status("starting", "Loading models... (may take minutes on Intel Mac)")
our_dir = BASE_DIR / REFERENCE_DIRS["our"]
competitor_dir = BASE_DIR / REFERENCE_DIRS["competitor"]
big_vendor_dir = BASE_DIR / REFERENCE_DIRS["big_vendor"]
_write_status("starting", "Loading competitor references...")
_write_status("starting", "Loading big vendor references...")
# Load models in background while message loop responds immediately to status requests
import threading
matcher = [None] # mutable container for thread to write into
load_done = threading.Event()
matcher = MultiMatcher(our_dir, competitor_dir, big_vendor_dir)
def _load():
matcher[0] = MultiMatcher(our_dir, competitor_dir, big_vendor_dir)
_write_status("ready", "Models loaded")
_log_stderr("[host] Models loaded, ready for matching")
load_done.set()
threading.Thread(target=_load, daemon=True).start()
today_votes = 0
today_skips = 0
luna_votes = 0
_write_status("ready", f"Models loaded, {today_votes} votes today")
_log_stderr("[host] Ready, waiting for messages...")
while True:
data = read_message()
......@@ -114,6 +120,13 @@ def main():
msg_type = data.get("type")
if msg_type == "match_request":
# Wait for model loading to complete
if not load_done.is_set():
_write_status("loading", "Waiting for models to load before matching...")
_log_stderr("[host] Waiting for models to load...")
load_done.wait()
_log_stderr("[host] Models ready, processing match request")
request_id = data["request_id"]
audio_left_url = data["audio_left"]
audio_right_url = data["audio_right"]
......@@ -148,8 +161,8 @@ def main():
})
continue
left_result = matcher.match_one(left_path)
right_result = matcher.match_one(right_path)
left_result = matcher[0].match_one(left_path)
right_result = matcher[0].match_one(right_path)
def _build_side(label, r):
our_sp, our_sc = r.our
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment