Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

プリセットファイルのパスを起動引数・環境変数で変更できるようにする #711

Merged
merged 19 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 から読み込みます。VV_PRESET_FILEの指定がない場合、--voicevox_dirのpresets.yamlから読み込みます。voicevox_dirの指定がない場合、実行ファイルのディレクトリのpresets.yamlから読み込みます。指定されたパスにファイルが存在しないまたは破損している場合、プリセットAPIはエラーを返します。
```

## アップデート
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
40 changes: 36 additions & 4 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,19 @@ 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 から読み込みます。"
"VV_PRESET_FILEの指定がない場合、--voicevox_dirのpresets.yamlから読み込みます。"
"voicevox_dirの指定がない場合、実行ファイルのディレクトリのpresets.yamlから読み込みます。"
"指定されたパスにファイルが存在しないまたは破損している場合、プリセットAPIはエラーを返します。"
),
)

args = parser.parse_args()

if args.output_log_utf8:
Expand All @@ -1296,7 +1307,9 @@ 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()
aoirint marked this conversation as resolved.
Show resolved Hide resolved

setting_loader = SettingLoader(args.setting_file)

Expand All @@ -1314,11 +1327,30 @@ def custom_openapi():
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_path 引数の指定がない場合
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"
Hiroshiba marked this conversation as resolved.
Show resolved Hide resolved

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