Files
kokoro-wayland/wayland_narrate.py
2026-02-11 19:58:40 +05:30

32 lines
818 B
Python

import subprocess
import sounddevice as sd
from kokoro import KPipeline
import torch
import sys
# 1. Fetch text from Wayland Primary Selection (highlighted text)
try:
# 'wl-paste -p' gets the current mouse highlight
text = subprocess.check_output(["wl-paste", "-p"], text=True).strip()
except Exception:
print("Nothing highlighted or wl-clipboard not installed.")
sys.exit(1)
if not text:
print("Selection is empty.")
sys.exit(0)
text.replace("\n", ".")
print(f"Narrating: {text[:50]}...")
# 2. Setup Kokoro (GPU)
device = "cuda" if torch.cuda.is_available() else "cpu"
pipeline = KPipeline(lang_code="a", device=device)
# 3. Generate and Play
generator = pipeline(text, voice="af_heart", speed=1.1)
for i, (gs, ps, audio) in enumerate(generator):
sd.play(audio, 24000)
sd.wait()