Created
May 7, 2025 01:59
-
-
Save rhulha/749a20030b2bce20a37ae0ef44e19a44 to your computer and use it in GitHub Desktop.
Here is a little Python script that detects clipboard text and plays it using Kokoro TTS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
os.environ["PHONEMIZER_ESPEAK_LIBRARY"] = "C:\\Program Files\\eSpeak NG\\libespeak-ng.dll" | |
os.environ["PHONEMIZER_ESPEAK_PATH"] = "C:\\Program Files\\eSpeak NG\\espeak-ng.exe" | |
import sounddevice as sd | |
import pyperclip | |
import time | |
import torch, re | |
from models import build_model | |
from kokoro import generate | |
device = 'cuda' if torch.cuda.is_available() else 'cpu' | |
MODEL = build_model('pth/kokoro-v0_19.pth', device) | |
VOICE_NAME = 'af' | |
VOICEPACK = torch.load(f'voices/{VOICE_NAME}.pt', weights_only=True).to(device) | |
print(f'Loaded voice: {VOICE_NAME}') | |
def play_audio_from_text(text): | |
sentences = re.split(r'[.\n]+', text) | |
for sentence in sentences: | |
if sentence.strip() == '': | |
continue | |
audio, out_ps = generate(MODEL, sentence, VOICEPACK, lang=VOICE_NAME[0]) | |
sd.play(audio, samplerate=24000) | |
sd.wait() | |
def monitor_clipboard(): | |
last_clipboard = pyperclip.paste() | |
while True: | |
time.sleep(0.5) # Reduce CPU usage | |
current_clipboard = pyperclip.paste() | |
if current_clipboard and current_clipboard != last_clipboard: | |
play_audio_from_text(current_clipboard) | |
last_clipboard = current_clipboard | |
if __name__ == "__main__": | |
print("Monitoring clipboard for text changes...") | |
monitor_clipboard() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> uv pip install torch --index-url https://download.pytorch.org/whl/cu124 | |
> uv pip install requests | |
> uv pip install numpy | |
> uv pip install scipy | |
> uv pip install phonemizer | |
> uv pip install munch | |
> uv pip install transformers | |
> uv pip install soundfile | |
> uv pip install pyperclip | |
And then you can run the code like this: | |
uv run monitor.py |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment