Skip to content

Commit

Permalink
プリセットファイルのパスを起動引数・環境変数で変更できるようにする (#711)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
aoirint and Hiroshiba authored Oct 9, 2023
1 parent b64db22 commit a398d3b
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 11 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,9 @@ VOICEVOXではセキュリティ保護のため`localhost`・`127.0.0.1`・`app:
```bash
$ python run.py -h

usage: run.py [-h] [--host HOST] [--port PORT] [--use_gpu] [--voicevox_dir VOICEVOX_DIR] [--voicelib_dir VOICELIB_DIR] [--runtime_dir RUNTIME_DIR] [--enable_mock] [--enable_cancellable_synthesis] [--init_processes INIT_PROCESSES] [--load_all_models]
[--cpu_num_threads CPU_NUM_THREADS] [--output_log_utf8] [--cors_policy_mode {CorsPolicyMode.all,CorsPolicyMode.localapps}] [--allow_origin [ALLOW_ORIGIN ...]] [--setting_file SETTING_FILE]
usage: run.py [-h] [--host HOST] [--port PORT] [--use_gpu] [--voicevox_dir VOICEVOX_DIR] [--voicelib_dir VOICELIB_DIR] [--runtime_dir RUNTIME_DIR] [--enable_mock] [--enable_cancellable_synthesis]
[--init_processes INIT_PROCESSES] [--load_all_models] [--cpu_num_threads CPU_NUM_THREADS] [--output_log_utf8] [--cors_policy_mode {CorsPolicyMode.all,CorsPolicyMode.localapps}]
[--allow_origin [ALLOW_ORIGIN ...]] [--setting_file SETTING_FILE] [--preset_file PRESET_FILE]

VOICEVOX のエンジンです。

Expand Down Expand Up @@ -319,6 +320,8 @@ options:
許可するオリジンを指定します。スペースで区切ることで複数指定できます。
--setting_file SETTING_FILE
設定ファイルを指定できます。
--preset_file PRESET_FILE
プリセットファイルを指定できます。指定がない場合、環境変数 VV_PRESET_FILE、--voicevox_dirのpresets.yaml、実行ファイルのディレクトリのpresets.yamlを順に探します。
```
## アップデート
Expand Down
5 changes: 5 additions & 0 deletions make_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

from voicevox_engine.dev.core import mock as core
from voicevox_engine.dev.synthesis_engine.mock import MockSynthesisEngine
from voicevox_engine.preset import PresetManager
from voicevox_engine.setting import USER_SETTING_PATH, SettingLoader
from voicevox_engine.utility import engine_root

if __name__ == "__main__":
import run
Expand All @@ -11,6 +13,9 @@
synthesis_engines={"mock": MockSynthesisEngine(speakers=core.metas())},
latest_core_version="mock",
setting_loader=SettingLoader(USER_SETTING_PATH),
preset_manager=PresetManager( # FIXME: impl MockPresetManager
preset_path=engine_root() / "presets.yaml",
),
)
with open("docs/api/index.html", "w") as f:
f.write(
Expand Down
46 changes: 37 additions & 9 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ def generate_app(
synthesis_engines: Dict[str, SynthesisEngineBase],
latest_core_version: str,
setting_loader: SettingLoader,
preset_manager: PresetManager,
root_dir: Optional[Path] = None,
cors_policy_mode: CorsPolicyMode = CorsPolicyMode.localapps,
allow_origin: Optional[List[str]] = None,
Expand Down Expand Up @@ -177,9 +178,6 @@ async def block_origin_middleware(request: Request, call_next):
status_code=403, content={"detail": "Origin not allowed"}
)

preset_manager = PresetManager(
preset_path=root_dir / "presets.yaml",
)
engine_manifest_data = EngineManifestLoader(
engine_root() / "engine_manifest.json", engine_root()
).load_manifest()
Expand Down Expand Up @@ -1273,6 +1271,17 @@ def custom_openapi():
"--setting_file", type=Path, default=USER_SETTING_PATH, help="設定ファイルを指定できます。"
)

parser.add_argument(
"--preset_file",
type=Path,
default=None,
help=(
"プリセットファイルを指定できます。"
"指定がない場合、環境変数 VV_PRESET_FILE、--voicevox_dirのpresets.yaml、"
"実行ファイルのディレクトリのpresets.yamlを順に探します。"
),
)

args = parser.parse_args()

if args.output_log_utf8:
Expand All @@ -1296,29 +1305,48 @@ def custom_openapi():
if args.enable_cancellable_synthesis:
cancellable_engine = CancellableEngine(args)

root_dir = args.voicevox_dir if args.voicevox_dir is not None else engine_root()
root_dir: Path | None = args.voicevox_dir
if root_dir is None:
root_dir = engine_root()

setting_loader = SettingLoader(args.setting_file)

settings = setting_loader.load_setting_file()

cors_policy_mode = (
args.cors_policy_mode
if args.cors_policy_mode is not None
else settings.cors_policy_mode
)
cors_policy_mode: CorsPolicyMode | None = args.cors_policy_mode
if cors_policy_mode is None:
cors_policy_mode = settings.cors_policy_mode

allow_origin = None
if args.allow_origin is not None:
allow_origin = args.allow_origin
elif settings.allow_origin is not None:
allow_origin = settings.allow_origin.split(" ")

# Preset Manager
# preset_pathの優先順: 引数、環境変数、voicevox_dir、実行ファイルのディレクトリ
# ファイルの存在に関わらず、優先順で最初に指定されたパスをプリセットファイルとして使用する
preset_path: Path | None = args.preset_file
if preset_path is None:
# 引数 --preset_file の指定がない場合
env_preset_path = os.getenv("VV_PRESET_FILE")
if env_preset_path is not None and len(env_preset_path) != 0:
# 環境変数 VV_PRESET_FILE の指定がある場合
preset_path = Path(env_preset_path)
else:
# 環境変数 VV_PRESET_FILE の指定がない場合
preset_path = root_dir / "presets.yaml"

preset_manager = PresetManager(
preset_path=preset_path,
)

uvicorn.run(
generate_app(
synthesis_engines,
latest_core_version,
setting_loader,
preset_manager=preset_manager,
root_dir=root_dir,
cors_policy_mode=cors_policy_mode,
allow_origin=allow_origin,
Expand Down
5 changes: 5 additions & 0 deletions test/e2e/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from fastapi.testclient import TestClient
from run import generate_app

from voicevox_engine.preset import PresetManager
from voicevox_engine.setting import SettingLoader
from voicevox_engine.synthesis_engine import make_synthesis_engines
from voicevox_engine.utility.core_version_utility import get_latest_core_version
Expand All @@ -14,11 +15,15 @@ def client():
synthesis_engines = make_synthesis_engines(use_gpu=False)
latest_core_version = get_latest_core_version(versions=synthesis_engines.keys())
setting_loader = SettingLoader(Path("./default_setting.yml"))
preset_manager = PresetManager( # FIXME: impl MockPresetManager
preset_path=Path("./presets.yaml"),
)

return TestClient(
generate_app(
synthesis_engines=synthesis_engines,
latest_core_version=latest_core_version,
setting_loader=setting_loader,
preset_manager=preset_manager,
)
)

0 comments on commit a398d3b

Please sign in to comment.