Package: xtts-api-server
A simple FastAPI Server to run XTTSv2This project is inspired bysilero-api-serverand utilizesXTTSv2.This server was created forSillyTavernbut you can use it for your needsFeel free to make PRs or use the code for your own needsThere's agoogle collab versionyou can use it if your computer is weak.If you want to get a high-quality voice clone, I advise you to usewebui for fine-tuning xttsIf you are looking for an option for normal XTTS use look herehttps://github.com/daswer123/xtts-webuiChangelogYou can keep track of all changes on therelease pageTODOMake it possible to change generation parameters through the generation request and through a different endpointInstallationSimple installation :pipinstallxtts-api-serverThis will install all the necessary dependencies, including aCPU support onlyversion of PyTorchI recommend that you install theGPU versionto improve processing speed ( up to 3 times faster )Windowspython-mvenvvenv
venv\Scripts\activate
pipinstallxtts-api-server
pipinstalltorch==2.1.1+cu118torchaudio==2.1.1+cu118--index-urlhttps://download.pytorch.org/whl/cu118Linuxpython-mvenvvenvsourcevenv\bin\activate
pipinstallxtts-api-server
pipinstalltorch==2.1.1+cu118torchaudio==2.1.1+cu118--index-urlhttps://download.pytorch.org/whl/cu118Manual# Clone REPOgitclonehttps://github.com/daswer123/xtts-api-servercdxtts-api-server# Create virtual envpython-mvenvvenv
venv/scripts/activateorsourcevenv/bin/activate# Install depspipinstall-rrequirements.txt
pipinstalltorch==2.1.1+cu118torchaudio==2.1.1+cu118--index-urlhttps://download.pytorch.org/whl/cu118# Launch serverpython-mxtts_api_serverUse Docker image with Docker ComposeA Dockerfile is provided to build a Docker image, and a docker-compose.yml file is provided to run the server with Docker Compose as a service.You can build the image with the following command:mkdirxtts-api-servercdxtts-api-server
dockerrun-ddaswer123/xtts-api-serverORcddocker
dockercomposebuildThen you can run the server with the following command:dockercomposeup# or with -d to run in backgroundStarting Serverpython -m xtts_api_serverwill run on default ip and port (localhost:8020)Use the--deepspeedflag to process the result fast ( 2-3x acceleration )usage: xtts_api_server [-h] [-hs HOST] [-p PORT] [-sf SPEAKER_FOLDER] [-o OUTPUT] [-t TUNNEL_URL] [-ms MODEL_SOURCE] [--listen] [--use-cache] [--lowvram] [--deepspeed] [--streaming-mode] [--stream-play-sync]

Run XTTSv2 within a FastAPI application

