From eec75210e3b032090f3e96362c78604e2642f927 Mon Sep 17 00:00:00 2001 From: McCloudS <64094529+McCloudS@users.noreply.github.com> Date: Thu, 21 Mar 2024 08:41:27 -0600 Subject: [PATCH 1/6] Update launcher.py --- launcher.py | 67 ++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 61 insertions(+), 6 deletions(-) diff --git a/launcher.py b/launcher.py index a0deadf..98bb6ef 100644 --- a/launcher.py +++ b/launcher.py @@ -5,9 +5,9 @@ import argparse def convert_to_bool(in_bool): # Convert the input to string and lower case, then check against true values - return str(in_bool).lower() in ('true', 'on', '1') + return str(in_bool).lower() in ('true', 'on', '1', 'y', 'yes') -def install_packages_from_requirements(requirements_file): +def install_packages_from_requirements(requirements_file, force_update): try: subprocess.run(['pip3', 'install', '-r', requirements_file, '--upgrade'], check=True) print(f"Requirements from {requirements_file} have been successfully installed.") @@ -23,6 +23,54 @@ def download_from_github(url, output_file): else: print(f"Failed to download file from {url}") +def prompt_and_save_bazarr_env_variables(): + """ + Prompts the user for Bazarr related environment variables with descriptions and saves them to a file. + If the user does not input anything, default values are used. + """ + # Instructions for the user + instructions = ( + "You will be prompted for several configuration values.\n" + "If you wish to use the default value for any of them, simply press Enter without typing anything.\n" + "The default values are shown in brackets [] next to the prompts.\n" + "Items can be the value of true, on, 1, y, yes, false, off, 0, n, no, or an appropriate text response.\n" + ) + print(instructions) + env_vars = { + 'WHISPER_MODEL': ('Whisper Model', 'Enter the Whisper model you want to run: tiny, tiny.en, base, base.en, small, small.en, medium, medium.en, large, distil-large-v2, distil-medium.en, distil-small.en', 'medium'), + 'WEBHOOKPORT': ('Webhook Port', 'Default listening port for subgen.py', '9000'), + 'TRANSCRIBE_DEVICE': ('Transcribe Device', 'Set as cpu or gpu', 'gpu'), + 'DEBUG': ('Debug', 'Enable debug logging', 'True'), + 'CLEAR_VRAM_ON_COMPLETE': ('Clear VRAM', 'Attempt to clear VRAM when complete (Windows users may need to set this to False)', 'False'), + 'APPEND': ('Append', 'Append \'Transcribed by whisper\' to generated subtitle', 'False'), + } + + # Dictionary to hold the user's input + user_input = {} + + # Prompt the user for each environment variable and write to .env file + with open('subgen.env', 'w') as file: + for var, (description, prompt, default) in env_vars.items(): + value = input(f"{prompt} [{default}]: ") or default + file.write(f"{var}={value}\n") + + print("Environment variables have been saved to subgen.env") + +def load_env_variables(env_filename='subgen.env'): + """ + Loads environment variables from a specified .env file and sets them. + """ + try: + with open(env_filename, 'r') as file: + for line in file: + var, value = line.strip().split('=', 1) + os.environ[var] = value + + print(f"Environment variables have been loaded from {env_filename}") + + except FileNotFoundError: + print(f"{env_filename} file not found. Please run prompt_and_save_env_variables() first.") + def main(): # Construct the argument parser parser = argparse.ArgumentParser() @@ -30,7 +78,10 @@ def main(): parser.add_argument('-i', '--install', default=False, action='store_true', help="Install/update all necessary packages (default: False)") parser.add_argument('-a', '--append', default=False, action='store_true', help="Append 'Transcribed by whisper' to generated subtitle (default: False)") parser.add_argument('-u', '--update', default=False, action='store_true', help="Update Subgen (default: False)") - parser.add_argument('-s', '--skiprun', default=False, action='store_true', help="Skip running subgen.py (default: False)") + parser.add_argument('-dnr', '--donotrun', default=False, action='store_true', help="Do not run subgen.py (default: False)") + parser.add_argument('-b', '--bazarrsetup', default=False, action='store_true', help="Prompt for common Bazarr setup parameters and save them for future runs (default: False)") + + args = parser.parse_args() @@ -38,13 +89,17 @@ def main(): os.environ['DEBUG'] = str(args.debug) os.environ['APPEND'] = str(args.append) + if args.bazarrsetup: + prompt_and_save_bazarr_env_variables() + load_env_variables() + # URL to the requirements.txt file on GitHub requirements_url = "https://raw.githubusercontent.com/McCloudS/subgen/main/requirements.txt" requirements_file = "requirements.txt" # Install packages from requirements.txt if the install or packageupdate argument is True if args.install: - install_packages_from_requirements(requirements_file) + install_packages_from_requirements(requirements_file, args.install) subgen_script_name = "./subgen.py" @@ -56,10 +111,10 @@ def main(): download_from_github("https://raw.githubusercontent.com/McCloudS/subgen/main/subgen.py", subgen_script_name) else: print("Environment variable UPDATE is not set or set to False, skipping download.") - if not args.skiprun: + if not args.donotrun: subprocess.run(['python3', '-u', 'subgen.py'], check=True) else: - print("not running subgen.py: -s or --skiprun set") + print("not running subgen.py: -dnr or --donotrun") if __name__ == "__main__": main() From 198577ac06c738761d41796f1b71f4e0bb9b8152 Mon Sep 17 00:00:00 2001 From: McCloudS <64094529+McCloudS@users.noreply.github.com> Date: Thu, 21 Mar 2024 09:02:03 -0600 Subject: [PATCH 2/6] Add files via upload --- subgen.env | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 subgen.env diff --git a/subgen.env b/subgen.env new file mode 100644 index 0000000..1e0f198 --- /dev/null +++ b/subgen.env @@ -0,0 +1,6 @@ +WHISPER_MODEL=medium +WEBHOOKPORT=9000 +TRANSCRIBE_DEVICE=gpu +DEBUG=True +CLEAR_VRAM_ON_COMPLETE=False +APPEND=False From e16becbe745edc1cb3bc6054decf1832c21fa52d Mon Sep 17 00:00:00 2001 From: McCloudS <64094529+McCloudS@users.noreply.github.com> Date: Thu, 21 Mar 2024 09:02:50 -0600 Subject: [PATCH 3/6] Update .gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 3e00af5..e6828a9 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,6 @@ # Built Visual Studio Code Extensions *.vsix + +#ignore our settings +subgen.env From efb8b0f8de9a12db65389d0ea6e877365b9ba903 Mon Sep 17 00:00:00 2001 From: McCloudS <64094529+McCloudS@users.noreply.github.com> Date: Thu, 21 Mar 2024 09:04:49 -0600 Subject: [PATCH 4/6] Update launcher.py --- launcher.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/launcher.py b/launcher.py index 98bb6ef..21787ca 100644 --- a/launcher.py +++ b/launcher.py @@ -72,6 +72,9 @@ def load_env_variables(env_filename='subgen.env'): print(f"{env_filename} file not found. Please run prompt_and_save_env_variables() first.") def main(): + #Make sure we're saving subgen.py and subgen.env in the right folder + os.chdir(os.path.dirname(os.path.abspath(__file__))) + # Construct the argument parser parser = argparse.ArgumentParser() parser.add_argument( '-d', '--debug', default=False, action='store_true', help="Enable console debugging (default: False)") From 71cf8aee1f7a4ccf51a004e435898fb7f6eddbf2 Mon Sep 17 00:00:00 2001 From: McCloudS <64094529+McCloudS@users.noreply.github.com> Date: Thu, 21 Mar 2024 09:08:40 -0600 Subject: [PATCH 5/6] Update launcher.py --- launcher.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/launcher.py b/launcher.py index 21787ca..c1f29a4 100644 --- a/launcher.py +++ b/launcher.py @@ -7,7 +7,7 @@ def convert_to_bool(in_bool): # Convert the input to string and lower case, then check against true values return str(in_bool).lower() in ('true', 'on', '1', 'y', 'yes') -def install_packages_from_requirements(requirements_file, force_update): +def install_packages_from_requirements(requirements_file): try: subprocess.run(['pip3', 'install', '-r', requirements_file, '--upgrade'], check=True) print(f"Requirements from {requirements_file} have been successfully installed.") @@ -102,7 +102,7 @@ def main(): # Install packages from requirements.txt if the install or packageupdate argument is True if args.install: - install_packages_from_requirements(requirements_file, args.install) + install_packages_from_requirements(requirements_file) subgen_script_name = "./subgen.py" From 525cb0c572c8f667d98ef1efb8d385629c23f131 Mon Sep 17 00:00:00 2001 From: McCloudS <64094529+McCloudS@users.noreply.github.com> Date: Thu, 21 Mar 2024 09:14:30 -0600 Subject: [PATCH 6/6] Update launcher.py --- launcher.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launcher.py b/launcher.py index c1f29a4..256685f 100644 --- a/launcher.py +++ b/launcher.py @@ -117,7 +117,7 @@ def main(): if not args.donotrun: subprocess.run(['python3', '-u', 'subgen.py'], check=True) else: - print("not running subgen.py: -dnr or --donotrun") + print("Not running subgen.py: -dnr or --donotrun set") if __name__ == "__main__": main()