Kokoro working, updated interface
This commit is contained in:
+37
-17
@@ -28,13 +28,14 @@ Classes:
|
||||
run(input_source: PathLike, output_dest: PathLike) -> subprocess.CompletedProcess: Execute backend command.
|
||||
|
||||
"""
|
||||
import os
|
||||
import re
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
from typing import Optional, Callable
|
||||
|
||||
from .vendor_utils import build_run_command
|
||||
from . import PathLike
|
||||
from .vendor_utils import build_run_command, ensure_venv
|
||||
from . import log, PathLike
|
||||
|
||||
class GenericTTSBackend:
|
||||
"""Generic TTS backend for handling text-to-speech conversion.
|
||||
@@ -47,6 +48,7 @@ class GenericTTSBackend:
|
||||
REPO: Optional[str] = None
|
||||
CMD: Optional[str] = None
|
||||
FLAGS: Optional[dict] = {}
|
||||
DEFAULT_FLAGS: Optional[dict] = {}
|
||||
INTERMEDIATE_TXT: Optional[bool] = False
|
||||
INTERMEDIATE_CALL: Optional[Callable] = None
|
||||
DEFAULT_SPEAKER: Optional[str] = None
|
||||
@@ -62,10 +64,20 @@ class GenericTTSBackend:
|
||||
|
||||
INPUT_FLAG = {True: "--input-dir", False: "--input-file"}
|
||||
OUTPUT_FLAG = {True: "--output-dir", False: "--output-file"}
|
||||
ENV = None # subclasses may override this to set environment variables for the backend
|
||||
|
||||
def __init__(self,**kwargs):
|
||||
def __init__(self, **kwargs):
|
||||
log(kwargs)
|
||||
for k,v in kwargs.items():
|
||||
setattr(self, k, v)
|
||||
if hasattr(self, 'CWD') and self.CWD is not None:
|
||||
self.CWD = Path(self.CWD).resolve()
|
||||
else:
|
||||
self.CWD = self._normalize_path(self.output).resolve() if hasattr(self, 'output') else Path.cwd()
|
||||
if not self.CWD.is_dir():
|
||||
self.CWD = self.CWD.parent.resolve()
|
||||
self.ENV = os.environ.copy()
|
||||
#log(f"SELF:{self.__dict__}")
|
||||
|
||||
|
||||
def _normalize_path(self, value: PathLike) -> Path:
|
||||
@@ -85,18 +97,23 @@ class GenericTTSBackend:
|
||||
return [self.OUTPUT_FLAG[p.is_dir()], str(p)]
|
||||
|
||||
def _build_command(self, input_source: PathLike, output_dest: PathLike) -> list[str]:
|
||||
if self.REPO is None or self.CMD is None:
|
||||
if not self.CMD and not self.REPO:
|
||||
raise ValueError("Either CMD or REPO must be specified for the backend.")
|
||||
if self.CMD and not self.REPO:
|
||||
command = [self.CMD]
|
||||
elif self.REPO and not self.CMD:
|
||||
command = [str(ensure_venv(self.REPO))]
|
||||
else:
|
||||
command = build_run_command(self.REPO, CMD=self.CMD)
|
||||
command = [str(ensure_venv(self.REPO))]
|
||||
command.extend(["-m",self.CMD] if isinstance(self.CMD, str)
|
||||
else self.CMD)
|
||||
command.extend(self.gen_input_flag(input_source))
|
||||
if self.gen_output_flag:
|
||||
command.extend(self.gen_output_flag(output_dest))
|
||||
for flag_name, var_name in self.FLAGS.items():
|
||||
if getattr(self, flag_name, None) is not None:
|
||||
command.extend([
|
||||
f"--{flag_name.replace('_', '-')}",
|
||||
getattr(self, var_name)])
|
||||
if getattr(self, var_name, self.DEFAULT_FLAGS[var_name]) is not None:
|
||||
command.extend([flag_name, str(getattr(self, var_name))
|
||||
])
|
||||
return command
|
||||
|
||||
def run(self,
|
||||
@@ -106,12 +123,15 @@ class GenericTTSBackend:
|
||||
) -> subprocess.CompletedProcess:
|
||||
|
||||
command = self._build_command(input_source, output_dest)
|
||||
print(command)
|
||||
print(f"Running command: {' '.join(command)}")
|
||||
completed = subprocess.run(
|
||||
self._build_command(input_source, output_dest),
|
||||
cwd=str(self._normalize_path(input_source).parent),
|
||||
check=True)
|
||||
log(command)
|
||||
|
||||
# PREPROCESSING STEP: If INTERMEDIATE_TXT is True, run the command to generate intermediate text first
|
||||
if not input_source.endswith(".txt"):
|
||||
completed = subprocess.run(
|
||||
self._build_command(input_source, output_dest),
|
||||
cwd=str(self.CWD),
|
||||
env=self.ENV,
|
||||
check=True)
|
||||
|
||||
if self.INTERMEDIATE_TXT:
|
||||
txt_file = self._normalize_path(input_source).with_suffix(".txt")
|
||||
@@ -124,11 +144,11 @@ class GenericTTSBackend:
|
||||
txt_file = txt_file.with_stem(txt_file.stem + "_replaced")
|
||||
completed = subprocess.run(
|
||||
self._build_command(txt_file, output_dest),
|
||||
cwd=str(self._normalize_path(input_source).parent),
|
||||
cwd=str(self.CWD),
|
||||
env=self.ENV,
|
||||
check=True
|
||||
)
|
||||
|
||||
|
||||
return completed
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user