Skip to content

Commit

Permalink
プリセットファイルのパスを起動引数・環境変数で変更できるようにする (VOICEVOX/voicevox_engine#711)
Browse files Browse the repository at this point in the history
Co-authored-by: Hiroshiba <[email protected]>
  • Loading branch information
takana-v and Hiroshiba committed Nov 28, 2023
1 parent 43f2f35 commit 7e5648f
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 @@ -402,8 +402,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 @@ -433,6 +434,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 @@ -110,6 +110,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 @@ -174,9 +175,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_loader = EngineManifestLoader(
engine_root() / "engine_manifest.json", engine_root()
)
Expand Down Expand Up @@ -1215,6 +1213,17 @@ def custom_openapi():
help="Bridge Configファイルのあるディレクトリです。",
)

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

args = parser.parse_args()

bridge_config_loader = BridgeConfigLoader(args.bridge_config_dir)
Expand All @@ -1233,17 +1242,17 @@ def custom_openapi():

cancellable_engine = None

root_dir = engine_root()
root_dir: Path | None = None
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:
Expand All @@ -1255,11 +1264,30 @@ def custom_openapi():
host = bridge_config.host if args.host is None else args.host
port = bridge_config.port if args.port is None else args.port

# 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 @@ -5,6 +5,7 @@
from run import generate_app

from voicevox_engine.bridge_config import BridgeConfigLoader
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 @@ -18,11 +19,15 @@ def client():
)
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 7e5648f

Please sign in to comment.