feat: add Typer CLI with daemon mode and wtype keyboard injection

Replace argparse CLI with Typer-based CLI supporting `cohere on/off/status`
commands. The daemon runs transcription in the background and types into the
focused Wayland window via wtype. Adds wtype to flake.nix and fixes the
hatchling build backend.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-30 21:09:32 +08:00
parent 8d517b3ea8
commit 92d8ba28d0
8 changed files with 277 additions and 43 deletions
+1
View File
@@ -19,6 +19,7 @@
python314
portaudio
cudaPackages.cudatoolkit
wtype
];
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath [
+3 -1
View File
@@ -14,14 +14,16 @@ dependencies = [
"soundfile>=0.13.1",
"torch>=2.12.0",
"transformers>=5.9.0",
"typer[all]>=0.15.0",
]
[project.scripts]
cohere = "cohere_transcribe.cli:main"
cohere-transcribe = "cohere_transcribe.cli:main"
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.backends"
build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel]
packages = ["src/cohere_transcribe"]
-40
View File
@@ -1,40 +0,0 @@
import argparse
from huggingface_hub import hf_hub_download
from transformers.audio_utils import load_audio
from .model import MODEL_ID, SAMPLE_RATE, load_model, record_audio, transcribe_audio
from .stream import stream_transcribe
def main():
parser = argparse.ArgumentParser(description="Cohere ASR Transcription")
group = parser.add_mutually_exclusive_group()
group.add_argument("--mic", type=int, nargs="?", const=5, metavar="SECONDS",
help="Record from microphone for N seconds (default: 5)")
group.add_argument("--stream", action="store_true",
help="Live streaming transcription with VAD")
parser.add_argument("--lang", default="en", help="Language code (default: en)")
args = parser.parse_args()
if args.stream:
processor, model = load_model()
stream_transcribe(processor, model, args.lang)
elif args.mic is not None:
processor, model = load_model()
try:
mic_audio = record_audio(args.mic)
print("Transcribing...")
text = transcribe_audio(processor, model, mic_audio, args.lang)
print(f"\nTranscription:\n{text}\n")
except OSError as e:
print(f"Microphone error: {e}")
print("Hint: Run with nix-shell for PortAudio support")
else:
processor, model = load_model()
print("Loading demo audio...")
audio_file = hf_hub_download(repo_id=MODEL_ID, filename="demo/voxpopuli_test_en_demo.wav")
audio = load_audio(audio_file, sampling_rate=SAMPLE_RATE)
print("Transcribing...")
text = transcribe_audio(processor, model, audio, args.lang)
print(f"\nTranscription:\n{text}\n")
+3
View File
@@ -0,0 +1,3 @@
from .cli import main
__all__ = ["main"]
+120
View File
@@ -0,0 +1,120 @@
import os
import subprocess
import sys
import time
import typer
from rich.console import Console
from ..daemon import STATE_FILE, is_running, read_state, stop_daemon
app = typer.Typer(help="Cohere live transcription — speaks into your keyboard.")
console = Console()
@app.command()
def on(
language: str = typer.Option("en", "--lang", "-l", help="Language code"),
foreground: bool = typer.Option(False, "--fg", help="Run in foreground (don't daemonize)"),
):
"""Start transcribing and typing into your focused window."""
if is_running():
console.print("[yellow]Already running.[/yellow]")
raise typer.Exit(1)
if foreground:
from ..daemon import run_daemon
console.print("[green]Starting cohere (foreground)...[/green]")
run_daemon(language)
return
console.print("[green]Starting cohere daemon...[/green]")
os.makedirs(os.path.dirname(STATE_FILE), exist_ok=True)
subprocess.Popen(
[sys.executable, "-m", "cohere_transcribe.daemon_main", "--lang", language],
start_new_session=True,
stdin=subprocess.DEVNULL,
stdout=open(os.path.join(os.path.dirname(STATE_FILE), "daemon.log"), "a"),
stderr=subprocess.STDOUT,
)
for _ in range(50):
time.sleep(0.1)
if is_running():
break
if is_running():
console.print("[green]Cohere is on — speak and it types.[/green]")
else:
console.print("[red]Failed to start daemon. Check ~/.local/state/cohere/daemon.log[/red]")
raise typer.Exit(1)
@app.command()
def off():
"""Stop transcribing."""
if not is_running():
console.print("[yellow]Not running.[/yellow]")
raise typer.Exit(0)
if stop_daemon():
console.print("[red]Cohere is off.[/red]")
else:
console.print("[red]Failed to stop daemon.[/red]")
raise typer.Exit(1)
@app.command()
def status():
"""Show whether cohere is running."""
state = read_state()
running = is_running()
if running:
started = state.get("started_at", 0)
elapsed = time.time() - started
minutes = int(elapsed) // 60
console.print(f"[green]ON[/green] — running for {minutes}m")
else:
console.print("[dim]OFF[/dim]")
@app.command()
def transcribe(
audio_file: str = typer.Argument(None, help="Audio file to transcribe"),
mic: int = typer.Option(None, "--mic", "-m", help="Record from mic for N seconds"),
stream: bool = typer.Option(False, "--stream", "-s", help="Live streaming mode (prints to terminal)"),
language: str = typer.Option("en", "--lang", "-l", help="Language code"),
):
"""One-shot transcription (file, mic, or stream to terminal)."""
from ..model import load_model, transcribe_audio
if stream:
from ..stream import stream_transcribe
processor, model = load_model()
stream_transcribe(processor, model, language)
elif mic is not None:
from ..model import record_audio
processor, model = load_model()
try:
audio = record_audio(mic)
console.print("Transcribing...")
text = transcribe_audio(processor, model, audio, language)
console.print(f"\n{text}\n")
except OSError as e:
console.print(f"[red]Microphone error: {e}[/red]")
raise typer.Exit(1)
elif audio_file:
from transformers.audio_utils import load_audio as load_audio_file
from ..model import SAMPLE_RATE
processor, model = load_model()
audio = load_audio_file(audio_file, sampling_rate=SAMPLE_RATE)
text = transcribe_audio(processor, model, audio, language)
console.print(f"\n{text}\n")
else:
console.print("[yellow]Provide an audio file, --mic, or --stream[/yellow]")
raise typer.Exit(1)
def main():
app()
+138
View File
@@ -0,0 +1,138 @@
import json
import os
import signal
import subprocess
import sys
import queue
import threading
import time
import numpy as np
import sounddevice as sd
from .model import SAMPLE_RATE, load_model, transcribe_audio
from .vad import FRAME_SIZE, VADStateMachine, calibrate_silence
STATE_DIR = os.path.expanduser("~/.local/state/cohere")
STATE_FILE = os.path.join(STATE_DIR, "state.json")
LOG_FILE = os.path.join(STATE_DIR, "daemon.log")
def _write_state(pid: int, status: str):
os.makedirs(STATE_DIR, exist_ok=True)
with open(STATE_FILE, "w") as f:
json.dump({"pid": pid, "status": status, "started_at": time.time()}, f)
def _type_text(text: str):
try:
subprocess.run(["wtype", "--", text], check=True, timeout=10)
except FileNotFoundError:
print("wtype not found — install it for keyboard injection", file=sys.stderr)
except subprocess.SubprocessError as e:
print(f"wtype error: {e}", file=sys.stderr)
def read_state() -> dict | None:
try:
with open(STATE_FILE) as f:
return json.load(f)
except (FileNotFoundError, json.JSONDecodeError):
return None
def is_running() -> bool:
state = read_state()
if state is None:
return False
pid = state.get("pid")
if pid is None:
return False
try:
os.kill(pid, 0)
return True
except OSError:
return False
def stop_daemon() -> bool:
state = read_state()
if state is None:
return False
pid = state.get("pid")
if pid is None:
return False
try:
os.kill(pid, signal.SIGTERM)
for _ in range(20):
time.sleep(0.1)
try:
os.kill(pid, 0)
except OSError:
break
_write_state(pid, "stopped")
return True
except OSError:
_write_state(pid, "stopped")
return False
def run_daemon(language: str = "en"):
pid = os.getpid()
_write_state(pid, "starting")
def handle_sigterm(signum, frame):
raise KeyboardInterrupt
signal.signal(signal.SIGTERM, handle_sigterm)
processor, model = load_model()
threshold = calibrate_silence()
vad = VADStateMachine(threshold)
seg_queue: queue.Queue = queue.Queue()
stop_event = threading.Event()
start_time = time.monotonic()
_write_state(pid, "running")
def transcription_worker():
while not stop_event.is_set() or not seg_queue.empty():
try:
_seg_start, audio = seg_queue.get(timeout=0.5)
except queue.Empty:
continue
text = transcribe_audio(processor, model, audio, language)
text = text.strip()
if text:
_type_text(text + " ")
worker = threading.Thread(target=transcription_worker, daemon=True)
worker.start()
def audio_callback(indata, frames, time_info, status):
if stop_event.is_set():
return
elapsed = time.monotonic() - start_time
result = vad.process_frame(indata[:, 0].copy(), elapsed)
if result is not None:
seg_queue.put(result)
stream = sd.InputStream(
samplerate=SAMPLE_RATE, channels=1, dtype="float32",
callback=audio_callback, blocksize=FRAME_SIZE,
)
try:
with stream:
while True:
time.sleep(0.1)
except KeyboardInterrupt:
pass
stop_event.set()
if vad.speaking and vad.segment:
seg_queue.put((vad.segment_start_time, np.concatenate(vad.segment)))
worker.join(timeout=30)
_write_state(pid, "stopped")
+8
View File
@@ -0,0 +1,8 @@
import argparse
from .daemon import run_daemon
parser = argparse.ArgumentParser()
parser.add_argument("--lang", default="en")
args = parser.parse_args()
run_daemon(args.lang)
Generated
+4 -2
View File
@@ -190,9 +190,9 @@ wheels = [
]
[[package]]
name = "cohere"
name = "cohere-transcribe"
version = "0.1.0"
source = { virtual = "." }
source = { editable = "." }
dependencies = [
{ name = "accelerate" },
{ name = "huggingface-hub" },
@@ -203,6 +203,7 @@ dependencies = [
{ name = "soundfile" },
{ name = "torch" },
{ name = "transformers" },
{ name = "typer" },
]
[package.metadata]
@@ -216,6 +217,7 @@ requires-dist = [
{ name = "soundfile", specifier = ">=0.13.1" },
{ name = "torch", specifier = ">=2.12.0" },
{ name = "transformers", specifier = ">=5.9.0" },
{ name = "typer", extras = ["all"], specifier = ">=0.15.0" },
]
[[package]]