options:
  -h, --help show this help message and exit
  -hs HOST, --host HOST
  -p PORT, --port PORT
  -d DEVICE, --device DEVICE `cpu` or `cuda`, you can specify which video card to use, for example, `cuda:0`
  -sf SPEAKER_FOLDER, --speaker-folder The folder where you get the samples for tts
  -o OUTPUT, --output Output folder
  -mf MODELS_FOLDERS, --model-folder Folder where models for XTTS will be stored, finetuned models should be stored in this folder
  -t TUNNEL_URL, --tunnel URL of tunnel used (e.g: ngrok, localtunnel)
  -ms MODEL_SOURCE, --model-source ["api","apiManual","local"]
  -v MODEL_VERSION, --version You can download the official model or your own model, official version you can find [here](https://huggingface.co/coqui/XTTS-v2/tree/main)  the model version name is the same as the branch name [v2.0.2,v2.0.3, main] etc. Or you can load your model, just put model in models folder
  --listen Allows the server to be used outside the local computer, similar to -hs 0.0.0.0
  --use-cache Enables caching of results, your results will be saved and if there will be a repeated request, you will get a file instead of generation
  --lowvram The mode in which the model will be stored in RAM and when the processing will move to VRAM, the difference in speed is small
  --deepspeed allows you to speed up processing by several times, automatically downloads the necessary libraries
  --streaming-mode Enables streaming mode, currently has certain limitations, as described below.
  --streaming-mode-improve Enables streaming mode, includes an improved streaming mode that consumes 2gb more VRAM and uses a better tokenizer and more context.
  --stream-play-sync Additional flag for streaming mod that allows you to play all audio one at a time without interruptionYou can specify the path to the file as text, then the path counts and the file will be voicedYou can load your own model, for this you need to create a folder in models and load the model with configs, note in the folder should be 3 filesconfig.jsonvocab.jsonmodel.pthIf you want your host to listen, use -hs 0.0.0.0 or use --listenThe -t or --tunnel flag is needed so that when you get speakers via get you get the correct link to hear the preview. More infohereModel-source defines in which format you want to use xtts:local- loads version 2.0.2 by default, but you can specify the version via the -v flag, model saves into the models folder and usesXttsConfigandinference.apiManual- loads version 2.0.2 by default, but you can specify the version via the -v flag, model saves into the models folder and uses thetts_to_filefunction from the TTS apiapi- will load the latest version of the model. The -v flag won't work.All versions of the XTTSv2 model can be foundherethe model version name is the same as the branch name [v2.0.2,v2.0.3, main] etc.The first time you run or generate, you may need to confirm that you agree to use XTTS.About Streaming modeStreaming mode allows you to get audio and play it back almost immediately. However, it has a number of limitations.You can see how this mode workshereandhereNow, about the limitationsCan only be used on a local computerPlaying audio from the your pcDoes not work endpointtts_to_fileonlytts_to_audioand it returns 1 second of silence.You can specify the version of the XTTS model by using the-vflag.Improved streaming mode is suitable for complex languages such as Chinese, Japanese, Hindi or if you want the language engine to take more information into account when processing speech.--stream-play-syncflag - Allows you to play all messages in queue order, useful if you use group chats. In SillyTavern you need to turn off streaming to work correctlyAPI DocsAPI Docs can be accessed fromhttp://localhost:8020/docsHow to add speakerBy default thespeakersfolder should appear in the folder, you need to put there the wav file with the voice sample, you can also create a folder and put there several voice samples, this will give more accurate resultsSelecting FolderYou can change the folders for speakers and the folder for output via the API.Note on creating samples for quality voice cloningThe following post is a quote by userMaterial1276 from redditSome suggestions on making good samplesKeep them about 7-9 seconds long. Longer isn't necessarily better.Make sure the audio is down sampled to a Mono, 22050Hz 16 Bit wav file. You will slow down processing by a large % and it seems cause poor quality results otherwise (based on a few tests). 24000Hz is the quality it outputs at anyway!Using the latest version of Audacity, select your clip and Tracks > Resample to 22050Hz, then Tracks > Mix > Stereo to Mono. and then File > Export Audio, saving it as a WAV of 22050HzIf you need to do any audio cleaning, do it before you compress it down to the above settings (Mono, 22050Hz, 16 Bit).Ensure the clip you use doesn't have background noises or music on e.g. lots of movies have quiet music when many of the actors are talking. Bad quality audio will have hiss that needs clearing up. The AI will pick this up, even if we don't, and to some degree, use it in the simulated voice to some extent, so clean audio is key!Try make your clip one of nice flowing speech, like the included example files. No big pauses, gaps or other sounds. Preferably one that the person you are trying to copy will show a little vocal range. Example files are inhereMake sure the clip doesn't start or end with breathy sounds (breathing in/out etc).Using AI generated audio clips may introduce unwanted sounds as its already a copy/simulation of a voice, though, this would need testing.CreditThanks to the authorKolja Beigelfor the repositoryRealtimeTTS, I took some of its code for my project.Thankserew123for the note about creating samples and the code to download the modelsThankslendotfor helping to fix the multiprocessing bug and adding code to use multiple samples for speakers