vimamsa 0.1.26 → 0.1.28
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.github/workflows/appimage.yml +93 -0
- data/.gitignore +4 -1
- data/Dockerfile +7 -4
- data/README.md +195 -99
- data/custom_example.rb +59 -17
- data/doc/TODO.md +678 -0
- data/doc/action_names_plan.md +442 -0
- data/doc/bundling_plan.md +570 -0
- data/doc/delta_syntax_plan.md +293 -0
- data/doc/demo_recording_plan.md +288 -0
- data/doc/design_goals.md +27 -0
- data/doc/intro_page_plan.md +266 -0
- data/doc/key_bindings_report_plan.md +213 -0
- data/doc/line_tree_plan.md +89 -0
- data/doc/missing_vim_bindings.md +61 -0
- data/doc/modules_settings_plan.md +433 -0
- data/doc/refactor_dedup_plan.md +117 -0
- data/doc/registries_plan.md +202 -0
- data/doc/startup_speed_plan.md +97 -0
- data/doc/treap_visualizer.html +585 -0
- data/doc/unused_code_plan.md +120 -0
- data/doc/vim_compatibility_plan.md +451 -0
- data/doc/vimlike_vim_conflicts.md +262 -0
- data/doc/vm_snapshots_plan.md +146 -0
- data/doc/wysiwyg_plan.md +231 -0
- data/exe/run_tests.rb +124 -3
- data/exe/vimamsa +68 -5
- data/fonts/OFL-Oxanium.txt +93 -0
- data/fonts/Oxanium[wght].ttf +0 -0
- data/help/intro.txt +30 -0
- data/help/vim_intro.txt +31 -0
- data/icon.png +0 -0
- data/img/demo.gif +0 -0
- data/img/demo.mp4 +0 -0
- data/lang/hyperplaintext.lang +7 -1
- data/lib/vimamsa/ack.rb +13 -1
- data/lib/vimamsa/actions.rb +385 -37
- data/lib/vimamsa/appimage_env.rb +35 -0
- data/lib/vimamsa/audio.rb +2 -5
- data/lib/vimamsa/bookmarks.rb +302 -0
- data/lib/vimamsa/buffer.rb +675 -118
- data/lib/vimamsa/buffer_changetext.rb +150 -68
- data/lib/vimamsa/buffer_cursor.rb +7 -0
- data/lib/vimamsa/buffer_list.rb +6 -2
- data/lib/vimamsa/buffer_manager.rb +10 -7
- data/lib/vimamsa/bundled_fonts.rb +32 -0
- data/lib/vimamsa/cli_help.rb +75 -0
- data/lib/vimamsa/clipboard.rb +9 -3
- data/lib/vimamsa/conf.rb +68 -3
- data/lib/vimamsa/constants.rb +3 -0
- data/lib/vimamsa/crash_handler.rb +149 -0
- data/lib/vimamsa/debug.rb +6 -3
- data/lib/vimamsa/demo_player.rb +458 -0
- data/lib/vimamsa/demo_recorder.rb +277 -0
- data/lib/vimamsa/desktop_install.rb +96 -0
- data/lib/vimamsa/diff_buffer.rb +40 -11
- data/lib/vimamsa/dot_repeat.rb +104 -0
- data/lib/vimamsa/easy_jump.rb +57 -3
- data/lib/vimamsa/editor.rb +327 -78
- data/lib/vimamsa/encrypt.rb +78 -29
- data/lib/vimamsa/file_finder.rb +4 -19
- data/lib/vimamsa/file_history.rb +4 -1
- data/lib/vimamsa/file_manager.rb +46 -31
- data/lib/vimamsa/fold.rb +226 -0
- data/lib/vimamsa/form_input.rb +388 -0
- data/lib/vimamsa/gui.rb +603 -119
- data/lib/vimamsa/gui_command_line.rb +247 -0
- data/lib/vimamsa/gui_dialog.rb +94 -42
- data/lib/vimamsa/gui_file_panel.rb +356 -25
- data/lib/vimamsa/gui_form_generator.rb +75 -40
- data/lib/vimamsa/gui_func_panel.rb +67 -22
- data/lib/vimamsa/gui_image.rb +3 -0
- data/lib/vimamsa/gui_key_hint.rb +165 -0
- data/lib/vimamsa/gui_keylog_panel.rb +408 -0
- data/lib/vimamsa/gui_menu.rb +84 -53
- data/lib/vimamsa/gui_select_window.rb +80 -75
- data/lib/vimamsa/gui_settings.rb +86 -41
- data/lib/vimamsa/gui_sourceview.rb +280 -92
- data/lib/vimamsa/gui_sourceview_autocomplete.rb +319 -90
- data/lib/vimamsa/gui_text.rb +63 -2
- data/lib/vimamsa/gui_theme.rb +615 -0
- data/lib/vimamsa/hook.rb +5 -0
- data/lib/vimamsa/hyper_plain_text.rb +3 -0
- data/lib/vimamsa/intro_page.rb +368 -0
- data/lib/vimamsa/key_actions.rb +382 -193
- data/lib/vimamsa/key_binding_tree.rb +467 -273
- data/lib/vimamsa/key_bindings_notepad.rb +45 -0
- data/lib/vimamsa/key_bindings_report.rb +484 -0
- data/lib/vimamsa/key_bindings_vim.rb +104 -0
- data/lib/vimamsa/key_bindings_vimlike.rb +353 -289
- data/lib/vimamsa/key_names.rb +80 -0
- data/lib/vimamsa/langservp.rb +271 -54
- data/lib/vimamsa/line_tree.rb +367 -0
- data/lib/vimamsa/macro.rb +23 -3
- data/lib/vimamsa/main.rb +14 -8
- data/lib/vimamsa/rbvma.rb +41 -1
- data/lib/vimamsa/registers.rb +125 -0
- data/lib/vimamsa/run_ruby.rb +64 -0
- data/lib/vimamsa/search.rb +3 -0
- data/lib/vimamsa/search_replace.rb +15 -14
- data/lib/vimamsa/string_util.rb +134 -40
- data/lib/vimamsa/terminal.rb +3 -0
- data/lib/vimamsa/test_framework.rb +28 -2
- data/lib/vimamsa/tests.rb +11 -8
- data/lib/vimamsa/text_objects.rb +310 -0
- data/lib/vimamsa/text_transforms.rb +18 -0
- data/lib/vimamsa/tmp_user_dir.rb +39 -0
- data/lib/vimamsa/user_dirs.rb +83 -0
- data/lib/vimamsa/util.rb +46 -7
- data/lib/vimamsa/version.rb +1 -1
- data/lib/vimamsa/vim_core.rb +355 -0
- data/lib/vimamsa/vim_operators.rb +365 -0
- data/lib/vimamsa.rb +0 -1
- data/modules/calculator/calculator.rb +5 -5
- data/modules/color_highlight/color_highlight.rb +118 -0
- data/modules/color_highlight/color_highlight_info.rb +3 -0
- data/modules/dictation/dictation.rb +1054 -0
- data/modules/dictation/dictation_info.rb +3 -0
- data/modules/dictation/whisper_worker.py +273 -0
- data/modules/ollama/ollama.rb +107 -0
- data/modules/ollama/ollama_info.rb +3 -0
- data/modules/terminal/terminal.rb +5 -5
- data/modules/tts/tts.rb +119 -0
- data/modules/tts/tts_info.rb +3 -0
- data/run_e2e_vm.sh +28 -0
- data/run_tests_vm.rb +138 -0
- data/scripts/appimage/AppRun +54 -0
- data/scripts/appimage/Dockerfile +18 -0
- data/scripts/appimage/build.sh +19 -0
- data/scripts/appimage/build_in_container.sh +192 -0
- data/scripts/appimage/smoke_test.sh +27 -0
- data/scripts/demo/demo_easyjump_steps.txt +60 -0
- data/scripts/demo/demo_files_steps.txt +52 -0
- data/scripts/demo/demo_steps.txt +67 -0
- data/scripts/demo/notes.txt +6 -0
- data/scripts/demo/record_demo.rb +156 -0
- data/scripts/demo/report.rb +7 -0
- data/scripts/demo/shapes.rb +29 -0
- data/scripts/demo/square/shapes.rb +29 -0
- data/scripts/demo/square/steps.txt +185 -0
- data/scripts/lib/perf_startup.rb +138 -0
- data/scripts/lib/script_helpers.rb +53 -0
- data/scripts/perf_compare.rb +34 -0
- data/scripts/profile_startup.rb +72 -0
- data/styles/134272-molokai.xml +3 -0
- data/styles/molokai_cyber.xml +48 -0
- data/styles/molokai_edit.xml +1 -0
- data/tests/support/virtual_keyboard.rb +254 -0
- data/tests/support/virtual_mic.rb +67 -0
- data/tests/test_ack.rb +22 -0
- data/tests/test_action_ids.rb +232 -0
- data/tests/test_auto_format.rb +122 -0
- data/tests/test_autocomplete.rb +211 -0
- data/tests/test_autosave.rb +145 -0
- data/tests/test_basic_editing.rb +46 -16
- data/tests/test_bookmarks.rb +206 -0
- data/tests/test_bracket_match.rb +88 -0
- data/tests/test_bundling.rb +92 -0
- data/tests/test_cli_help.rb +64 -0
- data/tests/test_color_highlight.rb +92 -0
- data/tests/test_command_line.rb +79 -0
- data/tests/test_command_line_file.rb +27 -0
- data/tests/test_context_menu_file.rb +147 -0
- data/tests/test_copy_paste.rb +106 -26
- data/tests/test_crash_handler.rb +39 -0
- data/tests/test_custom_two_phase.rb +92 -0
- data/tests/test_demo_player.rb +301 -0
- data/tests/test_demo_recorder.rb +202 -0
- data/tests/test_dialog_callback_errors.rb +44 -0
- data/tests/test_dictation.rb +294 -0
- data/tests/test_diff_buffer_name.rb +51 -0
- data/tests/test_dot_repeat.rb +88 -0
- data/tests/test_double_click.rb +41 -0
- data/tests/test_drag_selection.rb +125 -0
- data/tests/test_e2e_cmdline.rb +116 -0
- data/tests/test_e2e_dictation.rb +98 -0
- data/tests/test_e2e_replace_char.rb +58 -0
- data/tests/test_easy_jump.rb +93 -0
- data/tests/test_extract_file_links.rb +55 -0
- data/tests/test_file_format.rb +217 -0
- data/tests/test_file_manager.rb +72 -0
- data/tests/test_file_panel.rb +269 -0
- data/tests/test_flash.rb +78 -0
- data/tests/test_fold.rb +111 -0
- data/tests/test_font_refresh.rb +56 -0
- data/tests/test_form_input.rb +203 -0
- data/tests/test_func_panel.rb +68 -0
- data/tests/test_grep_apply.rb +46 -0
- data/tests/test_gtk_sync.rb +126 -0
- data/tests/test_hpt_highlight_visible.rb +75 -0
- data/tests/test_hpt_link_highlight.rb +82 -0
- data/tests/test_intro_page.rb +437 -0
- data/tests/test_jump_to_file.rb +41 -0
- data/tests/test_key_bindings.rb +208 -0
- data/tests/test_key_categories.rb +163 -0
- data/tests/test_keylog_panel.rb +261 -0
- data/tests/test_line_ends.rb +51 -0
- data/tests/test_line_tree.rb +86 -0
- data/tests/test_lsp_clangd.rb +281 -0
- data/tests/test_lsp_jump.rb +87 -0
- data/tests/test_lsp_position_encoding.rb +49 -0
- data/tests/test_lsp_symbols.rb +68 -0
- data/tests/test_macro.rb +137 -0
- data/tests/test_markdown_highlight.rb +57 -0
- data/tests/test_mode_badge.rb +41 -0
- data/tests/test_module_interface.rb +9 -9
- data/tests/test_notepad_bindings.rb +67 -0
- data/tests/test_perf_startup.rb +28 -0
- data/tests/test_popup_queue.rb +51 -0
- data/tests/test_random_edit.rb +109 -0
- data/tests/test_registers.rb +122 -0
- data/tests/test_revert.rb +127 -0
- data/tests/test_run_ruby.rb +77 -0
- data/tests/test_startup_file.rb +58 -0
- data/tests/test_status_area.rb +50 -0
- data/tests/test_text_objects.rb +163 -0
- data/tests/test_text_transforms.rb +341 -0
- data/tests/test_tmp_user_dir.rb +79 -0
- data/tests/test_tts.rb +68 -0
- data/tests/test_undo.rb +34 -34
- data/tests/test_user_dirs.rb +122 -0
- data/tests/test_vim_bindings.rb +353 -0
- data/tests/test_vim_conflicts.rb +100 -0
- data/tests/test_vim_operators.rb +116 -0
- data/tests/test_window_layout.rb +302 -0
- data/vimamsa.gemspec +3 -1
- data/vm_guest_run.sh +129 -0
- metadata +167 -10
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Persistent faster-whisper transcription worker for the Vimamsa dictation module.
|
|
3
|
+
|
|
4
|
+
Loads the model once and stays alive, transcribing audio files on demand so that
|
|
5
|
+
repeated dictation is fast (no per-call model reload). The Ruby side
|
|
6
|
+
(modules/dictation/dictation.rb) spawns this, reads the readiness line, then feeds
|
|
7
|
+
one WAV path per line and reads one JSON response per line.
|
|
8
|
+
|
|
9
|
+
Protocol (line-based, JSON so transcripts with newlines stay one line):
|
|
10
|
+
startup -> {"progress": {"phase": "download", "fraction": 0.42}} (repeatable)
|
|
11
|
+
-> {"progress": {"phase": "load"}}
|
|
12
|
+
-> {"ready": true} (model loaded)
|
|
13
|
+
or {"ready": false, "error": "..."} (load/deps failed; then exits)
|
|
14
|
+
stdin <- /path/to/audio.wav\\n (bare path), or
|
|
15
|
+
<- {"path": "...", "initial_prompt": "...", "beam_size": 1,
|
|
16
|
+
"normalize": false, "vad": false}\\n (per-request overrides)
|
|
17
|
+
stdout -> {"progress": {"phase": "transcribe", "fraction": 0.6}} (repeatable)
|
|
18
|
+
-> {"ok": true, "text": "..."} (per request)
|
|
19
|
+
or {"ok": false, "error": "..."}
|
|
20
|
+
|
|
21
|
+
"progress" lines may appear at any point before the terminating line of a
|
|
22
|
+
startup or a request; a client that does not care can skip any line carrying a
|
|
23
|
+
"progress" key and keep reading.
|
|
24
|
+
|
|
25
|
+
The transcription logic mirrors ~/Drive/util/bin/transcribe_audio.py (faster-whisper
|
|
26
|
+
+ ffmpeg level normalization + Silero VAD); it is kept self-contained here so the
|
|
27
|
+
editor does not depend on that personal script.
|
|
28
|
+
|
|
29
|
+
Requires: faster-whisper (pip install faster-whisper) and ffmpeg on PATH.
|
|
30
|
+
"""
|
|
31
|
+
|
|
32
|
+
import argparse
|
|
33
|
+
import json
|
|
34
|
+
import os
|
|
35
|
+
import subprocess
|
|
36
|
+
import sys
|
|
37
|
+
import tempfile
|
|
38
|
+
import threading
|
|
39
|
+
import time
|
|
40
|
+
from contextlib import contextmanager
|
|
41
|
+
|
|
42
|
+
# ffmpeg filter chain that evens out uneven speaker levels before ASR, so quiet
|
|
43
|
+
# speech isn't dropped by the VAD/decoder. Mirrors transcribe_audio.py.
|
|
44
|
+
NORMALIZE_FILTER = "afftdn=nr=12:nf=-25,dynaudnorm=f=150:g=7:p=0.9:m=25"
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def _emit(obj):
|
|
48
|
+
"""Write one JSON object as a single flushed line on stdout."""
|
|
49
|
+
sys.stdout.write(json.dumps(obj) + "\n")
|
|
50
|
+
sys.stdout.flush()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def _emit_progress(phase, fraction=None):
|
|
54
|
+
"""Report progress of a long phase. `fraction` is 0..1, or None when the
|
|
55
|
+
phase has no measurable progress (model load)."""
|
|
56
|
+
p = {"phase": phase}
|
|
57
|
+
if fraction is not None:
|
|
58
|
+
p["fraction"] = round(min(max(fraction, 0.0), 1.0), 4)
|
|
59
|
+
_emit({"progress": p})
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
MODEL_PATTERNS = ["config.json", "preprocessor_config.json", "model.bin",
|
|
63
|
+
"tokenizer.json", "vocabulary.*"]
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _repo_total_bytes(repo):
|
|
67
|
+
"""Total size of the files we are about to fetch, from the hub API. Returns
|
|
68
|
+
0 if it can't be determined, which disables percentage reporting."""
|
|
69
|
+
import fnmatch
|
|
70
|
+
from huggingface_hub import HfApi
|
|
71
|
+
|
|
72
|
+
info = HfApi().model_info(repo, files_metadata=True)
|
|
73
|
+
return sum(s.size or 0 for s in info.siblings
|
|
74
|
+
if any(fnmatch.fnmatch(s.rfilename, p) for p in MODEL_PATTERNS))
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _repo_cached_bytes(repo):
|
|
78
|
+
"""Bytes of this repo currently on disk, counting partial downloads. Only
|
|
79
|
+
the blobs dir holds real data — snapshots are symlinks into it, so walking
|
|
80
|
+
both would double count."""
|
|
81
|
+
from huggingface_hub.constants import HF_HUB_CACHE
|
|
82
|
+
|
|
83
|
+
blobs = os.path.join(HF_HUB_CACHE, "models--" + repo.replace("/", "--"), "blobs")
|
|
84
|
+
total = 0
|
|
85
|
+
for root, _dirs, files in os.walk(blobs):
|
|
86
|
+
for name in files:
|
|
87
|
+
path = os.path.join(root, name)
|
|
88
|
+
if os.path.islink(path):
|
|
89
|
+
continue
|
|
90
|
+
try:
|
|
91
|
+
total += os.path.getsize(path)
|
|
92
|
+
except OSError:
|
|
93
|
+
pass # file swapped out from under us mid-download
|
|
94
|
+
return total
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
@contextmanager
|
|
98
|
+
def _download_progress(repo):
|
|
99
|
+
"""Report download progress by watching the cache grow toward the repo's
|
|
100
|
+
known total size.
|
|
101
|
+
|
|
102
|
+
Deliberately not hooked into the transfer machinery: hub's progress bars
|
|
103
|
+
report an "incomplete total" for Xet-backed repos, so their byte totals are
|
|
104
|
+
unusable, and the details differ between hub versions. Sizes on disk do not.
|
|
105
|
+
"""
|
|
106
|
+
try:
|
|
107
|
+
total = _repo_total_bytes(repo)
|
|
108
|
+
except Exception:
|
|
109
|
+
total = 0
|
|
110
|
+
if not total:
|
|
111
|
+
yield
|
|
112
|
+
return
|
|
113
|
+
|
|
114
|
+
stop = threading.Event()
|
|
115
|
+
|
|
116
|
+
def poll():
|
|
117
|
+
while not stop.wait(0.5):
|
|
118
|
+
try:
|
|
119
|
+
_emit_progress("download", _repo_cached_bytes(repo) / total)
|
|
120
|
+
except Exception:
|
|
121
|
+
pass
|
|
122
|
+
|
|
123
|
+
_emit_progress("download", _repo_cached_bytes(repo) / total)
|
|
124
|
+
watcher = threading.Thread(target=poll, daemon=True)
|
|
125
|
+
watcher.start()
|
|
126
|
+
try:
|
|
127
|
+
yield
|
|
128
|
+
finally:
|
|
129
|
+
stop.set()
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _fetch_model(name):
|
|
133
|
+
"""Download the model snapshot up front so a first run reports progress
|
|
134
|
+
instead of sitting silent for several GB. Returns a local path, or `name`
|
|
135
|
+
unchanged when it is not a hub id we can resolve — WhisperModel then does
|
|
136
|
+
its own (silent) resolution, which is the pre-existing behaviour.
|
|
137
|
+
"""
|
|
138
|
+
if os.path.isdir(name):
|
|
139
|
+
return name
|
|
140
|
+
try:
|
|
141
|
+
from faster_whisper.utils import _MODELS
|
|
142
|
+
import huggingface_hub
|
|
143
|
+
|
|
144
|
+
repo = _MODELS.get(name, name)
|
|
145
|
+
if "/" not in repo:
|
|
146
|
+
return name
|
|
147
|
+
with _download_progress(repo):
|
|
148
|
+
return huggingface_hub.snapshot_download(
|
|
149
|
+
repo, allow_patterns=MODEL_PATTERNS)
|
|
150
|
+
except Exception:
|
|
151
|
+
# Offline, private repo, unexpected hub version: fall back rather than
|
|
152
|
+
# failing startup over a progress-reporting nicety.
|
|
153
|
+
return name
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
@contextmanager
|
|
157
|
+
def _normalized_audio(audio_path, normalize):
|
|
158
|
+
"""Yield a path to feed the decoder: the original file, or a normalized temp wav."""
|
|
159
|
+
if not normalize:
|
|
160
|
+
yield audio_path
|
|
161
|
+
return
|
|
162
|
+
with tempfile.NamedTemporaryFile(suffix=".wav", delete=True) as tmp:
|
|
163
|
+
cmd = ["ffmpeg", "-nostdin", "-v", "error", "-y", "-i", audio_path,
|
|
164
|
+
"-ac", "1", "-ar", "16000", "-af", NORMALIZE_FILTER, tmp.name]
|
|
165
|
+
subprocess.run(cmd, check=True)
|
|
166
|
+
yield tmp.name
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def main():
|
|
170
|
+
ap = argparse.ArgumentParser(description=__doc__.splitlines()[0])
|
|
171
|
+
ap.add_argument("--model", default="large-v3", help="faster-whisper model")
|
|
172
|
+
ap.add_argument("--language", default="en", help="spoken language code")
|
|
173
|
+
ap.add_argument("--device", default="auto", help="auto, cuda, or cpu")
|
|
174
|
+
ap.add_argument("--compute-type", default="float16",
|
|
175
|
+
help="compute type, e.g. float16 or int8")
|
|
176
|
+
ap.add_argument("--no-normalize", action="store_true",
|
|
177
|
+
help="skip ffmpeg level normalization (on by default)")
|
|
178
|
+
ap.add_argument("--initial-prompt", default="",
|
|
179
|
+
help="text prompt that biases decoding toward a vocabulary/style")
|
|
180
|
+
ap.add_argument("--beam-size", type=int, default=5, help="decoding beam size")
|
|
181
|
+
ap.add_argument("--idle-timeout", type=float, default=300.0,
|
|
182
|
+
help="exit after this many seconds of inactivity to release "
|
|
183
|
+
"VRAM (<= 0 disables)")
|
|
184
|
+
args = ap.parse_args()
|
|
185
|
+
normalize = not args.no_normalize
|
|
186
|
+
initial_prompt = args.initial_prompt or None
|
|
187
|
+
|
|
188
|
+
# Load the model once. Report failure (missing deps, bad device, OOM) and exit.
|
|
189
|
+
try:
|
|
190
|
+
from faster_whisper import WhisperModel
|
|
191
|
+
model_path = _fetch_model(args.model)
|
|
192
|
+
_emit_progress("load")
|
|
193
|
+
model = WhisperModel(model_path, device=args.device,
|
|
194
|
+
compute_type=args.compute_type)
|
|
195
|
+
except Exception as e: # noqa: BLE001 - report any startup failure to the client
|
|
196
|
+
_emit({"ready": False, "error": f"{type(e).__name__}: {e}"})
|
|
197
|
+
return 1
|
|
198
|
+
|
|
199
|
+
# Exit the whole process after a period of inactivity so the model weights and
|
|
200
|
+
# the CUDA context are fully released. `busy` keeps us alive mid-transcription
|
|
201
|
+
# even if a long final pass exceeds the timeout. The Ruby side respawns us on
|
|
202
|
+
# the next dictation.
|
|
203
|
+
state = {"last": time.monotonic(), "busy": False}
|
|
204
|
+
|
|
205
|
+
def _idle_watch(timeout):
|
|
206
|
+
while True:
|
|
207
|
+
time.sleep(min(max(timeout, 1.0), 15.0))
|
|
208
|
+
if not state["busy"] and (time.monotonic() - state["last"]) > timeout:
|
|
209
|
+
os._exit(0)
|
|
210
|
+
|
|
211
|
+
if args.idle_timeout > 0:
|
|
212
|
+
threading.Thread(target=_idle_watch, args=(args.idle_timeout,),
|
|
213
|
+
daemon=True).start()
|
|
214
|
+
|
|
215
|
+
_emit({"ready": True})
|
|
216
|
+
|
|
217
|
+
# One request per stdin line; one JSON response per line.
|
|
218
|
+
# A request is either a bare WAV path, or a JSON object with per-request
|
|
219
|
+
# overrides: {"path", "initial_prompt", "beam_size", "normalize", "vad"}.
|
|
220
|
+
# The CLI args above supply the defaults (used for the preliminary passes,
|
|
221
|
+
# which send fast overrides; the final pass sends high-quality overrides).
|
|
222
|
+
for line in sys.stdin:
|
|
223
|
+
line = line.strip()
|
|
224
|
+
if not line:
|
|
225
|
+
continue
|
|
226
|
+
state["busy"] = True
|
|
227
|
+
state["last"] = time.monotonic()
|
|
228
|
+
try:
|
|
229
|
+
if line.startswith("{"):
|
|
230
|
+
req = json.loads(line)
|
|
231
|
+
path = req["path"]
|
|
232
|
+
req_prompt = req.get("initial_prompt", args.initial_prompt) or None
|
|
233
|
+
req_beam = int(req.get("beam_size", args.beam_size))
|
|
234
|
+
req_norm = bool(req.get("normalize", normalize))
|
|
235
|
+
req_vad = bool(req.get("vad", True))
|
|
236
|
+
else:
|
|
237
|
+
path = line
|
|
238
|
+
req_prompt, req_beam, req_norm, req_vad = (
|
|
239
|
+
initial_prompt, args.beam_size, normalize, True)
|
|
240
|
+
|
|
241
|
+
with _normalized_audio(path, req_norm) as source:
|
|
242
|
+
segments_iter, info = model.transcribe(
|
|
243
|
+
source,
|
|
244
|
+
language=args.language,
|
|
245
|
+
vad_filter=req_vad,
|
|
246
|
+
beam_size=req_beam,
|
|
247
|
+
initial_prompt=req_prompt,
|
|
248
|
+
)
|
|
249
|
+
# transcribe() is lazy: decoding happens as segments are pulled.
|
|
250
|
+
# Each carries an end timestamp, so position within the clip is
|
|
251
|
+
# a usable progress measure for a long final pass.
|
|
252
|
+
duration = getattr(info, "duration", 0) or 0
|
|
253
|
+
parts = []
|
|
254
|
+
last = 0.0
|
|
255
|
+
for seg in segments_iter:
|
|
256
|
+
if seg.text.strip():
|
|
257
|
+
parts.append(seg.text.strip())
|
|
258
|
+
now = time.monotonic()
|
|
259
|
+
if duration and now - last > 0.5:
|
|
260
|
+
last = now
|
|
261
|
+
_emit_progress("transcribe", seg.end / duration)
|
|
262
|
+
_emit({"ok": True, "text": " ".join(parts)})
|
|
263
|
+
except Exception as e: # noqa: BLE001 - keep the worker alive across errors
|
|
264
|
+
_emit({"ok": False, "error": f"{type(e).__name__}: {e}"})
|
|
265
|
+
finally:
|
|
266
|
+
state["last"] = time.monotonic()
|
|
267
|
+
state["busy"] = False
|
|
268
|
+
|
|
269
|
+
return 0
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
if __name__ == "__main__":
|
|
273
|
+
sys.exit(main())
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
require "net/http"
|
|
2
|
+
require "json"
|
|
3
|
+
|
|
4
|
+
def ollama_improve_selection
|
|
5
|
+
return unless vma.buf.visual_mode?
|
|
6
|
+
r = vma.buf.get_visual_mode_range
|
|
7
|
+
txt = vma.buf[r]
|
|
8
|
+
return if txt.nil? || txt.strip.empty?
|
|
9
|
+
|
|
10
|
+
vma.buf.end_visual_mode
|
|
11
|
+
message("Ollama: thinking…")
|
|
12
|
+
|
|
13
|
+
Thread.new do
|
|
14
|
+
result = ollama_request(txt)
|
|
15
|
+
GLib::Idle.add do
|
|
16
|
+
if result
|
|
17
|
+
vma.buf.replace_range(r, result)
|
|
18
|
+
vma.buf.view.handle_deltas
|
|
19
|
+
message("Ollama: done")
|
|
20
|
+
else
|
|
21
|
+
message("Ollama: request failed — is ollama running?")
|
|
22
|
+
end
|
|
23
|
+
false
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def ollama_improve_selection_multi(n = 3)
|
|
29
|
+
return unless vma.buf.visual_mode?
|
|
30
|
+
|
|
31
|
+
r = vma.buf.get_visual_mode_range
|
|
32
|
+
txt = vma.buf[r]
|
|
33
|
+
return if txt.nil? || txt.strip.empty?
|
|
34
|
+
|
|
35
|
+
# Find end of paragraph: first blank line after selection end, or end of buffer
|
|
36
|
+
buf_str = vma.buf.to_s
|
|
37
|
+
blank = buf_str.index(/\n[ \t]*\n/, r.last)
|
|
38
|
+
para_end = blank || buf_str.rindex("\n") || buf_str.size - 1
|
|
39
|
+
|
|
40
|
+
vma.buf.end_visual_mode
|
|
41
|
+
message("Ollama: generating #{n} variants…")
|
|
42
|
+
|
|
43
|
+
Thread.new do
|
|
44
|
+
temp = cnf.ollama.multi_temperature! || 1.2
|
|
45
|
+
threads = n.times.map { Thread.new { ollama_request(txt, temperature: temp) } }
|
|
46
|
+
results = threads.map(&:value)
|
|
47
|
+
GLib::Idle.add do
|
|
48
|
+
successful = results.compact
|
|
49
|
+
if successful.any?
|
|
50
|
+
block = successful.map.with_index(1) { |v, i| "#{i}. #{v}" }.join("\n")
|
|
51
|
+
vma.buf.insert_txt_at(block, para_end + 1)
|
|
52
|
+
vma.buf.view.handle_deltas
|
|
53
|
+
message("Ollama: inserted #{successful.size} variant(s)")
|
|
54
|
+
else
|
|
55
|
+
message("Ollama: request failed — is ollama running?")
|
|
56
|
+
end
|
|
57
|
+
false
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def ollama_request(text, temperature: nil)
|
|
63
|
+
model = cnf.ollama.model! || "qwen3.5:0.8b"
|
|
64
|
+
host = cnf.ollama.host! || "localhost"
|
|
65
|
+
port = cnf.ollama.port! || 11434
|
|
66
|
+
prompt = "Improve the following English text. " \
|
|
67
|
+
"Return only the improved text, no explanation:\n\n#{text}"
|
|
68
|
+
|
|
69
|
+
uri = URI::HTTP.build(host: host, port: port, path: "/api/generate")
|
|
70
|
+
req_body = { model: model, prompt: prompt, stream: false, think: false }
|
|
71
|
+
req_body[:options] = { temperature: temperature } if temperature
|
|
72
|
+
body = JSON.generate(req_body)
|
|
73
|
+
|
|
74
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
|
75
|
+
http.read_timeout = cnf.ollama.timeout! || 60
|
|
76
|
+
req = Net::HTTP::Post.new(uri.path, "Content-Type" => "application/json")
|
|
77
|
+
req.body = body
|
|
78
|
+
|
|
79
|
+
resp = http.request(req)
|
|
80
|
+
# require "pry";binding.pry
|
|
81
|
+
return nil unless resp.is_a?(Net::HTTPSuccess)
|
|
82
|
+
|
|
83
|
+
JSON.parse(resp.body)["response"]&.strip
|
|
84
|
+
rescue => e
|
|
85
|
+
debug "Ollama error: #{e}"
|
|
86
|
+
nil
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def ollama_init
|
|
90
|
+
reg_act("ollama.improve_selection",
|
|
91
|
+
proc { ollama_improve_selection },
|
|
92
|
+
"Ollama: improve selected text")
|
|
93
|
+
reg_act("ollama.improve_selection_multi",
|
|
94
|
+
proc { ollama_improve_selection_multi },
|
|
95
|
+
"Ollama: generate N improved variants below paragraph")
|
|
96
|
+
|
|
97
|
+
bindings = { "V , o i" => "ollama.improve_selection" }
|
|
98
|
+
(1..9).each { |n| bindings["V , o #{n} i"] = "ollama_improve_selection_multi(#{n})" }
|
|
99
|
+
add_keys "ollama", bindings
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def ollama_disable
|
|
103
|
+
unreg_act("ollama.improve_selection")
|
|
104
|
+
unreg_act("ollama.improve_selection_multi")
|
|
105
|
+
unbindkey "V , o i"
|
|
106
|
+
(1..9).each { |n| unbindkey "V , o #{n} i" }
|
|
107
|
+
end
|
|
@@ -129,12 +129,12 @@ end
|
|
|
129
129
|
|
|
130
130
|
def terminal_init
|
|
131
131
|
require "vte4"
|
|
132
|
-
reg_act(
|
|
133
|
-
# add_keys "terminal", { "C , t" =>
|
|
134
|
-
vma.gui.menu.add_module_action(
|
|
132
|
+
reg_act("terminal.insert", proc { insert_terminal_in_buffer }, "Insert embedded terminal at cursor")
|
|
133
|
+
# add_keys "terminal", { "C , t" => "terminal.insert" }
|
|
134
|
+
vma.gui.menu.add_module_action("terminal.insert", "Insert Terminal")
|
|
135
135
|
end
|
|
136
136
|
|
|
137
137
|
def terminal_disable
|
|
138
|
-
unreg_act(
|
|
139
|
-
vma.gui.menu.remove_module_action(
|
|
138
|
+
unreg_act("terminal.insert")
|
|
139
|
+
vma.gui.menu.remove_module_action("terminal.insert")
|
|
140
140
|
end
|
data/modules/tts/tts.rb
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
require "open3"
|
|
2
|
+
require "json"
|
|
3
|
+
|
|
4
|
+
# Text-to-Speech module
|
|
5
|
+
#
|
|
6
|
+
# Select text (visual mode) and activate to hear it spoken aloud with Piper TTS.
|
|
7
|
+
# Mirrors ~/Drive/util/bin/speak.rb: piper --output-raw | aplay, with the sample
|
|
8
|
+
# rate auto-detected from the voice's .onnx.json. Playback runs in a background
|
|
9
|
+
# thread so the editor stays responsive, and can be stopped.
|
|
10
|
+
#
|
|
11
|
+
# V , s speak the current selection
|
|
12
|
+
# C , q stop speaking
|
|
13
|
+
#
|
|
14
|
+
# Configuration (set in ~/.config/vimamsa/settings.rb via cnf, all optional):
|
|
15
|
+
# cnf.tts.voice Piper voice name (default "en_GB-alan-medium")
|
|
16
|
+
# cnf.tts.data_dir voices directory (default "~/piper-voices")
|
|
17
|
+
# cnf.tts.rate sample rate (Hz) (default: from <voice>.onnx.json, else 22050)
|
|
18
|
+
#
|
|
19
|
+
# Requires piper and aplay on PATH and a Piper voice installed in data_dir.
|
|
20
|
+
|
|
21
|
+
class VmaTts
|
|
22
|
+
def initialize
|
|
23
|
+
@mutex = Mutex.new
|
|
24
|
+
@pids = []
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Speak `text` aloud. Cancels any current playback first. Non-blocking.
|
|
28
|
+
def speak(text)
|
|
29
|
+
return if text.nil? || text.strip.empty?
|
|
30
|
+
unless which("piper") && which("aplay")
|
|
31
|
+
message("TTS: need 'piper' and 'aplay' on PATH")
|
|
32
|
+
return
|
|
33
|
+
end
|
|
34
|
+
stop # one utterance at a time
|
|
35
|
+
|
|
36
|
+
voice = cnf.tts.voice! || "en_GB-alan-medium"
|
|
37
|
+
data_dir = File.expand_path(cnf.tts.data_dir! || "~/piper-voices")
|
|
38
|
+
rate = (cnf.tts.rate! || detect_rate(data_dir, voice) || 22050).to_s
|
|
39
|
+
|
|
40
|
+
piper = ["piper", "-m", voice, "--data-dir", data_dir, "--output-raw"]
|
|
41
|
+
player = ["aplay", "-q", "-r", rate, "-f", "S16_LE", "-t", "raw", "-"]
|
|
42
|
+
|
|
43
|
+
Thread.new do
|
|
44
|
+
begin
|
|
45
|
+
stdin, threads = Open3.pipeline_w(piper, player)
|
|
46
|
+
@mutex.synchronize { @pids = threads.map(&:pid) }
|
|
47
|
+
stdin.write(text)
|
|
48
|
+
stdin.close
|
|
49
|
+
threads.each(&:join)
|
|
50
|
+
rescue => e
|
|
51
|
+
GLib::Idle.add { message("TTS error: #{e.message}"); false }
|
|
52
|
+
ensure
|
|
53
|
+
@mutex.synchronize { @pids = [] }
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Stop any in-progress playback.
|
|
59
|
+
def stop
|
|
60
|
+
@mutex.synchronize do
|
|
61
|
+
@pids.each { |pid| Process.kill("TERM", pid) rescue nil }
|
|
62
|
+
@pids = []
|
|
63
|
+
end
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
private
|
|
67
|
+
|
|
68
|
+
# Read the voice's preferred sample rate from <data_dir>/<voice>.onnx.json.
|
|
69
|
+
def detect_rate(data_dir, voice)
|
|
70
|
+
cfg = File.join(data_dir, "#{voice}.onnx.json")
|
|
71
|
+
return nil unless File.exist?(cfg)
|
|
72
|
+
JSON.parse(File.read(cfg)).dig("audio", "sample_rate")
|
|
73
|
+
rescue
|
|
74
|
+
nil
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def tts_engine
|
|
79
|
+
$vma_tts ||= VmaTts.new
|
|
80
|
+
end
|
|
81
|
+
|
|
82
|
+
def tts_speak_selection
|
|
83
|
+
unless vma.buf.visual_mode?
|
|
84
|
+
message("TTS: select text first")
|
|
85
|
+
return
|
|
86
|
+
end
|
|
87
|
+
r = vma.buf.get_visual_mode_range
|
|
88
|
+
text = vma.buf[r]
|
|
89
|
+
vma.buf.end_visual_mode
|
|
90
|
+
if text.nil? || text.strip.empty?
|
|
91
|
+
message("TTS: nothing selected")
|
|
92
|
+
return
|
|
93
|
+
end
|
|
94
|
+
tts_engine.speak(text)
|
|
95
|
+
message("TTS: speaking…")
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def tts_stop
|
|
99
|
+
$vma_tts&.stop
|
|
100
|
+
message("TTS: stopped")
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def tts_init
|
|
104
|
+
reg_act("tts.speak_selection", proc { tts_speak_selection }, "Speak selected text")
|
|
105
|
+
reg_act("tts.stop", proc { tts_stop }, "Stop speaking")
|
|
106
|
+
add_keys "tts", { "V , s" => "tts.speak_selection", "C , q" => "tts.stop" }
|
|
107
|
+
vma.gui.menu.add_module_action("tts.speak_selection", "Speak Selection")
|
|
108
|
+
vma.gui.menu.add_module_action("tts.stop", "Stop Speaking")
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def tts_disable
|
|
112
|
+
$vma_tts&.stop
|
|
113
|
+
unreg_act("tts.speak_selection")
|
|
114
|
+
unreg_act("tts.stop")
|
|
115
|
+
unbindkey "V , s"
|
|
116
|
+
unbindkey "C , q"
|
|
117
|
+
vma.gui.menu.remove_module_action("tts.speak_selection")
|
|
118
|
+
vma.gui.menu.remove_module_action("tts.stop")
|
|
119
|
+
end
|
data/run_e2e_vm.sh
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# Run ONLY the uinput E2E tests in the test VM ($VM_NAME), semi-interactively.
|
|
3
|
+
#
|
|
4
|
+
# These tests type real key events through a virtual keyboard (uinput), which
|
|
5
|
+
# land in whatever window currently has focus. Wayland won't let a window that
|
|
6
|
+
# was launched non-interactively (via guestcontrol) steal focus, so this needs
|
|
7
|
+
# exactly ONE bit of interaction: when prompted, click the vimamsa window on the
|
|
8
|
+
# VM's desktop once. Every test in the run then finds it already focused.
|
|
9
|
+
#
|
|
10
|
+
# The heavy lifting (sync, provision, uinput/Wayland setup) is shared with
|
|
11
|
+
# run_tests_vm.rb; this wrapper just restricts the run to the E2E file and turns
|
|
12
|
+
# on the interactive focus wait.
|
|
13
|
+
#
|
|
14
|
+
# Usage:
|
|
15
|
+
# (VM_NAME, VM_USER and VM_PASS exported, as for run_tests_vm.rb)
|
|
16
|
+
# ./run_e2e_vm.sh
|
|
17
|
+
# ./run_e2e_vm.sh e2e_cmdline # explicit filter
|
|
18
|
+
set -euo pipefail
|
|
19
|
+
|
|
20
|
+
HERE="$(cd "$(dirname "$0")" && pwd)"
|
|
21
|
+
export VMA_E2E_INTERACTIVE=1
|
|
22
|
+
|
|
23
|
+
echo ">> E2E VM run: the vimamsa window will open on the VM desktop."
|
|
24
|
+
echo ">> When you see the '[E2E] Click the vimamsa window…' prompt, click that"
|
|
25
|
+
echo ">> window once to focus it; the tests then type into it via uinput."
|
|
26
|
+
|
|
27
|
+
# Default to the cmdline E2E file; forward any explicit filter args instead.
|
|
28
|
+
exec "$HERE/run_tests_vm.rb" "${@:-e2e_cmdline}"
|