From cadacb59f0a120c4835c2b14ea0f69ce9832ea6f Mon Sep 17 00:00:00 2001 From: Believethehype <1097224+believethehype@users.noreply.github.com> Date: Wed, 26 Jun 2024 10:55:17 +0200 Subject: [PATCH] better handling of dvms venvs (again with live feedback) --- nostr_dvm/dvm.py | 44 +++++++++++++++++++++++------- nostr_dvm/tasks/texttospeech.py | 6 +++-- tests/tts.py | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 11 deletions(-) create mode 100644 tests/tts.py diff --git a/nostr_dvm/dvm.py b/nostr_dvm/dvm.py index 49a7bb1..0d2661d 100644 --- a/nostr_dvm/dvm.py +++ b/nostr_dvm/dvm.py @@ -624,7 +624,24 @@ class DVM: return reaction_event.as_json() - async def run_subprocess(python_bin, dvm_config, request_form): + async def _read_stream(stream, cb): + while True: + line = await stream.readline() + if line: + cb(line) + else: + break + + async def _stream_subprocess(cmd, stdout_cb, stderr_cb): + process = await asyncio.create_subprocess_exec(*cmd, + stdout=asyncio.subprocess.PIPE, + stderr=asyncio.subprocess.PIPE) + + + + + async def run_subprocess(python_bin, dvm_config, request_form, stdout_cb, stderr_cb): + print("Running subprocess, please wait..") process = await asyncio.create_subprocess_exec( python_bin, dvm_config.SCRIPT, '--request', json.dumps(request_form), @@ -634,16 +651,23 @@ class DVM: stderr=asyncio.subprocess.PIPE ) - stdout, stderr = await process.communicate() + await asyncio.gather( + _read_stream(process.stdout, stdout_cb), + _read_stream(process.stderr, stderr_cb) + ) + return await process.wait() - retcode = process.returncode - if retcode != 0: - print(f"Error: {stderr.decode()}") - else: - print(f"Output: {stdout.decode()}") + #stdout, stderr = await process.communicate() - return retcode + #retcode = process.returncode + + #if retcode != 0: + # print(f"Error: {stderr.decode()}") + #else: + # print(f"Output: {stdout.decode()}") + + #return retcode async def do_work(job_event, amount): if (( @@ -669,7 +693,9 @@ class DVM: # '--request', json.dumps(request_form), # '--identifier', dvm_config.IDENTIFIER, # '--output', 'output.txt']) - await run_subprocess(python_bin, dvm_config, request_form) + await run_subprocess(python_bin, dvm_config, request_form, + lambda x: print("%s" % x.decode("utf-8").replace("\n", "")), + lambda x: print("STDERR: %s" % x.decode("utf-8"))) print("Finished processing, loading data..") with open(os.path.abspath('output.txt'), encoding="utf-8") as f: diff --git a/nostr_dvm/tasks/texttospeech.py b/nostr_dvm/tasks/texttospeech.py index 91205f2..22e7f85 100644 --- a/nostr_dvm/tasks/texttospeech.py +++ b/nostr_dvm/tasks/texttospeech.py @@ -106,9 +106,11 @@ class TextToSpeech(DVMTaskInterface): from TTS.api import TTS options = self.set_options(request_form) device = "cuda" if torch.cuda.is_available() else "cpu" - # else "mps" if torch.backends.mps.is_available() \ + #else "mps" if torch.backends.mps.is_available() + print(device) + + print(TTS().list_models().list_tts_models()) - print(TTS().list_models()) try: # model = "tts_models/deu/fairseq/vits" # model = "tts_models/multilingual/multi-dataset/your_tts" diff --git a/tests/tts.py b/tests/tts.py new file mode 100644 index 0000000..23f710b --- /dev/null +++ b/tests/tts.py @@ -0,0 +1,48 @@ +import json +from pathlib import Path +import dotenv +from nostr_dvm.tasks import texttospeech +from nostr_dvm.tasks.texttospeech import TextToSpeech +from nostr_dvm.utils.admin_utils import AdminConfig +from nostr_dvm.utils.dvmconfig import build_default_config +from nostr_dvm.utils.nip89_utils import NIP89Config, check_and_set_d_tag + +if __name__ == '__main__': + env_path = Path('.env') + if env_path.is_file(): + print(f'loading environment from {env_path.resolve()}') + dotenv.load_dotenv(env_path, verbose=True, override=True) + else: + raise FileNotFoundError(f'.env file not found at {env_path} ') + name = "TTS Guy Swann" + identifier = "ttsguy" + admin_config_tts = AdminConfig() + admin_config_tts.UPDATE_PROFILE = True + admin_config_tts.REBROADCAST_NIP65_RELAY_LIST = True + dvm_config = build_default_config(identifier) + dvm_config.USE_OWN_VENV = True + dvm_config.FIX_COST = 0 + dvm_config.PER_UNIT_COST = 0 + admin_config_tts.LUD16 = dvm_config.LN_ADDRESS + # use an alternative local wav file you want to use for cloning + options = {'input_file': ""} + nip89info = { + "name": name, + "image": "https://image.nostr.build/c33ca6fc4cc038ca4adb46fdfdfda34951656f87ee364ef59095bae1495ce669.jpg", + "about": "I Generate Speech from Text", + "encryptionSupported": True, + "cashuAccepted": True, + "nip90Params": { + "language": { + "required": False, + "values": [] + } + } + } + + nip89config = NIP89Config() + nip89config.DTAG = check_and_set_d_tag(identifier, name, dvm_config.PRIVATE_KEY, nip89info["image"]) + nip89config.CONTENT = json.dumps(nip89info) + tts = TextToSpeech(name=name, dvm_config=dvm_config, nip89config=nip89config, admin_config=admin_config_tts, + options=options) + tts.run()