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] 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()