Skip to content

Defer heavy ML imports to fix slow tray startup - #5

Merged
buzz39 merged 1 commit into
mainfrom
copilot/optimize-startup-time
Apr 22, 2026
Merged

buzz39 merged 1 commit into
mainfrom
copilot/optimize-startup-time

Conversation

Copilot AI commented Apr 22, 2026

Copy link
Copy Markdown
Contributor

Launching the tray (python recorder.py tray) was taking ~5 minutes to display the icon because torch, pyannote.audio, and faster-whisper were imported at module load even though none are needed to draw the tray UI.

Root cause

recorder.py had module-top from diarizer import Diarizer, and diarizer.py had module-top import torch + from pyannote.audio import Pipeline. Importing pyannote.audio transitively pulls in torch, torchaudio, pytorch-lightning, speechbrain, asteroid-filterbanks — minutes of cold-start cost on Windows.

Changes

  • diarizer.py — replaced top-level torch / pyannote.audio imports with a lazy _ensure_pyannote_imports() helper. Only invoked when the pyannote backend is actually selected (i.e. HF_TOKEN is set) or when PyannoteDiarizer._load_pipeline runs. Backend-selection semantics in _init_backend are preserved.
  • recorder.py — moved from diarizer import Diarizer / from transcriber import Transcriber into Recorder.__init__, and reordered main() to dispatch the tray subcommand before constructing Recorder. The tray app already builds the Recorder lazily inside its recording thread on first "Start Recording" click, so user-facing behavior is unchanged — the ML stack just loads on demand instead of at process start.
# recorder.py main()
if args.command == "tray":
    from tray_app import run_tray
    run_tray(config)
    return

recorder = Recorder(config)  # ML imports happen here, not at module load

After the change, import recorder completes in ~17 ms with no torch / pyannote / faster-whisper modules loaded.

@buzz39
buzz39 marked this pull request as ready for review April 22, 2026 06:39
@buzz39
buzz39 merged commit 0e0a7f3 into main Apr 22, 2026
11 checks passed
@buzz39
buzz39 deleted the copilot/optimize-startup-time branch April 22, 2026 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants