2024-01-09 00:15:51 +00:00
|
|
|
"""
|
|
|
|
project: lollms_webui
|
|
|
|
file: lollms_xtts.py
|
|
|
|
author: ParisNeo
|
|
|
|
description:
|
|
|
|
This module contains a set of FastAPI routes that provide information about the Lord of Large Language and Multimodal Systems (LoLLMs) Web UI
|
|
|
|
application. These routes allow users to
|
|
|
|
|
|
|
|
"""
|
|
|
|
from fastapi import APIRouter, Request
|
|
|
|
from lollms_webui import LOLLMSWebUI
|
|
|
|
from pydantic import BaseModel
|
|
|
|
from starlette.responses import StreamingResponse
|
|
|
|
from lollms.types import MSG_TYPE
|
|
|
|
from lollms.main_config import BaseConfig
|
2024-01-11 01:32:21 +00:00
|
|
|
from lollms.utilities import detect_antiprompt, remove_text_from_string, trace_exception, find_first_available_file_index, add_period, PackageManager
|
2024-01-09 22:26:41 +00:00
|
|
|
from pathlib import Path
|
2024-01-09 00:15:51 +00:00
|
|
|
from ascii_colors import ASCIIColors
|
|
|
|
import os
|
|
|
|
import platform
|
|
|
|
|
2024-01-09 22:26:41 +00:00
|
|
|
# ----------------------- Defining router and main class ------------------------------
|
2024-01-09 00:15:51 +00:00
|
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
lollmsElfServer:LOLLMSWebUI = LOLLMSWebUI.get_instance()
|
|
|
|
|
|
|
|
|
2024-01-09 22:26:41 +00:00
|
|
|
# ----------------------- voice ------------------------------
|
2024-01-09 00:15:51 +00:00
|
|
|
|
2024-01-11 01:32:21 +00:00
|
|
|
@router.get("/list_voices")
|
2024-01-09 22:26:41 +00:00
|
|
|
def list_voices():
|
|
|
|
ASCIIColors.yellow("Listing voices")
|
|
|
|
voices=["main_voice"]
|
|
|
|
voices_dir:Path=lollmsElfServer.lollms_paths.custom_voices_path
|
|
|
|
voices += [v.stem for v in voices_dir.iterdir() if v.suffix==".wav"]
|
|
|
|
return {"voices":voices}
|
2024-01-09 00:15:51 +00:00
|
|
|
|
2024-01-09 22:26:41 +00:00
|
|
|
@router.post("/set_voice")
|
|
|
|
async def set_voice(request: Request):
|
2024-01-09 00:15:51 +00:00
|
|
|
"""
|
2024-01-11 01:50:54 +00:00
|
|
|
Changes current voice
|
2024-01-09 00:15:51 +00:00
|
|
|
|
|
|
|
:param request: The HTTP request object.
|
|
|
|
:return: A JSON response with the status of the operation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = (await request.json())
|
2024-01-09 22:26:41 +00:00
|
|
|
lollmsElfServer.config.current_voice=data["voice"]
|
|
|
|
if lollmsElfServer.config.auto_save:
|
|
|
|
lollmsElfServer.config.save_config()
|
|
|
|
return {"status":True}
|
2024-01-09 00:15:51 +00:00
|
|
|
except Exception as ex:
|
|
|
|
trace_exception(ex)
|
|
|
|
lollmsElfServer.error(ex)
|
|
|
|
return {"status":False,"error":str(ex)}
|
|
|
|
|
2024-01-09 22:26:41 +00:00
|
|
|
@router.post("/text2Audio")
|
|
|
|
async def text2Audio(request: Request):
|
2024-01-09 00:15:51 +00:00
|
|
|
"""
|
2024-01-09 22:26:41 +00:00
|
|
|
Executes Python code and returns the output.
|
2024-01-09 00:15:51 +00:00
|
|
|
|
|
|
|
:param request: The HTTP request object.
|
|
|
|
:return: A JSON response with the status of the operation.
|
|
|
|
"""
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = (await request.json())
|
2024-01-09 22:26:41 +00:00
|
|
|
# Get the JSON data from the POST request.
|
|
|
|
try:
|
2024-01-10 20:22:17 +00:00
|
|
|
from lollms.services.xtts.lollms_xtts import LollmsXTTS
|
2024-01-09 22:26:41 +00:00
|
|
|
if lollmsElfServer.tts is None:
|
|
|
|
lollmsElfServer.tts = LollmsXTTS(lollmsElfServer, voice_samples_path=Path(__file__).parent/"voices")
|
|
|
|
except:
|
|
|
|
return {"url": None}
|
|
|
|
|
|
|
|
voice=data.get("voice",lollmsElfServer.config.current_voice)
|
|
|
|
index = find_first_available_file_index(lollmsElfServer.tts.output_folder, "voice_sample_",".wav")
|
|
|
|
output_fn=data.get("fn",f"voice_sample_{index}.wav")
|
|
|
|
if voice is None:
|
|
|
|
voice = "main_voice"
|
|
|
|
lollmsElfServer.info("Starting to build voice")
|
|
|
|
try:
|
2024-01-10 20:22:17 +00:00
|
|
|
from lollms.services.xtts.lollms_xtts import LollmsXTTS
|
2024-01-09 22:26:41 +00:00
|
|
|
if lollmsElfServer.tts is None:
|
|
|
|
lollmsElfServer.tts = LollmsXTTS(lollmsElfServer, voice_samples_path=Path(__file__).parent/"voices")
|
|
|
|
language = lollmsElfServer.config.current_language# convert_language_name()
|
|
|
|
if voice!="main_voice":
|
|
|
|
voices_folder = lollmsElfServer.lollms_paths.custom_voices_path
|
|
|
|
else:
|
2024-01-11 22:14:30 +00:00
|
|
|
voices_folder = Path(__file__).parent.parent/"voices"
|
2024-01-09 22:26:41 +00:00
|
|
|
lollmsElfServer.tts.set_speaker_folder(voices_folder)
|
|
|
|
url = f"audio/{output_fn}"
|
|
|
|
preprocessed_text= add_period(data['text'])
|
|
|
|
|
|
|
|
lollmsElfServer.tts.tts_to_file(preprocessed_text, f"{voice}.wav", f"{output_fn}", language=language)
|
|
|
|
lollmsElfServer.info("Voice file ready")
|
|
|
|
return {"url": url}
|
|
|
|
except:
|
|
|
|
return {"url": None}
|
2024-01-09 00:15:51 +00:00
|
|
|
except Exception as ex:
|
|
|
|
trace_exception(ex)
|
|
|
|
lollmsElfServer.error(ex)
|
2024-01-11 01:32:21 +00:00
|
|
|
return {"status":False,"error":str(ex)}
|
|
|
|
|
|
|
|
@router.get("/install_xtts")
|
|
|
|
def install_xtts():
|
|
|
|
try:
|
|
|
|
lollmsElfServer.ShowBlockingMessage("Installing xTTS api server\nPlease stand by")
|
|
|
|
PackageManager.install_package("xtts-api-server")
|
|
|
|
lollmsElfServer.HideBlockingMessage()
|
|
|
|
return {"status":True}
|
|
|
|
except Exception as ex:
|
|
|
|
lollmsElfServer.HideBlockingMessage()
|
|
|
|
return {"status":False, 'error':str(ex)}
|