diff --git a/README.md b/README.md index 3e728f2..6fd8e55 100644 --- a/README.md +++ b/README.md @@ -499,9 +499,67 @@ Deepspeed and other such things can be installed. Please read the relevant instr ### 🟠Overview The Text-to-Speech (TTS) Generation API allows you to generate speech from text input using various configuration options. This API supports both character and narrator voices, providing flexibility for creating dynamic and engaging audio content. -- URL: `http://127.0.0.1:7851/api/tts-generate`
-- Method: `POST`
-- Content-Type: `application/x-www-form-urlencoded`
+#### 🟠 Ready Endpoint
+Check if the Text-to-Speech (TTS) service is ready to accept requests. + +- URL: `http://127.0.0.1:7851/api/ready`
- Method: `GET`
+ + `curl -X GET "http://127.0.0.1:7851/api/ready"` + + Response: `Ready` + +#### 🟠 Voices List Endpoint
+Retrieve a list of available voices for generating speech. + +- URL: `http://127.0.0.1:7851/api/voices`
- Method: `GET`
+ + `curl -X GET "http://127.0.0.1:7851/api/voices"` + + JSON return: `{"voices": ["voice1.wav", "voice2.wav", "voice3.wav"]}` + +#### 🟠 Preview Voice Endpoint +Generate a preview of a specified voice with hardcoded settings. + +- URL: `http://127.0.0.1:7851/api/previewvoice/`
- Method: `POST`
- Content-Type: `application/x-www-form-urlencoded`
+ + `curl -X POST "http://127.0.0.1:7851/api/previewvoice/" -F "voice=female_01.wav"` + + Replace `female_01.wav` with the name of the voice sample you want to hear. + + JSON return: `{"status": "generate-success", "output_file_path": "/path/to/outputs/api_preview_voice.wav", "output_file_url": "http://127.0.0.1:7851/audio/api_preview_voice.wav"}` + +#### 🟠 Switching Model Endpoint
+ +- URL: `http://127.0.0.1:7851/api/reload`
- Method: `POST`

+ `curl -X POST "http://127.0.0.1:7851/api/reload?tts_method=API%20Local"`
+ `curl -X POST "http://127.0.0.1:7851/api/reload?tts_method=API%20TTS"`
+ `curl -X POST "http://127.0.0.1:7851/api/reload?tts_method=XTTSv2%20Local"`
+ + Switch between the 3 models respectively. + + JSON return `{"status": "model-success"}` + +#### 🟠 Switch DeepSpeed Endpoint
+ +- URL: `http://127.0.0.1:7851/api/deepspeed`
- Method: `POST`

+ `curl -X POST "http://127.0.0.1:7851/api/deepspeed?new_deepspeed_value=True"` + + Replace True with False to disable DeepSpeed mode. + + JSON return `{"status": "deepspeed-success"}` + +#### 🟠 Switching Low VRAM Endpoint
+ +- URL: `http://127.0.0.1:7851/api/lowvramsetting`
- Method: `POST`

+ `curl -X POST "http://127.0.0.1:7851/api/lowvramsetting?new_low_vram_value=True"` + + Replace True with False to disable Low VRAM mode. + + JSON return `{"status": "lowvram-success"}` + +### 🟠 TTS Generation Endpoint + +- URL: `http://127.0.0.1:7851/api/tts-generate`
- Method: `POST`
- Content-Type: `application/x-www-form-urlencoded`
### 🟠 Example command lines Standard TTS speech Example (standard text) generating a time-stamped file
@@ -558,22 +616,22 @@ Example: 🟠 **language**: Choose the language for TTS. Options: -`ar Arabic`
-`zh-cn Chinese (Simplified)`
-`cs Czech`
-`nl Dutch`
-`en English`
-`fr French`
-`de German`
-`hu Hungarian`
-`it Italian`
-`ja Japanese`
-`ko Korean`
-`pl Polish`
-`pt Portuguese`
-`ru Russian`
-`es Spanish`
-`tr Turkish`
+`ar` Arabic
+`zh-cn` Chinese (Simplified)
+`cs` Czech
+`nl` Dutch
+`en` English
+`fr` French
+`de` German
+`hu` Hungarian
+`it` Italian
+`ja` Japanese
+`ko` Korean
+`pl` Polish
+`pt` Portuguese
+`ru` Russian
+`es` Spanish
+`tr` Turkish
`-d "language=en"`
@@ -586,12 +644,12 @@ Example: `-d "output_file_timestamp=true"`
`-d "output_file_timestamp=false"` -🟠 **autoplay**: Feature not yet available. Enable or disable autoplay. Still needs to be specified in the JSON request. +🟠 **autoplay**: Enable or disable playing the generated TTS to your standard sound output device at time of TTS generation. `-d "autoplay=true"`
`-d "autoplay=false"` -🟠 **autoplay_volume**: Feature not yet available. Set the autoplay volume. Should be between 0.1 and 1.0. Still needs to be specified in the JSON request. +🟠 **autoplay_volume**: Set the autoplay volume. Should be between 0.1 and 1.0. Needs to be specified in the JSON request even if autoplay is false. `-d "autoplay_volume=0.8"` @@ -606,29 +664,6 @@ Example JSON TTS Generation Response: `{"status": "generate-success", "output_file_path": "C:\text-generation-webui\extensions\alltalk_tts\outputs\myoutputfile_1703149973.wav", "output_file_url": "http://127.0.0.1:7851/audio/myoutputfile_1703149973.wav"}` -🟠 **Switching Model**

-`curl -X POST "http://127.0.0.1:7851/api/reload?tts_method=API%20Local"`
-`curl -X POST "http://127.0.0.1:7851/api/reload?tts_method=API%20TTS"`
-`curl -X POST "http://127.0.0.1:7851/api/reload?tts_method=XTTSv2%20Local"`
- -Switch between the 3 models respectively. - -JSON return `{"status": "model-success"}` - -🟠 **Switch DeepSpeed**

-`curl -X POST "http://127.0.0.1:7851/api/deepspeed?new_deepspeed_value=True"` - -Replace True with False to disable DeepSpeed mode. - -JSON return `{"status": "deepspeed-success"}` - -🟠 **Switching Low VRAM**

-`curl -X POST "http://127.0.0.1:7851/api/lowvramsetting?new_low_vram_value=True"` - -Replace True with False to disable Low VRAM mode. - -JSON return `{"status": "lowvram-success"}` - ### πŸ”΄ Future to-do list - Voice output within the command prompt/terminal (TBD). - Correct a few spelling mistakes in the documentation. diff --git a/modeldownload.json b/modeldownload.json index 76afdcd..d18ab27 100644 --- a/modeldownload.json +++ b/modeldownload.json @@ -8,6 +8,7 @@ "model.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/v2.0.2/model.pth?download=true", "dvae.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/v2.0.2/dvae.pth?download=true", "mel_stats.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/v2.0.2/mel_stats.pth?download=true", + "speakers_xtts.pth": "https://huggingface.co/coqui/XTTS-v2/resolve/v2.0.2/speakers_xtts.pth?download=true", "vocab.json": "https://huggingface.co/coqui/XTTS-v2/resolve/v2.0.2/vocab.json?download=true" } -} +} \ No newline at end of file diff --git a/requirements_nvidia.txt b/requirements_nvidia.txt index 82579fd..b8d22e9 100644 --- a/requirements_nvidia.txt +++ b/requirements_nvidia.txt @@ -11,3 +11,5 @@ tqdm>=4.66.1 importlib-metadata>=4.8.1 packaging>=23.2 pydantic>=1.10.13 +sounddevice>=0.4.6 +python-multipart>=0.0.6 diff --git a/requirements_other.txt b/requirements_other.txt index aad8a62..6566ced 100644 --- a/requirements_other.txt +++ b/requirements_other.txt @@ -11,3 +11,5 @@ tqdm>=4.66.1 importlib-metadata>=4.8.1 packaging>=23.2 pydantic>=1.10.13 +sounddevice>=0.4.6 +python-multipart>=0.0.6 diff --git a/script.py b/script.py index 30eb714..38bac24 100644 --- a/script.py +++ b/script.py @@ -252,7 +252,7 @@ else: timeout = 120 # Adjust the timeout as needed # Introduce a delay before starting the check loop -time.sleep(25) # Wait 25 secs before checking if the tts_server.py has started up. +time.sleep(26) # Wait 26 secs before checking if the tts_server.py has started up. start_time = time.time() while time.time() - start_time < timeout: try: @@ -264,7 +264,7 @@ while time.time() - start_time < timeout: print( f"[{params['branding']}Startup] \033[91mWarning\033[0m TTS Subprocess has NOT started up yet, Will keep trying for 120 seconds maximum. Please wait." ) - time.sleep(1) + time.sleep(4) else: print( f"[{params['branding']}Startup] Startup timed out. Check the server logs for more information." diff --git a/tts_server.py b/tts_server.py index 61fb07b..57ac453 100644 --- a/tts_server.py +++ b/tts_server.py @@ -619,6 +619,53 @@ async def get_audio(filename: str): audio_path = this_dir / "outputs" / filename return FileResponse(audio_path) + +######################### +#### VOICES LIST API #### +######################### +# Define the new endpoint +@app.get("/api/voices") +async def get_voices(): + wav_files = list_files(this_dir / "voices") + return {"voices": wav_files} + +########################### +#### PREVIEW VOICE API #### +########################### +@app.post("/api/previewvoice/", response_class=JSONResponse) +async def 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 / "outputs" / f"{output_file_name}.wav" + await generate_audio(text, voice, language, output_file_path) + + # Generate the URL + output_file_url = f'http://{params["ip_address"]}:{params["port_number"]}/audio/{output_file_name}.wav' + + # 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) + ######################## #### GENERATION API #### ######################## @@ -627,9 +674,29 @@ import re import uuid import numpy as np import soundfile as sf +import sys + +# Check for PortAudio library on Linux +try: + import sounddevice as sd + sounddevice_installed=True +except OSError: + print(f"[{params['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"[{params['branding']}Startup] \033[91mInfo\033[0m please install PortAudio. This will not affect any other features or use of Alltalk.") + print(f"[{params['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"[{params['branding']}Startup] \033[91mInfo\033[0m On Linux, you can use the following command to install PortAudio:") + print(f"[{params['branding']}Startup] \033[91mInfo\033[0m sudo apt-get install portaudio19-dev") + from typing import Union, Dict from pydantic import BaseModel, ValidationError, Field +def play_audio(file_path, volume): + data, fs = sf.read(file_path) + sd.play(volume * data, fs) + sd.wait() + class Request(BaseModel): # Define the structure of the 'Request' class if needed pass @@ -827,6 +894,10 @@ async def tts_generate( else: cleaned_string = text_input await generate_audio(cleaned_string, character_voice_gen, language, output_file_path) + 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)}, status_code=200) except Exception as e: return JSONResponse(content={"status": "generate-failure", "error": "An error occurred"}, status_code=500) @@ -1673,6 +1744,33 @@ simple_webpage = """

curl -X POST "http://127.0.0.1:7851/api/lowvramsetting?new_low_vram_value=True"

Replace True with False to disable Low VRAM mode.

JSON return {"status": "lowvram-success"}

+

Ready Endpoint

+

Check if the Text-to-Speech (TTS) service is ready to accept requests.

+ +

curl -X GET "http://127.0.0.1:7851/api/ready"

+

Voices List Endpoint

+

Retrieve a list of available voices for generating speech.

+ +

curl -X GET "http://127.0.0.1:7851/api/voices"

+

JSON return: {"voices": ["voice1.wav", "voice2.wav", "voice3.wav"]}

+

Preview Voice Endpoint

+

Generate a preview of a specified voice with hardcoded settings.

+ +

curl -X POST "http://127.0.0.1:7851/api/previewvoice/" -F "voice=female_01.wav"

+

Replace female_01.wav with the name of the voice sample you want to hear.

+

JSON return: {"status": "generate-success", "output_file_path": "/path/to/outputs/api_preview_voice.wav", "output_file_url": "http://127.0.0.1:7851/audio/api_preview_voice.wav"}

+

Back to top of page

Debugging and TTS Generation Information

@@ -1717,10 +1815,20 @@ template = Template(simple_webpage) # Render the template with the dynamic values rendered_html = template.render(params=params) +############################### +#### Internal script ready #### +############################### @app.get("/ready") async def ready(): return Response("Ready endpoint") +############################ +#### External API ready #### +############################ +@app.get("/api/ready") +async def ready(): + return Response("Ready") + @app.get("/") async def read_root(): return HTMLResponse(content=rendered_html, status_code=200)