diff --git a/README.md b/README.md index 540c6af..3954a2f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ -# AllTalk TTS v2 BETA (ITS NOT UPLOADED YET SO DONT TRY TO DO ANY OF THIS) -This is the BETA of v2. To be clear, that means they may well be bugs, issues, missing/incomplete documentation, a variety of potential problems etc (This is what BETA refers to in the software world for those whom dont know). This means you may need a bit of technical know how to deal with things that could come up. +# AllTalk TTS v2 BETA +This is the BETA of v2. To be clear, that means they may well be bugs, issues, missing/incomplete documentation, a variety of potential problems etc (This is what BETA refers to in the software world for those whom dont know). This means you may need a bit of technical know how to deal with things that could come up and should only continue if you feel comfortable with that. + +Github discussions on the BETA are [here in the discussion board](https://github.com/erew123/alltalk_tts/discussions/245) #### If you are going to run the BETA, please read all the info below.... @@ -18,17 +20,19 @@ Seperate from Standalone installations, installation within the Text-generation- - Gradio web interface (see the screenshots) - Retrieval based Voice Conversion, AKA, RVC voice Pipeline, re-written to work on Python 3.11. - TTS Generator will now use any TTS engine. -- Vastly updated API Suite. **V2 is not 100% compatable with v1.**. New SillyTavern extension is included in the `/system/` folder. See here for an explanation https://github.com/erew123/alltalk_tts/issues/166 +- Vastly updated API Suite. **V2 is not 100% compatable with v1. (See here for an explanation https://github.com/erew123/alltalk_tts/issues/166)**. + - New SillyTavern extension is included in the `/system/` folder. + - New remote TGWUI extension if you want to run your TTS elsewhere from the AllTalk server. - OpenAI compatable endpoint/API. Meaning this should work as an alternative endpoint with any software that can send a TTS generation request to OpenAI. - Fully customisable API Settings. I have **NOT** tested any limits of any TTS engines. - Updated Finetuning for XTTS models - Audio Transcoding to about 6x formats (mp3, opus, etc) - About 50 gradio interface themes if you arent happy with the standard Gradio one. - Documentaton has been about 80% updated. Its built into the interface. +- Lots and lots of other things too numerous to mention. There is a welcome screen that covers a few other bits, along with the built in documentation. - --- ### Windows Systems diff --git a/atsetup.bat b/atsetup.bat new file mode 100644 index 0000000..1c546f7 --- /dev/null +++ b/atsetup.bat @@ -0,0 +1,676 @@ +@echo off +cd /D "%~dp0" +setlocal enabledelayedexpansion +@set "PATH=%PATH%;%SystemRoot%\system32" + +:: Generate the ESC character +for /F %%a in ('echo prompt $E ^| cmd') do set "ESC=%%a" + +:: Standard Colors +set "BLACK=%ESC%[30m" +set "RED=%ESC%[31m" +set "GREEN=%ESC%[32m" +set "YELLOW=%ESC%[33m" +set "BLUE=%ESC%[34m" +set "MAGENTA=%ESC%[35m" +set "CYAN=%ESC%[36m" +set "WHITE=%ESC%[37m" +set "L_BLACK=%ESC%[90m" +set "L_RED=%ESC%[91m" +set "L_GREEN=%ESC%[92m" +set "L_YELLOW=%ESC%[93m" +set "L_BLUE=%ESC%[94m" +set "L_MAGENTA=%ESC%[95m" +set "L_CYAN=%ESC%[96m" +set "L_WHITE=%ESC%[97m" +set "RESET=%ESC%[0m" + +echo "%CD%"| findstr /C:" " >nul && echo. && echo %L_BLUE%ALLTALK WINDOWS SETUP UTILITY%RESET% && echo. && echo. && echo You are trying to install AllTalk in a folder that has a space in the folder path e.g. && echo. && echo C:\%L_RED%program files%RESET%\alltalk_tts && echo. && echo This causes errors with Conda and Python scripts. Please follow this link for reference: && echo. && echo %L_CYAN%https://docs.anaconda.com/free/working-with-conda/reference/faq/#installing-anaconda%RESET% && echo. && echo Please use a folder path that has no spaces in it e.g. && echo. && echo C:\myfiles\alltalk_tts\ && echo. && echo. && pause && 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 Check if curl is available +curl --version >nul 2>&1 +if "%ERRORLEVEL%" NEQ "0" ( + echo curl is not available on this system. Please install curl then re-run the script https://curl.se/ + echo or perform a manual installation of a Conda Python environment. + goto end +) + +:MainMenu +cls +echo. +echo %L_BLUE%ALLTALK WINDOWS SETUP UTILITY%RESET% +echo. +echo INSTALLATION TYPE +echo 1) I am using AllTalk as part of %L_GREEN%Text-generation-webui%RESET% +echo 2) I am using AllTalk as a %L_BLUE%Standalone Application%RESET% +echo. +echo 9) %L_RED%Exit/Quit%RESET% +echo. +echo. +set /p UserOption=" Enter your choice: " + +if "%UserOption%"=="1" goto WebUIMenu +if "%UserOption%"=="2" goto StandaloneMenu +if "%UserOption%"=="9" goto End +goto MainMenu + +:WebUIMenu +cls +echo. +echo %L_BLUE%TEXT-GENERATION-WEBUI SETUP%RESET% +echo. +echo Please ensure you have started your Text-generation-webui Python +echo environment. If you have NOT done this, please run %L_GREEN%cmd_windows.bat%RESET% +echo in the %L_GREEN%text-generation-webui%RESET% folder and then re-run this script. +echo. +echo BASE REQUIREMENTS +echo 1) Apply/Re-Apply the requirements for %L_GREEN%Text-generation-webui%RESET%. +echo. +echo OPTIONAL +echo 2) Git Pull the latest AllTalk updates from Github +echo. +echo DEEPSPEED FOR %L_YELLOW=%PyTorch 2.1.x%RESET% +echo 4) Install DeepSpeed v11.2 for CUDA %L_GREEN%11.8%RESET% and Python-3.11.x and %L_YELLOW%PyTorch 2.1.x%RESET%. +echo 5) Install DeepSpeed v11.2 for CUDA %L_GREEN%12.1%RESET% and Python-3.11.x and %L_YELLOW%PyTorch 2.1.x%RESET%. +echo. +echo DEEPSPEED FOR %L_YELLOW=%PyTorch 2.2.x%RESET% (March 2024 builds of Text-gen-webui and later) +echo 6) Install DeepSpeed v14.0 for CUDA %L_GREEN%12.1%RESET% and Python-3.11.x and %L_YELLOW%PyTorch 2.2.x%RESET%. +echo 7) Install DeepSpeed v14.0 for CUDA %L_GREEN%11.8%RESET% and Python-3.11.x and %L_YELLOW%PyTorch 2.2.x%RESET%. +echo. +echo U) Uninstall DeepSpeed. +echo. +echo. OTHER +echo 8) Generate a diagnostics file. +echo. +echo 9) %L_RED%Exit/Quit%RESET% +echo. +set /p WebUIOption="Enter your choice: " +if "%WebUIOption%"=="1" goto InstallNvidiaTextGen +if "%WebUIOption%"=="2" goto TGGitpull +if "%WebUIOption%"=="4" goto InstallDeepSpeed118TextGen +if "%WebUIOption%"=="5" goto InstallDeepSpeed121TextGen +if "%WebUIOption%"=="6" goto InstallDeepSpeed121TextGenPytorch221 +if "%WebUIOption%"=="7" goto InstallDeepSpeed118TextGenPytorch221 +if "%WebUIOption%"=="U" goto UnInstallDeepSpeed +if "%WebUIOption%"=="u" goto UnInstallDeepSpeed +if "%WebUIOption%"=="8" goto GenerateDiagsTextGen +if "%WebUIOption%"=="9" goto End +goto WebUIMenu + +:StandaloneMenu +cls +echo. +echo %L_BLUE%ALLTALK STANDALONE APPLICATION SETUP%RESET% +echo. +echo BASE REQUIREMENTS +echo 1) Install AllTalk as a Standalone Application +echo. +echo OPTIONAL +echo 2) Git Pull the latest AllTalk updates from Github +echo 3) Re-Apply/Update the requirements file +echo 4) Delete AllTalk's custom Python environment +echo 5) Purge the PIP cache +echo. +echo. OTHER +echo 8) Generate a diagnostics file +echo. +echo 9) %L_RED%Exit/Quit%RESET% +echo. +set /p StandaloneOption="Enter your choice: " +if "%StandaloneOption%"=="1" goto InstallCustomStandalone +if "%StandaloneOption%"=="2" goto STGitpull +if "%StandaloneOption%"=="3" goto STReapplyrequirements +if "%StandaloneOption%"=="4" goto STDeleteCustomStandalone +if "%StandaloneOption%"=="5" goto STPurgepipcache +if "%StandaloneOption%"=="8" goto GenerateDiagsStandalone +if "%StandaloneOption%"=="9" goto EndStandalone +goto StandaloneMenu + +:InstallNvidiaTextGen +pip install -r system\requirements\requirements_textgen.txt +pip install -r system\requirements\requirements_textgen2.txt +echo ** Faiss ** +call "%CONDA_ROOT_PREFIX%\Scripts\conda.exe" install -y pytorch::faiss-cpu +echo ** FFmpeg ** +call "%CONDA_ROOT_PREFIX%\Scripts\conda.exe" install -y conda-forge::ffmpeg +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error installing the requirements. + echo Have you started your Text-gen-webui Python environment + echo with cmd_{yourOS} before running atsetup.bat? + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +Echo. +echo. +Echo Requirements installed successfully. +Echo. +pause +goto WebUIMenu + +:TGGitpull +git pull +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error pulling from Github. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +Echo. +echo AllTalk Updated from Github. Please re-apply +echo the latest requirements file. Option 1 +Echo. +pause +goto WebUIMenu + +:InstallDeepSpeed118TextGen +echo Downloading DeepSpeed... +curl -LO https://github.com/erew123/alltalk_tts/releases/download/deepspeed/deepspeed-0.11.2+cuda118-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to download DeepSpeed wheel file. + echo Please check your internet connection or try again later. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +echo DeepSpeed wheel file downloaded successfully. +echo Installing DeepSpeed... +pip install deepspeed-0.11.2+cuda118-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to install DeepSpeed. + echo Please check if the wheel file is compatible with your system. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +Echo. +echo. +Echo DeepSpeed installed successfully. +Echo. +del deepspeed-0.11.2+cuda118-cp311-cp311-win_amd64.whl +pause +goto WebUIMenu + +:InstallDeepSpeed121TextGen +echo Downloading DeepSpeed... +curl -LO https://github.com/erew123/alltalk_tts/releases/download/deepspeed/deepspeed-0.11.2+cuda121-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to download DeepSpeed wheel file. + echo Please check your internet connection or try again later. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +echo DeepSpeed wheel file downloaded successfully. +echo Installing DeepSpeed... +pip install deepspeed-0.11.2+cuda121-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to install DeepSpeed. + echo Please check if the wheel file is compatible with your system. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +Echo. +echo. +Echo DeepSpeed installed successfully. +Echo. +del deepspeed-0.11.2+cuda121-cp311-cp311-win_amd64.whl +pause +goto WebUIMenu + +:InstallDeepSpeed121TextGenPytorch221 +echo Downloading DeepSpeed... +curl -LO https://github.com/erew123/alltalk_tts/releases/download/DeepSpeed-14.0/deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to download DeepSpeed wheel file. + echo Please check your internet connection or try again later. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +echo DeepSpeed wheel file downloaded successfully. +echo Installing DeepSpeed... +pip install deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to install DeepSpeed. + echo Please check if the wheel file is compatible with your system. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +Echo. +echo. +Echo DeepSpeed installed successfully. +Echo. +del deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +pause +goto WebUIMenu + +:InstallDeepSpeed118TextGenPytorch221 +echo Downloading DeepSpeed... +curl -LO https://github.com/erew123/alltalk_tts/releases/download/DeepSpeed-14.0/deepspeed-0.14.0+cu118-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to download DeepSpeed wheel file. + echo Please check your internet connection or try again later. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +echo DeepSpeed wheel file downloaded successfully. +echo Installing DeepSpeed... +pip install deepspeed-0.14.0+cu118-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to install DeepSpeed. + echo Please check if the wheel file is compatible with your system. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +Echo. +echo. +Echo DeepSpeed installed successfully. +Echo. +del deepspeed-0.14.0+cu118-cp311-cp311-win_amd64.whl +pause +goto WebUIMenu + +:UnInstallDeepSpeed +pip uninstall deepspeed +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error uninstalling DeepSpeed. + echo Press any key to return to the menu. + echo. + pause + goto WebUIMenu +) +echo. +Echo DeepSpeed uninstalled successfully. +Echo. +pause +goto WebUIMenu + +:GenerateDiagsTextGen +Python diagnostics.py +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error running diagnostics. Have you correctly started your + echo Text-generation-webui Python environment with %L_GREEN%cmd_windows.bat%RESET%? + echo. + pause + goto WebUIMenu +) +Echo. +echo. +Echo Diagnostics.log generated. Please scroll up to look over the log. +Echo. +pause +goto WebUIMenu + +:InstallCustomStandalone +set PATH=%PATH%;%SystemRoot%\system32 + +@rem Check if curl is available +curl --version >nul 2>&1 +if "%ERRORLEVEL%" NEQ "0" ( + echo curl is not available on this system. Please install curl then re-run the script https://curl.se/ + echo or perform a manual installation of a Conda Python environment. + goto end +) +echo "%CD%"| findstr /C:" " >nul && echo This script relies on Miniconda which can not be silently installed under a path with spaces. Please correct your folder names and re-try installation. && goto exit + +@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%\alltalk_environment +set TEMP=%cd%\alltalk_environment + +@rem deactivate existing conda envs as needed to avoid conflicts +(call conda deactivate && call conda deactivate && call conda deactivate) 2>nul + +@rem config +set INSTALL_DIR=%cd%\alltalk_environment +set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda +set INSTALL_ENV_DIR=%cd%\alltalk_environment\env +set MINICONDA_DOWNLOAD_URL=https://repo.anaconda.com/miniconda/Miniconda3-py311_24.4.0-0-Windows-x86_64.exe +set conda_exists=F + +@rem figure out whether git and conda need to be installed +call "%CONDA_ROOT_PREFIX%\_conda.exe" --version >nul 2>&1 +if "%ERRORLEVEL%" EQU "0" ( + set conda_exists=T + call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" + goto RunScript +) + +@rem download and install conda if not installed +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% +echo Miniconda version: +call "%CONDA_ROOT_PREFIX%\_conda.exe" --version || ( echo. && echo Miniconda not found. && goto end ) + +@rem create the installer env +echo Packages to install: %PACKAGES_TO_INSTALL% +call "%CONDA_ROOT_PREFIX%\_conda.exe" create --no-shortcuts -y -k --prefix "%INSTALL_ENV_DIR%" python=3.11.9 || ( 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 activate installer env +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" || ( echo. && echo Miniconda hook not found. && goto end ) +rem Install required packages +@rem Enable conda logging +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" config --set log_level debug +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" config --set log_file "%INSTALL_ENV_DIR%\conda.log" +echo ** PyTorch 2.2.1 ** +call "%CONDA_ROOT_PREFIX%\Scripts\conda.exe" install -y pytorch==2.2.1 torchvision==0.17.1 torchaudio==2.2.1 pytorch-cuda=12.1 -c pytorch -c nvidia +echo ** Faiss ** +call "%CONDA_ROOT_PREFIX%\Scripts\conda.exe" install -y pytorch::faiss-cpu +echo ** FFmpeg ** +call "%CONDA_ROOT_PREFIX%\Scripts\conda.exe" install -y conda-forge::ffmpeg +echo ** Requirements file ** +pip install -r system\requirements\requirements_standalone.txt +echo ** Gradio Update ** +pip install --upgrade gradio==4.32.2 +echo ** DeepSpeed ** +curl -LO https://github.com/erew123/alltalk_tts/releases/download/DeepSpeed-14.0/deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +echo Installing DeepSpeed... +pip install deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +del deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +echo ** Clean Environment ** +call "%CONDA_ROOT_PREFIX%\Scripts\conda.exe" clean --all --force-pkgs-dirs -y + + +@rem Create start_environment.bat to run AllTalk environment +echo @echo off > start_environment.bat +echo cd /D "%~dp0" >> start_environment.bat +echo set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda >> start_environment.bat +echo set INSTALL_ENV_DIR=%cd%\alltalk_environment\env >> start_environment.bat +echo call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" >> start_environment.bat +@rem Create start_alltalk.bat to run AllTalk +echo @echo off > start_alltalk.bat +echo cd /D "%~dp0" >> start_alltalk.bat +echo set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda >> start_alltalk.bat +echo set INSTALL_ENV_DIR=%cd%\alltalk_environment\env >> start_alltalk.bat +echo call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" >> start_alltalk.bat +echo call python script.py >> start_alltalk.bat +@rem Create start_finetune.bat to run AllTalk +echo @echo off > start_finetune.bat +echo cd /D "%~dp0" >> start_finetune.bat +echo set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda >> start_finetune.bat +echo set INSTALL_ENV_DIR=%cd%\alltalk_environment\env >> start_finetune.bat +echo call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" >> start_finetune.bat +echo call python finetune.py >> start_finetune.bat +echo @echo off > start_diagnostics.bat +echo cd /D "%~dp0" >> start_diagnostics.bat +echo set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda >> start_diagnostics.bat +echo set INSTALL_ENV_DIR=%cd%\alltalk_environment\env >> start_diagnostics.bat +echo call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" >> start_diagnostics.bat +echo call python diagnostics.py >> start_diagnostics.bat +Echo. +Echo Run %L_YELLOW%start_alltalk.bat%RESET% to start AllTalk. +Echo Run %L_YELLOW%start_diagnostics.bat%RESET% to start the diagnostics. +Echo Run %L_YELLOW%start_finetune.bat%RESET% to start Finetuning. +Echo Run %L_YELLOW%start_environment.bat%RESET% to start the AllTalk Python environment. +Echo. +pause +goto StandaloneMenu + +:STDeleteCustomStandalone +@rem Check if the alltalk_environment directory exists +if not exist "%cd%\alltalk_environment\" ( + echo. + echo %L_GREEN%alltalk_environment%RESET% directory does not exist. No need to delete. + echo. + pause + goto StandaloneMenu +) +@rem Check if a Conda environment is active +if not defined CONDA_PREFIX goto NoCondaEnvDeleteCustomStandalone +@rem Deactivate Conda environment if it's active +Echo Exiting the Conda Environment. Please run %L_GREEN%atsetup.bat%RESET% again and delete the environment. +call conda deactivate +:NoCondaEnvDeleteCustomStandalone +echo Deleting "alltalk_environment". Please wait. +rd /s /q "alltalk_environment" +del start_alltalk.bat +del start_environment.bat +if %ERRORLEVEL% neq 0 ( + echo. + echo Failed to delete alltalk_environment folder. + echo Please make sure it is not in use and try again. + echo Worst case, manually delete the folder called 'alltalk_environment' + echo. + pause + goto StandaloneMenu +) +echo. +Echo. +echo Environment %L_GREEN%alltalk_environment%RESET% deleted. Please set up the environment again. +Echo. +pause +goto StandaloneMenu + +:GenerateDiagsStandalone +cd /D "%~dp0" +set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda +set INSTALL_ENV_DIR=%cd%\alltalk_environment\env +@rem Check if the Conda environment exists +if not exist "%INSTALL_ENV_DIR%\python.exe" ( + echo. + echo The Conda environment at "%INSTALL_ENV_DIR%" does not exist. + echo Please install the environment before proceeding. + echo. + pause + goto StandaloneMenu +) +@rem Attempt to activate the Conda environment +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" +if errorlevel 1 ( + echo. + echo Failed to activate the Conda environment. + echo Please check your installation and try again. + echo. + pause + goto StandaloneMenu +) +@rem Run diagnostics +Python diagnostics.py +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error running diagnostics. + echo Press any key to return to the menu. + echo. + pause + goto StandaloneMenu +) +Echo. +Echo. +Echo Diagnostics.log generated. Please scroll up to look over the log. +Echo. +pause +goto StandaloneMenu + +:STReapplyrequirements +cd /D "%~dp0" +set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda +set INSTALL_ENV_DIR=%cd%\alltalk_environment\env +@rem Check if the Conda environment exists +if not exist "%INSTALL_ENV_DIR%\python.exe" ( + echo. + echo The Conda environment at "%INSTALL_ENV_DIR%" does not exist. + echo Please install the environment before proceeding. + echo. + pause + goto StandaloneMenu +) +@rem Attempt to activate the Conda environment +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" +if errorlevel 1 ( + echo. + echo Failed to activate the Conda environment. + echo Please check your installation and try again. + echo. + pause + goto StandaloneMenu +) +@rem Run Reapply requirements +echo. +echo. +echo Installing requirements. If you have problems after this, you should +echo delete the alltalk_environment and run the setup routine again. +echo. +pip install -r system\requirements\requirements_standalone.txt +echo Installing DeepSpeed... +pip install deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +del deepspeed-0.14.0+ce78a63-cp311-cp311-win_amd64.whl +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error. + echo Press any key to return to the menu. + echo. + pause + goto StandaloneMenu +) +Echo. +Echo. +Echo Requirements have been re-applied/updated. +Echo. +pause +goto StandaloneMenu + +:STPurgepipcache +cd /D "%~dp0" +set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda +set INSTALL_ENV_DIR=%cd%\alltalk_environment\env +@rem Check if the Conda environment exists +if not exist "%INSTALL_ENV_DIR%\python.exe" ( + echo. + echo The Conda environment at "%INSTALL_ENV_DIR%" does not exist. + echo Please install the environment before proceeding. + echo. + pause + goto StandaloneMenu +) +@rem Attempt to activate the Conda environment +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" +if errorlevel 1 ( + echo. + echo Failed to activate the Conda environment. + echo Please check your installation and try again. + echo. + pause + goto StandaloneMenu +) +@rem Clear the PIP cache +echo. +echo Purging the PIP cache of downloaded files. +echo. +pip cache purge +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error. + echo Press any key to return to the menu. + echo. + pause + goto StandaloneMenu +) +Echo. +Echo The PIP cache has been purged. +Echo. +pause +goto StandaloneMenu + +:STGitpull +cd /D "%~dp0" +set CONDA_ROOT_PREFIX=%cd%\alltalk_environment\conda +set INSTALL_ENV_DIR=%cd%\alltalk_environment\env +@rem Check if the Conda environment exists +if not exist "%INSTALL_ENV_DIR%\python.exe" ( + echo. + echo The Conda environment at "%INSTALL_ENV_DIR%" does not exist. + echo Please install the environment before proceeding. + echo. + pause + goto StandaloneMenu +) +@rem Attempt to activate the Conda environment +call "%CONDA_ROOT_PREFIX%\condabin\conda.bat" activate "%INSTALL_ENV_DIR%" +if errorlevel 1 ( + echo. + echo Failed to activate the Conda environment. + echo Please check your installation and try again. + echo. + pause + goto StandaloneMenu +) +@rem Pull from Github +echo. +echo Pulling the latest updates. Please re-apply +echo the latest requirements file. Option 3 +echo. +git pull +if %ERRORLEVEL% neq 0 ( + echo. + echo There was an error pulling from Github. + echo Press any key to return to the menu. + echo. + pause + goto StandaloneMenu +) +Echo. +echo AllTalk Updated from Github. Please re-apply +echo the latest requirements file. Option 3 +Echo. +pause +goto StandaloneMenu + +:EndStandalone +echo Exiting AllTalk Setup Utility... +echo. +Echo Remember, after installation you can.... +Echo Run %L_YELLOW%start_alltalk.bat%RESET% to start AllTalk. +Echo Run %L_YELLOW%start_finetune.bat%RESET% to start Finetuning. +Echo Run %L_YELLOW%start_environment.bat%RESET% to start the AllTalk Python environment. +Echo. +exit /b + +:End +echo Exiting AllTalk Setup Utility... +exit /b diff --git a/atsetup.sh b/atsetup.sh new file mode 100644 index 0000000..1167901 --- /dev/null +++ b/atsetup.sh @@ -0,0 +1,590 @@ +#!/bin/bash + +# ANSI color codes +RED='\033[1;31m' +GREEN='\033[1;32m' +YELLOW='\033[1;33m' +BLUE='\033[1;34m' +CYAN='\033[1;36m' +MAGENTA='\033[1;35m' +WHITE='\033[1;37m' +L_RED='\033[1;91m' +L_GREEN='\033[1;92m' +L_YELLOW='\033[1;93m' +L_BLUE='\033[1;94m' +L_CYAN='\033[1;96m' +L_MAGENTA='\033[1;95m' +NC='\033[0m' # No Color + +# Navigate to the script's directory +cd "$(dirname "$0")" + +# Function to check if curl is installed +check_curl() { + if ! command -v curl &> /dev/null; then + echo "curl is not available on this system. Please install curl then re-run the script https://curl.se/" + echo "or perform a manual installation of a Conda Python environment." + exit 1 + fi +} + +# Check if the current directory path contains a space +containsSpace=false +currentPath=$(pwd) +if echo "$currentPath" | grep -q ' '; then + containsSpace=true +fi + +if [ "$containsSpace" = true ]; then + echo + echo -e " ${L_BLUE}ALLTALK LINUX SETUP UTILITY${NC}" + echo + echo + echo -e " You are trying to install AllTalk in a folder that has a space in the" + echo -e " folder path e.g." + echo + echo -e " /home/${L_RED}program files${NC}/alltalk_tts" + echo + echo -e " This causes errors with Conda and Python scripts. Please follow this" + echo -e " link for reference:" + echo + echo -e " ${L_CYAN}https://docs.anaconda.com/free/working-with-conda/reference/faq/#installing-anaconda${NC}" + echo + echo -e " Please use a folder path that has no spaces in it e.g." + echo + echo -e " /home/myfiles/alltalk_tts/" + echo + echo + read -p "Press Enter to continue..." + exit 1 +else + # Continue with the main menu + echo "Continue with the main menu." +fi + +# Main Menu +main_menu() { + while true; do + clear + echo + echo -e " ${L_BLUE}ALLTALK LINUX SETUP UTILITY${NC}" + echo + echo " INSTALLATION TYPE" + echo -e " 1) I am using AllTalk as part of ${L_GREEN}Text-generation-webui${NC}" + echo -e " 2) I am using AllTalk as a ${L_GREEN}Standalone Application${NC}" + echo + echo -e " 9)${L_RED} Exit/Quit${NC}" + echo + read -p " Enter your choice: " user_option + + case $user_option in + 1) webui_menu ;; + 2) standalone_menu ;; + 9) exit 0 ;; + *) echo "Invalid option"; sleep 2 ;; + esac + done +} + +# Text-generation-webui Menu +webui_menu() { + while true; do + clear + echo + echo -e " ${L_BLUE}TEXT-GENERATION-WEBUI SETUP${NC}" + echo + echo -e " Please ensure you have started your Text-generation-webui Python" + echo -e " environment. If you have NOT done this, please run ${L_GREEN}/cmd_linux.sh${NC}" + echo -e " in the ${L_GREEN}text-generation-webui${NC} folder and then re-run this script." + echo + echo " BASE REQUIREMENTS" + echo -e " 1) Apply/Re-Apply the requirements for an ${L_GREEN}Text-generation-webui${NC}" + echo + echo " OPTIONAL" + echo " 2) Git Pull the latest AllTalk updates from Github" + echo + echo " DEEPSPEED" + echo " 4) Install DeepSpeed." + echo " 5) Uninstall DeepSpeed." + echo + echo " OTHER" + echo " 6) Generate a diagnostics file." + echo + echo -e " 9)${L_RED} Exit/Quit${NC}" + echo + read -p " Enter your choice: " webui_option + + case $webui_option in + 1) install_nvidia_textgen ;; + 2) tg_gitpull ;; + 4) install_deepspeed ;; + 5) uninstall_deepspeed ;; + 6) generate_diagnostics_textgen ;; + 9) exit 0 ;; + *) echo "Invalid option"; sleep 2 ;; + esac + done +} + +install_nvidia_textgen() { + local requirements_file="system/requirements/requirements_textgen.txt" + echo " Installing Requirements from $requirements_file..." + if ! pip install -r "$requirements_file"; then + echo + echo " There was an error pulling from Github." + echo " Please check the output for details." + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + echo + echo " Requirements installed successfully." + + echo " Installing additional requirements..." + if ! pip install -r system/requirements/requirements_textgen2.txt; then + echo + echo " There was an error installing additional requirements." + echo " Please check the output for details." + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + + if ! conda install -y pytorch::faiss-cpu; then + echo + echo " There was an error installing faiss-cpu." + echo " Please check the output for details." + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + + if ! conda install -y conda-forge::ffmpeg; then + echo + echo " There was an error installing ffmpeg." + echo " Please check the output for details." + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + + echo + echo " Additional requirements installed successfully." + echo + echo -e " To install ${L_YELLOW}DeepSpeed${NC} on Linux, there are additional" + echo -e " steps required. Please see the Github or documentation on DeepSeed." + echo + read -p " Press any key to continue. " -n 1 + echo +} + + +tg_gitpull() { + echo + if ! git pull; then + echo + echo " There was an error installing the requirements." + echo " Please check the output for details." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + echo + echo " AllTalk Updated from Github. Please re-apply" + echo " the latest requirements file. (Option 1)" + echo + read -p " Press any key to continue. " -n 1 + echo +} + +# Function to install DeepSpeed +install_deepspeed() { + clear + echo + echo -e " ${L_BLUE}DEEPSPEED INSTALLATION REQUIREMENTS${NC}" + echo + echo -e " - Please go see this link as the instructions are there.${NC}" + echo -e " The instructions there are making it as simple as posisble.${NC}" + echo -e " ${L_GREEN}https://github.com/erew123/alltalk_tts/releases/tag/DeepSpeed-14.2-Linux${NC}" + echo + read -p " Have you completed all the above steps? (y/n): " confirm + + if [ "$confirm" != "y" ]; then + echo -e " ${RED}DeepSpeed installation cannot proceed without completing the prerequisites.${NC}" + return + fi + + echo -e "\n ${GREEN}Proceeding with DeepSpeed installation...${NC}" + + if [ $? -ne 0 ]; then + echo -e " ${RED}There was an error installing DeepSpeed.${NC}" + return + fi + + echo -e " ${GREEN}DeepSpeed installed successfully.${NC}" + read -p " Press any key to continue. " -n 1 + echo +} + +uninstall_deepspeed() { + echo "Uninstalling DeepSpeed..." + pip uninstall -y deepspeed + if [ $? -ne 0 ]; then + echo + echo " There was an error uninstalling DeepSpeed." + echo + echo " Press any key to return to the menu." + read -n 1 + return + fi + echo + echo " DeepSpeed uninstalled successfully." + echo + echo " Press any key to continue." + read -n 1 +} + +generate_diagnostics_textgen() { + # Run diagnostics + if ! python diagnostics.py; then + echo + echo " There was an error running diagnostics." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + echo + echo " Diagnostics log file generated successfully." + echo " Please see diagnostics.log" + echo + read -p " Press any key to continue. " -n 1 + echo +} + +# Standalone Menu +standalone_menu() { + while true; do + clear + echo + echo -e " ${L_BLUE}ALLTALK STANDALONE APPLICATION SETUP${NC}" + echo + echo " BASE REQUIREMENTS" + echo " 1) Install AllTalk as a Standalone Application" + echo + echo " OPTIONAL" + echo " 2) Git Pull the latest AllTalk updates from Github" + echo " 3) Re-Apply/Update the requirements file" + echo " 4) Delete AllTalk's custom Python environment" + echo " 5) Purge the PIP cache" + echo + echo " OTHER" + echo " 8) Generate a diagnostics file" + echo + echo -e " 9)${L_RED} Exit/Quit${NC}" + echo + read -p " Enter your choice: " standalone_option + + case $standalone_option in + 1) install_custom_standalone ;; + 2) gitpull_standalone ;; + 3) reapply_standalone ;; + 4) delete_custom_standalone ;; + 5) pippurge_standalone ;; + 8) generate_diagnostics_standalone ;; + 9) exit 0 ;; + *) echo "Invalid option"; sleep 2 ;; + esac + done +} + + +install_custom_standalone() { + 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* | 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)/alltalk_environment" + CONDA_ROOT_PREFIX="${INSTALL_DIR}/conda" + INSTALL_ENV_DIR="${INSTALL_DIR}/env" + MINICONDA_DOWNLOAD_URL="https://repo.anaconda.com/miniconda/Miniconda3-py311_24.4.0-0-Linux-${OS_ARCH}.sh" + if [ ! -x "${CONDA_ROOT_PREFIX}/bin/conda" ]; then + echo "Downloading Miniconda from $MINICONDA_DOWNLOAD_URL to ${INSTALL_DIR}/miniconda_installer.sh" + mkdir -p "${INSTALL_DIR}" + curl -L "${MINICONDA_DOWNLOAD_URL}" -o "${INSTALL_DIR}/miniconda_installer.sh" + chmod +x "${INSTALL_DIR}/miniconda_installer.sh" + bash "${INSTALL_DIR}/miniconda_installer.sh" -b -p "${CONDA_ROOT_PREFIX}" + echo "Miniconda installed." + fi + + if [ ! -d "${INSTALL_ENV_DIR}" ]; then + "${CONDA_ROOT_PREFIX}/bin/conda" create -y --prefix "${INSTALL_ENV_DIR}" -c conda-forge python=3.11.9 + echo "Conda environment created at ${INSTALL_ENV_DIR}." + fi + + # Activate the environment and install requirements + source "${CONDA_ROOT_PREFIX}/etc/profile.d/conda.sh" + conda activate "${INSTALL_ENV_DIR}" + # pip install torch==2.2.1+cu121 torchaudio==2.2.1+cu121 --upgrade --force-reinstall --extra-index-url https://download.pytorch.org/whl/cu121 --no-cache-dir + conda install -y pytorch=2.2.1 torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia + conda install -y nvidia/label/cuda-12.1.0::cuda-toolkit=12.1 + conda install -y pytorch::faiss-cpu + conda install -y conda-forge::ffmpeg + echo + echo + echo + echo " Fix Nvidia's broken symlinks in the /env/lib folder" + echo + # Define the environment path + env_path="${INSTALL_ENV_DIR}/lib" + + # List of broken symlinks and their correct targets + declare -A symlinks=( + ["libcufile.so"]="libcufile.so.1.10.0" + ["libcufile_rdma.so"]="libcufile_rdma.so.1.10.0" + ["libcudart.so"]="libcudart.so.12" + ["libnvjpeg.so"]="libnvjpeg.so.12.1.1.14" + ["libcurand.so"]="libcurand.so.10.3.6.39" + ["libnvJitLink.so"]="libnvJitLink.so.12.1.105" + ["libnvrtc-builtins.so"]="libnvrtc-builtins.so.12.1.105" + ["libnvrtc.so"]="libnvrtc.so.12.1.105" + ) + + # Function to fix broken symlinks + fix_broken_symlinks() { + echo "Fixing broken symlinks..." + for link in "${!symlinks[@]}"; do + target="${symlinks[$link]}" + if [ -L "$env_path/$link" ] && [ ! -e "$env_path/$link" ]; then + echo "Removing broken link: $env_path/$link" + rm "$env_path/$link" + fi + + if [ -e "$env_path/$target" ]; then + echo "Creating new symlink: $env_path/$link -> $env_path/$target" + ln -s "$env_path/$target" "$env_path/$link" + else + echo "Target file does not exist: $env_path/$target" + fi + done + echo "Verification of symbolic links:" + ls -l $env_path | grep -E "libcufile.so|libcudart.so|libcufile_rdma.so|libnvjpeg.so|libcurand.so|libnvJitLink.so|libnvrtc-builtins.so|libnvrtc.so" + } + + # Call the function to fix broken symlinks + fix_broken_symlinks + echo " Installing additional requirements." + echo + pip install -r system/requirements/requirements_standalone.txt + curl -LO https://github.com/erew123/alltalk_tts/releases/download/DeepSpeed-14.0/deepspeed-0.14.2+cu121torch2.2-cp311-cp311-manylinux_2_24_x86_64.whl + pip install --upgrade gradio==4.32.2 + echo Installing DeepSpeed... + pip install deepspeed-0.14.2+cu121torch2.2-cp311-cp311-manylinux_2_24_x86_64.whl + rm deepspeed-0.14.2+cu121torch2.2-cp311-cp311-manylinux_2_24_x86_64.whl + conda clean --all --force-pkgs-dirs -y + Create start_environment.sh to run AllTalk + cat << EOF > start_environment.sh +#!/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)/alltalk_environment/conda" +INSTALL_ENV_DIR="$(pwd)/alltalk_environment/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\"") +EOF + cat << EOF > start_alltalk.sh +#!/bin/bash +source "${CONDA_ROOT_PREFIX}/etc/profile.d/conda.sh" +conda activate "${INSTALL_ENV_DIR}" +python script.py +EOF + cat << EOF > start_finetune.sh +#!/bin/bash +export TRAINER_TELEMETRY=0 +source "${CONDA_ROOT_PREFIX}/etc/profile.d/conda.sh" +conda activate "${INSTALL_ENV_DIR}" +python finetune.py +EOF + cat << EOF > start_diagnostics.sh +#!/bin/bash +source "${CONDA_ROOT_PREFIX}/etc/profile.d/conda.sh" +conda activate "${INSTALL_ENV_DIR}" +python diagnostics.py +EOF + chmod +x start_alltalk.sh + chmod +x start_environment.sh + chmod +x start_finetune.sh + chmod +x start_diagnostics.sh + echo + echo + echo -e " Run ${L_YELLOW}./start_alltalk.sh${NC} to start AllTalk." + echo -e " Run ${L_YELLOW}./start_diagnostics.sh${NC} to start Diagnostics." + echo -e " Run ${L_YELLOW}./start_finetune.sh${NC} to start Finetuning." + echo -e " Run ${L_YELLOW}./start_environment.sh${NC} to start the AllTalk Python environment." + echo + echo -e " To install ${L_YELLOW}DeepSpeed${NC} on Linux, there are additional" + echo -e " steps required. Please see the Github or documentation on DeepSeed." + echo + read -p " Press any key to continue. " -n 1 +} + +delete_custom_standalone() { + local env_dir="$PWD/alltalk_environment" + # Check if the alltalk_environment directory exists + if [ ! -d "$env_dir" ]; then + echo " \"$env_dir\" directory does not exist. No need to delete." + read -p " Press any key to continue. " -n 1 + echo + return + fi + # Check if a Conda environment is active and deactivate it + if [ -n "$CONDA_PREFIX" ]; then + echo " Exiting the Conda environment. You may need to start ./atstart.sh again" + conda deactivate + fi + echo "Deleting \"$env_dir\". Please wait." + rm -rf "$env_dir" + if [ -d "$env_dir" ]; then + echo " Failed to delete \"$env_dir\" folder." + echo " Please make sure it is not in use and try again." + else + echo " Environment \"$env_dir\" deleted successfully." + fi + read -p " Press any key to continue. " -n 1 + echo +} + +generate_diagnostics_standalone() { + local env_dir="$PWD/alltalk_environment" + local conda_root_prefix="${env_dir}/conda" + local install_env_dir="${env_dir}/env" + if [ ! -d "${install_env_dir}" ]; then + echo + echo " The Conda environment at '${install_env_dir}' does not exist." + echo " Please install the environment before proceeding." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + source "${conda_root_prefix}/etc/profile.d/conda.sh" + conda activate "${install_env_dir}" + if ! python diagnostics.py; then + echo + echo " There was an error running diagnostics." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + echo + echo " Diagnostics completed successfully." + read -p " Press any key to continue. " -n 1 + echo +} + +gitpull_standalone() { + local env_dir="$PWD/alltalk_environment" + local conda_root_prefix="${env_dir}/conda" + local install_env_dir="${env_dir}/env" + if [ ! -d "${install_env_dir}" ]; then + echo + echo " The Conda environment at '${install_env_dir}' does not exist." + echo " Please install the environment before proceeding." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + source "${conda_root_prefix}/etc/profile.d/conda.sh" + conda activate "${install_env_dir}" + if ! git pull; then + echo + echo " There was an error pulling from Github." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + echo + echo " AllTalk Updated from Github. Please re-apply." + echo " the latest requirements file. (Option 3)" + echo + read -p " Press any key to continue. " -n 1 + echo +} + +pippurge_standalone() { + if ! pip cache purge; then + echo + echo " There was an error." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + echo + echo " The PIP cache has been purged." + echo + read -p " Press any key to continue. " -n 1 + echo +} + +reapply_standalone() { + local env_dir="$PWD/alltalk_environment" + local conda_root_prefix="${env_dir}/conda" + local install_env_dir="${env_dir}/env" + if [ ! -d "${install_env_dir}" ]; then + echo + echo " The Conda environment at '${install_env_dir}' does not exist." + echo " Please install the environment before proceeding." + echo + read -p " Press any key to return to the menu. " -n 1 + echo + return + fi + source "${conda_root_prefix}/etc/profile.d/conda.sh" + conda activate "${install_env_dir}" + echo + echo " Downloading and installing PyTorch. This step can take a long time" + echo " depending on your internet connection and hard drive speed. Please" + echo " be patient." + pip install torch>=2.2.1+cu121 torchaudio>=2.2.1+cu121 --upgrade --force-reinstall --extra-index-url https://download.pytorch.org/whl/cu121 + echo + echo " Installing additional requirements." + echo + pip install -r system/requirements/requirements_standalone.txt + echo + echo " Requirements have been re-applied/updated." + echo + read -p " Press any key to continue. " -n 1 + echo +} + + +# Start the main menu +main_menu diff --git a/confignew.json b/confignew.json new file mode 100644 index 0000000..2ca2d86 --- /dev/null +++ b/confignew.json @@ -0,0 +1,73 @@ +{ + "branding": "AllTalk ", + "delete_output_wavs": "Disabled", + "gradio_interface": true, + "output_folder": "outputs", + "gradio_port_number": 7852, + "firstrun_model": true, + "firstrun_splash": true, + "launch_gradio": true, + "transcode_audio_format": "Disabled", + "theme": { + "file": null, + "class": "gradio/base" + }, + "rvc_settings": { + "rvc_enabled": false, + "rvc_char_model_file": "Disabled", + "rvc_narr_model_file": "Disabled", + "split_audio": true, + "autotune": false, + "pitch": 0, + "filter_radius": 3, + "index_rate": 0.5, + "rms_mix_rate": 1, + "protect": 0.5, + "hop_length": 79, + "f0method": "fcpe", + "embedder_model": "hubert", + "training_data_size": 45000 + }, + "tgwui": { + "tgwui_activate_tts": true, + "tgwui_autoplay_tts": true, + "tgwui_narrator_enabled": "false", + "tgwui_non_quoted_text_is": "true", + "tgwui_deepspeed_enabled": false, + "tgwui_language": "English", + "tgwui_lowvram_enabled": false, + "tgwui_pitch_set": 0, + "tgwui_temperature_set": 0.75, + "tgwui_repetitionpenalty_set": 10, + "tgwui_generationspeed_set": 1, + "tgwui_narrator_voice": "female_01.wav", + "tgwui_show_text": true, + "tgwui_character_voice": "female_01.wav", + "tgwui_rvc_char_voice": "Disabled", + "tgwui_rvc_narr_voice": "Disabled" + }, + "api_def": { + "api_port_number": 7851, + "api_allowed_filter": "[^a-zA-Z0-9\\s.,;:!?\\-\\'\"$\\u0400-\\u04FF\\u00C0-\\u00FF\\u0150\\u0151\\u0170\\u0171\\u0900-\\u097F\\u2018\\u2019\\u201C\\u201D\\u3001\\u3002\\u3040-\\u309F\\u30A0-\\u30FF\\u4E00-\\u9FFF\\u3400-\\u4DBF\\uF900-\\uFAFF\\u0600-\\u06FF\\u0750-\\u077F\\uFB50-\\uFDFF\\uFE70-\\uFEFF\\uAC00-\\uD7A3\\u1100-\\u11FF\\u3130-\\u318F\\uFF01\\uFF0c\\uFF1A\\uFF1B\\uFF1F]", + "api_length_stripping": 3, + "api_max_characters": 2000, + "api_use_legacy_api": false, + "api_legacy_ip_address": "127.0.0.1", + "api_text_filtering": "standard", + "api_narrator_enabled": "false", + "api_text_not_inside": "character", + "api_language": "en", + "api_output_file_name": "myoutputfile", + "api_output_file_timestamp": true, + "api_autoplay": false, + "api_autoplay_volume": 0.5 + }, + "debugging": { + "debug_transcode": false, + "debug_tts": false, + "debug_openai": false, + "debug_concat": false, + "debug_tts_variables": false, + "debug_rvc": false + } +} \ No newline at end of file diff --git a/diagnostics.py b/diagnostics.py new file mode 100644 index 0000000..2ad8391 --- /dev/null +++ b/diagnostics.py @@ -0,0 +1,335 @@ +import platform +import subprocess +import logging +from importlib_metadata import distributions +import torch +import os +import re +import sys +import glob +import site +import textwrap +import packaging.version +import packaging.specifiers +from packaging.specifiers import SpecifierSet +from packaging.version import parse as parse_version +from pathlib import Path + +try: + import psutil +except ImportError: + print("psutil not found. Installing...") + subprocess.run(['pip', 'install', 'psutil']) + import psutil + +def get_requirements_file(): + this_dir = Path(__file__).parent + requirements_dir = this_dir / 'system' / 'requirements' + requirements_files = list(requirements_dir.glob('requirements*.txt')) + + if not requirements_files: + print("\033[91mNo requirements files found.\033[0m") + return None + + print("\033[94m\n Select a requirements file to check against.") + print("\033[94m Or press Enter for default 'requirements_standalone.txt'):\033[0m\n") + for i, file_path in enumerate(requirements_files, start=1): + print(f" {i}. {file_path.name}") + + choice = input("\n Enter the number of your choice: ") + try: + choice = int(choice) - 1 + return str(requirements_files[choice]) + except (ValueError, IndexError): + return str(requirements_dir / "requirements_standalone.txt") + +logging.basicConfig(filename='diagnostics.log', + filemode='w', + level=logging.INFO, + format='%(message)s') + +def get_gpu_info(): + try: + result = subprocess.run(['nvidia-smi'], stdout=subprocess.PIPE, text=True) + return result.stdout + except FileNotFoundError: + return "NVIDIA GPU information not available" + +def get_cpu_info(): + cpu_info = { + 'physical_cores': psutil.cpu_count(logical=False), + 'total_cores': psutil.cpu_count(logical=True), + 'max_frequency': psutil.cpu_freq().max + } + return cpu_info + +def get_disk_info(): + disk_info = [] + partitions = psutil.disk_partitions() + for p in partitions: + usage = psutil.disk_usage(p.mountpoint) + disk_info.append(f" Drive: {p.device} | Total: {usage.total / (1024 ** 3):.2f} GB | Used: {usage.used / (1024 ** 3):.2f} GB | Free: {usage.free / (1024 ** 3):.2f} GB | Type: {p.fstype}") + return disk_info + +def is_port_in_use(port): + for conn in psutil.net_connections(): + if conn.laddr.port == port: + return True + return False + +def satisfies_wildcard(installed_version, required_version): + if '*' in required_version: + required_parts = required_version.split('.') + installed_parts = installed_version.split('.') + for req, inst in zip(required_parts, installed_parts): + if req != '*' and req != inst: + return False + return True + return False + +def test_cuda(): + cuda_available = torch.cuda.is_available() + if cuda_available: + try: + torch.tensor([1.0, 2.0]).cuda() + return "Success - CUDA is available and working." + except Exception as e: + return f"Fail - CUDA is available but not working. Error: {e}" + else: + return "Fail - CUDA is not available." + +def get_cuda_details(): + if torch.cuda.is_available(): + device_id = torch.cuda.current_device() + device_name = torch.cuda.get_device_name(device_id) + device_memory = torch.cuda.get_device_properties(device_id).total_memory / (1024 ** 3) + cuda_version = torch.version.cuda + return device_name, device_memory, cuda_version + else: + return None, None, None + +def find_files_in_path_with_wildcard(pattern): + site_packages_path = site.getsitepackages() + found_paths = [] + sub_directory = "nvidia/cublas" + if platform.system() == "Linux": + sub_directory = os.path.join(sub_directory, "lib") + else: + sub_directory = os.path.join(sub_directory, "bin") + for directory in site_packages_path: + search_directory = os.path.join(directory, sub_directory) + for file_path in glob.glob(os.path.join(search_directory, pattern)): + if os.path.isfile(file_path): + found_paths.append(file_path) + return found_paths + +def log_system_info(): + os_version = platform.system() + " " + platform.version() + cuda_home = os.environ.get('CUDA_HOME', 'N/A') + gpu_info = get_gpu_info() + python_version = platform.python_version() + torch_version = torch.__version__ + cuda_test_result = test_cuda() + cuda_device_name, cuda_device_memory, cuda_version = get_cuda_details() + cpu_info = get_cpu_info() + disk_info = get_disk_info() + + try: + virtual_memory = psutil.virtual_memory() + total_ram = f"{virtual_memory.total / (1024 ** 3):.2f} GB" + available_ram = f"{virtual_memory.available / (1024 ** 3):.2f} GB" + system_ram = f"{available_ram} available out of {total_ram} total" + except NameError: + print("psutil is not installed. Unable to check system RAM.") + system_ram = "N/A" + + port_status = "N/A" + if 'psutil' in globals(): + port_to_check = 7851 + if is_port_in_use(port_to_check): + port_status = f"Port {port_to_check} is in use." + else: + port_status = f"Port {port_to_check} is available." + + package_versions = {d.metadata['Name']: d.version for d in distributions()} + python_executable = sys.executable + python_version_info = sys.version_info + python_virtual_env = os.environ.get('VIRTUAL_ENV', 'N/A') + conda_env = os.environ.get('CONDA_DEFAULT_ENV', 'N/A') + search_path = sys.path + path_env = os.environ.get('PATH', 'N/A') + file_name = 'cublas64_11.*' if platform.system() == "Windows" else 'libcublas.so.11*' + found_paths = find_files_in_path_with_wildcard(file_name) + requirements_file = get_requirements_file() + + if requirements_file: + required_packages = {} + installed_packages = {} + try: + with open(requirements_file, 'r') as req_file: + requirements = [line.strip() for line in req_file] + for req in requirements: + if ';' in req: + continue # Skip OS-specific requirements + match = re.match(r'([^\s>=]+)\s*([>=<]+)\s*([^,]+)', req) + if match: + package_name, operator, version_spec = match.groups() + installed_version = package_versions.get(package_name, 'Not installed') + if installed_version != 'Not installed': + required_packages[package_name] = (operator, version_spec) + installed_packages[package_name] = installed_version + except FileNotFoundError: + print(f"\n{requirements_file} not found. Skipping version checks.") + logging.info(f"NOTE {requirements_file} not found. Skipping version checks.") + + logging.info(f"OPERATING SYSTEM:") + logging.info(f" OS Version: {os_version}") + logging.info(f" Note: Windows 11 will list as build is 10.x.22xxx") + logging.info(f"\nHARDWARE ENVIRONMENT:") + logging.info(f" CPU: Physical Cores: {cpu_info['physical_cores']}, Total Cores: {cpu_info['total_cores']}, Max Frequency: {cpu_info['max_frequency']} MHz") + logging.info(f" System RAM: {system_ram}") + logging.info(f"\nGPU INFORMATION:") + logging.info(f" {gpu_info}") + logging.info(f" CUDA Device : {cuda_device_name}") + logging.info(f" CUDA Memory : {cuda_device_memory:.2f} GB") + logging.info(f" CUDA Version: {cuda_version}") + logging.info("DISK INFORMATION:") + for disk in disk_info: + logging.info(disk) + logging.info("\nNETWORK PORT:") + logging.info(f" Port Status : {port_status}") + logging.info("\nCUDA :") + logging.info(f" CUDA Working: {cuda_test_result}") + logging.info(f" CUDA_HOME : {cuda_home}") + if found_paths: + logging.info(f" Cublas64_11 Path: {', '.join(found_paths)}") + else: + logging.info(f" Cublas64_11 Path: Not found in any search path directories.") + logging.info("\nPYTHON & PYTORCH:") + logging.info(f" Torch Version: {torch_version}") + logging.info(f" Python Version: {python_version}") + logging.info(f" Python Version Info: {python_version_info}") + logging.info(f" Python Executable: {python_executable}") + logging.info(f" Python Virtual Environment: {python_virtual_env} (Should be N/A when in Text-generation-webui Conda Python environment)") + logging.info(f" Conda Environment: {conda_env}") + logging.info("\nPython Search Path:") + for path in search_path: + logging.info(f" {path}") + logging.info("\nOS SEARCHPATH ENVIRONMENT:") + for path in path_env.split(';'): + logging.info(f" {path}") + + if required_packages: + logging.info("\nPACKAGE VERSIONS vs REQUIREMENTS FILE:") + max_package_length = max(len(package) for package in required_packages.keys()) + for package_name, (operator, required_version) in required_packages.items(): + installed_version = installed_packages.get(package_name, 'Not installed') + logging.info(f" {package_name.ljust(max_package_length)} Required: {operator} {required_version.ljust(12)} Installed: {installed_version}") + + logging.info("\nPYTHON PACKAGES:") + for package, version in package_versions.items(): + logging.info(f" {package}>= {version}") + + deepspeed_requirements = ( + f"\nDeepSpeed Installation Requirements:\n" + f" OS : {platform.system()}\n" + f" Python : {python_version}\n" + f" PyTorch: {torch_version}\n" + f" CUDA : {'N/A' if not cuda_version else cuda_version}\n" + f"\nTo install DeepSpeed, run the following command based on your environment:" + f"\n pip install deepspeed== --no-cache-dir" + f"\nEnsure to choose the correct DeepSpeed version matching your environment." + ) + + logging.info(deepspeed_requirements) + + print(f"\n\033[94mOS Version:\033[0m \033[92m{os_version}\033[0m") + print(f"\033[94mOS Ver note:\033[0m \033[92m(Windows 11 will say build is 10.x.22xxx)\033[0m") + print(f"\033[94mSystem RAM:\033[0m \033[92m{system_ram}\033[0m") + for disk in disk_info: + print(f"\033[94mDisk:\033[0m \033[92m{disk}\033[0m") + print(f"\033[94m\nGPU Information:\033[0m {gpu_info}") + print(f"\033[94mPort Status :\033[0m \033[92m{port_status}\033[0m") + print(f"\033[0m\n If this network port is unavailable because something else is using it, your") + print(f"\033[0m firewall or antivirus is blocking it, AllTalk will fail to start.") + print(f"\n\033[94mCUDA Device :\033[0m \033[92m{cuda_device_name}\033[0m") + print(f"\033[94mCUDA Memory :\033[0m \033[92m{cuda_device_memory:.2f} GB\033[0m") + print(f"\033[94mCUDA Version:\033[0m \033[92m{cuda_version}\033[0m") + if "Fail" in cuda_test_result: + print(f"\033[91mCUDA Working:\033[0m \033[91m{cuda_test_result}\033[0m") + else: + print(f"\033[94mCUDA Working:\033[0m \033[92m{cuda_test_result}\033[0m") + print(f"\033[94mCUDA_HOME :\033[0m \033[92m{cuda_home}\033[0m") + if found_paths: + print(f"\033[94mCublas64_11 :\033[0m \033[92m{', '.join(found_paths)}\033[0m") + else: + print(f"\033[94mCublas64_11 :\033[0m \033[91mNot found in any search path directories.\033[0m") + print(f"\033[0m\n If you do not have a CUDA version and CUDA is failing, you will not have your") + print(f"\033[0m TTS engines being accelerated with CUDA. CUDA is only available on Nvidia GPU") + print(f"\033[0m and is setup by installing PyTorch with a correct CUDA version in your Python") + print(f"\033[0m virtual environment.") + print(f"\033[94m\nPyTorch Version :\033[0m \033[92m{torch_version}\033[0m") + print(f"\033[94mPython Version :\033[0m \033[92m{platform.python_version()}\033[0m") + print(f"\033[94mPython Executable:\033[0m \033[92m{python_executable}\033[0m") + print(f"\033[0m\n AllTalk has been validated to run on Python 3.11.x versions and also PyTorch") + print(f"\033[0m 2.0.x to 2.2.x. Earlier or later versions of PyTorch and Python may not work.") + print(f"\033[94m\nConda Environment:\033[0m \033[92m{conda_env}\033[0m") + print(f"\n\033[94mPython Search Path:\033[0m") + for path in search_path: + print(f" {path}") + print(f"\033[0m\n If you are correctly in the AllTalk Python virtual environment, you will") + print(f"\033[0m expect to see 'alltalk_environment' as part of the path of the above folders.") + print(f"\033[0m If you are running AllTalk as part of Text-generation-webui, you should see ") + print(f"\033[0m 'text-generation-webui' listed in the path of the above folders. If you dont") + print(f"\033[0m see them mentioned, you have probably not started the correct Python virtual") + print(f"\033[0m environment.") + if required_packages: + print("\033[94m\nRequirements file package comparison:\033[0m") + max_package_length = max(len(package) for package in required_packages.keys()) + + for package_name, (operator, required_version) in required_packages.items(): + installed_version = installed_packages.get(package_name, 'Not installed') + required_version_no_build = required_version.split("+")[0] + + if '*' in required_version: + condition_met = satisfies_wildcard(installed_version, required_version) + else: + required_specifier = SpecifierSet(f"{operator}{required_version_no_build}") + installed_version = parse_version(installed_version) + condition_met = installed_version in required_specifier + + color_required = "\033[92m" if condition_met else "\033[91m" + color_installed = "\033[92m" if condition_met else "\033[91m" + + print(f" {package_name.ljust(max_package_length)} Required: {color_required}{operator} {required_version.ljust(12)}\033[0m Installed: {color_installed}{installed_version}\033[0m") + + print("\033[94m\nRequirements file specifier meanings:\033[0m") + explanation = textwrap.dedent(""" + == Exact version != Any version except < Less than + <= Less than or equal to > Greater than >= Greater/equal to + ~ Compatible release ; Environment marker AND Logical AND + OR Logical OR + """) + print(explanation.strip()) + + print(f"\033[94m\nDeepSpeed Installation Requirements:\033[0m") + print("\n DeepSpeed is HIGHLY specific. It MUST be compiled for the correct major") + print(" revision of Python (e.g. 3.11.x), PyTorch (e.g. 2.2.x), and the CUDA version") + print(" that your PyTorch is using (e.g. 11.8 or 12.1). If any of these three") + print(" components change version number (e.g. you update to PyTorch 2.3.x or Python") + print(" 3.12.x), you will need to uninstall DeepSpeed and install a build that matches") + print(" your new Python environment.") + print("\n Here are the details of your current Python environment:") + print(f" OS: \033[92m{platform.system()}\033[0m Python: \033[92m{python_version}\033[0m PyTorch: \033[92m{torch_version}\033[0m CUDA: \033[92m{'N/A' if not cuda_version else cuda_version}\033[0m") + print("\n Therefore, you would need a DeepSpeed build for the above configuration. If") + print(" you change ANY of the versions, remember to reinstall DeepSpeed to ensure") + print(" compatibility with your updated environment. If the copy of DeepSpeed you have") + print(" is NOT built to match the above, it will error/fail/crash etc.\n\n") + print(f"\033[94mA diagnostic log file called \033[92mdiagnostics.log\033[94m has been created in the AllTalk") + print(f"\033[94mfolder. Up above, on SCREEN you can read a few sections to help you diagnose\033[0m") + print(f"\033[94many possible problems with your configuration or setup. So please take the\033[0m") + print(f"\033[94mtime to check them. \033[0m") + print(f"\033[94m\nIf you are going to ask for support, please upload the \033[92mdiagnostics.log\033[94m file.\033[0m") + +if __name__ == "__main__": + log_system_info() diff --git a/finetune.py b/finetune.py new file mode 100644 index 0000000..4d0561f --- /dev/null +++ b/finetune.py @@ -0,0 +1,1797 @@ +import argparse +import os +import sys +import platform +import site +import tempfile +import signal +import gradio as gr +import torch +import torchaudio +import traceback +from TTS.tts.configs.xtts_config import XttsConfig +from TTS.tts.models.xtts import Xtts +import random +import gc +import time +import shutil +import psutil +import pandas as pd +import glob +import json +from pathlib import Path +from tqdm import tqdm +from faster_whisper import WhisperModel +# Use a local Tokenizer to resolve Japanese support +# from TTS.tts.layers.xtts.tokenizer import multilingual_cleaners +from system.ft_tokenizer.tokenizer import multilingual_cleaners +import importlib.metadata as metadata +from packaging import version + +# STARTUP VARIABLES +this_dir = Path(__file__).parent.resolve() +audio_folder = this_dir / "finetune" / "put-voice-samples-in-here" +out_path = this_dir / "finetune" / "tmp-trn" +progress = 0 +theme = gr.themes.Default() +refresh_symbol = '🔄' +os.environ['TRAINER_TELEMETRY'] = '0' +pfc_status = "pass" +base_path = this_dir / "models" / "xtts" + +# Set the Gradio temporary directory +gradio_temp_dir = this_dir / "finetune" / "gradio_temp" +os.environ["GRADIO_TEMP_DIR"] = str(gradio_temp_dir) + +######################## +#### Find All Model #### +######################## +base_model_detected = False +def scan_models_folder(): + global base_model_detected + models_folder = base_path + available_models = {} + required_files = ["config.json", "model.pth", "mel_stats.pth", "speakers_xtts.pth", "vocab.json", "dvae.pth"] + try: + for subfolder in models_folder.iterdir(): + if subfolder.is_dir(): + model_name = subfolder.name + if all(subfolder.joinpath(file).exists() for file in required_files): + available_models[model_name] = True + base_model_detected = True + else: + print(f"[FINETUNE] \033[91mWarning\033[0m: Model folder '{model_name}' is missing required files") + if not available_models: + available_models["No Model Available"] = False + base_model_detected = False + except FileNotFoundError: + print("\n[FINETUNE] \033[91mError\033[0m: No XTTS models folder found. You have not yet downloaded any models or no XTTS") + print("[FINETUNE] \033[91mError\033[0m: models can be found. Please run AllTalk and download an XTTS model that can be") + print("[FINETUNE] \033[91mError\033[0m: used for training. Or place a full model in the following location") + print("[FINETUNE] \033[91mError\033[0m: \033[93m\\models\\xtts\\{modelfolderhere}\033[0m") + sys.exit(1) # Exit the script with an error status + + return available_models + +# Get available models +available_models = scan_models_folder() + +####################### +#### DIAGS for PFC #### +####################### + +def check_disk_space(): + global pfc_status + # Get the current working directory + current_directory = os.getcwd() + # Get the disk usage statistics for the current directory's disk + disk_usage = shutil.disk_usage(current_directory) + # Convert the free space to GB (1GB = 1 << 30 bytes) + free_space_gb = disk_usage.free / (1 << 30) + # Check if the free space is more than 18GB + is_more_than_18gb = free_space_gb > 18 + disk_space_icon = "✅" + if not is_more_than_18gb: + disk_space_icon ="❌" + pfc_status = "fail" # Update global status if disk space check fails + # Generating the markdown text for disk space check + disk_space_markdown = f""" + ### 🟩 Disk Space Check +      {disk_space_icon} **Disk Space (> 18 GB):** {'' if is_more_than_18gb else 'You have less than 18GB on this disk '} {free_space_gb:.2f} GB + """ + return disk_space_markdown + +def test_cuda(): + global pfc_status + cuda_home = os.environ.get('CUDA_HOME', 'N/A') + cuda_available = torch.cuda.is_available() + if cuda_available: + try: + # Attempt to create a tensor on GPU + torch.tensor([1.0, 2.0]).cuda() + cuda_status = "CUDA is available and working." + cuda_icon = "✅" + except Exception as e: + cuda_status = f"CUDA is available but not working. Error: {e}" + cuda_icon = "❌" + pfc_status = "fail" # Update global status + else: + cuda_status = "CUDA is not available." + pfc_status = "fail" # Update global status + return cuda_status, cuda_icon, cuda_home + +def find_files_in_path_with_wildcard(pattern): + # Get the site-packages directory of the current Python environment + site_packages_path = site.getsitepackages() + found_paths = [] + # Adjust the sub-directory based on the operating system + sub_directory = "nvidia/cublas" + if platform.system() == "Linux": + sub_directory = os.path.join(sub_directory, "lib") + else: + sub_directory = os.path.join(sub_directory, "bin") + # Iterate over each site-packages directory (there can be more than one) + for directory in site_packages_path: + # Construct the search directory path + search_directory = os.path.join(directory, sub_directory) + # Use glob to find all files matching the pattern in this directory + for file_path in glob.glob(os.path.join(search_directory, pattern)): + if os.path.isfile(file_path): # Ensure it's a file + found_paths.append(file_path) + return found_paths + + +def test_cuda(): + if torch.cuda.is_available(): + pytorch_cuda_version = torch.version.cuda + return f'CUDA {pytorch_cuda_version} is available in this Python environment', '✅', torch.cuda.get_device_name(0) + else: + return 'CUDA is not available in this Python environment', '❌', 'None' + +def generate_cuda_markdown(): + global pfc_status + pfc_status = "pass" # Initialize global status as pass + + # Check CUDA availability and get CUDA home path + cuda_status, cuda_icon, cuda_home = test_cuda() + + # Check for specific CUDA library files + file_name = 'cublas64_11.*' if platform.system() == "Windows" else 'libcublas.so.11*' + found_paths = find_files_in_path_with_wildcard(file_name) + if found_paths: + found_paths_str = ' '.join(str(path) for path in found_paths) + found_path_icon = '✅' + else: + found_paths_str = "cublas64_11 is not accessible." + found_path_icon = '❌' + pfc_status = "fail" # Update global status + + # Check PyTorch version and CUDA version + pytorch_version = torch.__version__ + pytorch_cuda_version = torch.version.cuda + + if pytorch_cuda_version in ['11.8', '12.1']: + pytorch_cuda_version_status = '' + pytorch_icon = '✅' + else: + pytorch_cuda_version_status = 'Pytorch CUDA version problem ' + pytorch_icon = '❌' + pfc_status = "fail" # Update global status + + cuda_markdown = f""" + ### 🟨 CUDA Information
+      {found_path_icon} **Cublas64_11 found:** {found_paths_str} +      {pytorch_icon} **CUDA_HOME path:** {cuda_home} **(Will list your GPU if no specifc path set, which is ok)** + """ + pytorch_markdown = f""" + ### 🟦 Python & Pytorch Information +      {pytorch_icon} **PyTorch Version:** {pytorch_cuda_version_status} {torch.__version__} +      {cuda_icon} **CUDA is working:** {cuda_status} + """ + return cuda_markdown, pytorch_markdown + +def get_system_ram_markdown(): + global pfc_status + virtual_memory = psutil.virtual_memory() + total_ram_gb = virtual_memory.total / (1024 ** 3) + available_ram_gb = virtual_memory.available / (1024 ** 3) + used_ram_percentage = virtual_memory.percent + + # Check if the available RAM is less than 8GB + warning_if_low_ram = available_ram_gb < 8 + + # Decide the message based on the available RAM + ram_status_message = "Warning" if warning_if_low_ram else "" + ram_status_icon = "⚠️" if warning_if_low_ram else "✅" + + if torch.cuda.is_available(): + gpu_device_id = torch.cuda.current_device() + gpu_device_name = torch.cuda.get_device_name(gpu_device_id) + # Get the total and available memory in bytes, then convert to GB + gpu_total_mem_gb = torch.cuda.get_device_properties(gpu_device_id).total_memory / (1024 ** 3) + # gpu_available_mem_gb = (torch.cuda.get_device_properties(gpu_device_id).total_memory - torch.cuda.memory_allocated(gpu_device_id)) / (1024 ** 3) + # gpu_available_mem_gb = (torch.cuda.get_device_properties(gpu_device_id).total_memory - torch.cuda.memory_reserved(gpu_device_id)) / (1024 ** 3) + gpu_reserved_mem_gb = torch.cuda.memory_reserved(gpu_device_id) / (1024 ** 3) + gpu_available_mem_gb = gpu_total_mem_gb - gpu_reserved_mem_gb + # Check if total or available memory is less than 11 GB and set icons + gpu_total_status_icon = "⚠️" if gpu_total_mem_gb < 12 else "✅" + gpu_available_status_icon = "⚠️" if gpu_available_mem_gb < 12 else "✅" + gpu_status_icon = "✅" + else: + gpu_status_icon = "⚠️" + gpu_device_name = "Cannot detect a CUDA card" + gpu_total_mem_gb = "Cannot detect a CUDA card" + gpu_available_mem_gb = "Cannot detect a CUDA card" + gpu_total_status_icon = gpu_status_icon + gpu_available_status_icon = gpu_status_icon + + system_ram_markdown = f""" + ### 🟪 System RAM and VRAM Information
+      {ram_status_icon} **Total RAM:** {total_ram_gb:.2f} GB
+      {ram_status_icon} **Available RAM:** {ram_status_message + ' - Available RAM is less than 8 GB. You have ' if warning_if_low_ram else ''} {available_ram_gb:.2f} GB available ({used_ram_percentage:.2f}% used)

+      {gpu_status_icon} **GPU Name:** {gpu_device_name}
+      {gpu_total_status_icon} **GPU Total RAM:** {gpu_total_mem_gb:.2f} GB
+      {gpu_available_status_icon} **GPU Available RAM:** {gpu_available_mem_gb:.2f} GB
+ """ + return system_ram_markdown + +def generate_base_model_markdown(base_model_detected): + global pfc_status + base_model_status = 'Base model detected' if base_model_detected else 'Base model not detected' + base_model_icon = '✅' if base_model_detected else '❌' + base_model_markdown = f""" + ### ⬛ XTTS Base Model Detection +      {base_model_icon} **Base XTTS Model Status:** {base_model_status} + """ + return base_model_markdown + +def check_tts_version(required_version="0.24.0"): + global pfc_status + try: + # Get the installed version of TTS + installed_version = metadata.version("coqui-tts") + # Check if the installed version meets the required version + if version.parse(installed_version) >= version.parse(required_version): + tts_status = f"TTS version {installed_version} is installed and meets the requirement." + tts_status_icon = "✅" + else: + tts_status = f"❌ Fail - TTS version {installed_version} is installed but does not meet the required version {required_version}." + tts_status_icon = "❌" + pfc_status = "fail" # Update global status + except metadata.PackageNotFoundError: + # If TTS is not installed + tts_status = "TTS is not installed." + pfc_status = "fail" # Update global status + tts_markdown = f""" + ### 🟥 TTS Information
+      {tts_status_icon} **TTS Version:** {tts_status} + """ + return tts_markdown + +# Disk space check results to append to the Markdown +disk_space_results = check_disk_space() +cuda_results, pytorch_results = generate_cuda_markdown() +system_ram_results = get_system_ram_markdown() +base_model_results = generate_base_model_markdown(base_model_detected) +tts_version_status = check_tts_version() + +def pfc_check_fail(): + global pfc_status + if pfc_status == "fail": + print("[FINETUNE]") + print("[FINETUNE] \033[91m****** WARNING PRE-FLIGHT CHECKS FAILED ******* WARNING PRE-FLIGHT CHECKS FAILED *****\033[0m") + print("[FINETUNE] \033[91m* Please refer to the \033[93mPre-flight check tab \033[91mand resolve any issues before continuing. *\033[0m") + print("[FINETUNE] \033[91m*********** Expect errors and failures if you do not resolve these issues. ***********\033[0m") + print("[FINETUNE]") + return + +##################### +#### STEP 1 BITS #### +##################### + +def create_temp_folder(): + temp_folder = os.path.join(os.path.dirname(__file__), 'temp_files') + os.makedirs(temp_folder, exist_ok=True) + return temp_folder + +def create_temporary_file(folder, suffix=".wav"): + unique_filename = f"custom_tempfile_{int(time.time())}_{random.randint(1, 1000)}{suffix}" + return os.path.join(folder, unique_filename) + +def format_audio_list(target_language, whisper_model, out_path, max_sample_length, eval_split_number, speaker_name_input, gradio_progress=None): + buffer = 0.2 + max_duration = float(max_sample_length) # Ensure max_duration is a float + eval_percentage = eval_split_number / 100.0 + speaker_name = speaker_name_input + audio_total_size = 0 + os.makedirs(out_path, exist_ok=True) + temp_folder = os.path.join(out_path, "temp") + os.makedirs(temp_folder, exist_ok=True) + audio_folder = os.path.join(out_path, "wavs") + os.makedirs(audio_folder, exist_ok=True) + original_samples_folder = os.path.join(out_path, "..", "put-voice-samples-in-here") + print("[FINETUNE] Preparing Audio/Generating the dataset") + + # Write the target language to lang.txt in the output directory + lang_file_path = os.path.join(out_path, "lang.txt") + current_language = None + if os.path.exists(lang_file_path): + with open(lang_file_path, 'r', encoding='utf-8') as existing_lang_file: + current_language = existing_lang_file.read().strip() + + if current_language != target_language: + with open(lang_file_path, 'w', encoding='utf-8') as lang_file: + lang_file.write(target_language + '\n') + print("[FINETUNE] Updated lang.txt with the target language.") + else: + print("[FINETUNE] The existing language matches the target language") + + device = "cuda" if torch.cuda.is_available() else "cpu" + asr_model = WhisperModel(whisper_model, device=device, compute_type="float32") + + metadata = {"audio_file": [], "text": [], "speaker_name": []} + + existing_metadata = {'train': None, 'eval': None} + train_metadata_path = os.path.join(out_path, "metadata_train.csv") + eval_metadata_path = os.path.join(out_path, "metadata_eval.csv") + + if os.path.exists(train_metadata_path): + existing_metadata['train'] = pd.read_csv(train_metadata_path, sep="|") + print("[FINETUNE] Existing training metadata found and loaded.") + + if os.path.exists(eval_metadata_path): + existing_metadata['eval'] = pd.read_csv(eval_metadata_path, sep="|") + print("[FINETUNE] Existing evaluation metadata found and loaded.") + + # Process original voice samples to create WAV files + original_audio_files = [os.path.join(original_samples_folder, file) for file in os.listdir(original_samples_folder) if file.endswith(('.mp3', '.flac', '.wav'))] + + if not original_audio_files: + print(f"[FINETUNE] No audio files found in {original_samples_folder}. Skipping processing.") + return None, None, 0 + + for idx, audio_path in tqdm(enumerate(original_audio_files)): + audio_file_name_without_ext, _ = os.path.splitext(os.path.basename(audio_path)) + temp_audio_path = os.path.join(temp_folder, f"{audio_file_name_without_ext}.wav") + + try: + shutil.copy2(audio_path, temp_audio_path) + except Exception as e: + print(f"[FINETUNE] Error copying file: {e}") + continue + + wav, sr = torchaudio.load(temp_audio_path, format="wav") + wav = torch.as_tensor(wav).clone().detach().t().to(torch.float32), sr + + prefix_check = f"wavs/{audio_file_name_without_ext}_" + skip_processing = False + + for key in ['train', 'eval']: + if existing_metadata[key] is not None: + mask = existing_metadata[key]['audio_file'].str.startswith(prefix_check) + if mask.any(): + print(f"[FINETUNE] Segments from {audio_file_name_without_ext} have been previously processed; skipping...") + skip_processing = True + audio_total_size = 121 + break + + if skip_processing: + continue + + wav, sr = torchaudio.load(audio_path) + if wav.size(0) != 1: + wav = torch.mean(wav, dim=0, keepdim=True) + + wav = wav.squeeze() + audio_total_size += (wav.size(-1) / sr) + + segments, _ = asr_model.transcribe(audio_path, vad_filter=True, word_timestamps=True, language=target_language) + segments = list(segments) + i = 0 + sentence = "" + sentence_start = None + first_word = True + words_list = [] + for _, segment in enumerate(segments): + words = list(segment.words) + words_list.extend(words) + + for word_idx, word in enumerate(words_list): + if first_word: + sentence_start = word.start + if word_idx == 0: + sentence_start = max(sentence_start - buffer, 0) + else: + previous_word_end = words_list[word_idx - 1].end + sentence_start = max(sentence_start - buffer, (previous_word_end + sentence_start) / 2) + sentence = word.word + first_word = False + else: + sentence += " " + word.word + + # Split segment if it ends with punctuation or exceeds max_duration + if word.word[-1] in ["!", ".", "?"] or (word.end - sentence_start) > max_duration: + sentence = sentence.strip() + # Clean and normalize the sentence + sentence = multilingual_cleaners(sentence, target_language) + audio_file_name, _ = os.path.splitext(os.path.basename(audio_path)) + audio_file = f"{audio_file_name}_{str(i).zfill(8)}.wav" + + if word_idx + 1 < len(words_list): + next_word_start = words_list[word_idx + 1].start + else: + next_word_start = (wav.shape[0] - 1) / sr + + word_end = min((word.end + next_word_start) / 2, word.end + buffer) + + absolute_path = os.path.join(audio_folder, audio_file) # Save to the audio_folder + os.makedirs(os.path.dirname(absolute_path), exist_ok=True) + i += 1 + first_word = True + + audio = wav[int(sr * sentence_start):int(sr * word_end)].unsqueeze(0) + + # If the audio is too long, split it into smaller chunks + while audio.size(-1) > max_duration * sr: + split_audio = audio[:, :int(max_duration * sr)] + audio = audio[:, int(max_duration * sr):] + split_file_name = f"wavs/{audio_file_name}_{str(i).zfill(8)}.wav" + split_absolute_path = os.path.join(audio_folder, split_file_name) + torchaudio.save(split_absolute_path, split_audio, sr) + metadata["audio_file"].append(split_file_name) + metadata["text"].append(sentence) + metadata["speaker_name"].append(speaker_name) + i += 1 + + if audio.size(-1) >= sr / 3: # if the remaining audio is at least 0.33 seconds + torchaudio.save(absolute_path, audio, sr) + else: + continue + + metadata["audio_file"].append(f"wavs/{audio_file_name}_{str(i).zfill(8)}.wav") + metadata["text"].append(sentence) + metadata["speaker_name"].append(speaker_name) + + os.remove(temp_audio_path) + + # Check if the WAV files folder contains files after processing + audio_files = [os.path.join(audio_folder, file) for file in os.listdir(audio_folder) if file.endswith('.wav')] + + if not audio_files: + print(f"[FINETUNE] No processed audio files found in {audio_folder}. Skipping processing.") + return None, None, 0 + + if os.path.exists(train_metadata_path) and os.path.exists(eval_metadata_path): + existing_train_df = existing_metadata['train'] + existing_eval_df = existing_metadata['eval'] + else: + existing_train_df = pd.DataFrame(columns=["audio_file", "text", "speaker_name"]) + existing_eval_df = pd.DataFrame(columns=["audio_file", "text", "speaker_name"]) + + new_data_df = pd.DataFrame(metadata) + combined_train_df = pd.concat([existing_train_df, new_data_df], ignore_index=True).drop_duplicates().reset_index(drop=True) + combined_train_df_shuffled = combined_train_df.sample(frac=1) + num_val_samples = int(len(combined_train_df_shuffled) * eval_percentage) + + final_eval_set = combined_train_df_shuffled[:num_val_samples] + final_training_set = combined_train_df_shuffled[num_val_samples:] + + final_training_set.sort_values('audio_file').to_csv(train_metadata_path, sep='|', index=False) + final_eval_set.sort_values('audio_file').to_csv(eval_metadata_path, sep='|', index=False) + + del asr_model, final_eval_set, final_training_set, new_data_df, existing_metadata + gc.collect() + if torch.cuda.is_available(): + torch.cuda.empty_cache() + return train_metadata_path, eval_metadata_path, audio_total_size + + + +###################### +#### STEP 2 BITS ##### +###################### + +from trainer import Trainer, TrainerArgs + +from TTS.config.shared_configs import BaseDatasetConfig +from TTS.tts.datasets import load_tts_samples +from TTS.tts.layers.xtts.trainer.gpt_trainer import GPTArgs, GPTTrainer, GPTTrainerConfig, XttsAudioConfig +from TTS.utils.manage import ModelManager + + +def basemodel_or_finetunedmodel_choice(value): + global basemodel_or_finetunedmodel + if value == "Base Model": + basemodel_or_finetunedmodel = True + elif value == "Existing finetuned model": + basemodel_or_finetunedmodel = False + +def train_gpt(language, num_epochs, batch_size, grad_acumm, train_csv, eval_csv, learning_rate, model_to_train, output_path, max_audio_length=255995, progress=gr.Progress()): + pfc_check_fail() + + if "No Models Available" in eval_csv: + print(f"[FINETUNE] \033[91mError\033[0m: Cannot train. You have not selected a model. Please download a model.") + print(f"[FINETUNE] \033[91mError\033[0m: into a sub folder within the correct models folder.") + return + # Logging parameters + RUN_NAME = "XTTS_FT" + PROJECT_NAME = "XTTS_trainer" + DASHBOARD_LOGGER = "tensorboard" + LOGGER_URI = None + model_path = this_dir / "models" / "xtts" / model_to_train + + # Set here the path that the checkpoints will be saved. Default: ./training/ + OUT_PATH = os.path.join(output_path, "training") + print("[FINETUNE] \033[94mStarting Step 2\033[0m - Fine-tuning the XTTS Encoder") + print(f"[FINETUNE] \033[94mLanguage: \033[92m{language} \033[94mEpochs: \033[92m{num_epochs} \033[94mBatch size: \033[92m{batch_size}\033[0m \033[94mGrad accumulation steps: \033[92m{grad_acumm}\033[0m") + print(f"[FINETUNE] \033[94mTraining : \033[92m{train_csv}\033[0m") + print(f"[FINETUNE] \033[94mEvaluation : \033[92m{eval_csv}\033[0m") + print(f"[FINETUNE] \033[94mModel used : \033[92m{model_path}\033[0m") + + if torch.cuda.is_available(): + torch.cuda.empty_cache() + # Get the current device ID + gpu_device_id = torch.cuda.current_device() + gpu_available_mem_gb = (torch.cuda.get_device_properties(gpu_device_id).total_memory - torch.cuda.memory_allocated(gpu_device_id)) / (1024 ** 3) + print(f"[FINETUNE] \033[94mAvailable VRAM: \033[92m{gpu_available_mem_gb:.2f} GB\033[0m") + if gpu_available_mem_gb < 12: + print(f"[FINETUNE]") + print(f"[FINETUNE] \033[91m****** WARNING PRE-FLIGHT CHECKS FAILED ******* WARNING PRE-FLIGHT CHECKS FAILED *****\033[0m") + print(f"[FINETUNE] \033[94mAvailable VRAM: \033[92m{gpu_available_mem_gb:.2f} GB\033[0m") + print(f"[FINETUNE] \033[94mIf you are running on a Linux system and you have 12GB's or less of VRAM, this step\033[0m") + print(f"[FINETUNE] \033[94mmay fail, due to not enough GPU VRAM. Windows systems will use system RAM as extended\033[0m") + print(f"[FINETUNE] \033[94mVRAM and so should work ok. However, Windows machines will need enough System RAM\033[0m") + print(f"[FINETUNE] \033[94mavailable. Please read the PFC help section available on the first tab of the web\033[0m") + print(f"[FINETUNE] \033[94minterface for more information.\033[0m") + print(f"[FINETUNE] \033[91m****** WARNING PRE-FLIGHT CHECKS FAILED ******* WARNING PRE-FLIGHT CHECKS FAILED *****\033[0m") + print(f"[FINETUNE]") + + # Create the directory + os.makedirs(OUT_PATH, exist_ok=True) + + # Training Parameters + OPTIMIZER_WD_ONLY_ON_WEIGHTS = True # for multi-gpu training please make it False + START_WITH_EVAL = False # if True it will star with evaluation + BATCH_SIZE = batch_size # set here the batch size + GRAD_ACUMM_STEPS = grad_acumm # set here the grad accumulation steps + + # Define here the dataset that you want to use for the fine-tuning on. + config_dataset = BaseDatasetConfig( + formatter="coqui", + dataset_name="ft_dataset", + path=os.path.dirname(train_csv), + meta_file_train=train_csv, + meta_file_val=eval_csv, + language=language, + ) + + # Add here the configs of the datasets + DATASETS_CONFIG_LIST = [config_dataset] + TOKENIZER_FILE = str(model_path / "vocab.json") + XTTS_CHECKPOINT = str(model_path / "model.pth") + XTTS_CONFIG_FILE = str(model_path / "config.json") + DVAE_CHECKPOINT = model_path / "dvae.pth" + MEL_NORM_FILE = model_path / "mel_stats.pth" + SPEAKERS_FILE = model_path / "speakers_xtts.pth" + + # Copy the supporting files + destination_dir = this_dir / "finetune" / "tmp-trn" / "chkptandnorm" + destination_dir.mkdir(parents=True, exist_ok=True) + shutil.copy2(DVAE_CHECKPOINT, destination_dir / DVAE_CHECKPOINT.name) + shutil.copy2(MEL_NORM_FILE, destination_dir / MEL_NORM_FILE.name) + shutil.copy2(SPEAKERS_FILE, destination_dir / SPEAKERS_FILE.name) + + # init args and config + model_args = GPTArgs( + max_conditioning_length=132300, # 6 secs + min_conditioning_length=66150, # 3 secs + debug_loading_failures=False, + max_wav_length=max_audio_length, # ~11.6 seconds + max_text_length=200, + mel_norm_file=MEL_NORM_FILE, + dvae_checkpoint=DVAE_CHECKPOINT, + xtts_checkpoint=XTTS_CHECKPOINT, # checkpoint path of the model that you want to fine-tune + tokenizer_file=TOKENIZER_FILE, + gpt_num_audio_tokens=1026, + gpt_start_audio_token=1024, + gpt_stop_audio_token=1025, + gpt_use_masking_gt_prompt_approach=True, + gpt_use_perceiver_resampler=True, + ) + # define audio config + audio_config = XttsAudioConfig(sample_rate=22050, dvae_sample_rate=22050, output_sample_rate=24000) + + # Resolve Japanese threading issue + number_of_workers = 8 + if language == "ja": + number_of_workers = 0 + + # training parameters config + config = GPTTrainerConfig( + epochs=num_epochs, + output_path=OUT_PATH, + model_args=model_args, + run_name=RUN_NAME, + project_name=PROJECT_NAME, + run_description="GPT XTTS training", + dashboard_logger=DASHBOARD_LOGGER, + logger_uri=LOGGER_URI, + audio=audio_config, + batch_size=BATCH_SIZE, + batch_group_size=48, + eval_batch_size=BATCH_SIZE, + num_loader_workers=number_of_workers, + eval_split_max_size=256, + print_step=50, + plot_step=100, + log_model_step=100, + save_step=1000, + save_n_checkpoints=1, + save_checkpoints=True, + # target_loss="loss", + print_eval=False, + # Optimizer values like tortoise, pytorch implementation with modifications to not apply WD to non-weight parameters. + optimizer="AdamW", + optimizer_wd_only_on_weights=OPTIMIZER_WD_ONLY_ON_WEIGHTS, + optimizer_params={"betas": [0.9, 0.96], "eps": 1e-8, "weight_decay": 1e-2}, + lr=learning_rate, # learning rate + lr_scheduler="MultiStepLR", + # it was adjusted accordly for the new step scheme + lr_scheduler_params={"milestones": [50000 * 18, 150000 * 18, 300000 * 18], "gamma": 0.5, "last_epoch": -1}, + test_sentences=[], + ) + progress(0, desc="Model is currently training. See console for more information") + # init the model from config + model = GPTTrainer.init_from_config(config) + # load training samples + train_samples, eval_samples = load_tts_samples( + DATASETS_CONFIG_LIST, + eval_split=True, + eval_split_max_size=config.eval_split_max_size, + eval_split_size=config.eval_split_size, + ) + + # init the trainer + trainer = Trainer( + TrainerArgs( + restore_path=None, # xtts checkpoint is restored via xtts_checkpoint key so no need of restore it using Trainer restore_path parameter + skip_train_epoch=False, + start_with_eval=START_WITH_EVAL, + grad_accum_steps=GRAD_ACUMM_STEPS, + ), + config, + output_path=OUT_PATH, + model=model, + train_samples=train_samples, + eval_samples=eval_samples, + ) + trainer.fit() + + # get the longest text audio file to use as speaker reference + samples_len = [len(item["text"].split(" ")) for item in train_samples] + longest_text_idx = samples_len.index(max(samples_len)) + speaker_ref = train_samples[longest_text_idx]["audio_file"] + + trainer_out_path = trainer.output_path + + # deallocate VRAM and RAM + del model, trainer, train_samples, eval_samples, config, model_args, config_dataset + gc.collect() + train_samples = None + eval_samples = None + config_dataset = None + trainer = None + model = None + model_args = None + try: + return XTTS_CONFIG_FILE, XTTS_CHECKPOINT, TOKENIZER_FILE, trainer_out_path, speaker_ref + except Exception as e: + print(f"Error returning values: {e}") + return "Error", "Error", "Error", "Error", "Error" + +########################## +#### STEP 3 AND OTHER #### +########################## + +def clear_gpu_cache(): + # clear the GPU cache + if torch.cuda.is_available(): + torch.cuda.empty_cache() + +def find_a_speaker_file(folder_path): + search_path = folder_path / "*" / "speakers_xtts.pth" + files = glob.glob(str(search_path), recursive=True) + latest_file = max(files, key=os.path.getctime, default=None) + return latest_file + +XTTS_MODEL = None +def load_model(xtts_checkpoint, xtts_config, xtts_vocab): + pfc_check_fail() + global XTTS_MODEL + clear_gpu_cache() + if not xtts_checkpoint or not xtts_config or not xtts_vocab: + return "No Models were selected. Click the Refresh Dropdowns button and try again." + xtts_speakers_pth = find_a_speaker_file(this_dir / "models") + config = XttsConfig() + config.load_json(xtts_config) + XTTS_MODEL = Xtts.init_from_config(config) + print("[FINETUNE] \033[94mStarting Step 3\033[0m Loading XTTS model!") + XTTS_MODEL.load_checkpoint(config, checkpoint_path=xtts_checkpoint, vocab_path=xtts_vocab, use_deepspeed=False, speaker_file_path=xtts_speakers_pth) + if torch.cuda.is_available(): + XTTS_MODEL.cuda() + + print("[FINETUNE] Model Loaded!") + return "Model Loaded!" + +def run_tts(lang, tts_text, speaker_audio_file): + if XTTS_MODEL is None or not speaker_audio_file: + return "You need to run the previous step to load the model !!", None, None + speaker_audio_file = str(speaker_audio_file) + gpt_cond_latent, speaker_embedding = XTTS_MODEL.get_conditioning_latents(audio_path=speaker_audio_file, gpt_cond_len=XTTS_MODEL.config.gpt_cond_len, max_ref_length=XTTS_MODEL.config.max_ref_len, sound_norm_refs=XTTS_MODEL.config.sound_norm_refs) + out = XTTS_MODEL.inference( + text=tts_text, + language=lang, + gpt_cond_latent=gpt_cond_latent, + speaker_embedding=speaker_embedding, + temperature=XTTS_MODEL.config.temperature, # Add custom parameters here + length_penalty=XTTS_MODEL.config.length_penalty, + repetition_penalty=XTTS_MODEL.config.repetition_penalty, + top_k=XTTS_MODEL.config.top_k, + top_p=XTTS_MODEL.config.top_p, + ) + + with tempfile.NamedTemporaryFile(suffix=".wav", delete=False) as fp: + out["wav"] = torch.tensor(out["wav"]).unsqueeze(0) + out_path = fp.name + torchaudio.save(out_path, out["wav"], 24000) + + return "Speech generated !", out_path, speaker_audio_file + +def get_available_voices(minimum_size_kb=1200): + voice_files = [ + voice for voice in Path(f"{this_dir}/finetune/tmp-trn/wavs").glob("*.wav") + if voice.stat().st_size > minimum_size_kb * 1200 # Convert KB to bytes + ] + return sorted([str(file) for file in voice_files]) # Return full path as string + +def find_best_models(directory): + """Find files named 'best_model.pth' in the given directory.""" + return [str(file) for file in Path(directory).rglob("best_model.pth")] + +def find_models(directory, extension): + """Find files with a specific extension in the given directory.""" + return [str(file) for file in Path(directory).rglob(f"*.{extension}")] + +def find_jsons(directory, filename): + """Find files with a specific filename in the given directory.""" + return [str(file) for file in Path(directory).rglob(filename)] + +# Your main directory +main_directory = Path(this_dir) / "finetune" / "tmp-trn" +# XTTS checkpoint files (best_model.pth) +xtts_checkpoint_files = find_best_models(main_directory) +# XTTS config files (config.json) +xtts_config_files = find_jsons(main_directory, "config.json") +# XTTS vocab files (vocab.json) +xtts_vocab_files = find_jsons(main_directory, "vocab.json") + +########################## +#### STEP 4 AND OTHER #### +########################## +import shutil +from pathlib import Path +import torch + +def compact_custom_model(xtts_checkpoint_copy, folder_path, overwrite_existing): + this_dir = Path(__file__).parent.resolve() + if not xtts_checkpoint_copy: + error_message = "No trained model was selected. Please click Refresh Dropdowns and try again." + print("[FINETUNE]", error_message) + return error_message + target_dir = this_dir / "models" / "xtts" / folder_path + if overwrite_existing == "Do not overwrite existing files" and target_dir.exists(): + error_message = "The target folder already exists. Please change folder name or allow overwrites." + print("[FINETUNE]", error_message) + return error_message + xtts_checkpoint_copy = Path(xtts_checkpoint_copy) + try: + checkpoint = torch.load(xtts_checkpoint_copy, map_location=torch.device("cpu")) + except Exception as e: + print("[FINETUNE] Error loading checkpoint:", e) + raise + del checkpoint["optimizer"] + target_dir.mkdir(parents=True, exist_ok=True) + # Remove dvae-related keys from checkpoint + for key in list(checkpoint["model"].keys()): + if "dvae" in key: + del checkpoint["model"][key] + torch.save(checkpoint, target_dir / "model.pth") + def copy_files(src_folder, dest_folder, files): + for file_name in files: + src_path = src_folder / file_name + dest_path = dest_folder / file_name + if src_path.exists(): + shutil.copy2(src_path, dest_path) + else: + print(f"[FINETUNE] Warning: {src_path} does not exist and will not be copied.") + # Copy first set of files + folder_path_new = xtts_checkpoint_copy.parent + files_to_copy = ["vocab.json", "config.json"] + copy_files(folder_path_new, target_dir, files_to_copy) + # Copy second set of files + chkptandnorm_path = this_dir / "finetune" / "tmp-trn" / "chkptandnorm" + files_to_copy2 = ["speakers_xtts.pth", "mel_stats.pth", "dvae.pth"] + copy_files(chkptandnorm_path, target_dir, files_to_copy2) + # Copy large wav files + source_wavs_dir = this_dir / "finetune" / "tmp-trn" / "wavs" + target_wavs_dir = target_dir / "wavs" + target_wavs_dir.mkdir(parents=True, exist_ok=True) + for file_path in source_wavs_dir.iterdir(): + if file_path.is_file() and file_path.stat().st_size > 1000 * 1024: # File size greater than 1000 KB + shutil.copy2(file_path, target_wavs_dir / file_path.name) + print(f"[FINETUNE] Model & Suitable WAV samples copied to '/models/xtts/{folder_path}/'") + return f"Model & Suitable WAV samples copied to '/models/xtts/{folder_path}/'" + +def delete_training_data(): + # Define the folder to be deleted + folder_to_delete = Path(this_dir / "finetune" / "tmp-trn") + + # Check if the folder exists before deleting + if folder_to_delete.exists(): + # Iterate over all files and subdirectories + for item in folder_to_delete.iterdir(): + # Exclude trainer_0_log.txt from deletion + if item.name != "trainer_0_log.txt": + try: + if item.is_file(): + item.unlink() + elif item.is_dir(): + shutil.rmtree(item) + except PermissionError: + print(f"[FINETUNE] PermissionError: Could not delete {item}. Skipping.") + + print(f"[FINETUNE] Folder {folder_to_delete} contents (excluding trainer_0_log.txt) deleted successfully.") + return "Folder '/finetune/tmp-trn/' contents (excluding trainer_0_log.txt) deleted successfully." + else: + print(f"[FINETUNE] Folder {folder_to_delete} does not exist.") + return "Folder '/finetune/tmp-trn/' does not exist." + +def clear_folder_contents(folder_path): + # Check if the folder exists before clearing its contents + if folder_path.exists() and folder_path.is_dir(): + # List all files and subdirectories in the folder + for item in os.listdir(folder_path): + item_path = folder_path / item + if item_path.is_file(): + # If it's a file, remove it + os.remove(item_path) + elif item_path.is_dir(): + # If it's a subdirectory, remove it recursively + shutil.rmtree(item_path) + + print(f"[FINETUNE] Contents of {folder_path} deleted successfully.") + return f"Contents of '{folder_path}' deleted successfully." + else: + print(f"[FINETUNE] Folder {folder_path} does not exist.") + return f"Folder '{folder_path}' does not exist." + +def delete_voice_sample_contents(): + # Define the folders to be cleared + voice_samples_folder = this_dir / "finetune" / "put-voice-samples-in-here" + gradio_temp_folder = this_dir / "finetune" / "gradio_temp" + # Clear the contents of the gradio_temp folder + gradio_temp_message = clear_folder_contents(gradio_temp_folder) + # Clear the contents of the voice samples folder + voice_samples_message = clear_folder_contents(voice_samples_folder) + return voice_samples_message + +####################### +#### OTHER Generic #### +####################### +# define a logger to redirect +class Logger: + def __init__(self, filename="finetune.log"): + self.log_file = filename + self.terminal = sys.stdout + self.log = open(self.log_file, "w") + + def write(self, message): + self.terminal.write(message) + self.log.write(message) + + def flush(self): + self.terminal.flush() + self.log.flush() + + def isatty(self): + return False + +# redirect stdout and stderr to a file +sys.stdout = Logger() +sys.stderr = sys.stdout + +# logging.basicConfig(stream=sys.stdout, level=logging.INFO) +import logging +logging.basicConfig( + level=logging.INFO, + format="[FINETUNE] %(message)s", + handlers=[ + logging.StreamHandler(sys.stdout) + ] +) + +def read_logs(): + sys.stdout.flush() + with open(sys.stdout.log_file, "r") as f: + return f.read() + +def cleanup_before_exit(signum, frame): + print("[FINETUNE] Received interrupt signal. Cleaning up and exiting...") + # Perform cleanup operations here if necessary + sys.exit(0) + +def create_refresh_button(refresh_components, refresh_methods, elem_class, interactive=True): + def refresh(): + updates = {} + for component, method in zip(refresh_components, refresh_methods): + args = method() if callable(method) else method + if args and 'choices' in args: + # Select the most recent file (last in the sorted list) + args['value'] = args['choices'][-1] if args['choices'] else "" + for k, v in args.items(): + setattr(component, k, v) + updates[component] = gr.update(**(args or {})) + return updates + + refresh_button = gr.Button("Refresh Dropdowns", elem_classes=elem_class, interactive=interactive) + refresh_button.click( + fn=refresh, + inputs=[], + outputs=refresh_components + ) + + return refresh_button + + + +pfc_markdown = f""" + ### 🚀 Pre-flight Checklist for Fine-tuning
+ ◽ Ensure each criterion is marked with a green check mark ✅ and a Pass status. Finetuning will fail otherwise.
+ ◽ The help tabs along the top will assist in resolving issues and you can also find additional help guides on the AllTalk [GitHub repository](https://github.com/erew123/alltalk_tts#-finetuning-a-model).
+ ◽ For an overview of fine-tuning procedures, please refer to the "General Finetuning info" tab or visit the AllTalk [GitHub repository](https://github.com/erew123/alltalk_tts#-finetuning-a-model). + """ + +custom_css = """ +body { + font-size: 16px; /* Adjust the base font size as needed */ +} +h1, h2, h3, h4, h5, h6 { + font-size: 1.25em; /* Adjust heading sizes relative to the base size */ +} +p { + font-size: 1.1em; /* Paragraph font size, relative to the base size */ + margin-bottom: 10px; /* Adjust paragraph spacing */ +} +.gradio_container { + zoom: 1.1; /* Adjust the zoom to scale the entire container */ +} +""" + +if __name__ == "__main__": + # Register the signal handler + signal.signal(signal.SIGINT, cleanup_before_exit) + +################ +#### GRADIO #### +################ + + parser = argparse.ArgumentParser( + description="""XTTS fine-tuning demo\n\n""" + """ + Example runs: + python3 TTS/demos/xtts_ft_demo/xtts_demo.py --port + """, + formatter_class=argparse.RawTextHelpFormatter, + ) + parser.add_argument( + "--num_epochs", + type=int, + help="Number of epochs to train. Default: 10", + default=10, + ) + parser.add_argument( + "--batch_size", + type=int, + help="Batch size. Default: 4", + default=4, + ) + parser.add_argument( + "--grad_acumm", + type=int, + help="Grad accumulation steps. Default: 1", + default=1, + ) + parser.add_argument( + "--max_audio_length", + type=int, + help="Max permitted audio size in seconds. Default: 11", + default=11, + ) + + args = parser.parse_args() + +##################### +#### GRADIO INFO #### +##################### + + with gr.Blocks(theme=gr.themes.Base(), css=custom_css) as demo: + with gr.Row(): + gr.Markdown("## XTTS Models Finetuning") + gr.Markdown("") + gr.Markdown("") + dark_mode_btn = gr.Button("Light/Dark Mode", variant="primary", size="sm") + dark_mode_btn.click(None, None, None, + js="""() => { + if (document.querySelectorAll('.dark').length) { + document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark')); + // localStorage.setItem('darkMode', 'disabled'); + } else { + document.querySelector('body').classList.add('dark'); + // localStorage.setItem('darkMode', 'enabled'); + } + }""", show_api=False) + with gr.Tab("🚀 PFC"): + with gr.Tab("🚀 Pre-Flight Checklist"): + gr.Markdown( + f""" + {pfc_markdown} + {disk_space_results} + {system_ram_results} + {cuda_results} + {pytorch_results} + {base_model_results} + {tts_version_status} + """ + ) + with gr.Tab("🟩 Disks Help"): + gr.Markdown( + f""" + {disk_space_results}

+ ◽ During actual training (Step 2) Finetuning will require approximately 18GB's of free disk space while performing training and will fail or perform badly if there is any less disk space. The majority of this disk space is used temporarily and will be cleared when you reach Step 4 and move & compact the model then delete the training data.
+ ◽ Because lots of data is being copied around, mechanical hard disks will be slow. + """ + ) + with gr.Tab("🟪 RAM & VRAM Help"): + gr.Markdown( + f""" + {system_ram_results}
+ ◽ During actual training (Step 2) Finetuning will use around **14GB's** of VRAM. If your GPU doesnt have 14GB's of VRAM:
+        - **Windows** systems will attempt to extend VRAM into your System RAM, and so should work.
+        - **Linux** systems can only use the available VRAM, so may fail on 12GB VRAM or smaller GPU's.
+ ◽ For **Windows** users with 12GB or less, if you also have very low or slow System RAM you can expect bad performance or the training to fail.
+        - 12GB cards may need 2GB System RAM.
+        - 8GB cards may need 6GB System RAM.
+        - 6GB cards may need 8GB System RAM.
+ ◽ Its hard to estimate what performance impact this could will have, due to different memory speeds, PCI speeds, GPU speeds etc.
+ ◽ If you have a low VRAM scenario and also are attempting to run on a mechanical hard drive, Finetuning could take ??? amount of time.
+ ◽ On Windows machines, please ensure you have **not** disabled System Memory Fallback for Stable Diffusion link here
+ """ + ) + with gr.Tab("🟨 CUDA & Cublas Help"): + gr.Markdown( + f""" + {cuda_results}

+ ◽ It DOESNT matter what version of CUDA you have installed within Python either, CUDA 11.8, CUDA 12.1 etc. The NVIDIA CUDA Development Toolkit is a completly different and seperate thing from Python/PyTorch.
+ ◽ Finetuning simply wants to access a tool within the CUDA Development Toolkit 11.8 called Cublas64_11.
+ ◽ If you dont have the toolkit installed, the idea is just to install the smallest bit possible and this will not affect or impact other things on your system.

+       ◽ You will need to download the Nvidia Cuda Toolkit 11.8 network install from link here
+       ◽ 1) Run the installer and select Custom Advanced Uncheck everything at the top then expand CUDA, Development > Compiler > and select nvcc then expand Libraries and select CUBLAS.
+       ◽ 2) Back at the top of CUDA, expand Runtime > Libraries and select CUBLAS. Click Next, accept the default path (taking a note of its location) and let the install run.
+       ◽ 3) You should be able to drop to your terminal or command prompt and type nvcc --version and have it report Cuda compilation tools, release 11.8. If it does you are good to go. If it doesn't > Step 4.
+       ◽ 4) Linux users, you can temporarily add these paths on your current terminal window with (you may need to confirm these are correct for your flavour of Linux):

+                  export LD_LIBRARY_PATH=/usr/local/cuda-11.8/lib64:${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}} (Add it to your ~/.bashrc if you want this to be permanent)
+                  export LD_LIBRARY_PATH=/usr/local/cuda-11.8/bin

+                  Windows users need the add the following to the PATH environment variable. Start menu and search for "Environment Variables" or "Edit the system environment variables.".
+                  Find and select the "Path" variable, then click on the "Edit...". Click on the "New" button and add:

+                 C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin.

+       ◽ 5) Once you have these set correctly, you should be able to open a new command prompt/terminal and nvcc --version at the command prompt/terminal, resulting in Cuda compilation tools, release 11.8.
+       ◽ 6) If the nvcc command doesn't work OR it reports a version different from 11.8, finetuning wont work, so you will to double check your environment variables and get them working correctly.
+ """ + ) + with gr.Tab("🟦 Python & PyTorch Help"): + gr.Markdown( + f""" + {pytorch_results}

+ ◽ On the PyTorch version the:
+           - first few digits are the version of PyTorch e.g. 2.1.0 is PyTorch 2.1.0
+           - last few digits refer to the CUDA version e.g. cu118 is Cuda 11.8. cu121 is Cuda 12.1.
+ ◽ Ensure you have started your Python envuronment before running finetuning otherwise you will have failures on the above checks.
+ ◽ If PyTorch does not show a CUDA version, then PyTorch will need reinstalling with CUDA. I would suggest running pip cache purge before installing PyTorch again.
+ ◽ It DOESNT matter what version of PyTorch and CUDA you have installed within Python, CUDA 11.8, CUDA 12.1 etc. The NVIDIA CUDA Development Toolkit is a completly different and seperate thing.
+ ◽ Finetuning simply wants to access a tool within the CUDA Development Toolkit called Cublas64_11.
+ ◽ If you dont have the toolkit installed, the idea is just to install the smallest bit possible and this will not affect or impact other things on your system.
+ """ + ) + with gr.Tab("⬛ XTTS Base Model Help"): + gr.Markdown( + f""" + {base_model_results}

+ ◽ If your basemodel is not being detected, please ensure that finetune.py is being run from the AllTalk main folder.
+ ◽ Ensure you have started AllTalk normally at least once. You can start it again and it will download any missing files.
+ ◽ Check that there is an XTTS model within the models folder e.g. /models/xttsv2_2.0.2/
+ ◽ The files required are "model.pth", "vocab.json", "config.json", "dvae.pth", "mel_stats.pth", "speakers_xtts.pth". + """ + ) + with gr.Tab("🟥 TTS Version Help"): + gr.Markdown( + f""" + {tts_version_status}

+ ◽ If your TTS version is showing as the incorrect version, please reinstall the Finetuning requirements at the command prompt/terminal.
+ ◽ pip install -r requirements_finetune.txt
+ """ + ) + + with gr.Tab("ℹ️ General Finetuning info"): + gr.Markdown( + f""" + ### 🟥 Important Note + ◽ finetune.py needs to be run from the /alltalk_tts/ folder. Don't move the location of this script. + ### 🟦 What you need to run finetuning + ◽ An Nvidia GPU.
+ ◽ If you have multiple Nvidia GPU's in your system, please see the Github Help section [Multiple GPU's](https://github.com/erew123/alltalk_tts#performance-and-compatibility-issues).
+ ◽ Some decent quality audio, multiple files if you like. Minimum of 2 minutes and Ive tested up to 20 minutes of audio.
+ ◽ There is no major need to chop down your audio files into small slices as Step 1 will do that for you automatically and prepare the training set. But this can be helpful in cases where:
+         - Whisper doesnt correctly detect and split down audio files.
+         - You do not get a lot of "Speaker Reference Audio" files at the end of training.
+ ◽ This process will need access to all your GPU and VRAM, so close any other software that's using your GPU currently.
+ ### 🟨 What do I do from here?
+ ◽ Proceed through Step 1, 2, 3 and onto "What to do next".
+ ### 🟩 Additional Information
+ ◽ Guidance is provided on each step of the process however, if you are after more detailed information please visit:
+           ◽ [AllTalk Github Finetuning](https://github.com/erew123/alltalk_tts#-finetuning-a-model)
+           ◽ [Coqui XTTS Documentation](https://docs.coqui.ai/en/latest/index.html)
+ """ + ) + +####################### +#### GRADIO STEP 1 #### +####################### + with gr.Tab("📁 Step 1 - Generating the dataset"): + with gr.Tab("Generate Dataset"): + out_path = gr.Textbox( + label="Output path (where data and checkpoints will be saved):", + value=out_path, + visible=False, + ) + # Define directories + this_dir = Path(__file__).parent.resolve() + voice_samples_dir = this_dir / "finetune" / "put-voice-samples-in-here" + training_data_dir = this_dir / "finetune" / "tmp-trn" / "wavs" + metadata_files = [ + this_dir / "finetune" / "tmp-trn" / "metadata_eval.csv", + this_dir / "finetune" / "tmp-trn" / "metadata_train.csv", + this_dir / "finetune" / "tmp-trn" / "lang.txt" + ] + + # Ensure directories exist + voice_samples_dir.mkdir(parents=True, exist_ok=True) + training_data_dir.mkdir(parents=True, exist_ok=True) + + def upload_audio(files): + for file in files: + shutil.copy(file.name, voice_samples_dir) + return f"Uploaded {len(files)} files to {voice_samples_dir}" + + def delete_existing_audio(): + for file in voice_samples_dir.iterdir(): + if file.is_file(): + file.unlink() + return f"Deleted all files in {voice_samples_dir}" + + def delete_existing_training_data(): + if training_data_dir.exists(): + for file in training_data_dir.iterdir(): + if file.is_file(): + file.unlink() + for metadata_file in metadata_files: + if metadata_file.exists(): + metadata_file.unlink() + return f"Deleted all files in {training_data_dir} and related metadata files" + + with gr.Row(): + with gr.Column(scale=1): + audio_files_upload = gr.Files(label="Upload Audio Files") + with gr.Column(scale=3): + with gr.Row(): + with gr.Column(scale=1): + audio_upload_button = gr.Button("Upload Audio") + delete_audio_button = gr.Button("Delete Existing Audio") + delete_dataset_button = gr.Button("Delete Existing Training Dataset") + with gr.Column(scale=2): + gr.Markdown(""" + You can manually copy your audio files to `/finetune/put-voice-samples-in-here/` or use the upload to the left and click "Upload Audio". Once you have uploaded files, you can start creating your dataset. + + - If you wish to delete previously uploaded audio files then use 'Delete Existing Audio'. + - If you wish to delete previously generated training datasets, please use 'Delete Existing Training Dataset'. + - If you wish to re-use your previously created training data, just click 'Create Dataset'. + """) + with gr.Row(): + output_text = gr.Textbox(label="Audio File Management Result", interactive=False) + + # Define actions for buttons + audio_upload_button.click(upload_audio, inputs=audio_files_upload, outputs=output_text) + delete_audio_button.click(delete_existing_audio, outputs=output_text) + delete_dataset_button.click(delete_existing_training_data, outputs=output_text) + + with gr.Row(): + whisper_model = gr.Dropdown( + label="Whisper Model", + value="large-v2", + choices=[ + "large-v3", + "large-v2", + "large", + "medium", + "small" + ], + scale=1, + ) + + lang = gr.Dropdown( + label="Dataset Language", + value="en", + choices=[ + "en", + "es", + "fr", + "de", + "it", + "pt", + "pl", + "tr", + "ru", + "nl", + "cs", + "ar", + "zh", + "hu", + "ko", + "ja" + ], + scale=1, + ) + max_sample_length = gr.Dropdown( + label="Max Audio Length Created by Whisper (seconds)", + value="30", + choices=["10", "15", "20", "25", "30", "35", "40", "45", "50", "55", "60", "65", "70", "75", "80", "85", "90"], + scale=2 + ) + eval_split_number = gr.Number( + label="Evaluation data Split (the % to use for Evaluation data)", + value=15, # Default value + minimum=5, # Minimum value + maximum=95, # Maximum value + step=1, # Increment step + scale=2, + ) + speaker_name_input = gr.Textbox( + label="The name of the speaker/person you are training", + value="personsname", + visible=True, + scale=2, + ) + progress_data = gr.Label( + label="Progress:" + ) + logs = gr.Textbox( + label="Logs:", + interactive=False, + lines=10, + ) + demo.load(read_logs, None, logs, every=1) + + prompt_compute_btn = gr.Button(value="Step 1 - Create dataset") + + def preprocess_dataset(language, whisper_model, out_path, max_sample_length, eval_split_number, speaker_name_input, progress=gr.Progress(track_tqdm=True)): + clear_gpu_cache() + test_for_audio_files = [file for file in os.listdir(audio_folder) if any(file.lower().endswith(ext) for ext in ['.wav', '.mp3', '.flac'])] + if not test_for_audio_files: + return "I cannot find any mp3, wav or flac files in the folder called 'put-voice-samples-in-here'", "", "" + else: + try: + + train_meta, eval_meta, audio_total_size = format_audio_list(target_language=language, whisper_model=whisper_model, out_path=out_path, max_sample_length=max_sample_length, eval_split_number=eval_split_number, speaker_name_input=speaker_name_input, gradio_progress=progress) + except: + traceback.print_exc() + error = traceback.format_exc() + return f"The data processing was interrupted due an error !! Please check the console to verify the full error message! \n Error summary: {error}", "", "" + + clear_gpu_cache() + + # if audio total len is less than 2 minutes raise an error + if audio_total_size < 120: + message = "The total duration of the audio file or files you provided, was less than 2 minutes in length. Please add more audio samples." + print("[FINETUNE] ",message) + return message, "", "" + + print("[FINETUNE] Dataset Generated. Move to Step 2") + return "Dataset Generated. Move to Step 2", train_meta, eval_meta + + with gr.Tab("Generate Dataset Instructions"): + gr.Markdown( + f""" + ### 📁 Generating the dataset
+ ### 🟥 Important Note - Windows - "UserWarning: huggingface_hub cache-system uses symlinks." + ◽ This error is caused by Huggingfaces download software downloading the Whisper model. If you get this error, please restart your Windows command prompt with "Run as Administrator" and restart finetuning.
+ ◽ This should only occur the 1st time it downloads the Whisper model. [Huggingface Reference here](https://huggingface.co/docs/huggingface_hub/en/guides/manage-cache#limitations) + ### 🟥 Important Note - Language support. + ◽ Although I have done my best to help automate this step, the Whisper model is not great with all languages and may fail to build your training data correctly. The Large-v3 may be better at certain languages.
+ ◽ You can find information about the Whisper model [here](https://github.com/openai/whisper?tab=readme-ov-file#available-models-and-languages) and you can find data about manually building training data [here](https://docs.coqui.ai/en/latest/formatting_your_dataset.html), as well as details below about the file structure this step performs.
+ ### 🟦 What you need to do + ◽ Please read Coqui's guide on what makes a good dataset [here](https://docs.coqui.ai/en/latest/what_makes_a_good_dataset.html#what-makes-a-good-dataset)
+ ◽ Place your audio files in {str(audio_folder)} + ◽ Your audio samples can be in the format mp3, wav, or flac.
+ ◽ You will need a minimum of 2 minutes of audio in either one or multiple audio files. 5 to 10 minutes of audio would probably be better, allowing for more varied sample data to be generated.
+ ◽ Very small sample files cause errors, so I would recommend that the samples are at least 30 seconds and longer.
+ ◽ FYI Anecdotal evidence suggests that the Whisper 2 model may yield superior results in audio splitting and dataset creation.
+ ### 🟨 What this step is doing
+ ◽ With step one, we are going to be stripping your audio file(s) into smaller files, using Whisper to find spoken words/sentences, compile that into excel sheets of training data, ready for Step 2.
+ ◽ Whisper is making a best effort to find spoken audio and break it down into smaller audio files. The content of these audio files is then transcribed into CSV fles (which you can edit in Excel or similar).
+ ◽ These files (audio and CSV) are used at the next step to train the model "this is what the audio sounds like and these are the words being spoken".
+ ◽ If you are using Audio that has multiple speakers/people speaking, you will HAVE to edit the generated CSV files and remove the **OTHER** person out of the training list.
+ ◽ If you wish to manually look at the CSV files before running the next step, you are welcome to do so and edit them as necessary. The greater the accuracy of the text the better the training will be.
+           ◽ /alltalk_tts/finetune/tmp-trn/lang.txt Contains a two digit langauage code e.g. `en`, `fr` etc.
+           ◽ /alltalk_tts/finetune/tmp-trn/metadata_train.csv Contains the list of wavs to text that will be used. This is what the model is trained with.
+           ◽ /alltalk_tts/finetune/tmp-trn/metadata_eval.csv Lists the evaluation wav to text, used to generate TTS while training and evaluate the quality of generated TTS to the original sample.
+           ◽ /alltalk_tts/finetune/tmp-trn/wavs/ These are the wavs split out from your original training samples and match the contents of the CSV files.
+ ◽ Whilst you can choose multiple Whisper models, its best only to use the 1 model as each one is about 3GB in size and will download to your local huggingface cache on first-time use.
+ ◽ If and when you have completed training, should you wish to delete this 3GB Whisper model from your system, you are welcome to do so.
+ ### 🟩 How long will this take?
+ ◽ First time, it needs to download the Whisper model which is 3GB. After that a few minutes on an average 3-4 year old system.
+ """ + ) + +####################### +#### GRADIO STEP 2 #### +####################### + with gr.Tab("💻 Step 2 - Training"): + with gr.Tab("Training the model"): + with gr.Row(): + model_to_train_choices = list(available_models.keys()) + model_to_train = gr.Dropdown( + choices=model_to_train_choices, + label="Select the Model to train", + value=model_to_train_choices[0] if model_to_train_choices else None + ) + train_csv = gr.Textbox( + label="Train CSV:", + scale=2, + ) + eval_csv = gr.Textbox( + label="Eval CSV:", + scale=2, + ) + learning_rates = gr.Dropdown( + value=5e-6, + label="Learning Rate", + choices=[ + ("1e-6", 1e-6), + ("5e-6", 5e-6), + ("1e-5", 1e-5), + ("5e-5", 5e-5), + ("1e-4", 1e-4), + ("5e-4", 5e-4), + ("1e-3", 1e-3), + ], + type="value", + allow_custom_value=True, + scale=0, + ) + with gr.Row(): + num_epochs = gr.Slider( + label="Number of epochs:", + minimum=1, + maximum=100, + step=1, + value=args.num_epochs, + ) + batch_size = gr.Slider( + label="Batch size:", + minimum=2, + maximum=512, + step=1, + value=args.batch_size, + ) + grad_acumm = gr.Slider( + label="Grad accumulation steps:", + minimum=1, + maximum=32, + step=1, + value=args.grad_acumm, + ) + max_audio_length = gr.Slider( + label="Max permitted audio size in seconds:", + minimum=2, + maximum=20, + step=1, + value=args.max_audio_length, + ) + + progress_train = gr.Label( + label="Progress:" + ) + logs_tts_train = gr.Textbox( + label="Logs:", + interactive=False, + lines=10, + ) + demo.load(read_logs, None, logs_tts_train, every=1) + train_btn = gr.Button(value="Step 2 - Run the training") + + def train_model(language, train_csv, eval_csv, learning_rates, model_to_train, num_epochs, batch_size, grad_acumm, output_path, max_audio_length, progress=gr.Progress()): + clear_gpu_cache() + if not train_csv or not eval_csv: + return "You need to run the data processing step or manually set `Train CSV` and `Eval CSV` fields !", "", "", "", "" + try: + # convert seconds to waveform frames + max_audio_length = int(max_audio_length * 22050) + learning_rate = float(learning_rates) # Convert the learning rate value to a float + progress(0, "Initializing training...") + config_path, original_xtts_checkpoint, vocab_file, exp_path, speaker_wav = train_gpt(language, num_epochs, batch_size, grad_acumm, train_csv, eval_csv, learning_rate, model_to_train, output_path=str(output_path), max_audio_length=max_audio_length, progress=gr.Progress()) + except: + traceback.print_exc() + error = traceback.format_exc() + return f"The training was interrupted due an error !! Please check the console to check the full error message! \n Error summary: {error}", "", "", "", "" + + # copy original files to avoid parameters changes issues + shutil.copy(config_path, exp_path) + shutil.copy(vocab_file, exp_path) + + ft_xtts_checkpoint = os.path.join(exp_path, "best_model.pth") + print("[FINETUNE] Model training done. Move to Step 3") + clear_gpu_cache() + return "Model training done. Move to Step 3", config_path, vocab_file, ft_xtts_checkpoint, speaker_wav + + with gr.Tab("Training the model Instructions"): + gr.Markdown( + f""" + ### 💻 Training
+ ### 🟥 Important Note - Language support. + ◽ If this step is failing/erroring you may wish to check your training data was created correctly (Detailed in Step 1), confirming that wav files have been generated and your `metadata_train.csv` and `metadata_eval.csv` files have been populated.
+ ◽ Ignore 'fatal: not a git repository (or any of the parent directories): .git' as this is a legacy training script issue.
+ ### 🟦 What you need to do + ◽ The Train CSV and Eval CSV should already be populated. If not, just go back to Step 1 and click "Create Dataset" again.
+ ◽ The default settings below are the suggested settings for most purposes, however you may choose to alter them depending on your specific use case.
+ ◽ There are no absolute correct settings for training. It will vary based on:
+          - What you are training (A human voice, A cartoon voice, A new language entirely etc)
+          - How much audio you have (you may want less or more eval or epochs)
+ ◽ The key indicator of sufficient model training is whether it sounds right to you. Coqui suggests beginning with the base settings for training. If the resulting audio doesn't meet your expectations, additional training sessions may be necessary.
+ ### 🟨 What this step is doing
+ ◽ Very simply put, it's taking all our wav files generated in Step 1, along with our recorded speech in out excel documents and its training the model on that voice e.g. listen to this audio file and this is what is being said in it, so learn to reproduce it.
+ ### 🟩 How long will this take?
+ ◽ On a RTX 4070 with factory settings (as below) and 20 minutes of audio, this took 20 minutes. Again, it will vary by system and how much audio you are throwing at it. You have time to go make a coffee.
+ ◽ Look at your command prompt/terminal window if you want to see what it is doing.
+ """ + ) + + with gr.Tab("Info - Grad Accumulation"): + gr.Markdown( + f""" + ### 🟩 Grad Accumulation
+ ◽ Gradient accumulation is a technique that allows you to simulate a larger batch size without actually increasing the memory consumption. Here's how it works:
+       ◽ Instead of updating the model's parameters after every batch, gradient accumulation enables you to process multiple batches and accumulate their gradients.
+       ◽ By setting steps greater than 1, you can process multiple batches before a single optimization step e.g if steps is set to 4, the gradients will be accumulated over 4 batches before updating the model's parameters.
+       ◽ This means that you can effectively use a larger batch size while keeping the actual batch size per iteration smaller, thereby reducing the memory footprint.
+       ◽ Increasing the steps allows you to find a balance between memory consumption and computational efficiency. You can process more examples per optimization step without exceeding the available memory.

+ ◽ However, it's important to note that increasing the gradient accumulation steps does have an impact on the training process:
+       ◽ Since the model's parameters are updated less frequently (every grad_accum_steps batches), the training dynamics may be slightly different compared to updating the model after every batch.
+       ◽ You may need to adjust the learning rate and other hyperparameters accordingly to compensate for the less frequent updates. Typically, you can increase the learning rate slightly when using gradient accumulation.
+       ◽ The training progress will be slower in terms of the number of optimization steps per epoch, as the model updates occur less frequently.
+       ◽ However, the overall training time may still be reduced compared to using a smaller batch size without gradient accumulation, as it allows for better utilization of GPU resources.
+       ◽ To start, you can try setting steps to a value like 4 and see if it resolves the OOM error. If the error persists, you can experiment with higher values until you find a balance that works for your specific setup. + """ + ) + with gr.Tab("Info - Batch Size"): + gr.Markdown( + f""" + ### 🟩 Batch Size
+ ◽ Batch size refers to the number of training examples processed in a single iteration (forward and backward pass) before updating the model's parameters.
+ ◽ A larger batch size allows for more efficient computation and utilization of hardware resources, especially when using GPUs.
+ ◽ However, increasing the batch size also increases the memory consumption during training, as more examples need to be stored in memory.
+ ◽ If the batch size is too large, it can lead to out-of-memory (OOM) errors, especially when training on GPUs with limited memory.
+ ◽ On the other hand, using a smaller batch size can result in slower training and may require more iterations to converge.
+ ◽ The optimal batch size depends on various factors, such as the model architecture, available hardware resources, and the specific problem being solved.
+ ◽ It's common to experiment with different batch sizes to find a balance between training speed and memory efficiency.
+ ◽ If you encounter OOM errors, try reducing the batch size until the training can proceed without memory issues.
+ ◽ Keep in mind that the batch size also affects the model's generalization and convergence behavior, so it's important to monitor the model's performance while adjusting the batch size. + """ + ) + with gr.Tab("Info - Learning Rate"): + gr.Markdown( + f""" + ### 🟩 Learning Rate
+ ◽ 1e-6: Very small learning rate, slow but stable learning.
+ ◽ 5e-6: Small learning rate, slow but stable learning.
+ ◽ 1e-5: Moderate learning rate, balanced between stability and convergence speed.
+ ◽ 5e-5: Higher learning rate, faster convergence but potential instability.
+ ◽ 1e-4: High learning rate, faster convergence but increased risk of instability.
+ ◽ 5e-4: Very high learning rate, fast convergence but higher risk of instability.
+ ◽ 1e-3: Extremely high learning rate, very fast convergence but high risk of instability.

+ The optimal learning rate depends on the model architecture, dataset, and other hyperparameters. + """ + ) + with gr.Tab("Info - Epochs"): + gr.Markdown( + f""" + ### 🟩 Epochs
+ ◽ An epoch represents a single pass through the entire training dataset during the training process.
+ ◽ In each epoch, the model sees and learns from all the training examples once.
+ ◽ The number of epochs determines how many times the model will iterate over the complete training dataset.
+ ◽ Increasing the number of epochs allows the model to learn more from the data and potentially improve its performance.
+ ◽ However, training for too many epochs can lead to overfitting, where the model becomes too specialized to the training data and fails to generalize well to unseen data.
+ ◽ The optimal number of epochs depends on factors such as the complexity of the problem, the size of the dataset, and the model's capacity.
+ ◽ It's common to use techniques like early stopping or validation monitoring to determine the appropriate number of epochs.
+ ◽ Early stopping involves monitoring the model's performance on a validation set and stopping the training when the performance starts to degrade, indicating potential overfitting.
+ ◽ Validation monitoring involves evaluating the model's performance on a separate validation set after each epoch and selecting the model checkpoint that performs best on the validation set.
+ ◽ It's recommended to start with a reasonable number of epochs and monitor the model's performance to determine if more epochs are needed or if early stopping should be applied + """ + ) + with gr.Tab("Info - Max Audio"): + gr.Markdown( + f""" + ### 🟩 Max Permitted Audio Size (in seconds)
+ ◽ The max permitted audio size determines the maximum duration of audio files that can be used as input to the model during training and inference.
+ ◽ It is specified in seconds and represents the longest audio clip that the model can process.
+ ◽ Limiting the audio size helps to control the memory usage and computational requirements of the model.
+ ◽ If the audio files in your dataset vary in length, it's important to consider the max permitted audio size to ensure that all files can be processed effectively.
+ ◽ Audio files longer than the max permitted size will typically be truncated or split into smaller segments before being fed into the model.
+ ◽ The choice of max permitted audio size depends on the specific requirements of your application and the available hardware resources.
+ ◽ Increasing the max permitted audio size allows the model to handle longer audio clips but also increases the memory consumption and computational burden.
+ ◽ If you encounter memory issues or slow processing times, you may need to reduce the max permitted audio size to find a suitable balance.
+ ◽ It's important to consider the nature of your audio data and choose a max permitted audio size that captures the relevant information while being computationally feasible. + """ + ) + + +####################### +#### GRADIO STEP 3 #### +####################### + + with gr.Tab("✅ Step 3 - Testing"): + with gr.Tab("Testing"): + with gr.Row(): + with gr.Column() as col1: + xtts_checkpoint = gr.Dropdown( + [str(file) for file in xtts_checkpoint_files], + label="XTTS checkpoint path (best_model.pth):", + value="", + allow_custom_value=True, + ) + + xtts_config = gr.Dropdown( + [str(file) for file in xtts_config_files], + label="XTTS config path (config.json):", + value="", + allow_custom_value=True, + ) + + xtts_vocab = gr.Dropdown( + [str(file) for file in xtts_vocab_files], + label="XTTS vocab path (vocab.json):", + value="", + allow_custom_value=True, + ) + progress_load = gr.Label( + label="Progress:" + ) + load_btn = gr.Button(value="Step 3 - Load Fine-tuned XTTS model") + + with gr.Column() as col2: + with gr.Row(): + # Gather the voice files + available_speaker_audios = get_available_voices() + + # Create Dropdown for speaker reference audio + speaker_reference_audio = gr.Dropdown( + available_speaker_audios, + label="Speaker reference audio:", + value="", # Set the default value if needed + allow_custom_value=True, # Allow custom values + scale=1, + ) + + with gr.Row(): + tts_language = gr.Dropdown( + label="Language", + value="en", + choices=[ + "en", + "es", + "fr", + "de", + "it", + "pt", + "pl", + "tr", + "ru", + "nl", + "cs", + "ar", + "zh", + "hu", + "ko", + "ja", + ] + ) + # Create refresh button + refresh_button = create_refresh_button( + [xtts_checkpoint, xtts_config, xtts_vocab, speaker_reference_audio], + [ + lambda: {"choices": find_best_models(main_directory), "value": ""}, + lambda: {"choices": find_jsons(main_directory, "config.json"), "value": ""}, + lambda: {"choices": find_jsons(main_directory, "vocab.json"), "value": ""}, + lambda: {"choices": get_available_voices(), "value": ""}, + ], + elem_class="refresh-button-class" + ) + tts_text = gr.Textbox( + label="Input Text:", + value="I've just fine tuned a text to speech language model and this is how it sounds. If it doesn't sound right, I will try a different Speaker Reference Audio file.", + lines=5, + ) + tts_btn = gr.Button(value="Step 4 - Inference (Generate TTS)") + + with gr.Row(): + progress_gen = gr.Label( + label="Progress:" + ) + with gr.Row(): + tts_output_audio = gr.Audio(label="TTS Generated Speech.") + reference_audio = gr.Audio(label="Speaker Reference Audio Sample.") + + with gr.Tab("Testing Instructions"): + gr.Markdown( + f""" + ### ✅ Testing
+ ### 🟥 Important Note + ◽ If you dont believe the dropdown lists are populated correctly, you can use the "Refresh Dropdowns" button to search the finetuning folder path and update the lists. You can also manually edit the path to the listed files.
+ ◽ You can use any config.json or vocab.json, as long as they match the model version you trained on. Only the XTTS checkpoint matters.
+ ◽ Upon successful processing of your speech data, expect to find multiple speaker reference files if your input consisted of over 3 minutes of speech.
+ ◽ Audio clips shorter than 7 seconds are automatically excluded from the reference list, as they generally do not provide sufficient length for effective TTS generation.
+ ◽ Should you find a lower number of reference files than anticipated, it may be due to the initial segmentation performed by Whisper (Step 1), which can occasionally result in clips that are too brief for our criteria or indeed too long. In such cases, consider manually segmenting your audio to give Whisper a better chance at generating training data and repeating the process.
+ ### 🟦 What you need to do + ◽ **Click** the **Refresh Dropdowns** button to correctly populate the **Speaker Reference Audio** with all the available WAV samples.
+ ◽ The model is now trained and you are at the testing stage. Hopefully all the dropdowns should be pre-populated now.
+ ◽ You need to Load Fine-tuned XTTS model and then select your Speaker Reference Audio. You can choose various Speaker Reference Audios to see which works best.
+ ◽ All the Speaker Reference Audios in the dropdown are ones that are 8 seconds long or more. You can use one of these later for your voice sample in All Talk, so remember the one you like.
+ ◽ Some Speaker Reference Audios are a bit long and may cause issues generating the TTS, so try different ones.
+ ◽ Once you are happy, move to the next tab for instruction on cleaning out the training data and how to copy your newly trained model over your current model.
+ ### 🟨 What this step is doing + ◽ Its loading the finetuned model into memory, loading a voice sample and generating TTS, so that you can test out how well fine tuning worked. + """ + ) + + + with gr.Tab("🔜 What to do next"): + gr.Markdown( + f""" + ### 🔜 What to do next
+ ### 🟦 What you need to do + You have a few options below:
+ ◽ **Compact & move model:** This will compress the raw finetuned model and move it, along with any large enough wav files created to the folder name of your choice.
+ ◽ **Delete Generated Training data:** This will delete the finetuned model and any other training data generated during this process. You will more than likely want to do this after you have Compacted & moved the model.
+ ◽ **Delete original voice samples:** This will delete the voice samples you placed in put-voice-samples-in-here (if you no longer have a use for them).
+ ◽ **Note:** Your sample wav files will be copied into the model folder. You will need to **MANUALLY** copy/move the WAV file(s) you want to use to the AllTalk **voices** folder, to make them available within the interface.
+ ### 🟨 Clearing up disk space + ◽ If you are not going to train anything again, you can delete the whisper model from inside of your huggingface cache (3GB approx)
+         ◽ **Linux:** ~/.cache/huggingface/hub/(folder-here)
+         ◽ **Windows:** C:\users(your-username)\.cache\huggingface\hub\(folder-here).
+ """ + ) + final_progress_data = gr.Label( + label="Progress:" + ) + with gr.Row(): + xtts_checkpoint_copy = gr.Dropdown( + [str(file) for file in xtts_checkpoint_files], + label="XTTS checkpoint path (Select the model you want to copy over):", + value="", + allow_custom_value=True, + scale=2, + ) + # Create refresh button + refresh_button = create_refresh_button( + [xtts_checkpoint_copy,], + [ + lambda: {"choices": find_best_models(main_directory), "value": ""}, + ], + elem_class="refresh-button-class") + with gr.Row(): + overwrite_existing = gr.Dropdown(value="Do not overwrite existing files", choices=["Overwrite existing files", "Do not overwrite existing files"], label="File Overwrite Options",) + folder_path = gr.Textbox(label="Enter a new folder name (will be sub the models folder)", lines=1, value="mycustomfolder") + compact_custom_btn = gr.Button(value="Compact and move model to a folder name of your choosing") + with gr.Row(): + gr.Textbox(value="This will DELETE your training data and the raw finetuned model from /finetune/tmp-trn", scale=2, show_label=False, interactive=False) + delete_training_btn = gr.Button(value="Delete generated training data") + with gr.Row(): + gr.Textbox(value="This will DELETE your original voice samples from /finetune/put-voice-samples-in-here/.", scale=2, show_label=False, interactive=False) + delete_voicesamples_btn = gr.Button(value="Delete original voice samples") + + prompt_compute_btn.click( + fn=preprocess_dataset, + inputs=[ + lang, + whisper_model, + out_path, + max_sample_length, + eval_split_number, + speaker_name_input, + ], + outputs=[ + progress_data, + train_csv, + eval_csv, + ], + ) + + + train_btn.click( + fn=train_model, + inputs=[ + lang, + train_csv, + eval_csv, + learning_rates, + model_to_train, + num_epochs, + batch_size, + grad_acumm, + out_path, + max_audio_length, + ], + outputs=[progress_train, xtts_config, xtts_vocab, xtts_checkpoint, speaker_reference_audio], + ) + + load_btn.click( + fn=load_model, + inputs=[ + xtts_checkpoint, + xtts_config, + xtts_vocab + ], + outputs=[progress_load], + ) + + tts_btn.click( + fn=run_tts, + inputs=[ + tts_language, + tts_text, + speaker_reference_audio, + ], + outputs=[progress_gen, tts_output_audio, reference_audio], + ) + compact_custom_btn.click( + fn=compact_custom_model, + inputs=[xtts_checkpoint_copy, folder_path, overwrite_existing], + outputs=[final_progress_data], + ) + delete_training_btn.click( + fn=delete_training_data, + outputs=[final_progress_data], + ) + delete_voicesamples_btn.click( + fn=delete_voice_sample_contents, + outputs=[final_progress_data], + ) + model_to_train.change(basemodel_or_finetunedmodel_choice, model_to_train, None) + + demo.queue().launch( + show_api=False, + inbrowser=True, + share=False, + debug=False, + server_port=7052, + server_name="127.0.0.1", + ) + diff --git a/script.py b/script.py new file mode 100644 index 0000000..1e312c0 --- /dev/null +++ b/script.py @@ -0,0 +1,1895 @@ +import os +import re +import sys +import json +import time +import shutil +import random +import atexit +import signal +import logging +import requests +import platform +import subprocess +import threading +import soundfile as sf +from pathlib import Path +from datetime import datetime, timedelta +from requests.exceptions import RequestException, ConnectionError +########################################################################### +# START-UP # Silence Character Normaliser when it checks the Ready Status # +########################################################################### +import warnings +warnings.filterwarnings('ignore', message='Trying to detect encoding from a tiny portion') +########################################### +# START-UP # AllTalk allowed startup time # +########################################### +startup_wait_time = 240 + +# You can change the above setting to a larger number to allow AllTAlk more time to start up. +# The default setting is 240 seconds (4 minutes). If its taking longer though, you may have a +# Very old system or system issue. + +############################################## +# START-UP # Load confignew.json into params # +############################################## +this_dir = Path(__file__).parent.resolve() + +def load_config(file_path): + with open(file_path, "r") as config_file: + config = json.load(config_file) + return config + +config_file_path = this_dir / "confignew.json" +params = load_config(config_file_path) +branding = params['branding'] +github_site = "erew123" +github_repository = "alltalk_tts" +current_folder = os.path.basename(os.getcwd()) +output_folder = this_dir / params["output_folder"] +delete_output_wavs_setting = params["delete_output_wavs"] +gradio_enabled = params["gradio_interface"] +script_path = this_dir / "tts_server.py" +tunnel_url = None +running_on_google_colab = False + +############################################ +# START-UP # Display initial splash screen # +############################################ +print(f"[{branding}TTS]\033[94m _ _ _ \033[1;35m_____ _ _ \033[0m _____ _____ ____ ") +print(f"[{branding}TTS]\033[94m / \ | | |\033[1;35m_ _|_ _| | | __ \033[0m |_ _|_ _/ ___| ") +print(f"[{branding}TTS]\033[94m / _ \ | | |\033[1;35m | |/ _` | | |/ / \033[0m | | | | \___ \ ") +print(f"[{branding}TTS]\033[94m / ___ \| | |\033[1;35m | | (_| | | < \033[0m | | | | ___) |") +print(f"[{branding}TTS]\033[94m /_/ \_\_|_|\033[1;35m |_|\__,_|_|_|\_\ \033[0m |_| |_| |____/ ") +print(f"[{branding}TTS]") + +###################################################### +# START-UP # Check if this is a first time start-up # +###################################################### +def run_firsttime_script(): + try: + subprocess.run(['python', 'system/config/firstrun.py'], check=True) + except Exception as e: + print(f"An unexpected error occurred: {e}") + +# Call the function to run the startup script +run_firsttime_script() + +########################################################### +# START-UP # Check for updates needed to the config file # +########################################################### +update_config_path = this_dir / "system" / "config" / "at_configupdate.json" +downgrade_config_path = this_dir / "system" / "config" / "at_configdowngrade.json" +def changes_needed(main_config, update_config, downgrade_config): + """Check if there are any changes to be made to the main configuration.""" + for key in downgrade_config.keys(): + if key in main_config: + return True + for key, value in update_config.items(): + if key not in main_config: + return True + return False + +def update_config(config_file_path, update_config_path, downgrade_config_path): + try: + with open(config_file_path, 'r') as file: + main_config = json.load(file) + with open(update_config_path, 'r') as file: + update_config = json.load(file) + with open(downgrade_config_path, 'r') as file: + downgrade_config = json.load(file) + + # Determine if changes are needed + if changes_needed(main_config, update_config, downgrade_config): + # Backup with timestamp to avoid overwriting + timestamp = datetime.now().strftime("%Y%m%d%H%M%S") + backup_path = config_file_path.with_suffix(f".{timestamp}.bak") + logging.info(f"Creating backup of the main config to {backup_path}") + shutil.copy(config_file_path, backup_path) + + # Proceed with updates and downgrades + for key, value in update_config.items(): + if key not in main_config: + main_config[key] = value + for key in downgrade_config.keys(): + if key in main_config: + del main_config[key] + + # Save the updated configuration + with open(config_file_path, 'w') as file: + json.dump(main_config, file, indent=4) + + print(f"[{branding}TTS] \033[92mConfig file update: \033[91mUpdates applied\033[0m") + else: + print(f"[{branding}TTS] \033[92mConfig file update: \033[93mNo Updates required\033[0m") + + except Exception as e: + print(f"[{branding}TTS] \033[92mConfig file update: \033[91mError updating\033[0m") + +# Update the configuration +update_config(config_file_path, update_config_path, downgrade_config_path) + +########################################################################################### +# START-UP # Check for updates needed to the TTS engines list when a new engine is added # +########################################################################################### +tts_engines_path = this_dir / "system" / "tts_engines" / "tts_engines.json" +new_engines_path = this_dir / "system" / "tts_engines" / "new_engines.json" + +# Load the JSON files +with open(tts_engines_path, 'r') as f: + tts_engines_data = json.load(f) + +with open(new_engines_path, 'r') as f: + new_engines_data = json.load(f) + +# Extract the list of current and new engines +current_engines = {engine['name'] for engine in tts_engines_data['engines_available']} +new_engines = new_engines_data['engines_available'] + +# Iterate over the new engines to see if they need to be added +for engine in new_engines: + engine_name = engine['name'] + if engine_name not in current_engines: + # Check if the directory for the new engine exists + engine_dir = this_dir / "system" / "tts_engines" / engine_name + if engine_dir.is_dir(): + # Add the new engine to the list + tts_engines_data['engines_available'].append(engine) + print(f"[{branding}TTS] \033[92mNew TTS Engines : \033[91mAdded {engine_name}\033[0m") + +# Save the updated tts_engines.json +with open(tts_engines_path, 'w') as f: + json.dump(tts_engines_data, f, indent=4) + +#################################################### +# START-UP # Re-load from confignew.json to params # +#################################################### +params = load_config(config_file_path) + +############################################################################# +# START-UP # Check current folder name has dashes '-' in it and error if so # +############################################################################# +if "-" in current_folder: + print(f"[{branding}TTS]") + print(f"[{branding}TTS] \033[91mWarning\033[0m The current folder name contains a dash ('\033[93m-\033[0m') and this causes errors/issues. Please ensure") + print(f"[{branding}TTS] \033[91mWarning\033[0m the folder name does not have a dash e.g. rename ('\033[93malltalk_tts-main\033[0m') to ('\033[93malltalk_tts\033[0m').") + print(f"[{branding}TTS]") + print(f"[{branding}TTS] \033[92mCurrent folder:\033[0m {current_folder}") + sys.exit(1) + +############################################################################### +# START-UP # Test if we are running within Text-gen-webui or as a Stanadalone # +############################################################################### +try: + import gradio as gr + from modules import chat, shared, ui_chat + from modules.logging_colors import logger + from modules.ui import create_refresh_button + from modules.utils import gradio + print(f"[{branding}TTS] \033[92mStart-up Mode : \033[93mText-gen-webui mode\033[0m") + running_in_standalone = False +except ModuleNotFoundError: + running_in_standalone = True + print(f"[{branding}TTS] \033[92mStart-up Mode : \033[93mStandalone mode\033[0m") + +########################################################### +# START-UP # Delete files in outputs folder if configured # +########################################################### +def delete_old_files(folder_path, days_to_keep): + current_time = datetime.now() + print(f"[{branding}TTS] \033[92mWAV file deletion :\033[93m", delete_output_wavs_setting,"\033[0m") + for file_name in os.listdir(folder_path): + file_path = os.path.join(folder_path, file_name) + if os.path.isfile(file_path): + file_creation_time = datetime.fromtimestamp(os.path.getctime(file_path)) + age = current_time - file_creation_time + if age > timedelta(days=days_to_keep): + os.remove(file_path) + +# Check and perform file deletion +if delete_output_wavs_setting.lower() == "disabled": + print(f"[{branding}TTS] \033[92mWAV file deletion :\033[93m Disabled\033[0m") +else: + try: + days_to_keep = int(delete_output_wavs_setting.split()[0]) + delete_old_files(output_folder, days_to_keep) + except ValueError: + print(f"[{branding}TTS] \033[92mWAV file deletion :\033[93m Invalid setting for deleting old wav files. Please use 'Disabled' or 'X Days' format\033[0m") + +##################################################################### +# START-UP # Check Githubs last update and output into splashscreen # +##################################################################### +def ordinal(n): + return "%d%s" % (n, "th" if 4 <= n % 100 <= 20 else {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")) + +def format_datetime(iso_str): + # Parse the ISO 8601 string to datetime + dt = datetime.strptime(iso_str, "%Y-%m-%dT%H:%M:%SZ") + # Format the datetime into a more readable string + return dt.strftime(f"{ordinal(dt.day)} %B %Y at %H:%M") + +def fetch_latest_commit_sha_and_date(owner, repo): + url = f"https://api.github.com/repos/{owner}/{repo}/commits" + try: + response = requests.get(url) + if response.status_code == 200: + latest_commit = response.json()[0] + latest_commit_sha = latest_commit['sha'] + latest_commit_date = latest_commit['commit']['committer']['date'] + return latest_commit_sha, latest_commit_date + else: + print(f"[{branding}TTS] \033[92mGithub updated :\033[91m Failed to fetch the latest commits due to an unexpected response from GitHub") + return None, None + except ConnectionError: + # This block is executed when a connection error occurs + print(f"[{branding}TTS] \033[92mGithub updated :\033[91m Could not reach GitHub to check for updates\033[0m") + return None, None + +def read_or_initialize_sha(file_path): + if os.path.exists(file_path): + with open(file_path, 'r') as file: + data = json.load(file) + return data.get("last_known_commit_sha") + else: + # File doesn't exist, fetch the latest SHA and create the file + latest_commit_sha = fetch_latest_commit_sha_and_date(github_site, github_repository) + if latest_commit_sha: + with open(file_path, 'w') as file: + json.dump({"last_known_commit_sha": latest_commit_sha}, file) + return latest_commit_sha + else: + # Handle the case where GitHub couldn't be reached + return None + +def update_sha_file(file_path, new_sha): + with open(file_path, 'w') as file: + json.dump({"last_known_commit_sha": new_sha}, file) + +# Define the file path based on your directory structure +sha_file_path = this_dir / "system" / "config" / "at_github_sha.json" + +# Read or initialize the SHA (adjusted for handling both SHA and date) +last_known_commit_sha = read_or_initialize_sha(sha_file_path) # Assuming adjustment for date + +# Assuming you have fetched the latest commit SHA and date +latest_commit_sha, latest_commit_date = fetch_latest_commit_sha_and_date(github_site, github_repository) + +formatted_date = format_datetime(latest_commit_date) if latest_commit_date else "an unknown date" + +if latest_commit_sha and latest_commit_sha != last_known_commit_sha: + #print(f"There's an update available for alltalk_tts.") + print(f"[{branding}TTS] \033[92mGithub updated :\033[93m {formatted_date}\033[0m") + # Update the file with the new SHA + update_sha_file(sha_file_path, latest_commit_sha) +elif latest_commit_sha == last_known_commit_sha: + #print(f"Your alltalk_tts software is up to date.") + print(f"[{branding}TTS] \033[92mGithub updated :\033[93m {formatted_date}\033[0m") + +################################################## +# START-UP # Configure the subprocess hanlder #### +################################################## +def signal_handler(sig, frame): + print(f"[{branding}Shutdown] \033[94mReceived Ctrl+C, terminating subprocess. Kill your Python processes if this fails to exit.\033[92m") + if process.poll() is None: + process.terminate() + process.wait() # Wait for the subprocess to finish + sys.exit(0) + +##################################################################### +# START-UP # Start the Subprocess and Check for Google Colab/Docker # +##################################################################### +try: + import google.colab + running_on_google_colab = True + #if deepspeed_installed: + #params["deepspeed_activate"] = True + with open('/content/alltalk_tts/googlecolab.json', 'r') as f: + data = json.load(f) + google_ip_address = data.get('google_ip_address', tunnel_url) +except FileNotFoundError: + print("Could not find IP address") + google_ip_address = tunnel_url +except ImportError: + pass + +# Attach the signal handler to the SIGINT signal (Ctrl+C) +signal.signal(signal.SIGINT, signal_handler) + +# Check if we're running in docker +if os.path.isfile("/.dockerenv") and 'google.colab' not in sys.modules: + print(f"[{branding}TTS] \033[94mRunning in Docker. Please wait.\033[0m") +else: + # Start the subprocess + process = subprocess.Popen(["python", script_path]) + + # Check if the subprocess has started successfully + if process.poll() is None: + if running_on_google_colab: + print(f"[{branding}TTS]") + print(f"[{branding}TTS] \033[94m{branding}Google Colab Address:\033[00m",f"\033[92m{google_ip_address}:443\033[00m") + print(f"[{branding}TTS]") + else: + print(f"[{branding}TTS] \033[91mWarning\033[0m TTS Subprocess Webserver failing to start process") + print(f"[{branding}TTS] \033[91mWarning\033[0m It could be that you have something on port:",params["port_number"],) + print(f"[{branding}TTS] \033[91mWarning\033[0m Or you have not started in a Python environement with all the necesssary bits installed") + print(f"[{branding}TTS] \033[91mWarning\033[0m Check you are starting Text-generation-webui with either the start_xxxxx file or the Python environment with cmd_xxxxx file.") + print(f"[{branding}TTS] \033[91mWarning\033[0m xxxxx is the type of OS you are on e.g. windows, linux or mac.") + print(f"[{branding}TTS] \033[91mWarning\033[0m Alternatively, you could check no other Python processes are running that shouldnt be e.g. Restart your computer is the simple way.") + # Cleanly kill off this script, but allow text-generation-webui to keep running, albeit without this alltalk_tts + sys.exit(1) + + timeout = startup_wait_time # Gather timeout setting from startup_wait_time + initial_delay = 5 # Initial delay before starting the check loop + warning_delay = 60 # Delay before displaying warnings + + # Introduce a delay before starting the check loop + time.sleep(initial_delay) + + start_time = time.time() + warning_displayed = False + + url = f"http://localhost:{params['api_def']['api_port_number']}/api/ready" + while time.time() - start_time < timeout: + try: + response = requests.get(url) + if response.status_code == 200 and response.text == "Ready": + break + except requests.RequestException as e: + # Log the exception if needed + pass + + if not warning_displayed and time.time() - start_time >= warning_delay: + print(f"[{branding}TTS] \033[91mWarning\033[0m TTS Engine has NOT started up yet. Will keep trying for {timeout} seconds maximum. Please wait.") + print(f"[{branding}TTS] \033[91mWarning\033[0m Mechanical hard drives and a slow PCI BUS are examples of things that can affect load times.") + print(f"[{branding}TTS] \033[91mWarning\033[0m Some TTS engines index their AI TTS models on loading, which can be slow on CPU or old systems.") + print(f"[{branding}TTS] \033[91mWarning\033[0m Using one of the other TTS engines on slower systems can help ease this issue.") + warning_displayed = True + + time.sleep(1) + else: + print(f"[{branding}TTS]") + print(f"[{branding}TTS] Startup timed out. Full help available here \033[92mhttps://github.com/erew123/alltalk_tts#-help-with-problems\033[0m") + print(f"[{branding}TTS] On older systems, you may wish to open and edit \033[94mscript.py\033[0m with a text editor and change the") + print(f"[{branding}TTS] \033[94mstartup_wait_time = 240\033[0m setting to something like \033[94mstartup_wait_time = 460\033[0m as this will allow") + print(f"[{branding}TTS] AllTalk more time (6 mins) to try load the model into your VRAM. Otherwise, please visit the GitHub for") + print(f"[{branding}TTS] a list of other possible troubleshooting options.") + # Cleanly kill off this script, but allow text-generation-webui to keep running, albeit without this alltalk_tts + sys.exit(1) + + print(f"[{branding}TTS]") + print(f"[{branding}TTS] \033[94mAPI Address :\033[00m",f"\033[92m127.0.0.1:{params['api_def']['api_port_number']}\033[00m") + print(f"[{branding}TTS] \033[94mGradio Light:\033[00m",f"\033[92mhttp://127.0.0.1:{params['gradio_port_number']}\033[00m") + print(f"[{branding}TTS] \033[94mGradio Dark :\033[00m \033[92mhttp://127.0.0.1:{params['gradio_port_number']}?__theme=dark\033[00m") + print(f"[{branding}TTS]") + + +######################################### +# START-UP # Espeak-ng check on Windows # +######################################### +def check_espeak_ng(): + if platform.system() == "Windows": + try: + result = subprocess.run(['espeak-ng', '--version'], capture_output=True, text=True, check=True) + return + except (subprocess.CalledProcessError, FileNotFoundError) as e: + # If the command returns an error, print the error message + print(f"[{branding}TTS]") + print(f"[{branding}TTS] Espeak-ng for Windows\033[91m WAS NOT FOUND. \033[0mYou can install\033[0m") + print(f"[{branding}TTS] \033[0mit from this location \033[93m\\alltalk_tts\\system\espeak-ng\\\033[0m") + print(f"[{branding}TTS] Then close this command prompt window and open a new") + print(f"[{branding}TTS] command prompt, before starting {branding}again.") + elif platform.system() == "Darwin": # macOS + try: + result = subprocess.run(['espeak-ng', '--version'], capture_output=True, text=True, check=True) + return + except FileNotFoundError: + print(f"[{branding}TTS]") + print(f"[{branding}TTS] Espeak-ng for macOS\033[91m WAS NOT FOUND. \033[0mYou can install\033[0m") + print(f"[{branding}TTS] \033[0mit using Homebrew: \033[93mbrew install espeak-ng\033[0m") + else: # Linux + try: + result = subprocess.run(['espeak-ng', '--version'], capture_output=True, text=True, check=True) + return + except FileNotFoundError: + print(f"[{branding}TTS]") + print(f"[{branding}TTS] Espeak-ng for Linux\033[91m WAS NOT FOUND. \033[0mYou can install\033[0m") + print(f"[{branding}TTS] \033[0mit using your package manager, e.g., \033[93mapt-get install espeak-ng\033[0m") + print(f"[{branding}TTS]") + print(f"[{branding}TTS]") + +check_espeak_ng() +#################################### +# START-UP # Subprecess management # +#################################### + +def start_subprocess(): + global process + if process is None or process.poll() is not None: + process = subprocess.Popen(["python", script_path]) + return "Subprocess started." + else: + return "Subprocess is already running." + +def stop_subprocess(): + global process + if process is not None: + process.terminate() + process.wait() + process = None + return "Subprocess stopped." + else: + return "Subprocess is not running." + +def restart_subprocess(): + stop_subprocess() + print(f"[{branding}ENG]") + print(f"[{branding}ENG] \033[94mSwapping TTS Engine. Please wait.\033[00m") + print(f"[{branding}ENG]") + return start_subprocess() + +def check_subprocess_status(): + global process + if process is None or process.poll() is not None: + return "Subprocess is not running." + else: + return "Subprocess is running." + + +################################################################### +# START-UP # Register the termination code to be executed at exit # +################################################################### +atexit.register(lambda: process.terminate() if process.poll() is None else None) + +############################################################################################################# +# _____ _ _ _ ____ _ _ # +# |_ _|____ _| |_ __ _ ___ _ __ __ _____| |__ _ _(_) / ___|_ __ __ _ __| (_) ___ # +# | |/ _ \ \/ / __|____ / _` |/ _ \ '_ \ ____\ \ /\ / / _ \ '_ \| | | | | | | _| '__/ _` |/ _` | |/ _ \ # +# | | __/> <| ||_____| (_| | __/ | | |_____\ V V / __/ |_) | |_| | | | |_| | | | (_| | (_| | | (_) | # +# |_|\___/_/\_\\__| \__, |\___|_| |_| \_/\_/ \___|_.__/ \__,_|_| \____|_| \__,_|\__,_|_|\___/ # +# |___/ # +############################################################################################################# +########################## +# Setup global variables # +########################## +# Load the language options from languages +with open(os.path.join(this_dir, "system", "config", "languages.json"), "r") as f: + languages_list = json.load(f) +current_model_loaded = None +#deepspeed_installed = True +tgwui_lovram = False +tgwui_deepspeed = False +# Create a global lock for tracking TTS generation occuring +process_lock = threading.Lock() +# Pull the values for IP address, port and protocol for communication with the AllTalk renote server +alltalk_protocol = "http://" +alltalk_ip_port = "127.0.0.1:7851" +# Pull the connection timeout value for communication requests with the AllTalk remote server +connection_timeout = 10 +# Create a few base global variables that are required +models_available = None # Gets populated with the list of all models/engines available on the AllTalk Server +at_settings = None # Gets populated with the list of all settings currently set on the AllTalk Server +current_model_loaded = None # Gets populated with the name of the current loaded TTS engine/model on the AllTalk Server +# Used to detect if a model is loaded in to AllTalk server to block TTS genereation if needed. +tts_model_loaded = None + +################################################# +# Pull all the settings from the AllTalk Server # +################################################# +def get_alltalk_settings(): + global current_model_loaded, models_available, engines_available, current_engine_loaded + voices_url = f"{alltalk_protocol}{alltalk_ip_port}/api/voices" + rvcvoices_url = f"{alltalk_protocol}{alltalk_ip_port}/api/rvcvoices" + settings_url = f"{alltalk_protocol}{alltalk_ip_port}/api/currentsettings" + + try: + voices_response = requests.get(voices_url, timeout=connection_timeout) + rvcvoices_response = requests.get(rvcvoices_url, timeout=connection_timeout) + settings_response = requests.get(settings_url, timeout=connection_timeout) + + if voices_response.status_code == 200 and settings_response.status_code == 200: + voices_data = voices_response.json() + rvcvoices_data = rvcvoices_response.json() + settings_data = settings_response.json() + + engines_available = sorted(settings_data["engines_available"]) + current_engine_loaded = settings_data["current_engine_loaded"] + models_available = sorted([model["name"] for model in settings_data["models_available"]]) + current_model_loaded = settings_data["current_model_loaded"] + + return { + "voices": sorted(voices_data["voices"], key=lambda s: s.lower()), + "rvcvoices": (rvcvoices_data["rvcvoices"]), + "engines_available": engines_available, + "current_engine_loaded": current_engine_loaded, + "models_available": models_available, + "current_model_loaded": current_model_loaded, + "manufacturer_name": settings_data.get("manufacturer_name", ""), + "deepspeed_capable": settings_data.get("deepspeed_capable", False), + "deepspeed_available": settings_data.get("deepspeed_available", False), + "deepspeed_enabled": settings_data.get("deepspeed_enabled", False), + "generationspeed_capable": settings_data.get("generationspeed_capable", False), + "generationspeed_set": settings_data.get("generationspeed_set", 1.0), + "lowvram_capable": settings_data.get("lowvram_capable", False), + "lowvram_enabled": settings_data.get("lowvram_enabled", False), + "pitch_capable": settings_data.get("pitch_capable", False), + "pitch_set": settings_data.get("pitch_enabled", False), + "repetitionpenalty_capable": settings_data.get("repetitionpenalty_capable", False), + "repetitionpenalty_set": settings_data.get("repetitionpenalty_set", 10.0), + "streaming_capable": settings_data.get("streaming_capable", False), + "temperature_capable": settings_data.get("temperature_capable", False), + "temperature_set": settings_data.get("temperature_set", 0.75), + "ttsengines_installed": settings_data.get("ttsengines_installed", False), + "languages_capable": settings_data.get("languages_capable", False), + "multivoice_capable": settings_data.get("multivoice_capable", False), + "multimodel_capable": settings_data.get("multimodel_capable", False), + } + else: + print(f"[{branding}Server] \033[91mWarning\033[0m Failed to retrieve {branding}settings from API.") + if voices_response.status_code != 200: + print(f"[{branding}Server] \033[91mWarning\033[0m Failed to retrieve voices from API. Status code:\n{voices_response.status_code}") + if settings_response.status_code != 200: + print(f"[{branding}Server] \033[91mWarning\033[0m Failed to retrieve current settings from API. Status code:\n{settings_response.status_code}") + return { + "voices": ["Please Refresh Settings"], + "rvcvoices": ["Please Refresh Settings"], + "engines_available": ["Please Refresh Settings"], + "current_engine_loaded": ["Please Refresh Settings"], + "models_available": ["Please Refresh Settings"], + "current_model_loaded": "Please Refresh Settings", + "manufacturer_name": "", + "deepspeed_capable": False, + "deepspeed_available": False, + "deepspeed_enabled": False, + "generationspeed_capable": False, + "generationspeed_set": 1.0, + "lowvram_capable": False, + "lowvram_enabled": False, + "pitch_capable": False, + "pitch_set": 0, + "repetitionpenalty_capable": False, + "repetitionpenalty_set": 10.0, + "streaming_capable": False, + "temperature_capable": False, + "temperature_set": 0.75, + "ttsengines_installed": False, + "languages_capable": False, + "multivoice_capable": False, + "multimodel_capable": False, + } + except (RequestException, ConnectionError) as e: + print(f"[{branding}Server] \033[91mWarning\033[0m Unable to connect to the {branding}server. Status code:\n{str(e)}") + return { + "voices": ["Please Refresh Settings"], + "rvcvoices": ["Please Refresh Settings"], + "engines_available": ["Please Refresh Settings"], + "current_engine_loaded": ["Please Refresh Settings"], + "models_available": ["Please Refresh Settings"], + "current_model_loaded": "Please Refresh Settings", + "manufacturer_name": "", + "deepspeed_capable": False, + "deepspeed_available": False, + "deepspeed_enabled": False, + "generationspeed_capable": False, + "generationspeed_set": 1.0, + "lowvram_capable": False, + "lowvram_enabled": False, + "pitch_capable": False, + "pitch_set": 0, + "repetitionpenalty_capable": False, + "repetitionpenalty_set": 10.0, + "streaming_capable": False, + "temperature_capable": False, + "temperature_set": 0.75, + "ttsengines_installed": False, + "languages_capable": False, + "multivoice_capable": False, + "multimodel_capable": False, + } + +at_settings = get_alltalk_settings() # Pull all the current settings from the AllTalk server, if its online. + +############################# +#### TTS STOP GENERATION #### +############################# +def stop_generate_tts(): + api_url = f"{alltalk_protocol}{alltalk_ip_port}/api/stop-generation" + try: + response = requests.put(api_url, timeout=connection_timeout) + if response.status_code == 200: + return response.json()["message"] + else: + print(f"[{branding}TTS] \033[91mWarning\033[0m Failed to stop generation. Status code:\n{response.status_code}") + return {"message": "Failed to stop generation"} + except (RequestException, ConnectionError) as e: + print(f"[{branding}TTS] \033[91mWarning\033[0m Unable to connect to the {branding}server. Status code:\n{str(e)}") + return {"message": "Failed to stop generation"} + +##################################### +#### MODEL LOADING AND UNLOADING #### +##################################### +# MODEL - Swap model based on Gradio selection +def send_reload_request(value_sent): + try: + url = f"{alltalk_protocol}{alltalk_ip_port}/api/reload" + payload = {"tts_method": value_sent} + response = requests.post(url, params=payload) + response.raise_for_status() # Raises an HTTPError for bad responses + except requests.exceptions.RequestException as e: + # Handle the HTTP request error + print(f"[{branding}TTS] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}") + return {"status": "error", "message": str(e)} + +#################################################################################################### +# TGWUI # Saves all settings back to the config file for the REMOTE version of the TGWUI extension # +#################################################################################################### +def tgwui_save_settings(): + # Read the existing settings from the file + try: + with open(config_file_path, "r") as file: + existing_settings = json.load(file) + except FileNotFoundError: + existing_settings = {} + # Update the specific settings you want to change + existing_settings.setdefault("tgwui", {}) + existing_settings["tgwui"]["tgwui_activate_tts"] = params["tgwui"]['tgwui_activate_tts'] + existing_settings["tgwui"]["tgwui_autoplay_tts"] = params["tgwui"]['tgwui_autoplay_tts'] + existing_settings["tgwui"]["tgwui_narrator_enabled"] = params["tgwui"]['tgwui_narrator_enabled'] + existing_settings["tgwui"]["tgwui_non_quoted_text_is"] = params["tgwui"]["tgwui_non_quoted_text_is"] + existing_settings["tgwui"]["tgwui_language"] = params["tgwui"]['tgwui_language'] + existing_settings["tgwui"]["tgwui_temperature_set"] = params["tgwui"]['tgwui_temperature_set'] + existing_settings["tgwui"]["tgwui_repetitionpenalty_set"] = params["tgwui"]['tgwui_repetitionpenalty_set'] + existing_settings["tgwui"]["tgwui_generationspeed_set"] = params["tgwui"]['tgwui_generationspeed_set'] + existing_settings["tgwui"]["tgwui_pitch_set"] = params["tgwui"]['tgwui_pitch_set'] + existing_settings["tgwui"]["tgwui_narrator_voice"] = params["tgwui"]['tgwui_narrator_voice'] + existing_settings["tgwui"]["tgwui_show_text"] = params["tgwui"]['tgwui_show_text'] + existing_settings["tgwui"]["tgwui_character_voice"] = params["tgwui"]['tgwui_character_voice'] + # Save the updated settings back to the file + with open(config_file_path, "w") as file: + json.dump(existing_settings, file, indent=4) + +############################################# +# Low VRAM change request to enable/disable # +############################################# +# LOW VRAM - Gradio Checkbox handling +def send_lowvram_request(value_sent): + try: + params["tgwui"]["tts_model_loaded"] = False + if value_sent: + audio_path = this_dir / "lowvramenabled.wav" + else: + audio_path = this_dir / "lowvramdisabled.wav" + url = f"{alltalk_protocol}{alltalk_ip_port}/api/lowvramsetting?new_low_vram_value={value_sent}" + headers = {"Content-Type": "application/json"} + response = requests.post(url, headers=headers) + response.raise_for_status() # Raises an HTTPError for bad responses + json_response = response.json() + # Check if the low VRAM request was successful + if json_response.get("status") == "lowvram-success": + # Update any relevant variables or perform other actions on success + params["tgwui"]["tts_model_loaded"] = True + return f'' + except requests.exceptions.RequestException as e: + # Handle the HTTP request error + print(f"[{branding}Server] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}") + return {"status": "error", "message": str(e)} + +################### +#### DeepSpeed #### +################### +def send_deepspeed_request(value_sent): + try: + params["tgwui"]["tts_model_loaded"] = False + if value_sent: + audio_path = this_dir / "deepspeedenabled.wav" + else: + audio_path = this_dir / "deepspeeddisabled.wav" + url = f"{alltalk_protocol}{alltalk_ip_port}/api/deepspeed?new_deepspeed_value={value_sent}" + headers = {"Content-Type": "application/json"} + response = requests.post(url, headers=headers) + response.raise_for_status() # Raises an HTTPError for bad responses + json_response = response.json() + # Check if the deepspeed request was successful + if json_response.get("status") == "deepspeed-success": + # Update any relevant variables or perform other actions on success + params["tgwui"]["tts_model_loaded"] = True + return f'' + except requests.exceptions.RequestException as e: + # Handle the HTTP request error + print(f"[{branding}Server] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}") + return {"status": "error", "message": str(e)} + +################################# +#### TTS STANDARD GENERATION #### +################################# +def send_and_generate(gen_text, gen_character_voice, gen_narrartor_voice, gen_narrator_activated, gen_textnotinisde, gen_repetition, gen_language, gen_filter, gen_speed, gen_pitch, gen_autoplay, gen_autoplay_vol, gen_file_name, gen_temperature, gen_filetimestamp, gen_stream, gen_stopcurrentgen): + api_url = f"{alltalk_protocol}{alltalk_ip_port}/api/tts-generate" + if gen_text == "": + print(f"[{branding}TTS] No Text was sent to generate as TTS") + return None, str("No Text was sent to generate as TTS") + if gen_stopcurrentgen: + stop_generate_tts() # Call for the current generation to stop + tgwui_save_settings() # Save the current TGWUI settings to the config file + if gen_stream == "true": + api_url = f"{alltalk_protocol}{alltalk_ip_port}/api/tts-generate-streaming" + encoded_text = requests.utils.quote(gen_text) + streaming_url = f"{api_url}?text={encoded_text}&voice={gen_character_voice}&language={gen_language}&output_file={gen_file_name}" + return streaming_url, str("TTS Streaming Audio Generated") + else: + data = { + "text_input": gen_text, + "text_filtering": gen_filter, + "character_voice_gen": gen_character_voice, + "narrator_enabled": str(gen_narrator_activated).lower(), + "narrator_voice_gen": gen_narrartor_voice, + "text_not_inside": gen_textnotinisde, + "language": gen_language, + "output_file_name": str(gen_file_name), + "output_file_timestamp": str(gen_filetimestamp).lower(), + "autoplay": str(gen_autoplay).lower(), + "autoplay_volume": str(gen_autoplay_vol), + "speed": str(gen_speed), + "pitch": str(gen_pitch), + "temperature": str(gen_temperature), + "repetition_penalty": str(gen_repetition), + } + print(f"Debug: Generate request param:", data) if params["debug_tts"] else None + try: + response = requests.post(api_url, data=data) + response.raise_for_status() # Raise an exception for 4xx or 5xx status codes + result = response.json() + if gen_autoplay == "true": + return None, str("TTS Audio Generated (Played remotely)") + else: + if params["api_def"]["api_use_legacy_api"]: + return result['output_file_url'], str("TTS Audio Generated") + else: + # Prepend the URL and PORT to the output_file_url + output_file_url = f"{alltalk_protocol}{alltalk_ip_port}{result['output_file_url']}" + return output_file_url, str("TTS Audio Generated") + except (RequestException, ConnectionError) as e: + print(f"[{branding}TTS] \033[91mWarning\033[0m Error occurred during the API request: Status code:\n{str(e)}") + return None, str("Error occurred during the API request") + +#################################################################### +# TGWUI # Text-generation-webui sends text for TTS generation here # +#################################################################### +def output_modifier(string, state): + if not params["tgwui"]["tgwui_activate_tts"]: + return string + # Strip out Images + img_info = "" + cleaned_text, img_info = tgwui_extract_and_remove_images(string) + if cleaned_text is None: + return + # Get current settings + language_code = languages_list.get(params["tgwui"]["tgwui_language"]) + character_voice = params["tgwui"]["tgwui_character_voice"] + narrator_voice = params["tgwui"]["tgwui_narrator_voice"] + narrator_enabled = params["tgwui"]["tgwui_narrator_enabled"] + text_not_inside = params["tgwui"]["tgwui_non_quoted_text_is"] + repetition_policy = params["tgwui"]["tgwui_repetitionpenalty_set"] + text_filtering = "html" + speed = params["tgwui"]["tgwui_generationspeed_set"] + pitch = params["tgwui"]["tgwui_pitch_set"] + autoplay = False # This is never used in TGWUI so a base False setting is sent + autoplay_volume = 0.8 # This is never used in TGWUI so a base False setting is sent + temperature = params["tgwui"]["tgwui_temperature_set"] + # Lock and process TTS request + if process_lock.acquire(blocking=False): + try: + if "character_menu" in state: + output_file = state["character_menu"] + else: + output_file = str("TTSOUT_") + generate_response, status_message = send_and_generate(cleaned_text, character_voice, narrator_voice, narrator_enabled, text_not_inside, repetition_policy, language_code, text_filtering, speed, pitch, autoplay, autoplay_volume, output_file, temperature, True, False, False) + if status_message == "TTS Audio Generated": + # Handle Gradio and playback + autoplay = "autoplay" if params["tgwui"]["tgwui_autoplay_tts"] else "" + string = f'' + if params["tgwui"]["tgwui_show_text"]: + string += tgwui_reinsert_images(cleaned_text, img_info) + shared.processing_message = "*Is typing...*" + return string + else: + print(f"[{branding}Server] \033[91mWarning\033[0m Audio generation failed. Status code:\n{status_message}") + finally: + # Always release the lock, whether an exception occurs or not + process_lock.release() + else: + # The lock is already acquired + print(f"[{branding}Model] \033[91mWarning\033[0m Audio generation is already in progress. Please wait.") + return + +########################################################################################################### +# TGWUI # Strips out images from the TGWUI string if needed and re-inserts the image after TTS generation # +########################################################################################################### +img_pattern = r']*src\s*=\s*["\'][^"\'>]+["\'][^>]*>' + +def tgwui_extract_and_remove_images(text): + """ + Extracts all image data from the text and removes it for clean TTS processing. + Returns the cleaned text and the extracted image data. + """ + img_matches = re.findall(img_pattern, text) + img_info = "\n".join(img_matches) # Store extracted image data + cleaned_text = re.sub(img_pattern, '', text) # Remove images from text + return cleaned_text, img_info + +def tgwui_reinsert_images(text, img_info): + """ + Reinserts the previously extracted image data back into the text. + """ + if img_info: # Check if there are images to reinsert + text += f"\n\n{img_info}" + return text + +################################################################ +# TGWUI # Used to generate a preview voice sample within TGWUI # +################################################################ +def random_sentence(): + with open(this_dir / "system" / "config" / "harvard_sentences.txt") as f: + return random.choice(list(f)).rstrip() + +def voice_preview(string): + if not params["tgwui"]["tgwui_activate_tts"]: + return string + language_code = languages_list.get(params["tgwui"]["tgwui_language"]) + if not string: + string = random_sentence() + generate_response, status_message = send_and_generate(string, params["tgwui"]["tgwui_character_voice"], params["tgwui"]["tgwui_narrator_voice"], params["tgwui"]["tgwui_narrator_enabled"], "character", params["tgwui"]["tgwui_repetitionpenalty_set"], language_code, "standard", params["tgwui"]["tgwui_generationspeed_set"], params["tgwui"]["tgwui_pitch_set"], False, 0.8, "previewvoice", params["tgwui"]["tgwui_temperature_set"], False, False, False) + if status_message == "TTS Audio Generated": + # Handle Gradio and playback + autoplay = "autoplay" if params["tgwui"]["tgwui_autoplay_tts"] else "" + return f'' + else: + # Handle the case where audio generation was not successful + return f"[{branding}Server] Audio generation failed. Status code:\n{status_message}" + +################################################################### +# TGWUI # Used to inform TGWUI that TTS is disabled/not activated # +################################################################### +def state_modifier(state): + if not params["tgwui"]["tgwui_activate_tts"]: + return state + state["stream"] = False + return state + +################################################################### +# TGWUI # Sends message to TGWUI interface during TTS generation # +################################################################### +def input_modifier(string, state): + if not params["tgwui"]["tgwui_activate_tts"]: + return string + shared.processing_message = "*Is recording a voice message...*" + return string + +######################################################################## +# TGWUI # Used to delete historic TTS audios from TGWUI chat interface # +######################################################################## +def remove_tts_from_history(history): + for i, entry in enumerate(history["internal"]): + history["visible"][i] = [history["visible"][i][0], entry[1]] + return history + +def toggle_text_in_history(history): + for i, entry in enumerate(history["visible"]): + visible_reply = entry[1] + if visible_reply.startswith("')[0]}\n\n{reply}", + ] + else: + history["visible"][i] = [ + history["visible"][i][0], + f"{visible_reply.split('')[0]}", + ] + return history + +def history_modifier(history): + # Remove autoplay from the last reply + if len(history["internal"]) > 0: + history["visible"][-1] = [ + history["visible"][-1][0], + history["visible"][-1][1].replace("controls autoplay>", "controls>"), + ] + return history + +########################################################### +# TGWUI # Update IP address and Port from TGWUI interface # +########################################################### +def tgwui_update_alltalk_ip_port(value_sent): + global alltalk_ip_port + alltalk_ip_port = value_sent + +########################################################### +# TGWUI # Update http/https protocol from TGWUI interface # +########################################################### +# Update Protocol from Gradio +def tgwui_update_alltalk_protocol(value_sent): + global alltalk_protocol + alltalk_protocol = value_sent + +########################################################################################################################### +# TGWUI # Debounces model updates when tgwui_update_dropdowns is called, preventing an update from causing a model reload # +########################################################################################################################### +def tgwui_handle_ttsmodel_dropdown_change(model_name): + if not getattr(tgwui_handle_ttsmodel_dropdown_change, "skip_reload", False): + send_reload_request(model_name) + tgwui_handle_ttsmodel_dropdown_change.skip_reload = False + +################################################################################################## +# TGWUI # Pulls the current AllTalk Server settings & updates gradio when Refresh button pressed # +################################################################################################## +def tgwui_update_dropdowns(): + global at_settings + at_settings = get_alltalk_settings() # Pull all the current settings from the AllTalk server, if its online. + current_voices = at_settings["voices"] + current_models_available = sorted(at_settings["models_available"]) + current_model_loaded = at_settings["current_model_loaded"] + current_character_voice = params["tgwui"]["tgwui_character_voice"] + current_narrator_voice = params["tgwui"]["tgwui_narrator_voice"] + current_lowvram_capable = at_settings["lowvram_capable"] + current_lowvram_enabled = at_settings["lowvram_enabled"] + current_temperature_capable = at_settings["temperature_capable"] + current_repetitionpenalty_capable = at_settings["repetitionpenalty_capable"] + current_generationspeed_capable = at_settings["generationspeed_capable"] + current_pitch_capable = at_settings["pitch_capable"] + current_deepspeed_capable = at_settings["deepspeed_capable"] + current_deepspeed_enabled = at_settings["deepspeed_enabled"] + current_non_quoted_text_is = params.get("tgwui")["tgwui_non_quoted_text_is"] # Use the correct parameter path + current_languages_capable = at_settings["languages_capable"] + if at_settings["languages_capable"]: + language_label = "Languages" + else: + language_label = "Model not multi language" + if current_character_voice not in current_voices: + current_character_voice = current_voices[0] if current_voices else "" + if current_narrator_voice not in current_voices: + current_narrator_voice = current_voices[0] if current_voices else "" + + tgwui_handle_ttsmodel_dropdown_change.skip_reload = True # Debounce tgwui_tts_dropdown_gr and stop it sending a model reload when it is updated. + + return ( + gr.Checkbox(interactive=current_lowvram_capable, value=current_lowvram_enabled), + gr.Checkbox(interactive=current_deepspeed_capable, value=current_deepspeed_enabled), + gr.Dropdown(choices=current_voices, value=current_character_voice), + gr.Dropdown(choices=current_voices, value=current_narrator_voice), + gr.Dropdown(choices=current_models_available, value=current_model_loaded), + gr.Dropdown(interactive=current_temperature_capable), + gr.Dropdown(interactive=current_repetitionpenalty_capable), + gr.Dropdown(interactive=current_languages_capable, label=language_label), + gr.Dropdown(interactive=current_generationspeed_capable), + gr.Dropdown(interactive=current_pitch_capable), + gr.Dropdown(value=current_non_quoted_text_is), + ) + +############################################################### +# TGWUI # Gradio interface & layout for Text-generation-webui # +############################################################### +def ui(): + global at_settings + with gr.Accordion(params["branding"] + " TTS (Text-gen-webui Remote)"): + # Activate alltalk_tts, Enable autoplay, Show text + with gr.Row(): + tgwui_activate_tts_gr = gr.Checkbox(value=params["tgwui"]["tgwui_activate_tts"], label="Enable TGWUI TTS") + tgwui_autoplay_gr = gr.Checkbox(value=params["tgwui"]["tgwui_autoplay_tts"], label="Autoplay TTS Generated") + tgwui_show_text_gr = gr.Checkbox(value=params["tgwui"]["tgwui_show_text"], label="Show Text in chat") + + # Low vram enable, Deepspeed enable, Link + with gr.Row(): + tgwui_lowvram_enabled_gr = gr.Checkbox(value=at_settings["lowvram_enabled"] if at_settings["lowvram_capable"] else False, label="Enable Low VRAM Mode", interactive=at_settings["lowvram_capable"]) + tgwui_lowvram_enabled_play_gr = gr.HTML(visible=False) + tgwui_deepspeed_enabled_gr = gr.Checkbox(value=params["tgwui"]["tgwui_deepspeed_enabled"], label="Enable DeepSpeed", interactive=at_settings["deepspeed_capable"],) + tgwui_deepspeed_enabled_play_gr = gr.HTML(visible=False) + tgwui_empty_space_gr = gr.HTML(f"

AllTalk Server & Documentation Link") + + # Model, Language, Character voice + with gr.Row(): + tgwui_tts_dropdown_gr = gr.Dropdown(choices=models_available, label="TTS Engine/Model", value=current_model_loaded,) + tgwui_language_gr = gr.Dropdown(languages_list.keys(), label="Languages" if at_settings["languages_capable"] else "Model not multi language", interactive=at_settings["languages_capable"], value=params["tgwui"]["tgwui_language"]) + tgwui_available_voices_gr = at_settings["voices"] + tgwui_default_voice_gr = params["tgwui"]["tgwui_character_voice"] + if tgwui_default_voice_gr not in tgwui_available_voices_gr: + tgwui_default_voice_gr = tgwui_available_voices_gr[0] if tgwui_available_voices_gr else "" + tgwui_character_voice_gr = gr.Dropdown(choices=tgwui_available_voices_gr, label="Character Voice", value=tgwui_default_voice_gr, allow_custom_value=True,) + + # Narrator + with gr.Row(): + with gr.Row(): + tgwui_narrator_voice_gr = gr.Dropdown(tgwui_available_voices_gr, label="Narrator Voice", allow_custom_value=True, value=params["tgwui"]["tgwui_narrator_voice"],) + tgwui_narrator_enabled_gr = gr.Dropdown(choices=[("Enabled", "true"), ("Disabled", "false"), ("Enabled (Silent)", "silent")], label="Narrator Enable", value="true" if params.get("tgwui_narrator_enabled") == "true" else ("silent" if params.get("tgwui_narrator_enabled") == "silent" else "false")) + tgwui_non_quoted_text_is_gr = gr.Dropdown(choices=[("Character", "character"), ("Narrator", "narrator"), ("Silent", "silent")], label='Narrator unmarked text is', value=params.get("tgwui_non_quoted_text_is", "character")) + + # Temperature, Repetition Penalty, Speed, pitch + with gr.Row(): + tgwui_temperature_set_gr = gr.Slider(minimum=0.05, maximum=1, step=0.05, label="Temperature", value=params["tgwui"]["tgwui_temperature_set"], interactive=at_settings["temperature_capable"]) + tgwui_repetitionpenalty_set_gr = gr.Slider(minimum=0.5, maximum=20, step=0.5, label="Repetition Penalty", value=params["tgwui"]["tgwui_repetitionpenalty_set"], interactive=at_settings["repetitionpenalty_capable"]) + with gr.Row(): + tgwui_generationspeed_set_gr = gr.Slider(minimum=0.30, maximum=2.00, step=0.10, label="TTS Speed", value=params["tgwui"]["tgwui_generationspeed_set"], interactive=at_settings["generationspeed_capable"]) + tgwui_pitch_set_gr = gr.Slider(minimum=-10, maximum=10, step=1, label="Pitch", value=params["tgwui"]["tgwui_pitch_set"], interactive=at_settings["pitch_capable"]) + + # Preview speech + with gr.Row(): + tgwui_preview_text_gr = gr.Text(show_label=False, placeholder="Preview text", elem_id="silero_preview_text", scale=2,) + tgwui_preview_play_gr = gr.Button("Generate Preview", scale=1) + tgwui_preview_audio_gr = gr.HTML(visible=False) + + with gr.Row(): + tgwui_protocol_gr = gr.Dropdown(choices=["http://", "https://"], label="AllTalk Server Protocol", value=alltalk_protocol, interactive=False, visible=False) + tgwui_protocol_gr.change(tgwui_update_alltalk_protocol, tgwui_protocol_gr, None) + tgwui_ip_address_port_gr = gr.Textbox(label="AllTalk Server IP:Port", value=alltalk_ip_port, interactive=False, visible=False) + tgwui_ip_address_port_gr.change(tgwui_update_alltalk_ip_port, tgwui_ip_address_port_gr, None) + tgwui_convert_gr = gr.Button("Remove old TTS audio and leave only message texts") + tgwui_convert_cancel_gr = gr.Button("Cancel", visible=False) + tgwui_convert_confirm_gr = gr.Button("Confirm (cannot be undone)", variant="stop", visible=False) + tgwui_stop_generation_gr = gr.Button("Stop current TTS generation") + tgwui_stop_generation_gr.click(stop_generate_tts, None, None,) + tgwui_refresh_settings_gr = gr.Button("Refresh settings & voices") + tgwui_refresh_settings_gr.click(tgwui_update_dropdowns, None, [tgwui_lowvram_enabled_gr, tgwui_deepspeed_enabled_gr, tgwui_character_voice_gr, tgwui_narrator_voice_gr, tgwui_tts_dropdown_gr, tgwui_temperature_set_gr, tgwui_repetitionpenalty_set_gr, tgwui_language_gr, tgwui_generationspeed_set_gr, tgwui_pitch_set_gr, tgwui_non_quoted_text_is_gr]) + + # Convert history with confirmation + convert_arr = [tgwui_convert_confirm_gr, tgwui_convert_gr, tgwui_convert_cancel_gr] + tgwui_convert_gr.click(lambda: [gr.update(visible=True), gr.update(visible=False), gr.update(visible=True),], None, convert_arr,) + tgwui_convert_confirm_gr.click(lambda: [ gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),], None, convert_arr, + ).then(remove_tts_from_history, gradio("history"), gradio("history") + ).then(chat.save_history, gradio("history", "unique_id", "character_menu", "mode"), None, + ).then(chat.redraw_html, gradio(ui_chat.reload_arr), gradio("display")) + tgwui_convert_cancel_gr.click(lambda: [gr.update(visible=False), gr.update(visible=True), gr.update(visible=False),], None, convert_arr,) + + # Toggle message text in history + tgwui_show_text_gr.change(lambda x: params["tgwui"].update({"tgwui_show_text": x}), tgwui_show_text_gr, None + ).then(toggle_text_in_history, gradio("history"), gradio("history") + ).then(chat.save_history, gradio("history", "unique_id", "character_menu", "mode"), None, + ).then(chat.redraw_html, gradio(ui_chat.reload_arr), gradio("display")) + + # Event functions to update the parameters in the backend + tgwui_activate_tts_gr.change(lambda x: params["tgwui"].update({"tgwui_activate_tts": x}), tgwui_activate_tts_gr, None) + tgwui_autoplay_gr.change(lambda x: params["tgwui"].update({"tgwui_autoplay_tts": x}), tgwui_autoplay_gr, None) + tgwui_lowvram_enabled_gr.change(lambda x: params["tgwui"].update({"tgwui_lowvram_enabled": x}), tgwui_lowvram_enabled_gr, None) + tgwui_lowvram_enabled_gr.change(lambda x: send_lowvram_request(x), tgwui_lowvram_enabled_gr, tgwui_lowvram_enabled_play_gr, None) + + # Trigger the send_reload_request function when the dropdown value changes + #tgwui_tts_dropdown_gr.change(send_reload_request, tgwui_tts_dropdown_gr, None) + tgwui_tts_dropdown_gr.change(tgwui_handle_ttsmodel_dropdown_change, tgwui_tts_dropdown_gr, None) + + tgwui_deepspeed_enabled_gr.change(send_deepspeed_request, tgwui_deepspeed_enabled_gr, tgwui_deepspeed_enabled_play_gr, None) + tgwui_character_voice_gr.change(lambda x: params["tgwui"].update({"tgwui_character_voice": x}), tgwui_character_voice_gr, None) + tgwui_language_gr.change(lambda x: params["tgwui"].update({"tgwui_language": x}), tgwui_language_gr, None) + + # TSS Settings + tgwui_temperature_set_gr.change(lambda x: params["tgwui"].update({"tgwui_temperature_set": x}), tgwui_temperature_set_gr, None) + tgwui_repetitionpenalty_set_gr.change(lambda x: params["tgwui"].update({"tgwui_repetitionpenalty_set": x}), tgwui_repetitionpenalty_set_gr, None) + tgwui_generationspeed_set_gr.change(lambda x: params["tgwui"].update({"tgwui_generationspeed_set": x}), tgwui_generationspeed_set_gr, None) + tgwui_pitch_set_gr.change(lambda x: params["tgwui"].update({"tgwui_pitch_set": x}), tgwui_pitch_set_gr, None) + + # Narrator selection actions + tgwui_narrator_enabled_gr.change(lambda x: params["tgwui"].update({"tgwui_narrator_enabled": x}), tgwui_narrator_enabled_gr, None) + tgwui_non_quoted_text_is_gr.change(lambda x: params["tgwui"].update({"tgwui_non_quoted_text_is": x}), tgwui_non_quoted_text_is_gr, None) + tgwui_narrator_voice_gr.change(lambda x: params["tgwui"].update({"tgwui_narrator_voice": x}), tgwui_narrator_voice_gr, None) + # Play preview + tgwui_preview_text_gr.submit(voice_preview, tgwui_preview_text_gr, tgwui_preview_audio_gr) + tgwui_preview_play_gr.click(voice_preview, tgwui_preview_text_gr, tgwui_preview_audio_gr) + +################################################################## +# _ _ _ _____ _ _ ____ _ _ # +# / \ | | |_ _|_ _| | | __ / ___|_ __ __ _ __| (_) ___ # +# / _ \ | | | | |/ _` | | |/ / | | _| '__/ _` |/ _` | |/ _ \ # +# / ___ \| | | | | (_| | | < | |_| | | | (_| | (_| | | (_) | # +# /_/ \_\_|_| |_|\__,_|_|_|\_\ \____|_| \__,_|\__,_|_|\___/ # +# # +################################################################## + +if gradio_enabled == True: + # Get the directory of the current script + script_dir = Path(__file__).resolve().parent + my_current_url = "null" + at_default_voice_gr = params["tgwui"]["tgwui_character_voice"] + # Load the theme list and select a theme + import system.gradio_pages.themes.loadThemes as loadThemes + # Load the theme list from JSON file + theme_list = loadThemes.get_list() + + # Load the selected theme from configuration + selected_theme = loadThemes.load_json() + + # Check if the script is running in standalone mode + if script_dir.name == "alltalk_tts": + # Running in standalone mode, add the script's directory to the Python module search path + sys.path.insert(0, str(script_dir)) + else: + # Running as part of text-generation-webui, add the parent directory of "alltalk_tts" to the Python module search path + sys.path.insert(0, str(script_dir.parent)) + + import importlib + import gradio as gr + from system.gradio_pages.alltalk_documentation import alltalk_documentation + from system.gradio_pages.alltalk_generation_help import alltalk_generation_help + from system.gradio_pages.alltalk_about import alltalk_about + from system.gradio_pages.alltalk_diskspace import get_disk_interface + from system.gradio_pages.api_documentation import api_documentation + if params["firstrun_splash"]: + from system.gradio_pages.alltalk_welcome import alltalk_welcome + + def get_tts_engines_data(): + global engines_available, engine_loaded, selected_model + tts_engines_file = os.path.join(this_dir, "system", "tts_engines", "tts_engines.json") + with open(tts_engines_file, "r") as f: + tts_engines_data = json.load(f) + engines_available = [engine["name"] for engine in tts_engines_data["engines_available"]] + engine_loaded = tts_engines_data["engine_loaded"] + selected_model = tts_engines_data["selected_model"] + return engines_available, engine_loaded, selected_model + + get_tts_engines_data() + # Dynamically import modules and load JSON data for each available engine + for engine_name in engines_available: + module_name = f"system.tts_engines.{engine_name}.{engine_name}_settings_page" + module = importlib.import_module(module_name) + globals()[f"{engine_name}_at_gradio_settings_page"] = getattr(module, f"{engine_name}_at_gradio_settings_page") + globals()[f"{engine_name}_model_update_settings"] = getattr(module, f"{engine_name}_model_update_settings") + json_file_path = os.path.join(this_dir, "system", "tts_engines", engine_name, "model_settings.json") + with open(json_file_path, "r") as f: + globals()[f"{engine_name}_model_config_data"] = json.load(f) + + def confirm(message): + return gr.Interface.fn("confirmation", f"""""") + + def save_config_data(): + try: + # Save the updated JSON data to confignew.json + with open(os.path.join(this_dir, "confignew.json"), "w") as f: + json.dump(params, f, indent=4) + reload_config() + except Exception as e: + error_message = "Failed to save config data." + print(f"[{branding}TTS] \033[91mError:\033[0m {error_message}") + print(f"[{branding}TTS] \033[91mError Details:\033[0m {str(e)}") + + def reload_config(): + # Send a GET request to the /reload_config endpoint + reload_config_url = f"http://localhost:{params['api_def']['api_port_number']}/api/reload_config" + try: + response = requests.get(reload_config_url) + if response.status_code == 200: + print(f"[{branding}TTS] Reloaded config") + else: + print(f"[{branding}TTS] Failed to reload config file") + except ConnectionError as e: + error_message = "Connection to the server has been lost. Please reload the webpage." + print(f"[{branding}TTS] \033[91mError:\033[0m {error_message}") + except Exception as e: + error_message = "An error occurred while reloading the config file." + print(f"[{branding}TTS] \033[91mError:\033[0m {error_message}") + print(f"[{branding}TTS] \033[91mError Details:\033[0m {str(e)}") + + ########################################################################################## + # Pulls the current AllTalk Server settings & updates gradio when Refresh button pressed # + ########################################################################################## + def at_update_dropdowns(): + global at_settings + at_settings = get_alltalk_settings() # Pull all the current settings from the AllTalk server, if its online. + engines_available = at_settings["engines_available"] + current_engine_loaded = at_settings["current_engine_loaded"] + current_voices = at_settings["voices"] + current_character_voice = params["tgwui"]["tgwui_character_voice"] + current_narrator_voice = params["tgwui"]["tgwui_narrator_voice"] + rvccurrent_voices = at_settings["rvcvoices"] + rvccurrent_character_voice = params["rvc_settings"]["rvc_char_model_file"] + rvccurrent_narrator_voice = params["rvc_settings"]["rvc_narr_model_file"] + current_temperature_capable = at_settings["temperature_capable"] + current_repetitionpenalty_capable = at_settings["repetitionpenalty_capable"] + current_languages_capable = at_settings["languages_capable"] + current_generationspeed_capable = at_settings["generationspeed_capable"] + current_pitch_capable = at_settings["pitch_capable"] + current_languages_capable = at_settings["languages_capable"] + if at_settings["languages_capable"]: + language_label = "Languages" + else: + language_label = "Model not multi language" + if at_settings["streaming_capable"]: + gen_choices = [("Standard", "false"), ("Streaming (Disable Narrator)", "true")] + else: + gen_choices = [("Standard", "false")] + if current_character_voice not in current_voices: + current_character_voice = current_voices[0] if current_voices else "" + if current_narrator_voice not in current_voices: + current_narrator_voice = current_voices[0] if current_voices else "" + if rvccurrent_character_voice not in rvccurrent_voices: + rvccurrent_character_voice = rvccurrent_voices[0] if rvccurrent_voices else "" + if rvccurrent_narrator_voice not in rvccurrent_voices: + rvccurrent_narrator_voice = rvccurrent_voices[0] if rvccurrent_voices else "" + return ( + gr.Dropdown(choices=gen_choices, interactive=True), + gr.Dropdown(choices=current_voices, value=current_character_voice, interactive=True), + gr.Dropdown(choices=rvccurrent_voices, value=rvccurrent_character_voice, interactive=True), + gr.Dropdown(choices=current_voices, value=current_narrator_voice, interactive=True), + gr.Dropdown(choices=rvccurrent_voices, value=rvccurrent_narrator_voice, interactive=True), + gr.Slider(interactive=current_generationspeed_capable), + gr.Slider(interactive=current_pitch_capable), + gr.Slider(interactive=current_temperature_capable), + gr.Slider(interactive=current_repetitionpenalty_capable), + gr.Dropdown(interactive=current_languages_capable, label=language_label), + gr.Dropdown(choices=models_available, value=current_model_loaded), + gr.Dropdown(choices=engines_available, value=current_engine_loaded), + ) + + ###################################################################################### + # Sends request to reload the current TTS engine & set the default TTS engine loaded # + ###################################################################################### + def set_engine_loaded(engine_name): + global engine_loaded, selected_model, tts_engines_data, models_available, at_update_dropdowns + tts_engines_file = os.path.join(this_dir, "system", "tts_engines", "tts_engines.json") + with open(tts_engines_file, "r") as f: + tts_engines_data = json.load(f) + for engine in tts_engines_data["engines_available"]: + if engine["name"] == engine_name: + tts_engines_data["engine_loaded"] = engine_name + tts_engines_data["selected_model"] = engine["selected_model"] + break + with open(tts_engines_file, "w") as f: + json.dump(tts_engines_data, f) + engine_loaded = engine_name + selected_model = tts_engines_data["selected_model"] + # Restart the subprocess + restart_subprocess() + # Wait for the engine to be ready with error handling and retries + max_retries = 40 + retry_delay = 1 # seconds + retries = 0 + while retries < max_retries: + try: + response = requests.get(url) + if response.status_code == 200 and response.text == "Ready": + break + except requests.exceptions.RequestException: + pass + + retries += 1 + if retries == max_retries: + raise Exception("Failed to connect to the TTS engine after multiple retries.") + + time.sleep(retry_delay) + models_available = at_settings["models_available"] + # Update the dropdowns directly + print(f"[{branding}ENG]") + print(f"[{branding}ENG] {branding}Server Ready") + gen_stream_value, gen_char_choices, rvcgen_char_choices, gen_narr_choices, rvcgen_narr_choices, gen_speed_interactive, gen_pitch_interactive, gen_temperature_interactive, gen_repetition_interactive, gen_lang_interactive, model_choices_gr_interactive, engine_choices_interactive = at_update_dropdowns() + return ("TTS Engine changed successfully!", # This is your output message + gen_stream_value, gen_char_choices, rvcgen_char_choices, gen_narr_choices, rvcgen_narr_choices, gen_speed_interactive, gen_pitch_interactive, gen_temperature_interactive, gen_repetition_interactive, gen_lang_interactive, model_choices_gr_interactive, engine_choices_interactive + ) + + ############################### + # Sends voice2rvc request off # + ############################### + def voice2rvc(audio, rvc_voice): + # Save the uploaded or recorded audio to a file + input_tts_path = this_dir / "outputs" / "voice2rvcInput.wav" + if rvc_voice == "Disabled": + print(f"[{branding}ENG] Voice2RVC Convert: No RVC voice was selected") + return + if audio == None: + print(f"[{branding}ENG] Voice2RVC Convert: No recorded audio was provided") + return + if isinstance(audio, tuple): + sample_rate, audio_data = audio + # Save the numpy array as a wav file + sf.write(input_tts_path, audio_data, sample_rate) + else: + # It's a file path + os.rename(audio, input_tts_path) + + # Define the output path for the processed audio + output_rvc_path = this_dir / "outputs" / "voice2rvcOutput.wav" + url = f"{alltalk_protocol}{alltalk_ip_port}/api/voice2rvc" + + # Submit the paths to the API endpoint + response = requests.post(url, data={ + "input_tts_path": str(input_tts_path), + "output_rvc_path": str(output_rvc_path), + "pth_name": rvc_voice + }) + + if response.status_code == 200: + result = response.json() + if result["status"] == "success": + result_path = result["output_path"] + else: + result_path = None + else: + result_path = None + + return result_path + + ################################################################################################## + # Sends request to reload the current TTS engine model & set the default TTS engine model loaded # + ################################################################################################## + def change_model_loaded(engine_name, selected_model): + global at_update_dropdowns + try: + print(f"[{branding}ENG]") + print(f"[{branding}ENG] \033[94mChanging model loaded. Please wait.\033[00m") + print(f"[{branding}ENG]") + url = f"{alltalk_protocol}{alltalk_ip_port}/api/reload" + payload = {"tts_method": selected_model} + response = requests.post(url, params=payload) + response.raise_for_status() # Raises an HTTPError for bad responses + # Update the tts_engines.json file + tts_engines_file = os.path.join(this_dir, "system", "tts_engines", "tts_engines.json") + with open(tts_engines_file, "r") as f: + tts_engines_data = json.load(f) + tts_engines_data["selected_model"] = f"{selected_model}" + for engine in tts_engines_data["engines_available"]: + if engine["name"] == engine_name: + engine["selected_model"] = f"{selected_model}" + break + with open(tts_engines_file, "w") as f: + json.dump(tts_engines_data, f) + print(f"[{branding}ENG]") + print(f"[{branding}ENG] {branding}Server Ready") + gen_stream_value, gen_char_choices, rvcgen_char_choices, gen_narr_choices, rvcgen_narr_choices, gen_speed_interactive, gen_pitch_interactive, gen_temperature_interactive, gen_repetition_interactive, gen_lang_interactive, model_choices_gr_interactive, engine_choices_interactive = at_update_dropdowns() + return ("TTS Model changed successfully!", # This is your output message + gen_stream_value, gen_char_choices, rvcgen_char_choices, gen_narr_choices, rvcgen_narr_choices, gen_speed_interactive, gen_pitch_interactive, gen_temperature_interactive, gen_repetition_interactive, gen_lang_interactive, model_choices_gr_interactive, engine_choices_interactive + ) + except requests.exceptions.RequestException as e: + # Handle the HTTP request error + print(f"[{branding}TTS] \033[91mWarning\033[0m Error during request to webserver process: Status code:\n{e}") + return {"status": "error", "message": str(e)} + + def update_settings_tg(activate, autoplay, show_text, language, narrator_enabled): + # Update the config_data dictionary with the new values + params["activate"] = activate == "Enabled" + params["autoplay"] = autoplay == "Enabled" + params["show_text"] = show_text == "Enabled" + params["language"] = language + params["narrator_enabled"] = narrator_enabled + print(f"[{branding}TTS] Default Text-gen-webui Settings Saved") + save_config_data() + return "Settings updated successfully!" + + debugging_options = params['debugging'] + debugging_choices = list(debugging_options.keys()) + default_values = [key for key, value in debugging_options.items() if value] + + def update_settings_at(delete_output_wavs, gradio_interface, gradio_port_number, output_folder, api_port_number, gr_debug_tts, transcode_audio_format): + # Update the config_data dictionary with the new values + params["delete_output_wavs"] = delete_output_wavs + params["gradio_interface"] = gradio_interface == "Enabled" + params["output_folder"] = output_folder + params['api_def']['api_port_number'] = api_port_number + params["gradio_port_number"] = gradio_port_number + for key in debugging_options.keys(): + params['debugging'][key] = key in gr_debug_tts + params["transcode_audio_format"] = transcode_audio_format + output_message = "Settings updated successfully!" + print(f"[{branding}TTS] Default Settings Saved") + save_config_data() + return output_message + + def update_settings_api(api_length_stripping, api_legacy_ip_address, api_allowed_filter, api_max_characters, api_use_legacy_api, api_text_filtering, api_narrator_enabled, api_text_not_inside, api_language, api_output_file_name, api_output_file_timestamp, api_autoplay, api_autoplay_volume): + # Update the params dictionary with the new values + params["api_def"]["api_length_stripping"] = api_length_stripping + params["api_def"]["api_allowed_filter"] = api_allowed_filter + params["api_def"]["api_max_characters"] = api_max_characters + params["api_def"]["api_use_legacy_api"] = api_use_legacy_api == "Legacy API" + params["api_def"]["api_legacy_ip_address"] = api_legacy_ip_address + params["api_def"]["api_text_filtering"] = api_text_filtering + params["api_def"]["api_narrator_enabled"] = api_narrator_enabled + params["api_def"]["api_text_not_inside"] = api_text_not_inside + params["api_def"]["api_language"] = api_language + params["api_def"]["api_output_file_name"] = api_output_file_name + params["api_def"]["api_output_file_timestamp"] = api_output_file_timestamp == "Timestamp files" + params["api_def"]["api_autoplay"] = api_autoplay == "Play remotely" + params["api_def"]["api_autoplay_volume"] = api_autoplay_volume + output_message = "Default API settings updated successfully!" + print(f"[{branding}TTS] API Settings Saved") + save_config_data() + return output_message + + def ensure_directory_exists(directory): + if not os.path.exists(directory): + os.makedirs(directory) + + def load_file_urls(json_path): + with open(json_path, 'r') as f: + return json.load(f) + + def download_file(url, dest_path): + response = requests.get(url, stream=True) + response.raise_for_status() + with open(dest_path, 'wb') as f: + for chunk in response.iter_content(chunk_size=8192): + f.write(chunk) + + def update_rvc_settings(rvc_enabled, rvc_char_model_file, rvc_narr_model_file, split_audio, autotune, pitch, + filter_radius, index_rate, rms_mix_rate, protect, hop_length, f0method, embedder_model, training_data_size, progress=gr.Progress(track_tqdm=True)): + params["rvc_settings"]["rvc_enabled"] = rvc_enabled + params["rvc_settings"]["rvc_char_model_file"] = rvc_char_model_file + params["rvc_settings"]["rvc_narr_model_file"] = rvc_narr_model_file + params["rvc_settings"]["split_audio"] = split_audio + params["rvc_settings"]["autotune"] = autotune + params["rvc_settings"]["pitch"] = pitch + params["rvc_settings"]["filter_radius"] = filter_radius + params["rvc_settings"]["index_rate"] = index_rate + params["rvc_settings"]["rms_mix_rate"] = rms_mix_rate + params["rvc_settings"]["protect"] = protect + params["rvc_settings"]["hop_length"] = hop_length + params["rvc_settings"]["f0method"] = f0method + params["rvc_settings"]["embedder_model"] = embedder_model + params["rvc_settings"]["training_data_size"] = training_data_size + if rvc_enabled: + base_dir = os.path.join(this_dir, "models", "rvc_base") + rvc_voices_dir = os.path.join(this_dir, "models", "rvc_voices") + ensure_directory_exists(base_dir) + ensure_directory_exists(rvc_voices_dir) + json_path = os.path.join(this_dir, "system", "tts_engines", "rvc_files.json") + file_urls = load_file_urls(json_path) + for idx, file in enumerate(file_urls): + if not os.path.exists(os.path.join(base_dir, file)): + progress(idx / len(file_urls), desc=f"Downloading Required Files: {file}...") + print(f"[{branding}TTS] Downloading {file}...") # Print statement for terminal + download_file(file_urls[file], os.path.join(base_dir, file)) + result = "Files downloaded successfully." if len(file_urls) > 0 else "All files are present." + print(f"[{branding}TTS] {result}") + print(f"[{branding}TTS] RVC Settings Saved") + save_config_data() + return "RVC settings updated successfully!" + + def generate_tts(gen_text, gen_char, rvcgen_char, gen_narr, rvcgen_narr, gen_narren, gen_textni, gen_repetition, gen_lang, gen_filter, gen_speed, gen_pitch, gen_autopl, gen_autoplvol, gen_filen, gen_temperature, gen_filetime, gen_stream, gen_stopcurrentgen): + api_url = f"http://{my_current_url}/api/tts-generate" + if gen_text == "": + print(f"[{branding}TTS] No Text was sent to generate as TTS") + return None, str("No Text was sent to generate as TTS") + if gen_stopcurrentgen: + stop_generate_tts() + if gen_stream == "true": + api_url = f"http://{my_current_url}/api/tts-generate-streaming" + encoded_text = requests.utils.quote(gen_text) + streaming_url = f"{api_url}?text={encoded_text}&voice={gen_char}&language={gen_lang}&output_file={gen_filen}" + return streaming_url, str("TTS Streaming Audio Generated") + else: + data = { + "text_input": gen_text, + "text_filtering": gen_filter, + "character_voice_gen": gen_char, + "rvccharacter_voice_gen": rvcgen_char, + "narrator_enabled": str(gen_narren).lower(), + "narrator_voice_gen": gen_narr, + "rvcnarrator_voice_gen": rvcgen_narr, + "text_not_inside": gen_textni, + "language": gen_lang, + "output_file_name": gen_filen, + "output_file_timestamp": str(gen_filetime).lower(), + "autoplay": str(gen_autopl).lower(), + "autoplay_volume": str(gen_autoplvol), + "speed": str(gen_speed), + "pitch": str(gen_pitch), + "temperature": str(gen_temperature), + "repetition_penalty": str(gen_repetition), + } + #print(f"Debug: Generate request param:", data) if params["debug_tts"] else None + try: + response = requests.post(api_url, data=data) + response.raise_for_status() # Raise an exception for 4xx or 5xx status codes + result = response.json() + if gen_autopl == "true": + return None, str("TTS Audio Generated (Played remotely)") + else: + if params["api_def"]["api_use_legacy_api"]: + return result['output_file_url'], str("TTS Audio Generated") + else: + # Set the protocol type + protocol = "http://" # or "https://" if using HTTPS + # Prepend the URL and PORT to the output_file_url + output_file_url = f"{protocol}{my_current_url}{result['output_file_url']}" + return output_file_url, str("TTS Audio Generated") + except requests.exceptions.RequestException as e: + error_message = "An error occurred. Please see console output." + print(f"[{branding}TTS] \033[91mError:\033[0m {error_message}") + print(f"[{branding}TTS] \033[91mError Details:\033[0m {str(e)}") + # Handle the error or return an appropriate error message + return None, str(error_message) + + def alltalk_gradio(): + global languages_list, at_settings + # Get the URL IP or domain name + def get_domain_name(request: gr.Request): + global my_current_url + if request: + host = request.headers.get("host", "Unknown") + my_current_url = host.split(":")[0] # Split the host by ":" and take the first part + my_current_url = my_current_url + ":" + str(params['api_def']['api_port_number']) + return None + else: + return "Unable to retrieve the domain name." + + # Get the list of languages from languages + languages = list(languages_list.keys()) + with gr.Blocks(theme=selected_theme, title="AllTalk") as app: + with gr.Row(): + gr.Markdown("## AllTalk TTS") + gr.Markdown("") + gr.Markdown("") + dark_mode_btn = gr.Button("Light/Dark Mode", variant="primary", size="sm") + dark_mode_btn.click(None, None, None, + js="""() => { + if (document.querySelectorAll('.dark').length) { + document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark')); + // localStorage.setItem('darkMode', 'disabled'); + } else { + document.querySelector('body').classList.add('dark'); + // localStorage.setItem('darkMode', 'enabled'); + } + }""", show_api=False) + if params["firstrun_splash"]: + alltalk_welcome() + with gr.Tab("Generate TTS"): + with gr.Tab("Generate"): + with gr.Row(): + gen_text = gr.Textbox(label="Text Input", lines=10) + with gr.Row(): + with gr.Group(): + with gr.Row(): + engine_choices = gr.Dropdown(choices=engines_available, label="TTS Engine", value=engine_loaded, show_label=False) + engine_btn = gr.Button("Swap TTS Engine") + with gr.Group(): + with gr.Row(): + model_choices_gr = gr.Dropdown(choices=models_available, label="TTS Models", value=current_model_loaded, interactive=True, show_label=False, allow_custom_value=True) + model_btn_gr = gr.Button("Load Different Model") + with gr.Group(): + at_available_voices_gr = at_settings["voices"] + rvcat_available_voices_gr = at_settings["rvcvoices"] + with gr.Row(): + at_default_voice_gr = params["tgwui"]["tgwui_character_voice"] + if at_default_voice_gr not in at_available_voices_gr: + at_default_voice_gr = at_available_voices_gr[0] if at_available_voices_gr else "" + gen_char = gr.Dropdown(choices=at_available_voices_gr, label="Character Voice", value=at_default_voice_gr, allow_custom_value=True,) + rvcat_default_voice_gr = params["rvc_settings"]["rvc_char_model_file"] + if rvcat_default_voice_gr not in rvcat_available_voices_gr: + rvcat_default_voice_gr = rvcat_available_voices_gr[0] if rvcat_available_voices_gr else "" + rvcgen_char = gr.Dropdown(choices=rvcat_available_voices_gr, label="RVC Character Voice", value=rvcat_default_voice_gr, allow_custom_value=True,) + at_narrator_voice_gr = params["tgwui"]["tgwui_narrator_voice"] + if at_narrator_voice_gr not in at_available_voices_gr: + at_narrator_voice_gr = at_available_voices_gr[0] if at_available_voices_gr else "" + gen_narr = gr.Dropdown(choices=at_available_voices_gr, label="Narrator Voice", value=at_narrator_voice_gr, allow_custom_value=True,) + rvcat_narrator_voice_gr = params["rvc_settings"]["rvc_narr_model_file"] + if rvcat_narrator_voice_gr not in rvcat_available_voices_gr: + rvcat_narrator_voice_gr = rvcat_available_voices_gr[0] if rvcat_available_voices_gr else "" + rvcgen_narr = gr.Dropdown(choices=rvcat_available_voices_gr, label="RVC Narrator Voice", value=rvcat_narrator_voice_gr, allow_custom_value=True,) + with gr.Row(): + #Get the current URL from the page + domain_name_output = gr.Textbox(label="Domain Name", visible=False) + def on_load(request: gr.Request): + domain_name = get_domain_name(request) + domain_name_output.value = domain_name + app.load(on_load, inputs=None, outputs=None) + if at_settings["streaming_capable"]: + gen_choices = [("Standard", "false"), ("Streaming (Disable Narrator)", "true")] + else: + gen_choices = [("Standard", "false")] + #Continue on with the Gradio interface + gen_stream = gr.Dropdown(choices=gen_choices, label="Generation Mode", value="false") + gen_lang = gr.Dropdown(value=params['api_def']['api_language'], choices=["ar", "zh", "cs", "nl", "en", "fr", "de", "hi", "hu", "it", "ja", "ko", "pl", "pt", "ru", "es", "tr"], label="Languages" if at_settings["languages_capable"] else "Model not multi language", interactive=at_settings["languages_capable"]) + gen_narren = gr.Dropdown(choices=[("Enabled", "true"), ("Disabled", "false"), ("Enabled (Silent)", "silent")], label="Narrator Enabled/Disabled", value="true" if params['api_def']['api_narrator_enabled'] == "true" else ("silent" if params['api_def']['api_narrator_enabled'] == "silent" else "false")) + gen_textni = gr.Dropdown(choices=[("Character", "character"), ("Narrator", "narrator"), ("Silent", "silent")], label="Narrator Text-not-inside", value=params['api_def']['api_text_not_inside']) + gen_stopcurrentgen = gr.Dropdown(choices={("Stop", "true"), ("Dont stop", "false")}, label="Auto-Stop current generation", value="true") + with gr.Row(): + gen_filter = gr.Dropdown(value=params['api_def']['api_text_filtering'], label="Text filtering", choices=["none", "standard", "html"]) + gen_filetime = gr.Dropdown(choices={("Timestamp files", "true"), ("Dont Timestamp (Over-write)", "false")}, label="Include Timestamp", value="true" if params['api_def']['api_output_file_timestamp'] else "false") + gen_autopl = gr.Dropdown(choices={("Play locally", "false"), ("Play remotely", "true")}, label="Play Locally or Remotely", value="true" if params['api_def']['api_autoplay'] else "false") + gen_autoplvol = gr.Dropdown(choices=[str(i / 10) for i in range(11)], value=str(params['api_def']['api_autoplay_volume']), label="Remote play volume", allow_custom_value=True) + gen_filen = gr.Textbox(value=params['api_def']['api_output_file_name'], label="Output File Name") + with gr.Row(): + gen_speed = gr.Slider(minimum=0.25, maximum=2.00, step=0.25, label="Speed", value="1.00", interactive=at_settings["generationspeed_capable"]) + gen_pitch = gr.Slider(minimum=-10, maximum=10, step=1, label="Pitch", value="1", interactive=at_settings["pitch_capable"]) + gen_temperature = gr.Slider(minimum=0.05, maximum=1.0, step=0.05, label="Temperature", value=0.75, interactive=at_settings["temperature_capable"]) + gen_repetition = gr.Slider(minimum=1.0, maximum=20.0, step=1.0, label="Repetition Penalty", value=10, interactive=at_settings["repetitionpenalty_capable"]) + #Toggle narrator selection on Streaming select + def update_narren_and_autopl(gen_stream): + if gen_stream == "true": + return "false", "false" + else: + return gen_narren.value, gen_autopl.value + gen_stream.change(update_narren_and_autopl, inputs=[gen_stream], outputs=[gen_narren, gen_autopl]) + with gr.Row(): + output_audio = gr.Audio(show_label=False, label="Generated Audio", autoplay=True, scale=3) + output_message = gr.Textbox( label="TTS Result", lines=5, scale=1) + with gr.Row(): + dark_mode_btn = gr.Button("Light/Dark Mode", variant="primary") + refresh_button = gr.Button("Refresh Server Settings", elem_id="refresh_button") + stop_button = gr.Button("Interupt TTS Generation") + submit_button = gr.Button("Generate TTS") + + model_btn_gr.click(fn=change_model_loaded, inputs=[engine_choices, model_choices_gr], outputs=[output_message, gen_stream, gen_char, rvcgen_char, gen_narr, rvcgen_narr, gen_speed, gen_pitch, gen_temperature, gen_repetition, gen_lang, model_choices_gr, engine_choices]) + engine_btn.click(fn=set_engine_loaded, inputs=[engine_choices], outputs=[output_message, gen_stream, gen_char, rvcgen_char, gen_narr, rvcgen_narr, gen_speed, gen_pitch, gen_temperature, gen_repetition, gen_lang, model_choices_gr, engine_choices]) + + dark_mode_btn.click(None, None, None, + js="""() => { + if (document.querySelectorAll('.dark').length) { + document.querySelectorAll('.dark').forEach(el => el.classList.remove('dark')); + // localStorage.setItem('darkMode', 'disabled'); + } else { + document.querySelector('body').classList.add('dark'); + // localStorage.setItem('darkMode', 'enabled'); + } + }""", show_api=False) + refresh_button.click(at_update_dropdowns, None, [gen_stream, gen_char, rvcgen_char, gen_narr, rvcgen_narr, gen_speed, gen_pitch, gen_temperature, gen_repetition, gen_lang, model_choices_gr, engine_choices]) + stop_button.click(stop_generate_tts, inputs=[], outputs=[output_message]) + submit_button.click(generate_tts, inputs=[gen_text, gen_char, rvcgen_char, gen_narr, rvcgen_narr, gen_narren, gen_textni, gen_repetition, gen_lang, gen_filter, gen_speed, gen_pitch, gen_autopl, gen_autoplvol, gen_filen, gen_temperature, gen_filetime, gen_stream, gen_stopcurrentgen], outputs=[output_audio, output_message]) + + with gr.Tab("Generate Help"): + help_content = alltalk_generation_help() + gr.Markdown(help_content) + + with gr.Tab("Voice2RVC"): + gr.Markdown("""Voice2RVC allows you to convert your spoken audio files into synthesized speech using advanced RVC (Retrieval-based Voice Conversion) models. You can either record your own speech or upload a pre-recorded audio file for processing. The tool offers features trim your input audio and undo changes if necessary. Simply record or upload your audio, select an RVC voice model, and submit it for processing. Once completed, you can download your synthesized speech.""") + with gr.Row(): + audio_input = gr.Audio(sources=["microphone", "upload"], type="numpy", label="Record audio or Upload a spoken audio file") + with gr.Row(): + rvc_voices_dropdown = gr.Dropdown(choices=at_settings["rvcvoices"], label="Select RVC Voice to generate as", value=at_settings["rvcvoices"][0]) + submit_button = gr.Button("Submit to RVC") + audio_output = gr.Audio(label="Converted Audio") + + submit_button.click(fn=voice2rvc, inputs=[audio_input, rvc_voices_dropdown], outputs=audio_output) + + with gr.Tab("TTS Generator"): + gr.Markdown("""With the TTS Generator you can create incredibly long audio e.g. entire books. Yet to be migrated into Gradio""") + gr.Markdown("""Please find it on the web address http://127.0.0.1:7851/static/tts_generator/tts_generator.html (Assuming you have not changed your IP Address)""") + + with gr.Tab("Global Settings"): + with gr.Tab("AllTalk Settings"): + with gr.Row(): + delete_output_wavs = gr.Dropdown(value=params["delete_output_wavs"], label="Del WAV's older than", choices=["Disabled", "1 Day", "2 Days", "3 Days", "4 Days", "5 Days", "6 Days", "7 Days", "14 Days", "21 Days", "28 Days"]) + api_port_number = gr.Number(value=int(params['api_def']['api_port_number']), label="API Port Number", precision=0) + gradio_port_number = gr.Number(value=int(params["gradio_port_number"]), label="Gradio Port Number", precision=0) + output_folder = gr.Textbox(value=params["output_folder"], label=f"Output Folder name (sub {branding})") + with gr.Row(): + transcode_audio_format = gr.Dropdown(choices={"Disabled": "disabled", "aac": "aac", "flac": "flac", "mp3": "mp3", "opus": "opus", "wav": "wav"}, label="Audio Transcoding", value=params["transcode_audio_format"]) + with gr.Row(): + themes_select = gr.Dropdown(loadThemes.get_list(), value=loadThemes.read_json(), label="Gradio Theme Selection", visible=True,) + themes_select.change(fn=loadThemes.select_theme, inputs=[themes_select], outputs=[gr.Textbox(label="Gradio Selection Result")],) + with gr.Row(): + with gr.Column(): + gr_debug_tts = gr.CheckboxGroup(choices=debugging_choices, label="Debugging Options list", value=default_values) + with gr.Column(): + gradio_interface = gr.Dropdown(choices={"Enabled": "true", "Disabled": "false"}, label="Gradio Interface", value="Enabled" if params["gradio_interface"] else "Disabled", info="**WARNING**: This will disable the AllTalk Gradio interface from loading. To re-enable the interface, go to the API address in a web browser and enable it there. http://127.0.0.1:7851/") + with gr.Row(): + submit_button = gr.Button("Update Settings") + output_message = gr.Textbox(label="Output Message", interactive=False, show_label=False) + + submit_button.click(update_settings_at, inputs=[delete_output_wavs, gradio_interface, gradio_port_number, output_folder, api_port_number, gr_debug_tts, transcode_audio_format], outputs=output_message) + + with gr.Tab("AllTalk API Defaults"): + gr.Markdown("""##   API Version Settings""") + with gr.Group(): + with gr.Row(): + api_use_legacy_api = gr.Dropdown(choices=["AllTalk v2 API", "AllTalk v1 API (Legacy)"], label=f"{branding} API version", value="AllTalk v1 API (Legacy)" if params['api_def']['api_use_legacy_api'] else "AllTalk v2 API", scale=1) + gr.Textbox(value="Determines the API version to use. The legacy API includes the full URL (protocol, IP address, and port) in the output responses, while the new API returns only the relative path of the output file. Default: AllTalk v2 API", interactive=False, show_label=False, lines=2, scale=4) + with gr.Row(): + api_legacy_ip_address = gr.Textbox(value=params['api_def']['api_legacy_ip_address'], label="AllTalk v1 API IP address") + gr.Textbox(value="Specifies the IP address to be included in the output responses when using the legacy API. Default: 127.0.0.1", interactive=False,show_label=False, lines=2, scale=4) + gr.Markdown("""##   API Default Settings""") + with gr.Group(): + with gr.Row(): + api_length_stripping = gr.Slider(minimum=1, maximum=20, step=1, value=int(params['api_def']['api_length_stripping']), label="Strip sentences shorter than", scale=1) + gr.Textbox(value="Defines the minimum length of a sentence (in characters) that will be processed for text-to-speech. Sentences shorter than the X characters value will be filtered out by the Narrator to remove unwanted text characters. Default: 3", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_max_characters = gr.Slider(minimum=50, maximum=10000, step=50, value=int(params['api_def']['api_max_characters']), label="Maximum amount of characters") + gr.Textbox(value="Sets the maximum number of characters allowed in a single text-to-speech generation request. Requests exceeding this limit will be rejected. Default: 2000", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_text_filtering = gr.Dropdown(value=params['api_def']['api_text_filtering'], label="Text filtering", choices=["none", "standard", "html"]) + gr.Textbox(value="Determines the text filtering method applied to the input text before processing. Available options are 'none' (no filtering), 'standard' (basic filtering), and 'html' (HTML-specific filtering). Default: Standard", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_language = gr.Dropdown(value=params['api_def']['api_language'], label="Language", choices=["ar", "zh", "cs", "nl", "en", "fr", "de", "hi", "hu", "it", "ja", "ko", "pl", "pt", "ru", "es", "tr"]) + gr.Textbox(value="Sets the default language for text-to-speech if no language is explicitly provided in the request. Default: en", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_narrator_enabled = gr.Dropdown( + choices={("Enabled", "true"), ("Disabled", "false"), ("Enabled (Silent)", "silent")}, + label="Narrator Enabled/Disable/Silent", + value=("true" if params['api_def']['api_narrator_enabled'] == "true" + else "false" if params['api_def']['api_narrator_enabled'] == "false" + else "silent") + ) + gr.Textbox(value="Determines whether the narrator functionality is enabled by default when not explicitly specified in the request. Please note, if you set `Enabled` or `Enabled (silent)` as the APi defaults, then all text will go into the narrator function unless `disabled` is sent as part of the TTS generation request, possibly resulting in silenced TTS. Default: Disabled", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_text_not_inside = gr.Dropdown(choices={"character", "narrator", "silent"}, label="Narrator Text-not-inside", value="character" if params['api_def']['api_text_not_inside'] else "narrator") + gr.Textbox(value="Defines how narrated text is split and processed when not explicitly specified in the request. The available options are 'character' (text is associated with the character) and 'narrator' (text is associated with the narrator). Default: Narrator", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_output_file_name = gr.Textbox(value=params['api_def']['api_output_file_name'], label="Output file name") + gr.Textbox(value="Specifies the default name for the output file when no filename is provided in the request. Default: myoutputfile", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_output_file_timestamp = gr.Dropdown(choices={"Timestamp files", "Dont Timestamp (Over-write)"}, label="Include Timestamp", value="Timestamp files" if params['api_def']['api_output_file_timestamp'] else "Dont Timestamp (Over-write)") + gr.Textbox(value="Determines whether a unique identifier (UUID) timestamp is appended to the generated text-to-speech output file. When enabled, each output file will have a unique timestamp, preventing overwriting of files. When disabled, files with the same name will be overwritten. Default: Timestamp files", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_autoplay = gr.Dropdown(choices={"Play locally", "Play remotely"}, label="Play Locally or Remotely", value="Play remotely" if params['api_def']['api_autoplay'] else "Play locally") + gr.Textbox(value="Specifies whether the generated audio should be played locally on the client-side or remotely on the server-side console/terminal. Default: Play locally", interactive=False,show_label=False, lines=2, scale=4) + with gr.Row(): + api_autoplay_volume = gr.Slider(minimum=0.1, maximum=0.9, step=0.1, label="Remote play volume", value=float(params['api_def']['api_autoplay_volume'])) + gr.Textbox(value="Adjusts the volume level for audio playback when the 'Play Remotely' option is selected. The value ranges from 0.1 (lowest) to 0.9 (highest). Default: 0.9", interactive=False,show_label=False, lines=2, scale=4) + gr.Markdown("""##   API Allowed Text Filtering/Passthrough Settings""") + with gr.Row(): + api_allowed_filter = gr.Textbox(value=params['api_def']['api_allowed_filter'], label="Allowed text filter", show_label=False, lines=6, scale=1) + gr.Textbox(value="""Defines the set of characters and Unicode ranges that are permitted to be processed by the AllTalk TTS system. This filter ensures that only valid and supported characters are passed to the TTS engine or AI model for generation. The allowed characters include ASCII letters and digits, punctuation, whitespace, and various Unicode ranges for different languages and scripts. + + a-zA-Z0-9: ASCII letters and digits + .,;:!?: Punctuation characters + '": Single and double quotes + \s: Whitespace characters + \-: Hyphen/dash + $: Dollar sign + \\u00C0-\\u00FF: Latin characters with diacritics (À-ÿ) + \\u0400-\\u04FF: Cyrillic characters + \\u0900-\\u097F: Devanagari characters + \\u4E00-\\u9FFF: Chinese characters (CJK Unified Ideographs) + \\u3400-\\u4DBF: Chinese characters (CJK Unified Ideographs Extension A) + \\uF900-\\uFAFF: Chinese characters (CJK Compatibility Ideographs) + \\u0600-\\u06FF: Arabic characters (Arabic) + \\u0750-\\u077F: Arabic characters (Arabic Supplement) + \\uFB50-\\uFDFF: Arabic characters (Arabic Presentation Forms-A) + \\uFE70-\\uFEFF: Arabic characters (Arabic Presentation Forms-B) + \\u3040-\\u309F: Hiragana characters (Japanese) + \\u30A0-\\u30FF: Katakana characters (Japanese) + \\uAC00-\\uD7A3: Hangul Syllables (Korean) + \\u1100-\\u11FF: Hangul Jamo (Korean) + \\u3130-\\u318F: Hangul Compatibility Jamo (Korean) + \\u0150\\u0151\\u0170\\u0171: Hungarian characters + \\u2018\\u2019: Left and right single quotation marks + \\u201C\\u201D: Left and right double quotation marks + \\u3001\\u3002: Ideographic comma and full stop + \\uFF01\\uFF0C\\uFF1A\\uFF1B\\uFF1F: Fullwidth exclamation, comma, colon, semicolon & question mark + """, interactive=False,show_label=False, lines=32, scale=1) + with gr.Row(): + submit_button = gr.Button("Update Settings") + output_message = gr.Textbox(label="Output Message", interactive=False, show_label=False) + + submit_button.click(update_settings_api, inputs=[api_length_stripping, api_legacy_ip_address, api_allowed_filter, api_max_characters, api_use_legacy_api, api_text_filtering, api_narrator_enabled, api_text_not_inside, api_language, api_output_file_name, api_output_file_timestamp, api_autoplay, api_autoplay_volume], outputs=output_message) + + def rvc_update_dropdowns(): + global at_settings + at_settings = get_alltalk_settings() # Pull all the current settings from the AllTalk server, if its online. + current_voices = at_settings["rvcvoices"] + current_char = params["rvc_settings"]["rvc_char_model_file"] + current_narr = params["rvc_settings"]["rvc_narr_model_file"] + if current_char not in current_voices: + current_char = current_voices[0] if current_voices else "" + if current_narr not in current_voices: + current_narr = current_voices[0] if current_voices else "" + return ( + gr.Dropdown(choices=current_voices, value=current_char, interactive=True), + gr.Dropdown(choices=current_voices, value=current_narr, interactive=True), + ) + + def gr_update_rvc_settings(rvc_enabled, rvc_char_model_file, rvc_narr_model_file, split_audio, autotune, pitch, + filter_radius, index_rate, rms_mix_rate, protect, hop_length, f0method, embedder_model, training_data_size): + progress = gr.Progress(track_tqdm=True) + return update_rvc_settings(rvc_enabled, rvc_char_model_file, rvc_narr_model_file, split_audio, autotune, pitch, + filter_radius, index_rate, rms_mix_rate, protect, hop_length, f0method, embedder_model, training_data_size, progress) + + with gr.Tab("RVC Settings"): + with gr.Row(): + with gr.Column(scale=3): + with gr.Row(): + rvc_model_file_available = at_settings["rvcvoices"] + rvc_char_model_file_default = params["rvc_settings"]["rvc_char_model_file"] + if rvc_char_model_file_default not in rvc_model_file_available: + rvc_char_model_file_default = rvc_model_file_available[0] if rvc_model_file_available else "" + rvc_char_model_file_gr = gr.Dropdown(choices=rvc_model_file_available, label="Default Character Voice Model", info="Select the Character voice model used for conversion.", value=rvc_char_model_file_default, allow_custom_value=True,) + rvc_narr_model_file_default = params["rvc_settings"]["rvc_narr_model_file"] + if rvc_narr_model_file_default not in rvc_model_file_available: + rvc_narr_model_file_default = rvc_model_file_available[0] if rvc_model_file_available else "" + rvc_narr_model_file_gr = gr.Dropdown(choices=rvc_model_file_available, label="Default Narrator Voice Model", info="Select the Narrator voice model used for conversion.", value=rvc_narr_model_file_default, allow_custom_value=True,) + rvc_refresh_button = gr.Button("Refresh Model Choices") + with gr.Column(scale=0): + rvc_enabled = gr.Checkbox(label="Enable RVC", info="RVC (Real-Time Voice Cloning) enhances TTS by replicating voice characteristics for characters or narrators, adding depth to synthesized speech.", value=params["rvc_settings"].get("rvc_enabled", False), interactive=True) + with gr.Row(equal_height=True): + with gr.Column(): + pitch = gr.Slider(minimum=-24, maximum=24, step=1, label="Pitch", info="Set the pitch of the audio, the higher the value, the higher the pitch.", value=params["rvc_settings"].get("pitch", 0), interactive=True,) + with gr.Column(): + hop_length = gr.Slider(minimum=1, maximum=512, step=1, label="Hop Length", info="Smaller hop lengths require more time for inference but tend to yield higher pitch accuracy.", value=params["rvc_settings"].get("hop_length", 128), interactive=True,) + with gr.Row(equal_height=True): + with gr.Column(): + training_data_size = gr.Slider(minimum=10000, maximum=100000, step=5000, label="Training Data Size", info="Determines the number of training data points used to train the FAISS index. Increasing the size may improve the quality but can also increase computation time.", value=params["rvc_settings"].get("training_data_size", 25000), interactive=True) + with gr.Column(): + index_rate = gr.Slider(minimum=0, maximum=1, label="Index Influence Ratio", info="Sets the influence exerted by the index file on the final output. A higher value increases the impact of the index, potentially enhancing detail but also increasing the risk of artifacts.", value=params["rvc_settings"].get("index_rate", 0.75), interactive=True) + with gr.Row(equal_height=True): + with gr.Column(): + rms_mix_rate = gr.Slider(minimum=0, maximum=1, label="Volume Envelope", info="Substitute or blend with the volume envelope of the output. The closer the ratio is to 1, the more the output envelope is employed.", value=params["rvc_settings"].get("rms_mix_rate", 1), interactive=True,) + with gr.Column(): + protect = gr.Slider(minimum=0, maximum=0.5, label="Protect Voiceless Consonants/Breath sounds", info="Prevents sound artifacts. Higher values (up to 0.5) provide stronger protection but may affect indexing.", value=params["rvc_settings"].get("protect", 0.5), interactive=True,) + with gr.Column(): + filter_radius = gr.Slider(minimum=0, maximum=7, label="Filter Radius", info="If the number is greater than or equal to three, employing median filtering on the collected tone results has the potential to decrease respiration.", value=params["rvc_settings"].get("filter_radius", 3), step=1, interactive=True,) + with gr.Row(): + with gr.Column(): + with gr.Row(): + embedder_model = gr.Radio(label="Embedder Model", info="Model used for learning speaker embedding.", choices=["hubert", "contentvec"], value=params["rvc_settings"].get("embedder_model", "hubert"), interactive=True,) + with gr.Row(): + split_audio = gr.Checkbox(label="Split Audio", info="Split the audio into chunks for inference to obtain better results in some cases.", value=params["rvc_settings"].get("split_audio", False), interactive=True,) + autotune = gr.Checkbox(label="Autotune", info="Apply a soft autotune to your inferences, recommended for singing conversions.", value=params["rvc_settings"].get("autotune", False), interactive=True,) + with gr.Column(): + f0method = gr.Radio(label="Pitch Extraction Algorithm", info="Select the algorithm to be used for extracting the pitch (F0) during audio conversion. The default algorithm is rmvpe, which is generally recommended for most cases due to its balance of accuracy and performance.", choices=["crepe", "crepe-tiny", "dio", "fcpe", "harvest", "hybrid[rmvpe+fcpe]", "pm", "rmvpe"], value=params["rvc_settings"].get("f0method", "rmvpe"), interactive=True,) + with gr.Row(): + update_button = gr.Button("Update RVC Settings") + update_output = gr.Textbox(label="Update Status", show_label=False) + rvc_refresh_button.click(rvc_update_dropdowns, None, [rvc_char_model_file_gr, rvc_narr_model_file_gr]) + update_button.click(fn=gr_update_rvc_settings, inputs=[rvc_enabled, rvc_char_model_file_gr, rvc_narr_model_file_gr, split_audio, autotune, pitch, filter_radius, index_rate, rms_mix_rate, protect, hop_length, f0method, embedder_model, training_data_size], outputs=[update_output]) + + with gr.Tab("Text-generation-webui Settings"): + with gr.Row(): + activate = gr.Dropdown(choices={"Enabled": "true", "Disabled": "false"}, label="Activate TTS", value="Enabled" if params["tgwui"]["tgwui_activate_tts"] else "Disabled") + autoplay = gr.Dropdown(choices={"Enabled": "true", "Disabled": "false"}, label="Autoplay TTS", value="Enabled" if params["tgwui"]["tgwui_autoplay_tts"] else "Disabled") + show_text = gr.Dropdown(choices={"Enabled": "true", "Disabled": "false"}, label="Show Text", value="Enabled" if params["tgwui"]["tgwui_show_text"] else "Disabled") + narrator_enabled = gr.Dropdown(choices=[("Enabled", "true"), ("Disabled", "false"), ("Enabled (Silent)", "silent")], label="Narrator enabled", value="true" if params["tgwui"]["tgwui_narrator_enabled"] == "true" else ("silent" if params["tgwui"]["tgwui_narrator_enabled"] == "silent" else "false")) + language = gr.Dropdown(value=params["tgwui"]["tgwui_language"], label="Default Language", choices=languages_list) + with gr.Row(): + submit_button = gr.Button("Update Settings") + output_message = gr.Textbox(label="Output Message", interactive=False, show_label=False) + + submit_button.click(update_settings_tg, inputs=[activate, autoplay, show_text, language, narrator_enabled], outputs=output_message) + + disk_space_page = get_disk_interface() + disk_space_page() + + + with gr.Tab("TTS Engines Settings"): + with gr.Tabs(): + for engine_name in engines_available: + with gr.Tab(f"{engine_name.capitalize()} TTS"): + gr.Markdown(f"###   {engine_name.capitalize()} TTS") + globals()[f"{engine_name}_at_gradio_settings_page"](globals()[f"{engine_name}_model_config_data"]) + + alltalk_documentation() + api_documentation() + with gr.Tab("About this project"): + alltalk_about() + return app + + if __name__ == "__main__": + app = alltalk_gradio().queue() + app.launch(server_port=params['gradio_port_number'], prevent_thread_lock=True, quiet=True) + + if not running_in_standalone: + app = alltalk_gradio().queue() + app.launch(server_port=params['gradio_port_number'], prevent_thread_lock=True, quiet=True) + +######################################### +# START-UP # Final Splash before Gradio # +######################################### +print(f"[{branding}TTS] Please use \033[91mCtrl+C\033[0m when exiting AllTalk otherwise a") +print(f"[{branding}TTS] subprocess may continue running in the background.") +print(f"[{branding}TTS]") +print(f"[{branding}TTS] {branding}Server Ready") + +############################################################################################### +# START-UP # Loop to keep the script from exiting out if its being run as a standalone script # +############################################################################################### +if running_in_standalone: + while True: + try: + time.sleep(1) # Add a small delay to avoid high CPU usage + except KeyboardInterrupt: + break # Allow graceful exit on Ctrl+C diff --git a/tts_server.py b/tts_server.py new file mode 100644 index 0000000..a425255 --- /dev/null +++ b/tts_server.py @@ -0,0 +1,1676 @@ +import os +import sys +import json +import time +import librosa +import logging +import importlib +import subprocess +from pathlib import Path +logging.disable(logging.WARNING) +##################### +# Webserver Imports # +##################### +from contextlib import asynccontextmanager +from fastapi.staticfiles import StaticFiles +from fastapi.templating import Jinja2Templates +from fastapi.middleware.cors import CORSMiddleware +from fastapi import FastAPI, Form, Request, Response, Depends, HTTPException, Query +from fastapi.responses import JSONResponse, HTMLResponse, RedirectResponse, FileResponse, StreamingResponse +########################## +# Generation API Imports # +########################## +import re +import html +import uuid +import hashlib +import numpy as np +import soundfile as sf +from typing import Union, Dict, List +from pydantic import BaseModel, ValidationError, Field, field_validator +######################################################################################## +# START-UP # Silence RVC warning about torch.nn.utils.weight_norm even though not used # +######################################################################################## +import warnings +warnings.filterwarnings("ignore", category=UserWarning, module="torch.nn.utils.weight_norm") +warnings.filterwarnings("ignore", category=UserWarning, module="torch.nn.functional", lineno=5476) +#################### +# Setup local path # +#################### +this_dir = Path(__file__).parent.resolve() # Set this_dir as the current alltalk_tts folder + +#################################################### +# Load params and api_defailts from confignew.json # +#################################################### +def load_config(): + global branding, output_directory, debug_transcode, debug_tts, debug_tts_variables, engines_available, engine_loaded, debug_rvc, debug_concat, debug_openai + # Define the path to the confignew.json file & load in api_defaults & params + configfile_path = this_dir / "confignew.json" + with open(configfile_path, "r") as configfile: + configfile_data = json.load(configfile) + api_defaults = { + "api_text_filtering": configfile_data["api_def"]["api_text_filtering"], + "api_narrator_enabled": configfile_data["api_def"]["api_narrator_enabled"], + "api_text_not_inside": configfile_data["api_def"]["api_text_not_inside"], + "api_language": configfile_data["api_def"]["api_language"], + "api_output_file_name": configfile_data["api_def"]["api_output_file_name"], + "api_output_file_timestamp": configfile_data["api_def"]["api_output_file_timestamp"], + "api_autoplay": configfile_data["api_def"]["api_autoplay"], + "api_autoplay_volume": configfile_data["api_def"]["api_autoplay_volume"], + "api_port_number": configfile_data["api_def"]["api_port_number"], + "api_allowed_filter": configfile_data["api_def"]["api_allowed_filter"], + "api_length_stripping": configfile_data["api_def"]["api_length_stripping"], + "api_max_characters": configfile_data["api_def"]["api_max_characters"], + "api_use_legacy_api": configfile_data["api_def"]["api_use_legacy_api"], + "api_legacy_ip_address": configfile_data["api_def"]["api_legacy_ip_address"], + } + rvc_settings = { + "rvc_enabled": configfile_data["rvc_settings"]["rvc_enabled"], + "rvc_char_model_file": configfile_data["rvc_settings"]["rvc_char_model_file"], + "rvc_narr_model_file": configfile_data["rvc_settings"]["rvc_narr_model_file"], + "split_audio": configfile_data["rvc_settings"]["split_audio"], + "autotune": configfile_data["rvc_settings"]["autotune"], + "pitch": configfile_data["rvc_settings"]["pitch"], + "filter_radius": configfile_data["rvc_settings"]["filter_radius"], + "index_rate": configfile_data["rvc_settings"]["index_rate"], + "rms_mix_rate": configfile_data["rvc_settings"]["rms_mix_rate"], + "protect": configfile_data["rvc_settings"]["protect"], + "hop_length": configfile_data["rvc_settings"]["hop_length"], + "f0method": configfile_data["rvc_settings"]["f0method"], + "embedder_model": configfile_data["rvc_settings"]["embedder_model"], + "training_data_size": configfile_data["rvc_settings"]["training_data_size"], + } + branding = configfile_data.get("branding", "") + debug_transcode = configfile_data.get("debugging").get("debug_transcode") + debug_tts = configfile_data.get("debugging").get("debug_tts") + debug_tts_variables = configfile_data.get("debugging").get("debug_tts_variables") + debug_rvc = configfile_data.get("debugging").get("debug_rvc") + debug_concat = configfile_data.get("debugging").get("debug_concat") + debug_openai = configfile_data.get("debugging").get("debug_openai") + output_directory = this_dir / configfile_data.get("output_folder", "") + output_directory.mkdir(parents=True, exist_ok=True) + tts_engines_file = os.path.join(this_dir, "system", "tts_engines", "tts_engines.json") + with open(tts_engines_file, "r") as f: + tts_engines_data = json.load(f) + engines_available = [engine["name"] for engine in tts_engines_data["engines_available"]] # List of the available TTS engines from tts_engines.json + engine_loaded = tts_engines_data["engine_loaded"] # The currently set TTS engine from tts_engines.json + selected_model = tts_engines_data["selected_model"] + # Conditionally import infer_pipeline if rvc_enabled is true + if rvc_settings["rvc_enabled"]: + from system.tts_engines.rvc.infer.infer import infer_pipeline + else: + infer_pipeline = None + return configfile_data, api_defaults, rvc_settings, infer_pipeline + +# Call to load in the params and api_defaults +params, api_defaults, rvc_settings, infer_pipeline = load_config() + +#################### +# Check for FFMPEG # +#################### +def check_ffmpeg(this_dir): + if sys.platform == "win32": + # Check for ffmpeg.exe in the specified directory on Windows + ffmpeg_path = os.path.join(this_dir, "system", "win_ffmpeg", "ffmpeg.exe") + if os.path.exists(ffmpeg_path): + return True + else: + # Check for FFmpeg on Linux and macOS using the command line + try: + subprocess.run(['ffmpeg', '-version'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True) + return True + except (subprocess.CalledProcessError, FileNotFoundError): + pass + return False + +# Check if FFmpeg is installed +ffmpeg_installed = check_ffmpeg(this_dir) + +if not ffmpeg_installed: + print(f"[{branding}ENG] \033[92mTranscoding :\033[94m ffmpeg not found\033[0m") + print(f"[{branding}ENG] FFmpeg is not installed. Transcoding will be disabled.") + print(f"[{branding}ENG] Please install FFmpeg on your system.") + + if sys.platform == "win32": + print(f"[{branding}ENG] \033[92mTranscoding :\033[94m ffmpeg not found\033[0m") + print(f"[{branding}ENG] Installation instructions for Windows:") + print(f"[{branding}ENG] Copy the 'ffmpeg.exe' file to '{os.path.join(this_dir, 'system', 'win_ffmpeg')}'") + else: + print(f"[{branding}ENG] \033[92mTranscoding :\033[94m ffmpeg not found\033[0m") + print(f"[{branding}ENG] Installation instructions:") + print(f"[{branding}ENG] Linux (Debian-based systems): Run 'sudo apt-get install ffmpeg' in the terminal.") + print(f"[{branding}ENG] macOS: Run 'brew install ffmpeg' in the terminal (requires Homebrew).") + # You can choose to exit the script or continue without FFmpeg + # exit(1) + +if ffmpeg_installed: + from ffmpeg.asyncio import FFmpeg + print(f"[{branding}ENG] \033[92mTranscoding :\033[93m ffmpeg found\033[0m") + +################################ +# Check for portaudio on Linux # +################################ +try: + import sounddevice as sd + sounddevice_installed=True +except OSError: + print(f"[{branding}Startup] \033[91mInfo\033[0m PortAudio library not found. If you wish to play TTS in standalone mode through the API suite") + print(f"[{branding}Startup] \033[91mInfo\033[0m please install PortAudio. This will not affect any other features or use of Alltalk.") + print(f"[{branding}Startup] \033[91mInfo\033[0m If you don't know what the API suite is, then this message is nothing to worry about.") + sounddevice_installed=False + if sys.platform.startswith('linux'): + print(f"[{branding}Startup] \033[91mInfo\033[0m On Linux, you can use the following command to install PortAudio:") + print(f"[{branding}Startup] \033[91mInfo\033[0m sudo apt-get install portaudio19-dev") + +####################################################################### +# Attempt to import the ModelLoader class for the selected TTS engine # +####################################################################### +if engine_loaded in engines_available: + loader_module = importlib.import_module(f"system.tts_engines.{engine_loaded}.model_engine") + tts_class = getattr(loader_module, "tts_class") + # Setup model_engine as the way to call the functions within the Class. + model_engine = tts_class() +else: + raise ValueError(f"Invalid TTS engine: {engine_loaded}") + +########################################## +# Run setup function in the model_engine # +########################################## +@asynccontextmanager +async def startup_shutdown(no_actual_value_it_demanded_something_be_here): + try: + await model_engine.setup() + except FileNotFoundError as e: + print(f"Error during setup: {e}. Continuing without the TTS model.") + yield + # Shutdown logic + +############################### +# Setup FastAPI with Lifespan # +############################### +app = FastAPI(lifespan=startup_shutdown) +# Allow all origins, and set other CORS options +app.add_middleware( + CORSMiddleware, + allow_origins=["*"], # Set this to the specific origins you want to allow + allow_credentials=True, + allow_methods=["*"], + allow_headers=["*"], +) + +############################## +# API Endpoint - /api/reload # +############################## +model_currently_changing = False +@app.route("/api/reload", methods=["POST"]) +async def apifunction_reload(request: Request): + global model_currently_changing + if model_currently_changing == True: + return Response(content=json.dumps({"status": "model-is currently changing"}), media_type="application/json") + model_currently_changing == True + requested_model = request.query_params.get("tts_method") + if requested_model not in model_engine.available_models: + model_currently_changing == False + return {"status": "error", "message": "Invalid TTS method specified"} + # Call the handle_tts_method_change method from the imported class + success = await model_engine.handle_tts_method_change(requested_model) + if success: + model_engine.current_model_loaded = requested_model + print(f"[{branding}ENG]") + print(f"[{branding}ENG] {branding} Server Ready.") + model_currently_changing == False + return Response(content=json.dumps({"status": "model-success"}), media_type="application/json") + else: + model_currently_changing == False + return Response(content=json.dumps({"status": "model-failure"}), media_type="application/json") + +#################################### +# API Endpoint - /api/enginereload # +#################################### +import asyncio +uvicorn_server = None +def restart_self(): + """Restart the current script.""" + #print(f"[{branding}ENG] Restarting subprocess...") + os.execv(sys.executable, ['python'] + sys.argv) + +async def handle_restart(): + global uvicorn_server + if uvicorn_server: + print(f"[{branding}ENG] Stopping uvicorn server...") + uvicorn_server.should_exit = True + uvicorn_server.force_exit = True + # Check if the server has stopped + while not uvicorn_server.is_stopped: + await asyncio.sleep(0.1) # Check every 100ms if server is stopped + restart_self() + +@app.post("/api/enginereload") +async def apifunction_reload(request: Request): + requested_engine = request.query_params.get("engine") + if requested_engine not in engines_available: + return {"status": "error", "message": "Invalid TTS engine specified"} + print(f"[{branding}ENG]") + print(f"[{branding}ENG] \033[94mChanging model loaded. Please wait.\033[00m") + print(f"[{branding}ENG]") + # Update the tts_engines.json file + tts_engines_file = os.path.join(this_dir, "system", "tts_engines", "tts_engines.json") + with open(tts_engines_file, "r") as f: + tts_engines_data = json.load(f) + for engine in tts_engines_data["engines_available"]: + if engine["name"] == requested_engine: + tts_engines_data["engine_loaded"] = requested_engine + tts_engines_data["selected_model"] = engine["selected_model"] + break + with open(tts_engines_file, "w") as f: + json.dump(tts_engines_data, f) + #print(f"[{branding}ENG] Reloading the engine...") + # Start the restart process in the background + asyncio.create_task(handle_restart()) + # Never goes past the above asyncio as the script is killed at that point + return Response(content=json.dumps({"status": "engine-success"}), media_type="application/json") + +####################################### +# API Endpoint - /api/stop-generation # +####################################### +# When this endpoint it called it will set tts_stop_generation in the model_engine to True, which can be used to interrupt the current TTS generation. +@app.put("/api/stop-generation") +async def apifunction_stop_generation(): + if model_engine.tts_generating_lock and not model_engine.tts_stop_generation: + model_engine.tts_stop_generation = True + return {"message": "Cancelling current TTS generation"} + +############################# +# API Endpoint - /api/audio # +############################# +# Gives web access to the output files as downloads +@app.get("/audio/{filename}") +async def apifunction_get_audio(filename: str): + audio_path = this_dir / output_directory / filename + return FileResponse(audio_path) + +################################## +# API Endpoint - /api/audiocache # +################################## +@app.get("/audiocache/{filename}") +async def apifunction_get_audio(filename: str): + audio_path = Path({output_directory}) / filename + if not audio_path.is_file(): + raise HTTPException(status_code=404, detail="File not found") + response = FileResponse(path=audio_path, media_type='audio/wav', filename=filename) + # Set caching headers + response.headers["Cache-Control"] = "public, max-age=604800" # Cache for one week + response.headers["ETag"] = str(audio_path.stat().st_mtime) # Use the file's last modified time as a simple ETag + return response + +############################## +# API Endpoint - /api/voices # +############################## +@app.get("/api/voices") +async def apifunction_get_voices(): + if not model_engine.multivoice_capable: + return {"status": "error", "message": f"The currently loaded TTS engine '{model_engine.engine_loaded}' does not support multiple voices."} + + available_voices = model_engine.voices_file_list() + return {"status": "success", "voices": available_voices} + +################################# +# API Endpoint - /api/rvcvoices # +################################# +@app.get("/api/rvcvoices") +async def apifunction_get_voices(): + global params, api_defaults, rvc_settings, infer_pipeline + params, api_defaults, rvc_settings, infer_pipeline = load_config() + if not rvc_settings["rvc_enabled"]: + return {"status": "success", "rvcvoices": ["Disabled"]} + # Define the directory path + directory = os.path.join(this_dir, "models", "rvc_voices") + # Check if the directory exists + if not os.path.exists(directory): + return {"status": "success", "rvcvoices": ["Disabled"]} + # List all .pth files recursively in the directory + pth_files = [] + for root, dirs, files in os.walk(directory): + for file in files: + if file.endswith(".pth"): + pth_files.append(os.path.relpath(os.path.join(root, file), start=directory)) + # If no .pth files found, return "Disabled" + if not pth_files: + return {"status": "success", "rvcvoices": ["Disabled"]} + # Sort the list alphabetically + pth_files.sort() + # Add "Disabled" at the beginning of the list + pth_files.insert(0, "Disabled") + return {"status": "success", "rvcvoices": pth_files} + +##################################### +# API Endpoint - /api/reload_config # +##################################### +@app.get("/api/reload_config") +async def apifunction_reload_config(): + global params, api_defaults, rvc_settings, infer_pipeline + model_engine.available_models = model_engine.scan_models_folder() + params, api_defaults, rvc_settings, infer_pipeline = load_config() + return Response("Config file reloaded successfully") + +############################# +# API Endpoint - /api/ready # +############################# +@app.get("/api/ready") +async def apifunction_ready(): + if model_engine.setup_has_run: + return Response("Ready") + else: + return Response("Unloaded") + +####################################### +# API Endpoint - /api/currentsettings # +####################################### +@app.get('/api/currentsettings') +def apifunction_get_current_settings(): + settings = { + "engines_available": engines_available, # Lists the currently available TTS engines + "current_engine_loaded": model_engine.engine_loaded, # Lists the currently loaded TTS engine + "models_available": [{"name": name} for name in model_engine.available_models.keys()], # Lists the currently available models that can be loaded on this TTS engine. + "current_model_loaded": model_engine.current_model_loaded, # Lists the currently loaded model + "manufacturer_name": model_engine.manufacturer_name, # The name of the person/company/group who made the current TTS engine + "audio_format": model_engine.audio_format, # The default audio format the the currently loaded TTS engine generates in + "deepspeed_capable": model_engine.deepspeed_capable, # Is the TTS engine capable of supporting DeepSpeed + "deepspeed_available": model_engine.deepspeed_available, # Was DeepSpeed detected as being available for use + "deepspeed_enabled": model_engine.deepspeed_enabled, # Is DeepSpeed current enabled or disabled + "generationspeed_capable": model_engine.generationspeed_capable, # Does the TTS engine support changing speed of generated TTS + "generationspeed_set": model_engine.generationspeed_set, # What is the currently set TTS speed + "lowvram_capable": model_engine.lowvram_capable, # Does the TTS engine support moving the model between VRAM and System Ram + "lowvram_enabled": model_engine.lowvram_enabled, # What is the currently enabled/disabled state of low vram + "pitch_capable": model_engine.pitch_capable, # Does the TTS engine support pitch on tts generation + "pitch_set": model_engine.pitch_set, # What is the currently set value for pitch + "repetitionpenalty_capable": model_engine.repetitionpenalty_capable, # Does the TTS engine support differnt repitition penalties + "repetitionpenalty_set": model_engine.repetitionpenalty_set, # What is the currently set repetition penalty + "streaming_capable": model_engine.streaming_capable, # Does the model support streaming TTS generation + "temperature_capable": model_engine.temperature_capable, # Does the TTS engine support model temperature settings + "temperature_set": model_engine.temperature_set, # What is the currently set temperature + "ttsengines_installed": model_engine.engine_installed, # + "languages_capable": model_engine.languages_capable, # Does the TTS engine support multi languages + "multivoice_capable": model_engine.multivoice_capable, # Does the TTS engine support multi voice models + "multimodel_capable": model_engine.multimodel_capable, # Does the engine support loading many different models + } + return settings + +###################################### +# API Endpoint - /api/lowvramsetting # +###################################### +@app.post("/api/lowvramsetting") +async def apifunction_low_vram(request: Request, new_low_vram_value: bool): + if not model_engine.lowvram_capable: + return {"status": "error", "message": f"The currently loaded TTS engine '{model_engine.engine_loaded}' does not support lowvram."} + try: + if new_low_vram_value is None: + raise ValueError("Missing 'low_vram' parameter") + if model_engine.lowvram_enabled == new_low_vram_value: + return Response(content=json.dumps({"status": "success", "message": f"[{branding}Model] LowVRAM is already {'enabled' if new_low_vram_value else 'disabled'}.",})) + model_engine.lowvram_enabled = new_low_vram_value + await model_engine.unload_model() + + if model_engine.cuda_is_available: + if model_engine.lowvram_enabled: + model_engine.device = "cpu" + print(f"[{branding}Engine] \033[94mLowVRAM Enabled.\033[0m Model will move between \033[93mVRAM(cuda) <> System RAM(cpu)\033[0m") + else: + model_engine.device = "cuda" + print(f"[{branding}Engine] \033[94mLowVRAM Disabled.\033[0m Model will stay in \033[93mVRAM(cuda)\033[0m") + await model_engine.setup() + else: + # Handle the case where CUDA is not available + print(f"[{branding}Engine] \033[91mError:\033[0m Nvidia CUDA is not available on this system. Unable to use LowVRAM mode.") + model_engine.lowvram_enabled = False + + return Response(content=json.dumps({"status": "lowvram-success"})) + except Exception as e: + return Response(content=json.dumps({"status": "error", "message": str(e)})) + +################################# +# API Endpoint - /api/deepspeed # +################################# +@app.post("/api/deepspeed") +async def deepspeed(request: Request, new_deepspeed_value: bool): + if not model_engine.deepspeed_capable or not model_engine.deepspeed_available: + return {"status": "error", "message": f"The currently loaded TTS engine '{model_engine.engine_loaded}' does not support DeepSpeed or DeepSpeed is not available on this system."} + try: + if new_deepspeed_value is None: + raise ValueError("Missing 'deepspeed' parameter") + if model_engine.deepspeed_enabled == new_deepspeed_value: + return Response( + content=json.dumps({"status": "success", "message": f"DeepSpeed is already {'enabled' if new_deepspeed_value else 'disabled'}.",})) + model_engine.deepspeed_enabled = new_deepspeed_value + await model_engine.handle_deepspeed_change(new_deepspeed_value) + return Response(content=json.dumps({"status": "deepspeed-success"})) + except Exception as e: + return Response(content=json.dumps({"status": "error", "message": str(e)})) + +################################# +# API Endpoint - /api/voice2rvc # +################################# +@app.post("/api/voice2rvc") +async def voice2rvc(input_tts_path: str = Form(...), output_rvc_path: str = Form(...), pth_name: str = Form(...)): + try: + # Handle "Disabled" case + if pth_name.lower() in ["disabled", "disable"]: + print(f"[{branding}TTS] \033[94mVoice2RVC Convert: No voice was specified or the name was Disabled\033[0m") + return {"status": "error", "message": "No voice was specified or the name was Disabled"} + input_tts_path = Path(input_tts_path) + output_rvc_path = Path(output_rvc_path) + # Check if input file exists + if not input_tts_path.is_file(): + raise HTTPException(status_code=400, detail=f"Input file {input_tts_path} does not exist.") + # Define the model path based on the selected model name + pth_path = this_dir / "models" / "rvc_voices" / pth_name + # Check if model file exists + if not pth_path.is_file(): + raise HTTPException(status_code=400, detail=f"Model file {pth_path} does not exist.") + # Run the RVC conversion + result_path = run_voice2rvc(input_tts_path, output_rvc_path, pth_path) + if result_path: + return {"status": "success", "output_path": str(result_path)} + else: + raise HTTPException(status_code=500, detail="RVC conversion failed.") + except Exception as e: + print(f"Error during Voice2RVC conversion: {e}") + raise HTTPException(status_code=500, detail=str(e)) + +# Define the run_rvc function +def run_voice2rvc(input_tts_path, output_rvc_path, pth_path): + print(f"[{branding}GEN] \033[94mVoice2RVC Convert: Started\033[0m") + generate_start_time = time.time() + f0up_key = rvc_settings["pitch"] + filter_radius = rvc_settings["filter_radius"] + index_rate = rvc_settings["index_rate"] + rms_mix_rate = rvc_settings["rms_mix_rate"] + protect = rvc_settings["protect"] + hop_length = rvc_settings["hop_length"] + f0method = rvc_settings["f0method"] + split_audio = rvc_settings["split_audio"] + f0autotune = rvc_settings["autotune"] + embedder_model = rvc_settings["embedder_model"] + training_data_size = rvc_settings["training_data_size"] + # Convert path variables to strings + input_tts_path = str(input_tts_path) + pth_path = str(pth_path) + # Check if the model file exists + if not os.path.isfile(pth_path): + print(f"Model file {pth_path} does not exist. Exiting.") + return + # Get the directory of the model file + model_dir = os.path.dirname(pth_path) + # Find all .index files in the model directory + index_files = [file for file in os.listdir(model_dir) if file.endswith(".index")] + if len(index_files) == 1: + index_path = str(os.path.join(model_dir, index_files[0])) + else: + index_path = "" + # Call the infer_pipeline function + infer_pipeline(f0up_key, filter_radius, index_rate, rms_mix_rate, protect, hop_length, f0method, + input_tts_path, output_rvc_path, pth_path, index_path, split_audio, f0autotune, embedder_model, training_data_size, debug_rvc) + generate_end_time = time.time() + generate_elapsed_time = generate_end_time - generate_start_time + print(f"[{branding}GEN] \033[94mVoice2RVC Convert: \033[91m{generate_elapsed_time:.2f} seconds.\033[0m") + return output_rvc_path + +################################# +# API Endpoint - /api/voice2rvc # +################################# +@app.post("/api/voice2rvc") +async def voice2rvc(input_tts_path: str = Form(...), output_rvc_path: str = Form(...), pth_name: str = Form(...)): + try: + input_tts_path = Path(input_tts_path) + output_rvc_path = Path(output_rvc_path) + # Check if input file exists + if not input_tts_path.is_file(): + raise HTTPException(status_code=400, detail=f"Input file {input_tts_path} does not exist.") + # Define the model path based on the selected model name + pth_path = this_dir / "models" / "rvc_voices" / pth_name + # Check if model file exists + if not pth_path.is_file(): + raise HTTPException(status_code=400, detail=f"Model file {pth_path} does not exist.") + # Run the RVC conversion + result_path = run_voice2rvc(input_tts_path, output_rvc_path, pth_path) + if result_path: + return {"status": "success", "output_path": str(result_path)} + else: + raise HTTPException(status_code=500, detail="RVC conversion failed.") + except Exception as e: + print(f"Error during Voice2RVC conversion: {e}") + raise HTTPException(status_code=500, detail=str(e)) + +# Define the run_rvc function +def run_voice2rvc(input_tts_path, output_rvc_path, pth_path): + print(f"[{branding}GEN] \033[94mVoice2RVC Convert: Started\033[0m") + generate_start_time = time.time() + f0up_key = rvc_settings["pitch"] + filter_radius = rvc_settings["filter_radius"] + index_rate = rvc_settings["index_rate"] + rms_mix_rate = rvc_settings["rms_mix_rate"] + protect = rvc_settings["protect"] + hop_length = rvc_settings["hop_length"] + f0method = rvc_settings["f0method"] + split_audio = rvc_settings["split_audio"] + f0autotune = rvc_settings["autotune"] + embedder_model = rvc_settings["embedder_model"] + training_data_size = rvc_settings["training_data_size"] + # Convert path variables to strings + input_tts_path = str(input_tts_path) + pth_path = str(pth_path) + # Check if the model file exists + if not os.path.isfile(pth_path): + print(f"Model file {pth_path} does not exist. Exiting.") + return + # Get the directory of the model file + model_dir = os.path.dirname(pth_path) + # Find all .index files in the model directory + index_files = [file for file in os.listdir(model_dir) if file.endswith(".index")] + if len(index_files) == 1: + index_path = str(os.path.join(model_dir, index_files[0])) + else: + index_path = "" + # Call the infer_pipeline function + infer_pipeline(f0up_key, filter_radius, index_rate, rms_mix_rate, protect, hop_length, f0method, + input_tts_path, output_rvc_path, pth_path, index_path, split_audio, f0autotune, embedder_model, training_data_size, debug_rvc) + generate_end_time = time.time() + generate_elapsed_time = generate_end_time - generate_start_time + print(f"[{branding}GEN] \033[94mVoice2RVC Convert: \033[91m{generate_elapsed_time:.2f} seconds.\033[0m") + return output_rvc_path + +################################## +# Transcode between file formats # +################################## +async def transcode_audio(input_file, output_format): + print(f"[{branding}Debug] *************************************************",) + print(f"[{branding}Debug] transcode_audio function called (debug_transcode)",) + print(f"[{branding}Debug] *************************************************",) + print(f"[{branding}Debug] Input file : {input_file}") if debug_transcode else None + print(f"[{branding}Debug] Output format : {output_format}") if debug_transcode else None + if output_format == "Disabled": + print(f"[{branding}Debug] Transcode format is set to Disabled so skipping transcode.") if debug_transcode else None + return input_file + if not ffmpeg_installed: + print(f"[{branding}TTS] FFmpeg is not installed. Format conversion is not possible.") + raise Exception("FFmpeg is not installed. Format conversion is not possible.") + # Get the file extension of the input file + input_extension = os.path.splitext(input_file)[1][1:].lower() + print(f"[{branding}Debug] Input file extension: {input_extension}") if debug_transcode else None + # Check if the input extension matches the requested output format + if input_extension == output_format.lower(): + print(f"[{branding}Debug] Input file is already in the requested format: {output_format}") if debug_transcode else None + return input_file + output_file = os.path.splitext(input_file)[0] + f".{output_format}" + print(f"[{branding}Debug] Output file: {output_file}") if debug_transcode else None + ffmpeg_path = "ffmpeg" # Default path for Linux and macOS + if sys.platform == "win32": + ffmpeg_path = os.path.join(this_dir, "system", "win_ffmpeg", "ffmpeg.exe") + ffmpeg = (FFmpeg(ffmpeg_path).option("y").input(input_file).output(output_file)) + print(f"[{branding}Debug] Transcoding to {output_format}") if debug_transcode else None + if output_format == "opus": + print(f"[{branding}Debug] Configuring Opus options") if debug_transcode else None + ffmpeg.output(output_file, {"codec:a": "libopus", "b:a": "128k", "vbr": "on", "compression_level": 10, "frame_duration": 60, "application": "voip"}) + elif output_format == "aac": + print(f"[{branding}Debug] Configuring AAC options") if debug_transcode else None + ffmpeg.output(output_file, {"codec:a": "aac", "b:a": "192k"}) + elif output_format == "flac": + print(f"[{branding}Debug] Configuring FLAC options") if debug_transcode else None + ffmpeg.output(output_file, {"codec:a": "flac", "compression_level": 8}) + elif output_format == "wav": + print(f"[{branding}Debug] Configuring WAV options") if debug_transcode else None + ffmpeg.output(output_file, {"codec:a": "pcm_s16le"}) + elif output_format == "mp3": + print(f"[{branding}Debug] Configuring MP3 options") if debug_transcode else None + ffmpeg.output(output_file, {"codec:a": "libmp3lame", "b:a": "192k"}) + else: + print(f"[{branding}TTS] Unsupported output format: {output_format}") + raise ValueError(f"Unsupported output format: {output_format}") + try: + print(f"[{branding}Debug] Starting transcoding process") if debug_transcode else None + await ffmpeg.execute() + print(f"[{branding}Debug] Transcoding completed successfully") if debug_transcode else None + except Exception as e: + print(f"[{branding}TTS] Error occurred during transcoding: {str(e)}") + raise + print(f"[{branding}Debug] Deleting original input file") if debug_transcode else None + os.remove(input_file) + print(f"[{branding}Debug] Transcoding process completed") if debug_transcode else None + print(f"[{branding}Debug] Transcoded file: {output_file}") if debug_transcode else None + return output_file + +############################## +# Central Transcode function # +############################## +async def transcode_audio_if_necessary(output_file, model_audio_format, output_audio_format): + if debug_transcode: + print(f"[{branding}Debug] model_engine.audio_format is:", model_audio_format) + print(f"[{branding}Debug] audio format is:", output_audio_format) + print(f"[{branding}Debug] Entering the transcode condition") + try: + if debug_transcode: + print(f"[{branding}Debug] Calling transcode_audio function") + output_file = await transcode_audio(output_file, output_audio_format) + if debug_transcode: + print(f"[{branding}Debug] Transcode completed successfully") + except Exception as e: + print(f"[{branding}Debug] Error occurred during transcoding:", str(e)) + raise + if debug_transcode: + print(f"[{branding}Debug] Transcode condition completed") + # Update the output file path and URLs based on the transcoded file + if debug_tts: + print(f"[{branding}Debug] Updating output file paths and URLs") + if params["api_def"]["api_use_legacy_api"]: + output_file_url = f'http://{api_defaults["api_legacy_ip_address"]}:{api_defaults["api_port_number"]}/audio/{os.path.basename(output_file)}' + output_cache_url = f'http://{api_defaults["api_legacy_ip_address"]}:{api_defaults["api_port_number"]}/audiocache/{os.path.basename(output_file)}' + else: + output_file_url = f'/audio/{os.path.basename(output_file)}' + output_cache_url = f'/audiocache/{os.path.basename(output_file)}' + if debug_tts: + print(f"[{branding}Debug] Output file paths and URLs updated") + return output_file, output_file_url, output_cache_url + +############################## +#### Streaming Generation #### +############################## +@app.get("/api/tts-generate-streaming", response_class=StreamingResponse) +async def apifunction_generate_streaming(text: str, voice: str, language: str, output_file: str): + if model_engine.streaming_capable == False: + print(f"[{branding}GEN] Streaming is unavailable for this TTS Engine") + try: + output_file_path = f'{this_dir / output_directory / output_file}.{model_engine.audio_format}' + stream = await generate_audio(text, voice, language, model_engine.temperature_set, model_engine.repetitionpenalty_set, 1.0, 1.0, output_file_path, streaming=True) + return StreamingResponse(stream, media_type="audio/wav") + except Exception as e: + print(f"An error occurred: {e}") + return JSONResponse(content={"error": "An error occurred"}, status_code=500) + +@app.post("/api/tts-generate-streaming", response_class=JSONResponse) +async def tts_generate_streaming(request: Request, text: str = Form(...), voice: str = Form(...), language: str = Form(...), output_file: str = Form(...)): + if model_engine.streaming_capable == False: + print(f"[{branding}GEN] Streaming is unavailable for this TTS Engine") + try: + output_file_path = f'{this_dir / output_directory / output_file}.{model_engine.audio_format}' + await generate_audio(text, voice, language, model_engine.temperature_set, model_engine.repetitionpenalty_set, "1.0", "1.0", output_file_path, streaming=False) + return JSONResponse(content={"output_file_path": str(output_file)}, status_code=200) + except Exception as e: + print(f"An error occurred: {e}") + return JSONResponse(content={"error": "An error occurred"}, status_code=500) + +################################### +# Central generate_audio function # +################################### +async def generate_audio(text, voice, language, temperature, repetition_penalty, speed, pitch, output_file, streaming=False): + # Get the async generator from the internal function + response = model_engine.generate_tts(text, voice, language, temperature, repetition_penalty, speed, pitch, output_file, streaming) + # If streaming, then return the generator as-is, otherwise just exhaust it and return + if streaming: + return response + async for _ in response: + pass + +########################### +#### PREVIEW VOICE API #### +########################### +@app.post("/api/previewvoice/", response_class=JSONResponse) +async def apifunction_preview_voice(request: Request, voice: str = Form(...)): + try: + # Hardcoded settings + language = "en" + output_file_name = "api_preview_voice" + # Clean the voice filename for inclusion in the text + clean_voice_filename = re.sub(r'\.wav$', '', voice.replace(' ', '_')) + clean_voice_filename = re.sub(r'[^a-zA-Z0-9]', ' ', clean_voice_filename) + # Generate the audio + text = f"Hello, this is a preview of voice {clean_voice_filename}." + # Generate the audio + output_file_path = this_dir / output_directory / f'{output_file_name}.{model_engine.audio_format}' + await generate_audio(text, voice, language, model_engine.temperature_set, model_engine.repetitionpenalty_set, 1.0, 0, output_file_path, streaming=False) + # Generate the URL + output_file_url = f'/audio/{output_file_name}.{model_engine.audio_format}' + # Return the response with both local file path and URL + return JSONResponse( + content={ + "status": "generate-success", + "output_file_path": str(output_file_path), + "output_file_url": str(output_file_url), + }, + status_code=200, + ) + except Exception as e: + print(f"An error occurred: {e}") + return JSONResponse(content={"error": "An error occurred"}, status_code=500) + +################################################################## +# API Endpoint - OpenAI Speech API compatable endpoint Validator # +################################################################## +class OpenAIInput(BaseModel): + model: str = Field(..., description="The TTS model to use. Currently ignored.") + input: str = Field(..., max_length=4096, description="The text to generate audio for.") + voice: str = Field(..., description="The voice to use when generating the audio.") + response_format: str = Field(default="wav", description="The format of the audio. Currently only 'wav' is supported.") + speed: float = Field(default=1.0, ge=0.25, le=4.0, description="The speed of the generated audio.") + + @field_validator('voice') + def validate_voice(cls, value): + supported_voices = ["alloy", "echo", "fable", "nova", "onyx", "shimmer"] + if value not in supported_voices: + raise ValueError(f'Voice must be one of {supported_voices}') + return value + +class OpenAIGenerator: + @staticmethod + def validate_input(json_data: dict) -> Union[None, str]: + try: + OpenAIInput(**json_data) + return None + except ValidationError as e: + errors = [] + for err in e.errors(): + field = err['loc'][0] + message = err['msg'] + description = OpenAIInput.model_fields[field].field_info.description + errors.append(f"Error in field '{field}': {message}. Description: {description}") + return ', '.join(errors) + +######################################################################### +# API Endpoint - OpenAI Speech API compatable endpoint /v1/audio/speech # +######################################################################### +@app.post("/v1/audio/speech", response_class=JSONResponse) +async def openai_tts_generate(request: Request): + try: + json_data = await request.json() + print(f"[{branding}Debug] Received JSON data: {json_data}") if debug_openai else None + validation_error = OpenAIGenerator.validate_input(json_data) + if validation_error: + print(f"Validation error: {validation_error}") if debug_openai else None + return JSONResponse(content={"error": validation_error}, status_code=400) + model = json_data["model"] # Currently ignored + input_text = json_data["input"] + voice = json_data["voice"] + response_format = json_data["response_format"].lower() + speed = json_data["speed"] + print(f"[{branding}Debug] Input text: {input_text}") if debug_openai else None + print(f"[{branding}Debug] Voice: {voice}") if debug_openai else None + print(f"[{branding}Debug] Speed: {speed}") if debug_openai else None + cleaned_string = html.unescape(standard_filtering(input_text)) + # Map the OpenAI voice to the corresponding internal voice + voice_mapping = { + "alloy": model_engine.openai_alloy, + "echo": model_engine.openai_echo, + "fable": model_engine.openai_fable, + "nova": model_engine.openai_nova, + "onyx": model_engine.openai_onyx, + "shimmer": model_engine.openai_shimmer + } + mapped_voice = voice_mapping.get(voice) + if not mapped_voice: + print(f"Unsupported voice: {voice}") + return JSONResponse(content={"error": "Unsupported voice"}, status_code=400) + print(f"[{branding}Debug]Mapped voice: {mapped_voice}") if debug_openai else None + # Generate the audio + # Generate a unique filename + unique_id = uuid.uuid4() + timestamp = int(time.time()) + output_file_path = f'{this_dir / output_directory / f"openai_output_{unique_id}_{timestamp}.{model_engine.audio_format}"}' + await generate_audio(cleaned_string, mapped_voice, "en", model_engine.temperature_set, + model_engine.repetitionpenalty_set, speed, model_engine.pitch_set, + output_file_path, streaming=False) + print(f"[{branding}Debug] Audio generated at: {output_file_path}") if debug_openai else None + if rvc_settings["rvc_enabled"]: + if rvc_settings["rvc_char_model_file"].lower() in ["disabled", "disable"]: + print(f"[{branding}Debug] Pass rvccharacter_voice_gen") if debug_openai else None + pass # Skip RVC processing for character part + else: + print(f"[{branding}Debug] send to rvc") if debug_openai else None + pth_path = this_dir / "models" / "rvc_voices" / rvc_settings["rvc_char_model_file"] + run_rvc(output_file_path, pth_path, infer_pipeline) + # Transcode the audio to the requested format + transcoded_file_path = await transcode_for_openai(output_file_path, response_format) + print(f"[{branding}Debug] Audio transcoded to: {transcoded_file_path}") if debug_openai else None + # Return the audio file + return FileResponse(transcoded_file_path, media_type=f"audio/{response_format}", filename=f"output.{response_format}") + except Exception as e: + print(f"[{branding}Debug] An error occurred: {str(e)}") if debug_openai else None + return JSONResponse(content={"error": "An error occurred"}, status_code=500) + +########################################################################### +# API Endpoint - OpenAI Speech API compatable endpoint Transcode Function # +########################################################################### +async def transcode_for_openai(input_file, output_format): + print(f"[{branding}Debug] ************************************") if debug_openai else None + print(f"[{branding}Debug] transcode_for_openai function called") if debug_openai else None + print(f"[{branding}Debug] ************************************") if debug_openai else None + print(f"[{branding}Debug] Input file : {input_file}") if debug_openai else None + print(f"[{branding}Debug] Output format : {output_format}") if debug_openai else None + if not ffmpeg_installed: + print(f"[{branding}TTS] FFmpeg is not installed. Format conversion is not possible.") + raise Exception("FFmpeg is not installed. Format conversion is not possible.") + # Get the file extension of the input file + input_extension = os.path.splitext(input_file)[1][1:].lower() + print(f"[{branding}Debug] Input file extension: {input_extension}") if debug_openai else None + # Check if the input extension matches the requested output format + if input_extension == output_format.lower(): + print(f"[{branding}Debug] Input file is already in the requested format: {output_format}") if debug_openai else None + return input_file + output_file = os.path.splitext(input_file)[0] + f".{output_format}" + print(f"[{branding}Debug] Output file: {output_file}") if debug_openai else None + ffmpeg_path = "ffmpeg" # Default path for Linux and macOS + if sys.platform == "win32": + ffmpeg_path = os.path.join(this_dir, "system", "win_ffmpeg", "ffmpeg.exe") + ffmpeg = (FFmpeg(ffmpeg_path).option("y").input(input_file).output(output_file)) + print(f"[{branding}Debug] Transcoding to {output_format}") if debug_openai else None + if output_format == "opus": + print(f"[{branding}Debug] Configuring Opus options") if debug_openai else None + ffmpeg.output(output_file, {"codec:a": "libopus", "b:a": "128k", "vbr": "on", "compression_level": 10, "frame_duration": 60, "application": "voip"}) + elif output_format == "aac": + print(f"[{branding}Debug] Configuring AAC options") if debug_openai else None + ffmpeg.output(output_file, {"codec:a": "aac", "b:a": "192k"}) + elif output_format == "flac": + print(f"[{branding}Debug] Configuring FLAC options") if debug_openai else None + ffmpeg.output(output_file, {"codec:a": "flac", "compression_level": 8}) + elif output_format == "wav": + print(f"[{branding}Debug] Configuring WAV options") if debug_openai else None + ffmpeg.output(output_file, {"codec:a": "pcm_s16le"}) + elif output_format == "mp3": + print(f"[{branding}Debug] Configuring MP3 options") if debug_openai else None + ffmpeg.output(output_file, {"codec:a": "libmp3lame", "b:a": "192k"}) + elif output_format in ["ogg", "m4a"]: + if output_format == "ogg": + print(f"[{branding}Debug] Configuring OGG options") if debug_openai else None + ffmpeg.output(output_file, {"codec:a": "libvorbis"}) + elif output_format == "m4a": + print(f"[{branding}Debug] Configuring M4A options") if debug_openai else None + ffmpeg.output(output_file, {"codec:a": "aac", "b:a": "192k"}) + else: + print(f"[{branding}TTS] Unsupported output format: {output_format}") + raise ValueError(f"Unsupported output format: {output_format}") + try: + print(f"[{branding}Debug] Starting transcoding process") if debug_openai else None + await ffmpeg.execute() + print(f"[{branding}Debug] Transcoding completed successfully") if debug_openai else None + except Exception as e: + print(f"[{branding}TTS] Error occurred during transcoding: {str(e)}") + raise + print(f"[{branding}Debug] Transcoding process completed") if debug_openai else None + print(f"[{branding}Debug] Transcoded file: {output_file}") if debug_openai else None + return output_file + + +####################### +# Play at the console # +####################### +def play_audio(file_path, volume): + print(f"[{branding}GEN] \033[94m File path is: {file_path}") if debug_tts else None + print(f"[{branding}GEN] \033[94m Volume is: {volume}") if debug_tts else None + normalized_file_path = os.path.normpath(file_path) + print(f"[{branding}GEN] \033[94m Normalized file path is: {normalized_file_path}") if debug_tts else None + directory = os.path.dirname(normalized_file_path) + if os.path.isdir(directory): + print(f"[{branding}GEN] \033[94m Directory contents: {os.listdir(directory)}") if debug_tts else None + else: + print(f"[{branding}GEN] \033[94mError: Directory does not exist: {directory}") if debug_tts else None + if not os.path.isfile(normalized_file_path): + print(f"[{branding}GEN] \033[94mError: File does not exist: {normalized_file_path}\033[0m") if debug_tts else None + return + # Check for AAC format + if normalized_file_path.lower().endswith('.aac'): + print(f"[{branding}GEN] \033[94mPlay Audio : \033[0mAAC format files cannot be played at the console. Please choose another format") + return + try: + print(f"[{branding}GEN] \033[94mPlay Audio : \033[0mPlaying audio at console") + data, fs = sf.read(normalized_file_path) + sd.play(volume * data, fs) + sd.wait() + except Exception as e: + print(f"[{branding}GEN] \033[94mError playing audio file: {e}\033[0m") + +class Request(BaseModel): + # Define the structure of the 'Request' class if needed + pass + + +class TTSGenerator: + @staticmethod + def validate_json_input(json_data: Union[Dict, str]) -> Union[None, str]: + try: + if isinstance(json_data, str): + json_data = json.loads(json_data) + JSONInput(**json_data) + return None # JSON is valid + except ValidationError as e: + return str(e) + +################################################################# +# /api/tts-generate Generation API Endpoint Narration Filtering # +################################################################# +def process_text(text): + # Normalize HTML encoded quotes + text = html.unescape(text) + # Replace ellipsis with a single dot + text = re.sub(r'\.{3,}', '.', text) + # Pattern to identify combined narrator and character speech + combined_pattern = r'(\*[^*"]+\*|"[^"*]+")' + # List to hold parts of speech along with their type + ordered_parts = [] + # Track the start of the next segment + start = 0 + # Find all matches + for match in re.finditer(combined_pattern, text): + # Add the text before the match, if any, as ambiguous + if start < match.start(): + ambiguous_text = text[start:match.start()].strip() + if ambiguous_text: + ordered_parts.append(('ambiguous', ambiguous_text)) + # Add the matched part as either narrator or character + matched_text = match.group(0) + if matched_text.startswith('*') and matched_text.endswith('*'): + ordered_parts.append(('narrator', matched_text.strip('*').strip())) + elif matched_text.startswith('"') and matched_text.endswith('"'): + ordered_parts.append(('character', matched_text.strip('"').strip())) + else: + # In case of mixed or improperly formatted parts + if '*' in matched_text: + ordered_parts.append(('narrator', matched_text.strip('*').strip('"'))) + else: + ordered_parts.append(('character', matched_text.strip('"').strip('*'))) + # Update the start of the next segment + start = match.end() + # Add any remaining text after the last match as ambiguous + if start < len(text): + ambiguous_text = text[start:].strip() + if ambiguous_text: + ordered_parts.append(('ambiguous', ambiguous_text)) + return ordered_parts + +def standard_filtering(text_input): + text_output = (text_input + .replace("***", "") + .replace("**", "") + .replace("*", "") + .replace("\n\n", "\n") + .replace("'", "'") + ) + return text_output + +################################################################# +# /api/tts-generate Generation API Endpoint Narration Combining # +################################################################# +def combine(output_file_timestamp, output_file_name, audio_files, target_sample_rate=44100): + audio = np.array([]) + try: + for audio_file in audio_files: + normalized_audio_file = os.path.normpath(audio_file) + print(f"[{branding}Debug] Processing file: {normalized_audio_file}") if debug_concat else None + # Check if the file exists + if not os.path.isfile(normalized_audio_file): + print(f"[{branding}Debug] Error: File does not exist: {normalized_audio_file}") if debug_concat else None + return None, None + # Read the audio file + audio_data, current_sample_rate = sf.read(normalized_audio_file) + print(f"[{branding}Debug] Read file: {normalized_audio_file}, Sample rate: {current_sample_rate}, Data shape: {audio_data.shape}") if debug_concat else None + # Resample if necessary + if current_sample_rate != target_sample_rate: + print(f"[{branding}Debug] Resampling file from {current_sample_rate} to {target_sample_rate} Hz") if debug_concat else None + audio_data = librosa.resample(audio_data, orig_sr=current_sample_rate, target_sr=target_sample_rate) + # Concatenate audio data + if audio.size == 0: + audio = audio_data + else: + audio = np.concatenate((audio, audio_data)) + if output_file_timestamp: + timestamp = int(time.time()) + output_file_path = os.path.join(this_dir / "outputs" / f'{output_file_name}_{timestamp}_combined.wav') + sf.write(output_file_path, audio, target_sample_rate) + # Legacy API or New API return + if params["api_def"]["api_use_legacy_api"]: + output_file_url = f"http://{api_defaults['api_legacy_ip_address']}:{params['api_def']['api_port_number']}/audio/{output_file_name}_{timestamp}_combined.wav" + output_cache_url = f"http://{api_defaults['api_legacy_ip_address']}:{params['api_def']['api_port_number']}/audiocache/{output_file_name}_{timestamp}_combined.wav" + else: + output_file_url = f'/audio/{output_file_name}_{timestamp}_combined.wav' + output_cache_url = f'/audiocache/{output_file_name}_{timestamp}_combined.wav' + else: + output_file_path = os.path.join(this_dir / "outputs" / f'{output_file_name}_combined.wav') + sf.write(output_file_path, audio, target_sample_rate) + # Legacy API or New API return + if params["api_def"]["api_use_legacy_api"]: + output_file_url = f"http://{api_defaults['api_legacy_ip_address']}:{api_defaults['api_port_number']}/audio/{output_file_name}_combined.wav" + output_cache_url = f"http://{api_defaults['api_legacy_ip_address']}:{api_defaults['api_port_number']}/audiocache/{output_file_name}_combined.wav" + else: + output_file_url = f'/audio/{output_file_name}_combined.wav' + output_cache_url = f'/audiocache/{output_file_name}_combined.wav' + print(f"[{branding}Debug] Output file changed to:", output_file_path) if debug_concat else None + print(f"[{branding}Debug] Output file changed to:", output_file_url) if debug_concat else None + print(f"[{branding}Debug] Output file changed to:", output_cache_url) if debug_concat else None + except Exception as e: + print(f"Error occurred: {e}") + return output_file_path, output_file_url, output_cache_url + +################################## +# Central RVC Generation request # +################################## +def run_rvc(input_tts_path, pth_path, infer_pipeline): + generate_start_time = time.time() + f0up_key = rvc_settings["pitch"] + filter_radius = rvc_settings["filter_radius"] + index_rate = rvc_settings["index_rate"] + rms_mix_rate = rvc_settings["rms_mix_rate"] + protect = rvc_settings["protect"] + hop_length = rvc_settings["hop_length"] + f0method = rvc_settings["f0method"] + split_audio = rvc_settings["split_audio"] + f0autotune = rvc_settings["autotune"] + embedder_model = rvc_settings["embedder_model"] + training_data_size = rvc_settings["training_data_size"] + # Convert path variables to strings + input_tts_path = str(input_tts_path) + pth_path = str(pth_path) + # Check if the model file exists + if not os.path.isfile(pth_path): + print(f"Model file {pth_path} does not exist. Exiting.") + return + # Get the directory of the model file + model_dir = os.path.dirname(pth_path) + # Get the filename of pth_path + pth_filename = os.path.basename(pth_path) + # Find all .index files in the model directory + index_files = [file for file in os.listdir(model_dir) if file.endswith(".index")] + if len(index_files) == 1: + index_path = str(os.path.join(model_dir, index_files[0])) + # Get the filename of index_path + index_filename = os.path.basename(index_path) + index_filename_print = index_filename + index_size_print = training_data_size + elif len(index_files) > 1: + print(f"[{branding}GEN] \033[94mRVC Convert :\033[0m Multiple RVC index files found in the models folder where \033[93m{pth_filename}\033[0m is") + print(f"[{branding}GEN] \033[94mRVC Convert :\033[0m located. Unable to determine which index to use. Continuing without an index file.") + index_path = "" + index_filename = None + index_filename_print = "None used" + index_size_print = "N/A" + else: + index_path = "" + index_filename = None + index_filename_print = "None used" + index_size_print = "N/A" + # Set the output path to be the same as the input path + output_rvc_path = input_tts_path + if debug_rvc: + debug_lines = [ + f"[{branding}Debug] ************************************", + f"[{branding}Debug] run_rvc function called (debug_rvc)", + f"[{branding}Debug] ***********************************", + f"[{branding}Debug] f0up_key : {f0up_key}", + f"[{branding}Debug] filter_radius : {filter_radius}", + f"[{branding}Debug] index_rate : {index_rate}", + f"[{branding}Debug] rms_mix_rate : {rms_mix_rate}", + f"[{branding}Debug] protect : {protect}", + f"[{branding}Debug] hop_length : {hop_length}", + f"[{branding}Debug] f0method : {f0method}", + f"[{branding}Debug] input_tts_path : {input_tts_path}", + f"[{branding}Debug] output_rvc_path : {output_rvc_path}", + f"[{branding}Debug] pth_path : {pth_path}", + f"[{branding}Debug] index_path : {index_path}", + f"[{branding}Debug] split_audio : {split_audio}", + f"[{branding}Debug] f0autotune : {f0autotune}", + f"[{branding}Debug] embedder_model : {embedder_model}", + f"[{branding}Debug] training_data_size: {training_data_size}" + ] + debug_info = "\n".join(debug_lines) + print(debug_info) + # Call the infer_pipeline function + infer_pipeline(f0up_key, filter_radius, index_rate, rms_mix_rate, protect, hop_length, f0method, + input_tts_path, output_rvc_path, pth_path, index_path, split_audio, f0autotune, embedder_model, training_data_size, debug_rvc) + generate_end_time = time.time() + generate_elapsed_time = generate_end_time - generate_start_time + print(f"[{branding}GEN] \033[94mRVC Convert : \033[94mModel: \033[93m{pth_filename} \033[94mIndex: \033[93m{index_filename_print}\033[0m") + print(f"[{branding}GEN] \033[94mRVC Convert : \033[93m{generate_elapsed_time:.2f} seconds. \033[94mMethod: \033[93m{f0method} \033[94mIndex size used \033[93m{index_size_print}\033[0m") + return + +#################################################################### +# /api/tts-generate Generation API Endpoint Narration RVC handling # +#################################################################### +def process_rvc_narrator(part_type, voice_gen, default_model_file, output_file, infer_pipeline): + if voice_gen.lower() in ["disabled", "disable"]: + return + else: + voice_gen_path = this_dir / "models" / "rvc_voices" / voice_gen + pth_path = voice_gen_path if voice_gen else default_model_file + run_rvc(output_file, pth_path, infer_pipeline) + +############################################################# +# /api/tts-generate Generation API Endpoint JSON validation # +############################################################# +class JSONInput(BaseModel): + text_input: str = Field(..., max_length=int(api_defaults["api_max_characters"]), description=f"text_input needs to be {api_defaults['api_max_characters']} characters or less.") + text_filtering: str = Field(..., pattern="^(none|standard|html)$", description="text_filtering needs to be 'none', 'standard' or 'html'.") + character_voice_gen: str = Field(..., pattern=r'^[\(\)a-zA-Z0-9\_\-./\s]+$', description="character_voice_gen needs to be the name of a valid voice for the loaded TTS engine.") + rvccharacter_voice_gen: str = Field(..., description="rvccharacter_voice_gen needs to be the name of a valid pth file in the 'folder\\file.pth' format or the word 'Disabled'.") + narrator_enabled: str = Field(..., pattern="^(true|false|silent)$", description="narrator_enabled needs to be 'true', 'false' or 'silent'.") + narrator_voice_gen: str = Field(..., pattern=r'^[\(\)a-zA-Z0-9\_\-./\s]+$', description="character_voice_gen needs to be the name of a valid voice for the loaded TTS engine.") + rvcnarrator_voice_gen: str = Field(..., description="rvcnarrator_voice_gen needs to be the name of a valid pth file in the 'folder\\file.pth' format or the word 'Disabled'.") + text_not_inside: str = Field(..., pattern="^(character|narrator|silent)$", description="text_not_inside needs to be 'character', 'narrator' or 'silent'.") + language: str = Field(..., pattern="^(ar|zh-cn|cs|nl|en|fr|de|hu|hi|it|ja|ko|pl|pt|ru|es|tr)$", description="language needs to be one of the following: ar, zh-cn, cs, nl, en, fr, de, hu, hi, it, ja, ko, pl, pt, ru, es, tr.") + output_file_name: str = Field(..., pattern="^[a-zA-Z0-9_]+$", description="output_file_name needs to be the name without any special characters or file extension, e.g., 'filename'.") + output_file_timestamp: bool = Field(..., description="output_file_timestamp needs to be true or false.") + autoplay: bool = Field(..., description="autoplay needs to be a true or false value.") + autoplay_volume: float = Field(..., ge=0.1, le=1.0, description="autoplay_volume needs to be from 0.1 to 1.0.") + speed: float = Field(..., ge=0.25, le=2.0, description="speed needs to be between 0.25 and 2.0.") + pitch: float = Field(..., ge=-10, le=10, description="pitch needs to be between -10 and 10.") + temperature: float = Field(..., ge=0.1, le=1.0, description="temperature needs to be between 0.1 and 1.0.") + repetition_penalty: float = Field(..., ge=1.0, le=20.0, description="repetition_penalty needs to be between 1.0 and 20.0.") + + @classmethod + def validate_autoplay_volume(cls, value): + if not (0.1 <= value <= 1.0): + raise ValueError("Autoplay volume must be between 0.1 and 1.0") + return value + + def validate_rvccharacter_voice_gen(cls, v): + if v.lower() == "disabled": + return v + pattern = re.compile(r'^.*\.(pth)$') + if not pattern.match(v): + raise ValueError("rvccharacter_voice_gen needs to be the name of a valid pth file in the 'folder\\file.pth' format or 'Disabled'.") + return v + + def validate_rvccharacter_voice_gen(cls, v): + if v.lower() == "disabled": + return v + pattern = re.compile(r'^.*\.(pth)$') + if not pattern.match(v): + raise ValueError("rvcnarrator_voice_gen needs to be the name of a valid pth file in the 'folder\\file.pth' format or 'Disabled'.") + return v + +def validate_json_input(json_input_data): + try: + JSONInput(**json_input_data) + except ValidationError as e: + error_messages = [] + for error in e.errors(): + field = error["loc"][0] + description = JSONInput.__fields__[field].description + error_messages.append(f"{field}: {description}") + error_message = "\n".join(error_messages) + print(f"[{branding}API] \033[91mError with API request:\033[0m", error_message) + return error_message + return None + +############################################################### +# /api/tts-generate Generation API Endpoint pull fresh values # +############################################################### +def get_api_text_filtering(): + return api_defaults["api_text_filtering"] +def get_api_narrator_enabled(): + return api_defaults["api_narrator_enabled"] +def get_api_text_not_inside(): + return api_defaults["api_text_not_inside"] +def get_api_language(): + return api_defaults["api_language"] +def get_api_output_file_name(): + return api_defaults["api_output_file_name"] +def get_api_output_file_timestamp(): + return api_defaults["api_output_file_timestamp"] +def get_api_autoplay(): + return api_defaults["api_autoplay"] +def get_api_autoplay_volume(): + return api_defaults["api_autoplay_volume"] +def get_params_speed(): + return model_engine.generationspeed_set +def get_params_temperature(): + return model_engine.temperature_set +def get_params_repetition(): + return model_engine.repetitionpenalty_set +def get_params_pitch(): + return model_engine.pitch_set +def get_character_voice_gen(): + return model_engine.def_character_voice +def get_rvccharacter_voice_gen(): + return rvc_settings["rvc_char_model_file"] +def get_narrator_voice_gen(): + return model_engine.def_narrator_voice +def get_rvcnarrator_voice_gen(): + return rvc_settings["rvc_narr_model_file"] + +############################################# +# /api/tts-generate Generation API Endpoint # +############################################# +@app.post("/api/tts-generate", response_class=JSONResponse) +async def apifunction_generate_tts_standard( + text_input: str = Form(...), + text_filtering: str = Form(None), + character_voice_gen: str = Form(None), + rvccharacter_voice_gen: str = Form(None), + narrator_enabled: str = Form(None), + narrator_voice_gen: str = Form(None), + rvcnarrator_voice_gen: str = Form(None), + text_not_inside: str = Form(None), + language: str = Form(None), + output_file_name: str = Form(None), + output_file_timestamp: bool = Form(None), + autoplay: bool = Form(None), + autoplay_volume: float = Form(None), + streaming: bool = Form(False), + speed: float = Form(None), + temperature: float = Form(None), + repetition_penalty: float = Form(None), + pitch: float = Form(None), + _text_filtering: str = Depends(get_api_text_filtering), + _character_voice_gen: str = Depends(get_character_voice_gen), + _rvccharacter_voice_gen: str = Depends(get_rvccharacter_voice_gen), + _narrator_enabled: str = Depends(get_api_narrator_enabled), + _narrator_voice_gen: str = Depends(get_narrator_voice_gen), + _rvcnarrator_voice_gen: str = Depends(get_rvcnarrator_voice_gen), + _text_not_inside: str = Depends(get_api_text_not_inside), + _language: str = Depends(get_api_language), + _output_file_name: str = Depends(get_api_output_file_name), + _output_file_timestamp: bool = Depends(get_api_output_file_timestamp), + _autoplay: bool = Depends(get_api_autoplay), + _autoplay_volume: float = Depends(get_api_autoplay_volume), + _speed: float = Depends(get_params_speed), + _temperature: float = Depends(get_params_temperature), + _repetition_penalty: float = Depends(get_params_repetition), + _pitch: float = Depends(get_params_pitch), +): + if debug_tts or debug_tts_variables: + debug_lines = [ + f"[{branding}Debug] *******************************************************", + f"[{branding}Debug] /api/tts_generate function called (debug_tts_variables)", + f"[{branding}Debug] **** PRE validation checks ****", + f"[{branding}Debug] *******************************************************", + f"[{branding}Debug] Defaults will be pulled from Settings if not specified in API Request!", + f"[{branding}Debug] max_characters : {api_defaults['api_max_characters']}", + f"[{branding}Debug] length_stripping : {api_defaults['api_length_stripping']}", + f"[{branding}Debug] legacy_api : {api_defaults['api_use_legacy_api']}", + f"[{branding}Debug] legacy_api IPadd : {api_defaults['api_legacy_ip_address']}", + f"[{branding}Debug] allowed_filter : {api_defaults['api_allowed_filter']}", + f"[{branding}Debug] text_filtering : {text_filtering}", + f"[{branding}Debug] character_voice_gen : {character_voice_gen}", + f"[{branding}Debug] rvccharacter_voice_gen : {rvccharacter_voice_gen}", + f"[{branding}Debug] narrator_enabled : {narrator_enabled}", + f"[{branding}Debug] narrator_voice_gen : {narrator_voice_gen}", + f"[{branding}Debug] rvcnarrator_voice_gen : {rvcnarrator_voice_gen}", + f"[{branding}Debug] text_not_inside : {text_not_inside}", + f"[{branding}Debug] language : {language}", + f"[{branding}Debug] output_file_name : {output_file_name}", + f"[{branding}Debug] output_file_timestamp : {output_file_timestamp}", + f"[{branding}Debug] autoplay : {autoplay}", + f"[{branding}Debug] autoplay_volume : {autoplay_volume}", + f"[{branding}Debug] streaming : {streaming}", + f"[{branding}Debug] speed : {speed}", + f"[{branding}Debug] temperature : {temperature}", + f"[{branding}Debug] repetition_penalty : {repetition_penalty}", + f"[{branding}Debug] pitch : {pitch}" + ] + debug_info = "\n".join(debug_lines) + print(debug_info) + text_filtering = text_filtering or _text_filtering + character_voice_gen = character_voice_gen or _character_voice_gen + rvccharacter_voice_gen = rvccharacter_voice_gen or _rvccharacter_voice_gen + narrator_enabled = narrator_enabled or _narrator_enabled # NEED TO COME BACK AND LOOK AT THIS!!! + narrator_voice_gen = narrator_voice_gen or _narrator_voice_gen + rvcnarrator_voice_gen = rvcnarrator_voice_gen or _rvcnarrator_voice_gen + text_not_inside = text_not_inside or _text_not_inside + language = language or _language + output_file_name = output_file_name or _output_file_name + output_file_timestamp = output_file_timestamp if output_file_timestamp is not None else _output_file_timestamp + autoplay = autoplay if autoplay is not None else _autoplay + autoplay_volume = autoplay_volume or _autoplay_volume + speed = speed or _speed + temperature = temperature or _temperature + repetition_penalty = repetition_penalty or _repetition_penalty + pitch = pitch or _pitch + try: + json_input_data = { + "text_input": text_input, + "text_filtering": text_filtering, + "character_voice_gen": character_voice_gen, + "rvccharacter_voice_gen": rvccharacter_voice_gen, + "narrator_enabled": narrator_enabled, + "narrator_voice_gen": narrator_voice_gen, + "rvcnarrator_voice_gen": rvcnarrator_voice_gen, + "text_not_inside": text_not_inside, + "language": language, + "output_file_name": output_file_name, + "output_file_timestamp": output_file_timestamp, + "autoplay": autoplay, + "autoplay_volume": autoplay_volume, + "streaming": streaming, + "speed": speed, + "temperature": temperature, + "repetition_penalty": repetition_penalty, + "pitch": pitch, + } + JSONresult = validate_json_input(json_input_data) + if JSONresult is None: + pass + else: + return JSONResponse(content={"error": JSONresult}, status_code=400) + + if debug_tts or debug_tts_variables: + debug_lines = [ + f"[{branding}Debug] *******************************************************", + f"[{branding}Debug] /api/tts_generate function called (debug_tts_variables)", + f"[{branding}Debug] **** POST validation checks ****", + f"[{branding}Debug] *******************************************************", + f"[{branding}Debug] Defaults will be pulled from Settings if not specified in API Request!", + f"[{branding}Debug] max_characters : {api_defaults['api_max_characters']}", + f"[{branding}Debug] length_stripping : {api_defaults['api_length_stripping']}", + f"[{branding}Debug] legacy_api : {api_defaults['api_use_legacy_api']}", + f"[{branding}Debug] legacy_api IPadd : {api_defaults['api_legacy_ip_address']}", + f"[{branding}Debug] allowed_filter : {api_defaults['api_allowed_filter']}", + f"[{branding}Debug] text_filtering : {text_filtering}", + f"[{branding}Debug] character_voice_gen : {character_voice_gen}", + f"[{branding}Debug] rvccharacter_voice_gen : {rvccharacter_voice_gen}", + f"[{branding}Debug] narrator_enabled : {narrator_enabled}", + f"[{branding}Debug] narrator_voice_gen : {narrator_voice_gen}", + f"[{branding}Debug] rvcnarrator_voice_gen : {rvcnarrator_voice_gen}", + f"[{branding}Debug] text_not_inside : {text_not_inside}", + f"[{branding}Debug] language : {language}", + f"[{branding}Debug] output_file_name : {output_file_name}", + f"[{branding}Debug] output_file_timestamp : {output_file_timestamp}", + f"[{branding}Debug] autoplay : {autoplay}", + f"[{branding}Debug] autoplay_volume : {autoplay_volume}", + f"[{branding}Debug] streaming : {streaming}", + f"[{branding}Debug] speed : {speed}", + f"[{branding}Debug] temperature : {temperature}", + f"[{branding}Debug] repetition_penalty : {repetition_penalty}", + f"[{branding}Debug] pitch : {pitch}" + ] + debug_info = "\n".join(debug_lines) + print(debug_info) + if narrator_enabled.lower() == "silent" and text_not_inside.lower() == "silent": + print(f"[{branding}GEN] \033[92mBoth Narrator & Text-not-inside are set to \033[91msilent\033[92m. If you get no TTS, this is why.\033[0m") + if narrator_enabled.lower() in ["true", "silent"]: + if model_engine.lowvram_enabled and model_engine.device == "cpu": + await model_engine.handle_lowvram_change() + model_engine.tts_narrator_generatingtts = True + print(f"[{branding}Debug] Moved into Narrator") if debug_tts else None + print(f"[{branding}Debug] original text : {text_input}") if debug_tts else None + processed_parts = process_text(text_input) + audio_files_all_paragraphs = [] + for part_type, part in processed_parts: + # Skip parts that are too short + if len(part.strip()) <= int(params["api_def"]["api_length_stripping"]): + continue + # Determine the voice to use based on the part type + if part_type == 'narrator': + if narrator_enabled.lower() == "silent": + print(f"[{branding}GEN] \033[95mNarrator Silent:\033[0m", part) # Green + continue # Skip generating audio for narrator parts if set to "silent" + voice_to_use = narrator_voice_gen + print(f"[{branding}GEN] \033[92mNarrator:\033[0m", part) # Purple + elif part_type == 'character': + voice_to_use = character_voice_gen + print(f"[{branding}GEN] \033[36mCharacter:\033[0m", part) # Yellow + else: + # Handle ambiguous parts based on user preference + if text_not_inside == "silent": + print(f"[{branding}GEN] \033[95mText-not-inside Silent:\033[0m", part) # Purple + continue # Skip generating audio for ambiguous parts if set to "silent" + voice_to_use = character_voice_gen if text_not_inside == "character" else narrator_voice_gen + voice_description = "\033[36mCharacter (Text-not-inside)\033[0m" if text_not_inside == "character" else "\033[92mNarrator (Text-not-inside)\033[0m" + print(f"[{branding}GEN] {voice_description}:", part) + # Replace multiple exclamation marks, question marks, or other punctuation with a single instance + cleaned_part = re.sub(r'([!?.])\1+', r'\1', part) + # Further clean to remove any other unwanted characters + cleaned_part = re.sub(rf'{api_defaults["api_allowed_filter"]}', '', cleaned_part) + print(f"[{branding}Debug] text after api_allowed: {text_input}") if debug_tts else None + # Remove all newline characters (single or multiple) + cleaned_part = re.sub(r'\n+', ' ', cleaned_part) + output_file = this_dir / output_directory / f'{output_file_name}_{uuid.uuid4()}_{int(time.time())}.{model_engine.audio_format}' + output_file_str = output_file.as_posix() + response = await generate_audio(cleaned_part, voice_to_use, language,temperature, repetition_penalty, speed, pitch, output_file_str, streaming) + audio_path = output_file_str + if rvc_settings["rvc_enabled"]: + if part_type == 'character': + process_rvc_narrator(part_type, rvccharacter_voice_gen, rvc_settings["rvc_char_model_file"], output_file, infer_pipeline) + elif part_type == 'narrator': + process_rvc_narrator(part_type, rvcnarrator_voice_gen, rvc_settings["rvc_narr_model_file"], output_file, infer_pipeline) + else: + if text_not_inside == 'character': + process_rvc_narrator('character', rvccharacter_voice_gen, rvc_settings["rvc_char_model_file"], output_file, infer_pipeline) + elif text_not_inside == 'narrator': + process_rvc_narrator('narrator', rvcnarrator_voice_gen, rvc_settings["rvc_narr_model_file"], output_file, infer_pipeline) + print(f"[{branding}Debug] Appending audio path to list") if debug_tts else None + audio_files_all_paragraphs.append(audio_path) + # Combine audio files across paragraphs + model_engine.tts_narrator_generatingtts = False + print(f"[{branding}Debug] Narrator sending to combine") if debug_tts else None + output_file_path, output_file_url, output_cache_url = combine(output_file_timestamp, output_file_name, audio_files_all_paragraphs) + # Transcode audio if necessary + model_audio_format = str(model_engine.audio_format).lower() + output_audio_format = str(params["transcode_audio_format"]).lower() + if output_audio_format == "disabled": + pass + else: + if model_audio_format != output_audio_format and not model_engine.tts_narrator_generatingtts: + print(f"[{branding}Debug] Pre Transcode call - Output file: {output_file_path}") if debug_tts else None + output_file_path, output_file_url, output_cache_url = await transcode_audio_if_necessary(output_file_path, model_audio_format, output_audio_format) + print(f"[{branding}Debug] Post Transcode call - Output file: {output_file_path}") if debug_tts else None + if sounddevice_installed == False: + autoplay = False + if autoplay: + play_audio(output_file_path, autoplay_volume) + # Move model back to cpu system ram if needed. + if model_engine.lowvram_enabled and model_engine.device == "cuda" and model_engine.lowvram_capable: + await model_engine.handle_lowvram_change() + return JSONResponse(content={"status": "generate-success", "output_file_path": str(output_file_path), "output_file_url": str(output_file_url), "output_cache_url": str(output_cache_url)}, status_code=200) + else: + print(f"[{branding}Debug] Moved into Standard generation") if debug_tts else None + print(f"[{branding}Debug] original text : {text_input}") if debug_tts else None + if output_file_timestamp: + timestamp = int(time.time()) + # Generate a standard UUID + original_uuid = uuid.uuid4() + # Hash the UUID using SHA-256 + hash_object = hashlib.sha256(str(original_uuid).encode()) + hashed_uuid = hash_object.hexdigest() + # Truncate to the desired length, for example, 16 characters + short_uuid = hashed_uuid[:5] + output_file_path = this_dir / output_directory / f'{output_file_name}_{timestamp}{short_uuid}.{model_engine.audio_format}' + #Legacy API or New API return + if params["api_def"]["api_use_legacy_api"]: + output_file_url = f'http://{api_defaults["api_legacy_ip_address"]}:{api_defaults["api_port_number"]}/audio/{output_file_name}_{timestamp}{short_uuid}.{model_engine.audio_format}' + output_cache_url = f'http://{api_defaults["api_legacy_ip_address"]}:{api_defaults["api_port_number"]}/audiocache/{output_file_name}_{timestamp}{short_uuid}.{model_engine.audio_format}' + else: + output_file_url = f'/audio/{output_file_name}_{timestamp}{short_uuid}.{model_engine.audio_format}' + output_cache_url = f'/audiocache/{output_file_name}_{timestamp}{short_uuid}.{model_engine.audio_format}' + else: + output_file_path = this_dir / output_directory / f"{output_file_name}.{model_engine.audio_format}" + # Legacy API or New API return + if params["api_def"]["api_use_legacy_api"]: + output_file_url = f'http://{api_defaults["api_legacy_ip_address"]}:{api_defaults["api_port_number"]}/audio/{output_file_name}.{model_engine.audio_format}' + output_cache_url = f'http://{api_defaults["api_legacy_ip_address"]}:{api_defaults["api_port_number"]}/audiocache/{output_file_name}.{model_engine.audio_format}' + else: + output_file_url = f'/audio/{output_file_name}.{model_engine.audio_format}' + output_cache_url = f'/audiocache/{output_file_name}.{model_engine.audio_format}' + if text_filtering == "html": + cleaned_string = html.unescape(standard_filtering(text_input)) + cleaned_string = re.sub(r'([!?.])\1+', r'\1', cleaned_string) + # Further clean to remove any other unwanted characters + cleaned_string = re.sub(rf'{api_defaults["api_allowed_filter"]}', '', cleaned_string) + print(f"[{branding}Debug] HTML Filtering - text after api_allowed: {cleaned_string}") if debug_tts else None + # Remove all newline characters (single or multiple) + cleaned_string = re.sub(r'\n+', ' ', cleaned_string) + elif text_filtering == "standard": + cleaned_string = re.sub(r'([!?.])\1+', r'\1', text_input) + # Further clean to remove any other unwanted characters + cleaned_string = re.sub(rf'{api_defaults["api_allowed_filter"]}', '', cleaned_string) + print(f"[{branding}Debug] Standard Filtering - text after api_allowed: {cleaned_string}") if debug_tts else None + # Remove all newline characters (single or multiple) + cleaned_string = re.sub(r'\n+', ' ', cleaned_string) + else: + print(f"[{branding}Debug] No filtering text : {text_input}") if debug_tts else None + cleaned_string = text_input + print(f"[{branding}GEN]", cleaned_string) + print(f"[{branding}Debug] Sending request to generate_audio in tts_server.py") if debug_tts else None + try: + if streaming: + response = await generate_audio(cleaned_string, character_voice_gen, language, temperature, repetition_penalty, speed, pitch, output_file_path, streaming) + return StreamingResponse(response, media_type="audio/wav") + else: + response = await generate_audio(cleaned_string, character_voice_gen, language, temperature, repetition_penalty, speed, pitch, output_file_path, streaming) + if rvc_settings["rvc_enabled"]: + if rvccharacter_voice_gen.lower() in ["disabled", "disable"]: + print(f"[{branding}Debug] Pass rvccharacter_voice_gen") if debug_tts else None + pass # Skip RVC processing for character part + else: + print(f"[{branding}Debug] send to rvc") if debug_tts else None + rvccharacter_voice_gen = this_dir / "models" / "rvc_voices" / rvccharacter_voice_gen + pth_path = rvccharacter_voice_gen if rvccharacter_voice_gen else rvc_settings["rvc_char_model_file"] + run_rvc(output_file_path, pth_path, infer_pipeline) + # Transcode audio if necessary + model_audio_format = str(model_engine.audio_format).lower() + output_audio_format = str(params["transcode_audio_format"]).lower() + print(f"[{branding}Debug] At Model audio format") if debug_tts else None + if output_audio_format == "disabled": + pass + else: + if model_audio_format != output_audio_format and not model_engine.tts_narrator_generatingtts: + print(f"[{branding}Debug] Pre Transcode call - Output file: {output_file_path}") if debug_tts else None + output_file_path, output_file_url, output_cache_url = await transcode_audio_if_necessary(output_file_path, model_audio_format, output_audio_format) + print(f"[{branding}Debug] Post Transcode call - Output file: {output_file_path}") if debug_tts else None + if sounddevice_installed == False: + autoplay = False + if autoplay: + play_audio(output_file_path, autoplay_volume) + return JSONResponse(content={"status": "generate-success", "output_file_path": str(output_file_path), "output_file_url": str(output_file_url), "output_cache_url": str(output_cache_url)}, status_code=200) + except Exception as e: + return JSONResponse(content={"status": "generate-failure", "error": "An error occurred"}, status_code=500) + except Exception as e: + return JSONResponse(content={"status": "generate-failure", "error": "An error occurred"}, status_code=500) + +############################# +#### Word Add-in Sharing #### +############################# +# Mount the static files from the 'word_addin' directory +app.mount("/api/word_addin", StaticFiles(directory=os.path.join(this_dir / 'system' / 'word_addin')), name="word_addin") + +############################################# +#### TTS Generator Comparision Endpoints #### +############################################# +import aiofiles +class TTSItem(BaseModel): + id: int + fileUrl: str + text: str + characterVoice: str + language: str + +class TTSData(BaseModel): + ttsList: List[TTSItem] + +@app.post("/api/save-tts-data") +async def apifunction_save_tts_data(tts_data: List[TTSItem]): + # Convert the list of Pydantic models to a list of dictionaries + tts_data_list = [item.dict() for item in tts_data] + # Serialize the list of dictionaries to a JSON string + tts_data_json = json.dumps(tts_data_list, indent=4) + async with aiofiles.open(this_dir / output_directory / "ttsList.json", 'w') as f: + await f.write(tts_data_json) + return {"message": "Data saved successfully"} + +######################################## +# Trigger TTS Gen Text/Speech Analysis # +######################################## +@app.get("/api/trigger-analysis") +async def apifunction_trigger_analysis(threshold: int = Query(default=98)): + venv_path = sys.prefix + env = os.environ.copy() + env["PATH"] = os.path.join(venv_path, "bin") + ":" + env["PATH"] + ttslist_path = this_dir / output_directory / "ttsList.json" + wavfile_path = this_dir / output_directory + subprocess.run(["python", "tts_diff.py", f"--threshold={threshold}", f"--ttslistpath={ttslist_path}", f"--wavfilespath={wavfile_path}"], cwd=this_dir / "system" / "tts_diff", env=env) + # Read the analysis summary + try: + with open(this_dir / output_directory / "analysis_summary.json", "r") as summary_file: + summary_data = json.load(summary_file) + except FileNotFoundError: + summary_data = {"error": "Analysis summary file not found."} + return {"message": "Analysis Completed", "summary": summary_data} + +########################################### +# TTS Generator SRT Subtitiles generation # +########################################### +@app.get("/api/srt-generation") +async def apifunction_srt_generation(): + venv_path = sys.prefix + env = os.environ.copy() + env["PATH"] = os.path.join(venv_path, "bin") + ":" + env["PATH"] + ttslist_path = this_dir / output_directory / "ttsList.json" + wavfile_path = this_dir / output_directory + subprocess.run(["python", "tts_srt.py", f"--ttslistpath={ttslist_path}", f"--wavfilespath={wavfile_path}"], cwd=this_dir / "system" / "tts_srt", env=env) + srt_file_path = this_dir / output_directory / "subtitles.srt" + if not srt_file_path.exists(): + raise HTTPException(status_code=404, detail="Subtitle file not found.") + return FileResponse(path=srt_file_path, filename="subtitles.srt", media_type='application/octet-stream') + +################################# +# Static Mount for file serving # +################################# +app.mount("/static", StaticFiles(directory=str(this_dir / "system")), name="static") + +######################################## +# Legacy JSON update settings function # +######################################## +# Setup logging +logging.basicConfig(level=logging.DEBUG) +def get_json_data(): + with open(this_dir / "confignew.json", "r") as json_file: + data = json.load(json_file) + return data + +@app.get("/settings") +async def get_settings(request: Request): + data = get_json_data() + return templates.TemplateResponse("admin.html", {"request": request, "data": data}) + +@app.get("/settings-json") +async def get_settings_json(): + return get_json_data() + +@app.post("/update-settings") +async def update_settings( + request: Request, + delete_output_wavs: str = Form(...), + gradio_interface: str = Form(...), + gradio_port_number: int = Form(...), + api_port_number: int = Form(...), +): + data = get_json_data() + # Update the settings based on the form values + data["delete_output_wavs"] = delete_output_wavs + data["gradio_interface"] = gradio_interface.lower() == 'true' + data["gradio_port_number"] = gradio_port_number + data["api_def"]["api_port_number"] = api_port_number + # Save the updated settings back to the JSON file + with open(this_dir / "confignew.json", "w") as json_file: + json.dump(data, json_file, indent=4) + # Redirect to the settings page to display the updated settings + return templates.TemplateResponse("admin.html", {"request": request, "data": data}) + +# Create an instance of Jinja2Templates for rendering HTML templates +templates = Jinja2Templates(directory=this_dir / "system") +# Get the admin interface template +template = templates.get_template("admin.html") +# Render the template with the dynamic values +rendered_html = template.render(params=params) + +################################################### +#### Webserver Startup & Initial model Loading #### +################################################### +@app.get("/") +async def read_root(): + return HTMLResponse(content=rendered_html, status_code=200) + +# Start Uvicorn Webserver +port_parameter = int(params["api_def"]["api_port_number"]) + +if __name__ == "__main__": + import uvicorn + + # Start Uvicorn Webserver + uvicorn_server = uvicorn.run(app, host="0.0.0.0", port=port_parameter, log_level="debug") + diff --git a/voices/Arnold.wav b/voices/Arnold.wav new file mode 100644 index 0000000..cccdf01 Binary files /dev/null and b/voices/Arnold.wav differ