From 90847b0b68ddcf2c8e9284d31f8ccd3f93540bfe Mon Sep 17 00:00:00 2001 From: Ayo Date: Thu, 4 Sep 2025 11:06:40 +0200 Subject: [PATCH] feat: implement using text from the clipboard as input --- README.md | 11 +++++++++++ requirements.txt | 1 + tts.py | 24 +++++++++++++++++++----- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 4a25f3b..25436c5 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,8 @@ Running the program without arguments will use the demo text `tongue-twister.txt $ python tts.py # will use default arguments ``` +### Providing text inputs + You can pass a string as first argument: ```bash @@ -89,6 +91,15 @@ $ python tts.py --input_file demo/tongue-twister.txt $ python tts.py -i demo/tongue-twister.txt ``` +You can also use the text stored in your clipboard (i.e., copied text). Select a text from anywhere (e.g., your web browser), copy it with `+C` or the context menu, then use the flag `--clipboard`: + +```bash +$ python tts.py --clipboard + +# or shorter... +$ python tts.py -c +``` + ### Voices Optionally, you can indicate a voice you want to use with the `--voice` flag. See [all voices available](https://huggingface.co/hexgrad/Kokoro-82M/blob/main/VOICES.md). diff --git a/requirements.txt b/requirements.txt index 1d8c3b3..117b119 100644 --- a/requirements.txt +++ b/requirements.txt @@ -4,3 +4,4 @@ python-vlc tqdm argparse torch +pyperclip \ No newline at end of file diff --git a/tts.py b/tts.py index 1b08179..94ecd9e 100644 --- a/tts.py +++ b/tts.py @@ -8,6 +8,7 @@ from kokoro import KPipeline import soundfile as sf import vlc from tqdm import tqdm +import pyperclip # Disable all warnings warnings.filterwarnings("ignore") @@ -45,6 +46,13 @@ def parse_args(): default="demo/tongue-twister.txt", help="Path to the input text file", ) + parser.add_argument( + "--clipboard", + "-c", + required=False, + action="store_true", + help="Use text from the clipboard (i.e., copied text)", + ) parser.add_argument( "--device", "-d", @@ -98,11 +106,17 @@ def main(): # filename argument if args.input_text == "": - file_path = args.input_file - directory, file_name = os.path.split(file_path) - name = '.'.join(file_name.split('.')[:-1]) - file = open(file_path, "r") - text = file.read() + if args.clipboard: + # use copied text + print('Using copied text as input...') + text = pyperclip.paste() + name = 'copied' + else: + file_path = args.input_file + directory, file_name = os.path.split(file_path) + name = '.'.join(file_name.split('.')[:-1]) + file = open(file_path, "r") + text = file.read() else: name = "chat" text = args.input_text