diff --git a/.gitignore b/.gitignore index 61006cec..f66d828c 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,4 @@ models/config-user.yaml .DS_Store Thumbs.db +installer_files/ diff --git a/CMD_FLAGS.txt b/CMD_FLAGS.txt new file mode 100644 index 00000000..e69de29b diff --git a/README.md b/README.md index c3bfe137..c8a387da 100644 --- a/README.md +++ b/README.md @@ -30,15 +30,28 @@ To learn how to use the various features, check out the Documentation: https://g ### One-click installers -| Windows | Linux | macOS | WSL | -|--------|--------|--------|--------| -| [oobabooga-windows.zip](https://github.com/oobabooga/text-generation-webui/releases/download/installers/oobabooga_windows.zip) | [oobabooga-linux.zip](https://github.com/oobabooga/text-generation-webui/releases/download/installers/oobabooga_linux.zip) |[oobabooga-macos.zip](https://github.com/oobabooga/text-generation-webui/releases/download/installers/oobabooga_macos.zip) | [oobabooga-wsl.zip](https://github.com/oobabooga/text-generation-webui/releases/download/installers/oobabooga_wsl.zip) | +1) Run the `start_linux.sh`, `start_windows.bat`, `start_macos.sh`, or `start_wsl.bat` script depending on your OS. +2) Select your GPU vendor when asked. +3) Have fun! -Just download the zip above, extract it, and double-click on "start". The web UI and all its dependencies will be installed in the same folder. +#### More information -* The source codes and more information can be found here: https://github.com/oobabooga/one-click-installers -* There is no need to run the installers as admin. -* Huge thanks to [@jllllll](https://github.com/jllllll), [@ClayShoaf](https://github.com/ClayShoaf), and [@xNul](https://github.com/xNul) for their contributions to these installers. +The script will create a folder called `installer_files` where it will create a Conda environment using Miniconda. The installation is self-contained: if you want to reinstall, just delete `installer_files` and run the start script again. + +To launch the webui in the future after it is already installed, run the same `start` script. + +To update, run `update_linux.sh`, `update_windows.bat`, `update_macos.sh`, or `update_wsl.bat`. + +To run commands in the `installer_files` environment, run the appropriate cmd script: `cmd_linux.sh`, `cmd_windows.bat`, `cmd_macos.sh`, or `cmd_wsl.bat`. + +To define persistent command-line flags like `--listen` or `--api`, edit the `CMD_FLAGS.txt` file with a text editor and add them there. Flags can also be provided directly to the start scripts, for instance, `./start-linux.sh --listen`. + +Some observations: + +* There is no need to run any of those scripts as admin/root. +* If you need to move your installation folder, you will need to delete `installer_files` and install again, as some links will be broken. So choose a good location for your `text-generation-webui` folder before installing. +* Additional instructions for WSL and AMD can be found here: [Instructions](https://github.com/oobabooga/text-generation-webui/blob/one-click/docs/One-Click-Installers.md). +* The installer has been tested mostly on NVIDIA GPUs. If you can find a way to improve it for your AMD/Intel Arc/Mac Metal GPU, you are highly encouraged to submit a PR to this repository. The main file to be edited is `one_click.py`. ### Manual installation using Conda diff --git a/cmd_linux.sh b/cmd_linux.sh new file mode 100755 index 00000000..1685050a --- /dev/null +++ b/cmd_linux.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +cd "$(dirname "${BASH_SOURCE[0]}")" + +if [[ "$(pwd)" =~ " " ]]; then echo This script relies on Miniconda which can not be silently installed under a path with spaces. && exit; fi + +# deactivate existing conda envs as needed to avoid conflicts +{ conda deactivate && conda deactivate && conda deactivate; } 2> /dev/null + +# config +CONDA_ROOT_PREFIX="$(pwd)/installer_files/conda" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" + +# environment isolation +export PYTHONNOUSERSITE=1 +unset PYTHONPATH +unset PYTHONHOME +export CUDA_PATH="$INSTALL_ENV_DIR" +export CUDA_HOME="$CUDA_PATH" + +# activate env +bash --init-file <(echo "source \"$CONDA_ROOT_PREFIX/etc/profile.d/conda.sh\" && conda activate \"$INSTALL_ENV_DIR\"") diff --git a/cmd_macos.sh b/cmd_macos.sh new file mode 100755 index 00000000..1b052e5c --- /dev/null +++ b/cmd_macos.sh @@ -0,0 +1,24 @@ +#!/bin/bash + +cd "$(dirname "${BASH_SOURCE[0]}")" + +if [[ "$(pwd)" =~ " " ]]; then echo This script relies on Miniconda which can not be silently installed under a path with spaces. && exit; fi + +# deactivate existing conda envs as needed to avoid conflicts +{ conda deactivate && conda deactivate && conda deactivate; } 2> /dev/null + +# config +CONDA_ROOT_PREFIX="$(pwd)/installer_files/conda" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" + +# environment isolation +export PYTHONNOUSERSITE=1 +unset PYTHONPATH +unset PYTHONHOME +export CUDA_PATH="$INSTALL_ENV_DIR" +export CUDA_HOME="$CUDA_PATH" + +# activate env +source $CONDA_ROOT_PREFIX/etc/profile.d/conda.sh +conda activate $INSTALL_ENV_DIR +exec bash --norc diff --git a/cmd_windows.bat b/cmd_windows.bat new file mode 100755 index 00000000..82e1dac9 --- /dev/null +++ b/cmd_windows.bat @@ -0,0 +1,34 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. && goto end + +@rem fix failed install when installing to a separate drive +set TMP=%cd%\installer_files +set TEMP=%cd%\installer_files + +@rem deactivate existing conda envs as needed to avoid conflicts +(conda deactivate && conda deactivate && conda deactivate) 2>nul + +@rem config +set CONDA_ROOT_PREFIX=%cd%\installer_files\conda +set INSTALL_ENV_DIR=%cd%\installer_files\env + +@rem environment isolation +set PYTHONNOUSERSITE=1 +set PYTHONPATH= +set PYTHONHOME= +set "CUDA_PATH=%INSTALL_ENV_DIR%" +set "CUDA_HOME=%CUDA_PATH%" + +@rem activate installer env +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end ) + +@rem enter commands +cmd /k "%*" + +:end +pause diff --git a/cmd_wsl.bat b/cmd_wsl.bat new file mode 100755 index 00000000..f9f4348a --- /dev/null +++ b/cmd_wsl.bat @@ -0,0 +1,11 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +@rem sed -i 's/\x0D$//' ./wsl.sh converts newlines to unix format in the wsl script +call wsl -e bash -lic "sed -i 's/\x0D$//' ./wsl.sh; source ./wsl.sh cmd" + +:end +pause diff --git a/docs/One-Click-Installers.md b/docs/One-Click-Installers.md new file mode 100644 index 00000000..56db9a12 --- /dev/null +++ b/docs/One-Click-Installers.md @@ -0,0 +1,64 @@ +# Additional one-click installers info + +## Using an AMD GPU in Linux + +Requires ROCm SDK 5.4.2 or 5.4.3 to be installed. Some systems may also +need: sudo apt-get install libstdc++-12-dev + +Edit the "one_click.py" script using a text editor and un-comment and +modify the lines near the top of the script according to your setup. In +particular, modify the os.environ["ROCM_PATH"] = '/opt/rocm' line to +point to your ROCm installation. + +## WSL instructions + +If you do not have WSL installed, see here: +https://learn.microsoft.com/en-us/windows/wsl/install + +If you want to install Linux to a drive other than C +Open powershell and enter these commands: + +cd D:\Path\To\Linux +$ProgressPreference = 'SilentlyContinue' +Invoke-WebRequest -Uri -OutFile Linux.appx -UseBasicParsing +mv Linux.appx Linux.zip + +Then open Linux.zip and you should see several .appx files inside. +The one with _x64.appx contains the exe installer that you need. +Extract the contents of that _x64.appx file and run .exe to install. + +Linux Distro URLs: +https://learn.microsoft.com/en-us/windows/wsl/install-manual#downloading-distributions + +****************************************************************************** +*ENSURE THAT THE WSL LINUX DISTRO THAT YOU WISH TO USE IS SET AS THE DEFAULT!* +****************************************************************************** + +Do this by using these commands: +wsl -l +wsl -s + +### Web UI Installation + +Run the "start" script. By default it will install the web UI in WSL: +/home/{username}/text-gen-install + +To launch the web UI in the future after it is already installed, run +the same "start" script. Ensure that one_click.py and wsl.sh are next to it! + +### Updating the web UI + +As an alternative to running the "update" script, you can also run "wsl.sh update" in WSL. + +### Running an interactive shell + +As an alternative to running the "cmd" script, you can also run "wsl.sh cmd" in WSL. + +### Changing the default install location + +To change this, you will need to edit the scripts as follows: +wsl.sh: line ~22 INSTALL_DIR="/path/to/install/dir" + +Keep in mind that there is a long-standing bug in WSL that significantly +slows drive read/write speeds when using a physical drive as opposed to +the virtual one that Linux is installed in. diff --git a/modules/one_click_installer_check.py b/modules/one_click_installer_check.py new file mode 100644 index 00000000..1a7dd2b9 --- /dev/null +++ b/modules/one_click_installer_check.py @@ -0,0 +1,8 @@ +from pathlib import Path +from modules.logging_colors import logger + +if Path('../webui.py').exists(): + logger.warning('\nIt looks like you are running an outdated version of ' + 'the one-click-installers.\n' + 'Please migrate your installation following the instructions here:\n' + 'https://github.com/oobabooga/text-generation-webui/wiki/Migrating-an-old-one%E2%80%90click-install') diff --git a/one_click.py b/one_click.py new file mode 100644 index 00000000..0717bbf6 --- /dev/null +++ b/one_click.py @@ -0,0 +1,274 @@ +import argparse +import glob +import os +import subprocess +import site +import sys + +script_dir = os.getcwd() +conda_env_path = os.path.join(script_dir, "installer_files", "env") + +# Remove the '# ' from the following lines as needed for your AMD GPU on Linux +# os.environ["ROCM_PATH"] = '/opt/rocm' +# os.environ["HSA_OVERRIDE_GFX_VERSION"] = '10.3.0' +# os.environ["HCC_AMDGPU_TARGET"] = 'gfx1030' + +# Command-line flags +cmd_flags_path = os.path.join(script_dir, "CMD_FLAGS.txt") +if os.path.exists(cmd_flags_path): + with open(cmd_flags_path, 'r') as f: + CMD_FLAGS = ' '.join(line.strip() for line in f if line.strip() and not line.strip().startswith('#')) +else: + CMD_FLAGS = '' + + +def is_linux(): + return sys.platform.startswith("linux") + + +def is_windows(): + return sys.platform.startswith("win") + + +def is_macos(): + return sys.platform.startswith("darwin") + + +def is_installed(): + for sitedir in site.getsitepackages(): + if "site-packages" in sitedir and conda_env_path in sitedir: + site_packages_path = sitedir + break + + if site_packages_path: + return os.path.isfile(os.path.join(site_packages_path, 'torch', '__init__.py')) + else: + return os.path.isdir(conda_env_path) + + +def check_env(): + # If we have access to conda, we are probably in an environment + conda_exist = run_cmd("conda", environment=True, capture_output=True).returncode == 0 + if not conda_exist: + print("Conda is not installed. Exiting...") + sys.exit() + + # Ensure this is a new environment and not the base environment + if os.environ["CONDA_DEFAULT_ENV"] == "base": + print("Create an environment for this project and activate it. Exiting...") + sys.exit() + + +def clear_cache(): + run_cmd("conda clean -a -y", environment=True) + run_cmd("python -m pip cache purge", environment=True) + + +def print_big_message(message): + message = message.strip() + lines = message.split('\n') + print("\n\n*******************************************************************") + for line in lines: + if line.strip() != '': + print("*", line) + + print("*******************************************************************\n\n") + + +def run_cmd(cmd, assert_success=False, environment=False, capture_output=False, env=None): + # Use the conda environment + if environment: + if is_windows(): + conda_bat_path = os.path.join(script_dir, "installer_files", "conda", "condabin", "conda.bat") + cmd = "\"" + conda_bat_path + "\" activate \"" + conda_env_path + "\" >nul && " + cmd + else: + conda_sh_path = os.path.join(script_dir, "installer_files", "conda", "etc", "profile.d", "conda.sh") + cmd = ". \"" + conda_sh_path + "\" && conda activate \"" + conda_env_path + "\" && " + cmd + + # Run shell commands + result = subprocess.run(cmd, shell=True, capture_output=capture_output, env=env) + + # Assert the command ran successfully + if assert_success and result.returncode != 0: + print("Command '" + cmd + "' failed with exit status code '" + str(result.returncode) + "'. Exiting...") + sys.exit() + + return result + + +def install_webui(): + + print("What is your GPU") + print() + print("A) NVIDIA") + print("B) AMD (Linux/MacOS only. Requires ROCm SDK 5.4.2/5.4.3 on Linux)") + print("C) Apple M Series") + print("D) None (I want to run models in CPU mode)") + print() + + # Select your GPU, or choose to run in CPU mode + choice = input("Input> ").upper() + while choice not in ['A', 'B', 'C', 'D']: + print("Invalid choice. Please try again.") + choice = input("Input> ").upper() + if choice == "D": + print_big_message("Once the installation ends, make sure to open CMD_FLAGS.txt with\na text editor and add the --cpu flag.") + + # Find the proper Pytorch installation command + install_git = "conda install -y -k ninja git" + install_pytorch = "python -m pip install torch torchvision torchaudio" + + if choice == "A": + install_pytorch = "python -m pip install torch==2.0.1+cu117 torchvision torchaudio --index-url https://download.pytorch.org/whl/cu117" + elif is_macos() and choice == "B": + if is_linux(): + install_pytorch = "python -m pip install torch==2.0.1+rocm5.4.2 torchvision torchaudio --index-url https://download.pytorch.org/whl/rocm5.4.2" + else: + print("AMD GPUs are only supported on Linux. Exiting...") + sys.exit() + elif is_linux() and (choice == "C" or choice == "D"): + install_pytorch = "python -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu" + + # Install Git and then Pytorch + run_cmd(f"{install_git} && {install_pytorch}", assert_success=True, environment=True) + + # Install the webui requirements + update_requirements(initial_installation=True) + + +def update_requirements(initial_installation=False): + + # Create .git directory if missing + if not os.path.isdir(os.path.join(script_dir, ".git")): + git_creation_cmd = 'git init -b main && git remote add origin https://github.com/oobabooga/text-generation-webui && git fetch && git remote set-head origin -a && git reset origin/HEAD && git branch --set-upstream-to=origin/HEAD' + run_cmd(git_creation_cmd, environment=True, assert_success=True) + + run_cmd("git pull --autostash", assert_success=True, environment=True) + + # Initial installation only: install the extensions requirements + if initial_installation: + extensions = next(os.walk("extensions"))[1] + for extension in extensions: + if extension in ['superbooga']: # No wheels available for requirements + continue + + extension_req_path = os.path.join("extensions", extension, "requirements.txt") + if os.path.exists(extension_req_path): + run_cmd("python -m pip install -r " + extension_req_path + " --upgrade", assert_success=True, environment=True) + + textgen_requirements = open("requirements.txt").read().splitlines() + + # Workaround for git+ packages not updating properly. Also store requirements.txt for later use + git_requirements = [req for req in textgen_requirements if req.startswith("git+")] + + # Loop through each "git+" requirement and uninstall it + for req in git_requirements: + # Extract the package name from the "git+" requirement + url = req.replace("git+", "") + package_name = url.split("/")[-1].split("@")[0] + + # Uninstall the package using pip + run_cmd("python -m pip uninstall -y " + package_name, environment=True) + print(f"Uninstalled {package_name}") + + # Install/update the project requirements + run_cmd("python -m pip install -r requirements.txt --upgrade", assert_success=True, environment=True) + + # The following requirements are for CUDA, not CPU + # Parse output of 'pip show torch' to determine torch version + torver_cmd = run_cmd("python -m pip show torch", assert_success=True, environment=True, capture_output=True) + torver = [v.split()[1] for v in torver_cmd.stdout.decode('utf-8').splitlines() if 'Version:' in v][0] + + # Check for '+cu' or '+rocm' in version string to determine if torch uses CUDA or ROCm. Check for pytorch-cuda as well for backwards compatibility + if '+cu' not in torver and '+rocm' not in torver and run_cmd("conda list -f pytorch-cuda | grep pytorch-cuda", environment=True, capture_output=True).returncode == 1: + clear_cache() + return + + if not os.path.exists("repositories/"): + os.mkdir("repositories") + + os.chdir("repositories") + + # Install or update ExLlama as needed + if not os.path.exists("exllama/"): + run_cmd("git clone https://github.com/turboderp/exllama.git", environment=True) + else: + os.chdir("exllama") + run_cmd("git pull", environment=True) + os.chdir("..") + + # Pre-installed ExLlama module does not support AMD GPU + if '+rocm' in torver: + run_cmd("python -m pip uninstall -y exllama", environment=True) + # Get download URL for latest ExLlama ROCm wheel + exllama_rocm = run_cmd('curl -s https://api.github.com/repos/jllllll/exllama/releases/latest | grep browser_download_url | grep rocm5.4.2-cp310-cp310-linux_x86_64.whl | cut -d : -f 2,3 | tr -d \'"\'', environment=True, capture_output=True).stdout.decode('utf-8') + if 'rocm5.4.2-cp310-cp310-linux_x86_64.whl' in exllama_rocm: + run_cmd("python -m pip install " + exllama_rocm, environment=True) + + # Fix JIT compile issue with ExLlama in Linux/WSL + if is_linux() and not os.path.exists(f"{conda_env_path}/lib64"): + run_cmd(f'ln -s "{conda_env_path}/lib" "{conda_env_path}/lib64"', environment=True) + + # On some Linux distributions, g++ may not exist or be the wrong version to compile GPTQ-for-LLaMa + if is_linux(): + gxx_output = run_cmd("g++ -dumpfullversion -dumpversion", environment=True, capture_output=True) + if gxx_output.returncode != 0 or int(gxx_output.stdout.strip().split(b".")[0]) > 11: + # Install the correct version of g++ + run_cmd("conda install -y -k conda-forge::gxx_linux-64=11.2.0", environment=True) + + # Install/Update ROCm AutoGPTQ for AMD GPUs + if '+rocm' in torver: + auto_gptq_version = [req for req in textgen_requirements if req.startswith('https://github.com/PanQiWei/AutoGPTQ/releases/download/')][0].split('/')[7] + auto_gptq_wheel = run_cmd(f'curl -s https://api.github.com/repos/PanQiWei/AutoGPTQ/releases/tags/{auto_gptq_version} | grep browser_download_url | grep rocm5.4.2-cp310-cp310-linux_x86_64.whl | cut -d : -f 2,3 | tr -d \'"\'', environment=True, capture_output=True).stdout.decode('utf-8') + if not auto_gptq_wheel and run_cmd(f"python -m pip install {auto_gptq_wheel} --force-reinstall --no-deps", environment=True).returncode != 0: + print_big_message("ERROR: AutoGPTQ wheel installation failed!\n You will not be able to use GPTQ-based models with AutoGPTQ.") + + # Install GPTQ-for-LLaMa for ROCm + if '+rocm' in torver: + gptq_wheel = run_cmd('curl -s https://api.github.com/repos/jllllll/GPTQ-for-LLaMa-CUDA/releases/latest | grep browser_download_url | grep rocm5.4.2-cp310-cp310-linux_x86_64.whl | cut -d : -f 2,3 | tr -d \'"\'', environment=True, capture_output=True).stdout.decode('utf-8') + install_gptq = run_cmd("python -m pip install " + gptq_wheel, environment=True).returncode == 0 + if install_gptq: + print("Wheel installation success!") + else: + print("ERROR: GPTQ wheel installation failed.") + print("You will not be able to use GPTQ-based models with GPTQ-for-LLaMa.") + + clear_cache() + + +def download_model(): + run_cmd("python download-model.py", environment=True) + + +def launch_webui(): + flags = [flag for flag in sys.argv[1:] if flag != '--update'] + run_cmd(f"python server.py {' '.join(flags)} {CMD_FLAGS}", environment=True) + + +if __name__ == "__main__": + # Verifies we are in a conda environment + check_env() + + parser = argparse.ArgumentParser() + parser.add_argument('--update', action='store_true', help='Update the web UI.') + args, _ = parser.parse_known_args() + + if args.update: + update_requirements() + else: + # If webui has already been installed, skip and run + if not is_installed(): + install_webui() + os.chdir(script_dir) + + # Check if a model has been downloaded yet + if len([item for item in glob.glob('models/*') if not item.endswith(('.txt', '.yaml'))]) == 0: + print_big_message("WARNING: You haven't downloaded any model yet.\nOnce the web UI launches, head over to the \"Model\" tab and download one.") + + # Workaround for llama-cpp-python loading paths in CUDA env vars even if they do not exist + conda_path_bin = os.path.join(conda_env_path, "bin") + if not os.path.exists(conda_path_bin): + os.mkdir(conda_path_bin) + + # Launch the webui + launch_webui() diff --git a/server.py b/server.py index fc99ef72..6f7b03ec 100644 --- a/server.py +++ b/server.py @@ -1,6 +1,7 @@ import os import warnings +import modules.one_click_installer_check from modules.block_requests import OpenMonkeyPatch, RequestBlocker from modules.logging_colors import logger diff --git a/start_linux.sh b/start_linux.sh new file mode 100755 index 00000000..d9d2ab07 --- /dev/null +++ b/start_linux.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +cd "$(dirname "${BASH_SOURCE[0]}")" + +if [[ "$(pwd)" =~ " " ]]; then echo This script relies on Miniconda which can not be silently installed under a path with spaces. && exit; fi + +# deactivate existing conda envs as needed to avoid conflicts +{ conda deactivate && conda deactivate && conda deactivate; } 2> /dev/null + +OS_ARCH=$(uname -m) +case "${OS_ARCH}" in + x86_64*) OS_ARCH="x86_64";; + arm64*) OS_ARCH="aarch64";; + aarch64*) OS_ARCH="aarch64";; + *) echo "Unknown system architecture: $OS_ARCH! This script runs only on x86_64 or arm64" && exit +esac + +# config +INSTALL_DIR="$(pwd)/installer_files" +CONDA_ROOT_PREFIX="$(pwd)/installer_files/conda" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" +MINICONDA_DOWNLOAD_URL="https://repo.anaconda.com/miniconda/Miniconda3-py310_23.3.1-0-Linux-${OS_ARCH}.sh" +conda_exists="F" + +# figure out whether git and conda needs to be installed +if "$CONDA_ROOT_PREFIX/bin/conda" --version &>/dev/null; then conda_exists="T"; fi + +# (if necessary) install git and conda into a contained environment +# download miniconda +if [ "$conda_exists" == "F" ]; then + echo "Downloading Miniconda from $MINICONDA_DOWNLOAD_URL to $INSTALL_DIR/miniconda_installer.sh" + + mkdir -p "$INSTALL_DIR" + curl -Lk "$MINICONDA_DOWNLOAD_URL" > "$INSTALL_DIR/miniconda_installer.sh" + + chmod u+x "$INSTALL_DIR/miniconda_installer.sh" + bash "$INSTALL_DIR/miniconda_installer.sh" -b -p $CONDA_ROOT_PREFIX + + # test the conda binary + echo "Miniconda version:" + "$CONDA_ROOT_PREFIX/bin/conda" --version +fi + +# create the installer env +if [ ! -e "$INSTALL_ENV_DIR" ]; then + "$CONDA_ROOT_PREFIX/bin/conda" create -y -k --prefix "$INSTALL_ENV_DIR" python=3.10 +fi + +# check if conda environment was actually created +if [ ! -e "$INSTALL_ENV_DIR/bin/python" ]; then + echo "Conda environment is empty." + exit +fi + +# environment isolation +export PYTHONNOUSERSITE=1 +unset PYTHONPATH +unset PYTHONHOME +export CUDA_PATH="$INSTALL_ENV_DIR" +export CUDA_HOME="$CUDA_PATH" + +# activate installer env +source "$CONDA_ROOT_PREFIX/etc/profile.d/conda.sh" # otherwise conda complains about 'shell not initialized' (needed when running in a script) +conda activate "$INSTALL_ENV_DIR" + +# setup installer env +python one_click.py $@ diff --git a/start_macos.sh b/start_macos.sh new file mode 100755 index 00000000..7fa82d81 --- /dev/null +++ b/start_macos.sh @@ -0,0 +1,67 @@ +#!/bin/bash + +cd "$(dirname "${BASH_SOURCE[0]}")" + +if [[ "$(pwd)" =~ " " ]]; then echo This script relies on Miniconda which can not be silently installed under a path with spaces. && exit; fi + +# deactivate existing conda envs as needed to avoid conflicts +{ conda deactivate && conda deactivate && conda deactivate; } 2> /dev/null + +# M Series or Intel +OS_ARCH=$(uname -m) +case "${OS_ARCH}" in + x86_64*) OS_ARCH="x86_64";; + arm64*) OS_ARCH="arm64";; + *) echo "Unknown system architecture: $OS_ARCH! This script runs only on x86_64 or arm64" && exit +esac + +# config +INSTALL_DIR="$(pwd)/installer_files" +CONDA_ROOT_PREFIX="$(pwd)/installer_files/conda" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" +MINICONDA_DOWNLOAD_URL="https://repo.anaconda.com/miniconda/Miniconda3-py310_23.3.1-0-MacOSX-${OS_ARCH}.sh" +conda_exists="F" + +# figure out whether git and conda needs to be installed +if "$CONDA_ROOT_PREFIX/bin/conda" --version &>/dev/null; then conda_exists="T"; fi + +# (if necessary) install git and conda into a contained environment +# download miniconda +if [ "$conda_exists" == "F" ]; then + echo "Downloading Miniconda from $MINICONDA_DOWNLOAD_URL to $INSTALL_DIR/miniconda_installer.sh" + + mkdir -p "$INSTALL_DIR" + curl -Lk "$MINICONDA_DOWNLOAD_URL" > "$INSTALL_DIR/miniconda_installer.sh" + + chmod u+x "$INSTALL_DIR/miniconda_installer.sh" + bash "$INSTALL_DIR/miniconda_installer.sh" -b -p $CONDA_ROOT_PREFIX + + # test the conda binary + echo "Miniconda version:" + "$CONDA_ROOT_PREFIX/bin/conda" --version +fi + +# create the installer env +if [ ! -e "$INSTALL_ENV_DIR" ]; then + "$CONDA_ROOT_PREFIX/bin/conda" create -y -k --prefix "$INSTALL_ENV_DIR" python=3.10 +fi + +# check if conda environment was actually created +if [ ! -e "$INSTALL_ENV_DIR/bin/python" ]; then + echo "Conda environment is empty." + exit +fi + +# environment isolation +export PYTHONNOUSERSITE=1 +unset PYTHONPATH +unset PYTHONHOME +export CUDA_PATH="$INSTALL_ENV_DIR" +export CUDA_HOME="$CUDA_PATH" + +# activate installer env +source "$CONDA_ROOT_PREFIX/etc/profile.d/conda.sh" # otherwise conda complains about 'shell not initialized' (needed when running in a script) +conda activate "$INSTALL_ENV_DIR" + +# setup installer env +python one_click.py $@ diff --git a/start_windows.bat b/start_windows.bat new file mode 100755 index 00000000..da8050f0 --- /dev/null +++ b/start_windows.bat @@ -0,0 +1,84 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. && goto end + +@rem Check for special characters in installation path +set "SPCHARMESSAGE="WARNING: Special characters were detected in the installation path!" " This can cause the installation to fail!"" +echo "%CD%"| findstr /R /C:"[!#\$%&()\*+,;<=>?@\[\]\^`{|}~]" >nul && ( + call :PrintBigMessage %SPCHARMESSAGE% +) +set SPCHARMESSAGE= + +@rem fix failed install when installing to a separate drive +set TMP=%cd%\installer_files +set TEMP=%cd%\installer_files + +@rem deactivate existing conda envs as needed to avoid conflicts +(conda deactivate && conda deactivate && conda deactivate) 2>nul + +@rem config +set INSTALL_DIR=%cd%\installer_files +set CONDA_ROOT_PREFIX=%cd%\installer_files\conda +set INSTALL_ENV_DIR=%cd%\installer_files\env +set MINICONDA_DOWNLOAD_URL=https://repo.anaconda.com/miniconda/Miniconda3-py310_23.3.1-0-Windows-x86_64.exe +set conda_exists=F + +@rem figure out whether git and conda needs to be installed +call "%CONDA_ROOT_PREFIX%\_conda.exe" --version >nul 2>&1 +if "%ERRORLEVEL%" EQU "0" set conda_exists=T + +@rem (if necessary) install git and conda into a contained environment +@rem download conda +if "%conda_exists%" == "F" ( + echo Downloading Miniconda from %MINICONDA_DOWNLOAD_URL% to %INSTALL_DIR%\miniconda_installer.exe + + mkdir "%INSTALL_DIR%" + call curl -Lk "%MINICONDA_DOWNLOAD_URL%" > "%INSTALL_DIR%\miniconda_installer.exe" || ( echo. && echo Miniconda failed to download. && goto end ) + + echo Installing Miniconda to %CONDA_ROOT_PREFIX% + start /wait "" "%INSTALL_DIR%\miniconda_installer.exe" /InstallationType=JustMe /NoShortcuts=1 /AddToPath=0 /RegisterPython=0 /NoRegistry=1 /S /D=%CONDA_ROOT_PREFIX% + + @rem test the conda binary + echo Miniconda version: + call "%CONDA_ROOT_PREFIX%\_conda.exe" --version || ( echo. && echo Miniconda not found. && goto end ) +) + +@rem create the installer env +if not exist "%INSTALL_ENV_DIR%" ( + echo Packages to install: %PACKAGES_TO_INSTALL% + call "%CONDA_ROOT_PREFIX%\_conda.exe" create --no-shortcuts -y -k --prefix "%INSTALL_ENV_DIR%" python=3.10 || ( echo. && echo Conda environment creation failed. && goto end ) +) + +@rem check if conda environment was actually created +if not exist "%INSTALL_ENV_DIR%\python.exe" ( echo. && echo Conda environment is empty. && goto end ) + +@rem environment isolation +set PYTHONNOUSERSITE=1 +set PYTHONPATH= +set PYTHONHOME= +set "CUDA_PATH=%INSTALL_ENV_DIR%" +set "CUDA_HOME=%CUDA_PATH%" + +@rem activate installer env +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end ) + +@rem setup installer env +call python one_click.py %* + +@rem below are functions for the script next line skips these during normal execution +goto end + +:PrintBigMessage +echo. && echo. +echo ******************************************************************* +for %%M in (%*) do echo * %%~M +echo ******************************************************************* +echo. && echo. +exit /b + +:end +pause diff --git a/start_wsl.bat b/start_wsl.bat new file mode 100755 index 00000000..d7bacead --- /dev/null +++ b/start_wsl.bat @@ -0,0 +1,11 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +@rem sed -i 's/\x0D$//' ./wsl.sh converts newlines to unix format in the wsl script +call wsl -e bash -lic "sed -i 's/\x0D$//' ./wsl.sh; source ./wsl.sh %*" + +:end +pause diff --git a/update_linux.sh b/update_linux.sh new file mode 100755 index 00000000..28686b0f --- /dev/null +++ b/update_linux.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +cd "$(dirname "${BASH_SOURCE[0]}")" + +if [[ "$(pwd)" =~ " " ]]; then echo This script relies on Miniconda which can not be silently installed under a path with spaces. && exit; fi + +# deactivate existing conda envs as needed to avoid conflicts +{ conda deactivate && conda deactivate && conda deactivate; } 2> /dev/null + +# config +CONDA_ROOT_PREFIX="$(pwd)/installer_files/conda" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" + +# environment isolation +export PYTHONNOUSERSITE=1 +unset PYTHONPATH +unset PYTHONHOME +export CUDA_PATH="$INSTALL_ENV_DIR" +export CUDA_HOME="$CUDA_PATH" + +# activate installer env +source "$CONDA_ROOT_PREFIX/etc/profile.d/conda.sh" # otherwise conda complains about 'shell not initialized' (needed when running in a script) +conda activate "$INSTALL_ENV_DIR" + +# update installer env +python one_click.py --update + +echo +echo "Done!" diff --git a/update_macos.sh b/update_macos.sh new file mode 100755 index 00000000..28686b0f --- /dev/null +++ b/update_macos.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +cd "$(dirname "${BASH_SOURCE[0]}")" + +if [[ "$(pwd)" =~ " " ]]; then echo This script relies on Miniconda which can not be silently installed under a path with spaces. && exit; fi + +# deactivate existing conda envs as needed to avoid conflicts +{ conda deactivate && conda deactivate && conda deactivate; } 2> /dev/null + +# config +CONDA_ROOT_PREFIX="$(pwd)/installer_files/conda" +INSTALL_ENV_DIR="$(pwd)/installer_files/env" + +# environment isolation +export PYTHONNOUSERSITE=1 +unset PYTHONPATH +unset PYTHONHOME +export CUDA_PATH="$INSTALL_ENV_DIR" +export CUDA_HOME="$CUDA_PATH" + +# activate installer env +source "$CONDA_ROOT_PREFIX/etc/profile.d/conda.sh" # otherwise conda complains about 'shell not initialized' (needed when running in a script) +conda activate "$INSTALL_ENV_DIR" + +# update installer env +python one_click.py --update + +echo +echo "Done!" diff --git a/update_windows.bat b/update_windows.bat new file mode 100755 index 00000000..b08773cf --- /dev/null +++ b/update_windows.bat @@ -0,0 +1,37 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. && goto end + +@rem fix failed install when installing to a separate drive +set TMP=%cd%\installer_files +set TEMP=%cd%\installer_files + +@rem deactivate existing conda envs as needed to avoid conflicts +(conda deactivate && conda deactivate && conda deactivate) 2>nul + +@rem config +set CONDA_ROOT_PREFIX=%cd%\installer_files\conda +set INSTALL_ENV_DIR=%cd%\installer_files\env + +@rem environment isolation +set PYTHONNOUSERSITE=1 +set PYTHONPATH= +set PYTHONHOME= +set "CUDA_PATH=%INSTALL_ENV_DIR%" +set "CUDA_HOME=%CUDA_PATH%" + +@rem activate installer env +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end ) + +@rem update installer env +call python one_click.py --update + +echo. +echo Done! + +:end +pause diff --git a/update_wsl.bat b/update_wsl.bat new file mode 100755 index 00000000..36d019a8 --- /dev/null +++ b/update_wsl.bat @@ -0,0 +1,11 @@ +@echo off + +cd /D "%~dp0" + +set PATH=%PATH%;%SystemRoot%\system32 + +@rem sed -i 's/\x0D$//' ./wsl.sh converts newlines to unix format in the wsl script calling wsl.sh with 'update' will run updater +call wsl -e bash -lic "sed -i 's/\x0D$//' ./wsl.sh; source ./wsl.sh update" + +:end +pause diff --git a/wsl.sh b/wsl.sh new file mode 100755 index 00000000..4ff72c04 --- /dev/null +++ b/wsl.sh @@ -0,0 +1,110 @@ +#!/bin/bash + +# detect if build-essential is missing or broken +if ! dpkg-query -W -f'${Status}' "build-essential" 2>/dev/null | grep -q "ok installed"; then +echo "build-essential not found or broken! + +A C++ compiler is required to build needed Python packages! +To install one, run cmd_wsl.bat and enter these commands: + +sudo apt-get update +sudo apt-get install build-essential +" +read -n1 -p "Continue the installer anyway? [y,n]" EXIT_PROMPT +# only continue if user inputs 'y' else exit +if ! [[ $EXIT_PROMPT == "Y" || $EXIT_PROMPT == "y" ]]; then exit; fi +fi + +# deactivate existing conda envs as needed to avoid conflicts +{ conda deactivate && conda deactivate && conda deactivate; } 2> /dev/null + +# config unlike other scripts, can't use current directory due to file IO bug in WSL, needs to be in virtual drive +INSTALL_DIR_PREFIX="$HOME/text-gen-install" +if [[ ! $(realpath "$(pwd)/..") = /mnt/* ]]; then + INSTALL_DIR_PREFIX="$(realpath "$(pwd)/..")" && INSTALL_INPLACE=1 +fi +INSTALL_DIR="$INSTALL_DIR_PREFIX/text-generation-webui" +CONDA_ROOT_PREFIX="$INSTALL_DIR/installer_files/conda" +INSTALL_ENV_DIR="$INSTALL_DIR/installer_files/env" +MINICONDA_DOWNLOAD_URL="https://repo.anaconda.com/miniconda/Miniconda3-py310_23.3.1-0-Linux-x86_64.sh" +conda_exists="F" + +# environment isolation +export PYTHONNOUSERSITE=1 +unset PYTHONPATH +unset PYTHONHOME +export CUDA_PATH="$INSTALL_ENV_DIR" +export CUDA_HOME="$CUDA_PATH" + +# /usr/lib/wsl/lib needs to be added to LD_LIBRARY_PATH to fix years-old bug in WSL where GPU drivers aren't linked properly +export LD_LIBRARY_PATH="$CUDA_HOME/lib:/usr/lib/wsl/lib:$LD_LIBRARY_PATH" + +# open bash cli if called with 'wsl.sh cmd' with workarounds for existing conda +if [ "$1" == "cmd" ]; then + exec bash --init-file <(echo ". ~/.bashrc; conda deactivate 2> /dev/null; cd $INSTALL_DIR || cd $HOME; source $CONDA_ROOT_PREFIX/etc/profile.d/conda.sh; conda activate $INSTALL_ENV_DIR") + exit +fi + +if [[ "$INSTALL_DIR" =~ " " ]]; then echo This script relies on Miniconda which can not be silently installed under a path with spaces. && exit; fi + +# create install dir if missing and copy one_click.py to install dir to maintain functionality without edit +if [ ! -d "$INSTALL_DIR" ]; then mkdir -p "$INSTALL_DIR" || exit; fi +cp -u "./one_click.py" "$INSTALL_DIR" +if [ -f "./CMD_FLAGS.txt" ]; then cp -u "./CMD_FLAGS.txt" "$INSTALL_DIR"; fi + +# figure out whether git and conda needs to be installed +if "$CONDA_ROOT_PREFIX/bin/conda" --version &>/dev/null; then conda_exists="T"; fi + +# (if necessary) install git and conda into a contained environment +# download miniconda +if [ "$conda_exists" == "F" ]; then + echo "Downloading Miniconda from $MINICONDA_DOWNLOAD_URL to $INSTALL_DIR/miniconda_installer.sh" + + curl -Lk "$MINICONDA_DOWNLOAD_URL" > "$INSTALL_DIR/miniconda_installer.sh" + + chmod u+x "$INSTALL_DIR/miniconda_installer.sh" + bash "$INSTALL_DIR/miniconda_installer.sh" -b -p $CONDA_ROOT_PREFIX + + # test the conda binary + echo "Miniconda version:" + "$CONDA_ROOT_PREFIX/bin/conda" --version +fi + +# create the installer env +if [ ! -e "$INSTALL_ENV_DIR" ]; then + "$CONDA_ROOT_PREFIX/bin/conda" create -y -k --prefix "$INSTALL_ENV_DIR" python=3.10 git +fi + +# check if conda environment was actually created +if [ ! -e "$INSTALL_ENV_DIR/bin/python" ]; then + echo "Conda environment is empty." + exit +fi + +# activate installer env +source "$CONDA_ROOT_PREFIX/etc/profile.d/conda.sh" # otherwise conda complains about 'shell not initialized' (needed when running in a script) +conda activate "$INSTALL_ENV_DIR" + +# copy one_click.py and CMD_FLAGS.txt to install dir to allow edits within Windows +if [[ $INSTALL_INPLACE != 1 ]]; then + cp -u "./one_click.py" "$INSTALL_DIR" + if [ -f "./CMD_FLAGS.txt" ]; then cp -u "./CMD_FLAGS.txt" "$INSTALL_DIR"; fi +fi + +cd $INSTALL_DIR + +if [ ! -f "./server.py" ]; then + git init -b main + git remote add origin https://github.com/oobabooga/text-generation-webui + git fetch + git remote set-head origin -a + git reset origin/HEAD --hard + git branch --set-upstream-to=origin/HEAD + git restore -- . :!./one_click.py :!./CMD_FLAGS.txt +fi + +# setup installer env update env if called with 'wsl.sh update' +case "$1" in +("update") python one_click.py --update;; +(*) python one_click.py $@;; +esac