forked from lmacan1/talktype
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_wizard.py
More file actions
353 lines (290 loc) · 11.6 KB
/
Copy pathsetup_wizard.py
File metadata and controls
353 lines (290 loc) · 11.6 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#!/usr/bin/env python3
"""First-run setup wizard for TalkType."""
from pathlib import Path
import shutil
import sys
from pynput import keyboard
from rich.console import Console
from rich.panel import Panel
from rich.prompt import Prompt
from beaupy import select, Config
from beaupy.spinners import Spinner, DOTS
import yaml
import time
console = Console()
CONFIG_PATH = Path.home() / ".config" / "talktype" / "config.yaml"
# Configure beaupy styling
Config.raise_on_interrupt = True
def capture_hotkey(prompt: str, default: str) -> str:
"""Capture a single keypress from the user."""
import termios
import tty
console.print(f"\n [bold]{prompt}[/bold]")
console.print(f" [dim]Press any key... (default: {default.upper()})[/dim]")
captured = [None]
# Save terminal settings and switch to raw mode (no echo)
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
def on_press(key):
try:
if hasattr(key, 'name'):
captured[0] = key.name.lower()
elif hasattr(key, 'char') and key.char:
captured[0] = key.char.lower()
else:
captured[0] = default
except Exception:
captured[0] = default
return False
try:
tty.setraw(fd)
with keyboard.Listener(on_press=on_press) as listener:
listener.join()
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
result = captured[0] or default
console.print(f" [green]\u2713[/green] Set to [bold cyan]{result.upper()}[/bold cyan]")
return result
def select_option(title: str, options: list[str], cursor_index: int = 0) -> int:
"""Arrow-navigable selection menu using beaupy."""
console.print(f"\n [bold]{title}[/bold]\n")
result = select(
options=options,
cursor=" \u203a ",
cursor_style="cyan bold",
cursor_index=cursor_index,
return_index=True,
)
if result is None:
raise KeyboardInterrupt
return result
def run_wizard() -> dict:
"""Run the interactive setup wizard."""
console.clear()
# Welcome banner
console.print()
console.print(Panel(
"[bold cyan]TalkType Setup[/bold cyan]\n\n"
"Voice typing that works everywhere.\n"
"Let's configure your preferences.",
border_style="cyan",
padding=(1, 3),
width=50
))
config = {"hotkeys": {}, "transcription": {}, "ui": {}, "history": {}}
# Step 1: Mode
console.print()
console.rule("[cyan]Step 1 of 4: Mode[/cyan]", style="dim")
mode_options = [
"Local Server \u2192 Your whisper_server.py",
"Cloud API \u2192 Groq, OpenAI, etc.",
"Local Model \u2192 Standalone (slower startup)",
]
mode_choice = select_option("How do you want to run transcription?", mode_options, 0)
if mode_choice == 0: # Local server
config["transcription"]["mode"] = "api"
config["transcription"]["api_url"] = "http://localhost:8002/transcribe"
elif mode_choice == 1: # Cloud API
config["transcription"]["mode"] = "api"
console.print()
provider_options = [
"Groq \u2192 Free tier, very fast",
"OpenAI \u2192 Official Whisper API",
"Custom \u2192 Enter your own URL",
]
provider = select_option("Select provider:", provider_options, 0)
if provider == 0:
config["transcription"]["api_url"] = "https://api.groq.com/openai/v1/audio/transcriptions"
config["transcription"]["api_model"] = "whisper-large-v3"
console.print("\n [yellow]\u26a0[/yellow] Set [bold]GROQ_API_KEY[/bold] env variable")
elif provider == 1:
config["transcription"]["api_url"] = "https://api.openai.com/v1/audio/transcriptions"
config["transcription"]["api_model"] = "whisper-1"
console.print("\n [yellow]\u26a0[/yellow] Set [bold]OPENAI_API_KEY[/bold] env variable")
else:
console.print()
url = Prompt.ask(" [bold]API URL[/bold]")
config["transcription"]["api_url"] = url
model = Prompt.ask(" [bold]Model name[/bold]", default="whisper-1")
config["transcription"]["api_model"] = model
else: # Local model
config["transcription"]["mode"] = "local"
# Step 2: Hotkeys
console.print()
console.rule("[cyan]Step 2 of 4: Hotkeys[/cyan]", style="dim")
config["hotkeys"]["record"] = capture_hotkey("Press your RECORD hotkey:", "f9")
config["hotkeys"]["retry"] = capture_hotkey("Press your RETRY hotkey:", "f7")
config["hotkeys"]["recovery"] = capture_hotkey("Press your RECOVERY hotkey:", "f8")
# Step 3: Model
console.print()
console.rule("[cyan]Step 3 of 4: Model[/cyan]", style="dim")
models = ["tiny", "base", "small", "medium", "large-v3"]
model_options = [
"tiny \u2192 ~1GB Fastest",
"base \u2192 ~1GB Recommended",
"small \u2192 ~2GB Better accuracy",
"medium \u2192 ~5GB High accuracy",
"large-v3 \u2192 ~10GB Best accuracy",
]
model_choice = select_option("Select Whisper model:", model_options, 1)
config["transcription"]["model"] = models[model_choice]
# Step 4: Language
console.print()
console.rule("[cyan]Step 4 of 4: Language[/cyan]", style="dim")
langs = [None, "en", "es", "fr", "de", None]
lang_options = [
"Auto-detect",
"English",
"Spanish",
"French",
"German",
"Other...",
]
lang_choice = select_option("Language preference:", lang_options, 0)
if lang_choice == 5: # Other
console.print()
code = Prompt.ask(" [bold]Language code[/bold]", default="en")
config["transcription"]["language"] = code
else:
config["transcription"]["language"] = langs[lang_choice]
# Defaults
config["ui"]["minimal"] = False
config["history"]["limit"] = 100
# Save
CONFIG_PATH.parent.mkdir(parents=True, exist_ok=True)
with open(CONFIG_PATH, "w") as f:
yaml.dump(config, f, default_flow_style=False, sort_keys=False)
# Summary
console.print()
console.rule("[green]Setup Complete[/green]", style="dim")
lang_display = config["transcription"]["language"] or "Auto-detect"
mode_map = {"api": "API", "local": "Local Model"}
lines = [
f"[green]\u2713[/green] [bold]TalkType Configured[/bold]",
"",
f" Mode: {mode_map.get(config['transcription']['mode'], 'API')}",
]
if config["transcription"].get("api_url"):
url = config["transcription"]["api_url"]
# Shorten URL for display
if "localhost" in url:
lines.append(f" Server: localhost:8002")
elif "groq" in url:
lines.append(f" Provider: Groq")
elif "openai" in url:
lines.append(f" Provider: OpenAI")
else:
lines.append(f" API: {url[:40]}...")
lines.extend([
f" Model: {config['transcription']['model']}",
f" Language: {lang_display}",
"",
f" [dim]Record:[/dim] {config['hotkeys']['record'].upper()}",
f" [dim]Retry:[/dim] {config['hotkeys']['retry'].upper()}",
f" [dim]Recovery:[/dim] {config['hotkeys']['recovery'].upper()}",
])
console.print(Panel("\n".join(lines), border_style="green", padding=(1, 2), width=50))
# PATH tip
if not shutil.which("talktype"):
venv_bin = Path(__file__).parent / "venv" / "bin"
console.print()
console.print(Panel(
f"[yellow]Tip:[/yellow] To run [bold]talktype[/bold] from anywhere:\n\n"
f" echo 'export PATH=\"$PATH:{venv_bin}\"' >> ~/.zshrc\n"
f" source ~/.zshrc",
title="Add to PATH",
title_align="left",
border_style="yellow",
padding=(1, 2),
width=60
))
# Ask what to do next
console.print()
run_options = [
"Run now \u2192 Start in this terminal",
"Run at startup \u2192 Install as system service (always on)",
"Exit \u2192 Just save config, run later",
]
run_choice = select_option("What would you like to do?", run_options, 0)
if run_choice == 0:
console.print("\n [bold]Starting TalkType...[/bold]\n")
time.sleep(0.3)
return config, True # Run now
elif run_choice == 1:
# Install systemd user service
install_systemd_service(config)
return config, False # Don't run (service handles it)
else:
console.print("\n [green]\u2713[/green] Config saved! Run [bold cyan]talktype[/bold cyan] anytime.\n")
return config, False
def install_systemd_service(config: dict):
"""Install TalkType as a systemd user service."""
import subprocess
service_dir = Path.home() / ".config" / "systemd" / "user"
service_dir.mkdir(parents=True, exist_ok=True)
# Find talktype command - works whether pip installed or run from source
talktype_cmd = shutil.which("talktype")
if talktype_cmd:
cmd_parts = [talktype_cmd]
else:
# Fallback: use current Python + talktype.py
talktype_path = Path(__file__).parent / "talktype.py"
cmd_parts = [sys.executable, str(talktype_path)]
# Build command based on config
if config["transcription"].get("api_url"):
cmd_parts.extend(["--api", config["transcription"]["api_url"]])
if config["transcription"].get("language"):
cmd_parts.extend(["--language", config["transcription"]["language"]])
service_content = f"""[Unit]
Description=TalkType Voice Typing
After=graphical-session.target
[Service]
Type=simple
ExecStart={' '.join(cmd_parts)}
Restart=on-failure
RestartSec=5
Environment=DISPLAY=:0
[Install]
WantedBy=default.target
"""
service_file = service_dir / "talktype.service"
service_file.write_text(service_content)
# Enable and start the service
console.print("\n [dim]Installing systemd service...[/dim]")
try:
subprocess.run(["systemctl", "--user", "daemon-reload"], check=True, capture_output=True)
subprocess.run(["systemctl", "--user", "enable", "talktype.service"], check=True, capture_output=True)
subprocess.run(["systemctl", "--user", "start", "talktype.service"], check=True, capture_output=True)
console.print()
console.print(Panel(
"[green]\u2713[/green] [bold]TalkType installed as system service![/bold]\n\n"
" It will start automatically on login.\n\n"
" [dim]Manage with:[/dim]\n"
" systemctl --user status talktype\n"
" systemctl --user stop talktype\n"
" systemctl --user restart talktype",
border_style="green",
padding=(1, 2),
width=55
))
except subprocess.CalledProcessError as e:
console.print(f"\n [red]\u2717[/red] Failed to install service: {e}")
console.print(f" [dim]Service file saved to: {service_file}[/dim]")
except FileNotFoundError:
console.print("\n [yellow]\u26a0[/yellow] systemctl not found. Service file saved to:")
console.print(f" [dim]{service_file}[/dim]")
def check_first_run() -> bool:
"""Return True if this is first run (no config file)."""
return not CONFIG_PATH.exists()
def load_config() -> dict:
"""Load config from YAML file."""
if not CONFIG_PATH.exists():
return {}
with open(CONFIG_PATH) as f:
return yaml.safe_load(f) or {}
if __name__ == "__main__":
try:
run_wizard()
except KeyboardInterrupt:
console.print("\n [dim]Setup cancelled.[/dim]\n")
sys.exit(0)