From 146b5354d371ff53857420807c904cf6c69e5f2a Mon Sep 17 00:00:00 2001 From: tarepan Date: Sat, 25 May 2024 14:46:13 +0000 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=E3=83=A9=E3=82=A4=E3=83=96?= =?UTF-8?q?=E3=83=A9=E3=83=AA=E9=96=A2=E9=80=A3=E3=81=AE=20Model=20?= =?UTF-8?q?=E3=82=92=E7=A7=BB=E5=8B=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voicevox_engine/app/openapi_schema.py | 2 +- voicevox_engine/app/routers/library.py | 7 ++- voicevox_engine/app/routers/speaker.py | 3 +- voicevox_engine/library_manager.py | 61 +++++++++++++++++++++++--- voicevox_engine/model.py | 57 +----------------------- 5 files changed, 63 insertions(+), 67 deletions(-) diff --git a/voicevox_engine/app/openapi_schema.py b/voicevox_engine/app/openapi_schema.py index af7218376..75e17b913 100644 --- a/voicevox_engine/app/openapi_schema.py +++ b/voicevox_engine/app/openapi_schema.py @@ -5,7 +5,7 @@ from fastapi import FastAPI from fastapi.openapi.utils import get_openapi -from voicevox_engine.model import BaseLibraryInfo, VvlibManifest +from voicevox_engine.library_manager import BaseLibraryInfo, VvlibManifest def configure_openapi_schema(app: FastAPI) -> FastAPI: diff --git a/voicevox_engine/app/routers/library.py b/voicevox_engine/app/routers/library.py index 6e643b1fc..ca0592949 100644 --- a/voicevox_engine/app/routers/library.py +++ b/voicevox_engine/app/routers/library.py @@ -7,8 +7,11 @@ from fastapi import APIRouter, Depends, HTTPException, Path, Request from voicevox_engine.engine_manifest import EngineManifest -from voicevox_engine.library_manager import LibraryManager -from voicevox_engine.model import DownloadableLibraryInfo, InstalledLibraryInfo +from voicevox_engine.library_manager import ( + DownloadableLibraryInfo, + InstalledLibraryInfo, + LibraryManager, +) from ..dependencies import check_disabled_mutable_api diff --git a/voicevox_engine/app/routers/speaker.py b/voicevox_engine/app/routers/speaker.py index 834db34a9..1d3863a62 100644 --- a/voicevox_engine/app/routers/speaker.py +++ b/voicevox_engine/app/routers/speaker.py @@ -9,9 +9,8 @@ from pydantic import parse_obj_as from voicevox_engine.core.core_initializer import CoreManager -from voicevox_engine.metas.Metas import StyleId +from voicevox_engine.metas.Metas import Speaker, SpeakerInfo, StyleId from voicevox_engine.metas.MetasStore import MetasStore, filter_speakers_and_styles -from voicevox_engine.model import Speaker, SpeakerInfo def b64encode_str(s: bytes) -> str: diff --git a/voicevox_engine/library_manager.py b/voicevox_engine/library_manager.py index 45e9d6916..600a1a7de 100644 --- a/voicevox_engine/library_manager.py +++ b/voicevox_engine/library_manager.py @@ -7,20 +7,69 @@ from typing import BinaryIO from fastapi import HTTPException -from pydantic import ValidationError +from pydantic import BaseModel, Field, StrictStr, ValidationError from semver.version import Version -from voicevox_engine.model import ( - DownloadableLibraryInfo, - InstalledLibraryInfo, - VvlibManifest, -) +from .metas.Metas import Speaker, SpeakerInfo __all__ = ["LibraryManager"] INFO_FILE = "metas.json" +class LibrarySpeaker(BaseModel): + """ + 音声ライブラリに含まれる話者の情報 + """ + + speaker: Speaker = Field(title="話者情報") + speaker_info: SpeakerInfo = Field(title="話者の追加情報") + + +class BaseLibraryInfo(BaseModel): + """ + 音声ライブラリの情報 + """ + + name: str = Field(title="音声ライブラリの名前") + uuid: str = Field(title="音声ライブラリのUUID") + version: str = Field(title="音声ライブラリのバージョン") + download_url: str = Field(title="音声ライブラリのダウンロードURL") + bytes: int = Field(title="音声ライブラリのバイト数") + speakers: list[LibrarySpeaker] = Field(title="音声ライブラリに含まれる話者のリスト") + + +# 今後InstalledLibraryInfo同様に拡張する可能性を考え、モデルを分けている +class DownloadableLibraryInfo(BaseLibraryInfo): + """ + ダウンロード可能な音声ライブラリの情報 + """ + + pass + + +class InstalledLibraryInfo(BaseLibraryInfo): + """ + インストール済み音声ライブラリの情報 + """ + + uninstallable: bool = Field(title="アンインストール可能かどうか") + + +class VvlibManifest(BaseModel): + """ + vvlib(VOICEVOX Library)に関する情報 + """ + + manifest_version: StrictStr = Field(title="マニフェストバージョン") + name: StrictStr = Field(title="音声ライブラリ名") + version: StrictStr = Field(title="音声ライブラリバージョン") + uuid: StrictStr = Field(title="音声ライブラリのUUID") + brand_name: StrictStr = Field(title="エンジンのブランド名") + engine_name: StrictStr = Field(title="エンジン名") + engine_uuid: StrictStr = Field(title="エンジンのUUID") + + class LibraryManager: """音声ライブラリ (`.vvlib`) の管理""" diff --git a/voicevox_engine/model.py b/voicevox_engine/model.py index beb4b4098..5ea822d3d 100644 --- a/voicevox_engine/model.py +++ b/voicevox_engine/model.py @@ -2,9 +2,7 @@ from re import findall, fullmatch from typing import Any -from pydantic import BaseModel, Field, StrictStr, validator - -from .metas.Metas import Speaker, SpeakerInfo +from pydantic import BaseModel, Field, validator class Mora(BaseModel): @@ -165,45 +163,6 @@ def __init__(self, style_id: int, *args: object, **kywrds: object) -> None: super().__init__(f"style_id {style_id} is not found.", *args, **kywrds) -class LibrarySpeaker(BaseModel): - """ - 音声ライブラリに含まれる話者の情報 - """ - - speaker: Speaker = Field(title="話者情報") - speaker_info: SpeakerInfo = Field(title="話者の追加情報") - - -class BaseLibraryInfo(BaseModel): - """ - 音声ライブラリの情報 - """ - - name: str = Field(title="音声ライブラリの名前") - uuid: str = Field(title="音声ライブラリのUUID") - version: str = Field(title="音声ライブラリのバージョン") - download_url: str = Field(title="音声ライブラリのダウンロードURL") - bytes: int = Field(title="音声ライブラリのバイト数") - speakers: list[LibrarySpeaker] = Field(title="音声ライブラリに含まれる話者のリスト") - - -# 今後InstalledLibraryInfo同様に拡張する可能性を考え、モデルを分けている -class DownloadableLibraryInfo(BaseLibraryInfo): - """ - ダウンロード可能な音声ライブラリの情報 - """ - - pass - - -class InstalledLibraryInfo(BaseLibraryInfo): - """ - インストール済み音声ライブラリの情報 - """ - - uninstallable: bool = Field(title="アンインストール可能かどうか") - - USER_DICT_MIN_PRIORITY = 0 USER_DICT_MAX_PRIORITY = 10 @@ -343,17 +302,3 @@ class SupportedFeaturesInfo(BaseModel): title="疑似疑問文に対応しているかどうか" ) support_switching_device: bool = Field(title="CPU/GPUの切り替えが可能かどうか") - - -class VvlibManifest(BaseModel): - """ - vvlib(VOICEVOX Library)に関する情報 - """ - - manifest_version: StrictStr = Field(title="マニフェストバージョン") - name: StrictStr = Field(title="音声ライブラリ名") - version: StrictStr = Field(title="音声ライブラリバージョン") - uuid: StrictStr = Field(title="音声ライブラリのUUID") - brand_name: StrictStr = Field(title="エンジンのブランド名") - engine_name: StrictStr = Field(title="エンジン名") - engine_uuid: StrictStr = Field(title="エンジンのUUID") From 5bccebb3684e3eb1abc4ff69b40dce8acfa2baf1 Mon Sep 17 00:00:00 2001 From: tarepan Date: Tue, 28 May 2024 10:14:56 +0000 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20model=20=E3=82=92=E5=88=86?= =?UTF-8?q?=E9=9B=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/test_library_manager.py | 2 +- voicevox_engine/app/application.py | 2 +- voicevox_engine/app/openapi_schema.py | 2 +- voicevox_engine/app/routers/library.py | 7 +-- .../{ => library}/library_manager.py | 61 ++---------------- voicevox_engine/library/model.py | 62 +++++++++++++++++++ 6 files changed, 73 insertions(+), 63 deletions(-) rename voicevox_engine/{ => library}/library_manager.py (83%) create mode 100644 voicevox_engine/library/model.py diff --git a/test/test_library_manager.py b/test/test_library_manager.py index f9368d378..8471979cf 100644 --- a/test/test_library_manager.py +++ b/test/test_library_manager.py @@ -11,7 +11,7 @@ from fastapi import HTTPException -from voicevox_engine.library_manager import LibraryManager +from voicevox_engine.library.library_manager import LibraryManager vvlib_manifest_name = "vvlib_manifest.json" diff --git a/voicevox_engine/app/application.py b/voicevox_engine/app/application.py index b8773c286..457b78f06 100644 --- a/voicevox_engine/app/application.py +++ b/voicevox_engine/app/application.py @@ -18,7 +18,7 @@ from voicevox_engine.cancellable_engine import CancellableEngine from voicevox_engine.core.core_initializer import CoreManager from voicevox_engine.engine_manifest import EngineManifest -from voicevox_engine.library_manager import LibraryManager +from voicevox_engine.library.library_manager import LibraryManager from voicevox_engine.metas.MetasStore import MetasStore from voicevox_engine.preset.PresetManager import PresetManager from voicevox_engine.setting.Setting import CorsPolicyMode, SettingHandler diff --git a/voicevox_engine/app/openapi_schema.py b/voicevox_engine/app/openapi_schema.py index 75e17b913..815bf4808 100644 --- a/voicevox_engine/app/openapi_schema.py +++ b/voicevox_engine/app/openapi_schema.py @@ -5,7 +5,7 @@ from fastapi import FastAPI from fastapi.openapi.utils import get_openapi -from voicevox_engine.library_manager import BaseLibraryInfo, VvlibManifest +from voicevox_engine.library.model import BaseLibraryInfo, VvlibManifest def configure_openapi_schema(app: FastAPI) -> FastAPI: diff --git a/voicevox_engine/app/routers/library.py b/voicevox_engine/app/routers/library.py index ca0592949..cc02afa6b 100644 --- a/voicevox_engine/app/routers/library.py +++ b/voicevox_engine/app/routers/library.py @@ -7,11 +7,8 @@ from fastapi import APIRouter, Depends, HTTPException, Path, Request from voicevox_engine.engine_manifest import EngineManifest -from voicevox_engine.library_manager import ( - DownloadableLibraryInfo, - InstalledLibraryInfo, - LibraryManager, -) +from voicevox_engine.library.library_manager import LibraryManager +from voicevox_engine.library.model import DownloadableLibraryInfo, InstalledLibraryInfo from ..dependencies import check_disabled_mutable_api diff --git a/voicevox_engine/library_manager.py b/voicevox_engine/library/library_manager.py similarity index 83% rename from voicevox_engine/library_manager.py rename to voicevox_engine/library/library_manager.py index 600a1a7de..9ad3af3c5 100644 --- a/voicevox_engine/library_manager.py +++ b/voicevox_engine/library/library_manager.py @@ -7,69 +7,20 @@ from typing import BinaryIO from fastapi import HTTPException -from pydantic import BaseModel, Field, StrictStr, ValidationError +from pydantic import ValidationError from semver.version import Version -from .metas.Metas import Speaker, SpeakerInfo +from voicevox_engine.library.model import ( + DownloadableLibraryInfo, + InstalledLibraryInfo, + VvlibManifest, +) __all__ = ["LibraryManager"] INFO_FILE = "metas.json" -class LibrarySpeaker(BaseModel): - """ - 音声ライブラリに含まれる話者の情報 - """ - - speaker: Speaker = Field(title="話者情報") - speaker_info: SpeakerInfo = Field(title="話者の追加情報") - - -class BaseLibraryInfo(BaseModel): - """ - 音声ライブラリの情報 - """ - - name: str = Field(title="音声ライブラリの名前") - uuid: str = Field(title="音声ライブラリのUUID") - version: str = Field(title="音声ライブラリのバージョン") - download_url: str = Field(title="音声ライブラリのダウンロードURL") - bytes: int = Field(title="音声ライブラリのバイト数") - speakers: list[LibrarySpeaker] = Field(title="音声ライブラリに含まれる話者のリスト") - - -# 今後InstalledLibraryInfo同様に拡張する可能性を考え、モデルを分けている -class DownloadableLibraryInfo(BaseLibraryInfo): - """ - ダウンロード可能な音声ライブラリの情報 - """ - - pass - - -class InstalledLibraryInfo(BaseLibraryInfo): - """ - インストール済み音声ライブラリの情報 - """ - - uninstallable: bool = Field(title="アンインストール可能かどうか") - - -class VvlibManifest(BaseModel): - """ - vvlib(VOICEVOX Library)に関する情報 - """ - - manifest_version: StrictStr = Field(title="マニフェストバージョン") - name: StrictStr = Field(title="音声ライブラリ名") - version: StrictStr = Field(title="音声ライブラリバージョン") - uuid: StrictStr = Field(title="音声ライブラリのUUID") - brand_name: StrictStr = Field(title="エンジンのブランド名") - engine_name: StrictStr = Field(title="エンジン名") - engine_uuid: StrictStr = Field(title="エンジンのUUID") - - class LibraryManager: """音声ライブラリ (`.vvlib`) の管理""" diff --git a/voicevox_engine/library/model.py b/voicevox_engine/library/model.py new file mode 100644 index 000000000..c460c3ac4 --- /dev/null +++ b/voicevox_engine/library/model.py @@ -0,0 +1,62 @@ +""" +ライブラリ機能に関して API と ENGINE 内部実装が共有するモデル + +「API と ENGINE 内部実装が共有するモデル」については `voicevox_engine/model.py` の module docstring を確認すること。 +""" + +from pydantic import BaseModel, Field, StrictStr + +from voicevox_engine.metas.Metas import Speaker, SpeakerInfo + + +class LibrarySpeaker(BaseModel): + """ + 音声ライブラリに含まれる話者の情報 + """ + + speaker: Speaker = Field(title="話者情報") + speaker_info: SpeakerInfo = Field(title="話者の追加情報") + + +class BaseLibraryInfo(BaseModel): + """ + 音声ライブラリの情報 + """ + + name: str = Field(title="音声ライブラリの名前") + uuid: str = Field(title="音声ライブラリのUUID") + version: str = Field(title="音声ライブラリのバージョン") + download_url: str = Field(title="音声ライブラリのダウンロードURL") + bytes: int = Field(title="音声ライブラリのバイト数") + speakers: list[LibrarySpeaker] = Field(title="音声ライブラリに含まれる話者のリスト") + + +# 今後InstalledLibraryInfo同様に拡張する可能性を考え、モデルを分けている +class DownloadableLibraryInfo(BaseLibraryInfo): + """ + ダウンロード可能な音声ライブラリの情報 + """ + + pass + + +class InstalledLibraryInfo(BaseLibraryInfo): + """ + インストール済み音声ライブラリの情報 + """ + + uninstallable: bool = Field(title="アンインストール可能かどうか") + + +class VvlibManifest(BaseModel): + """ + vvlib(VOICEVOX Library)に関する情報 + """ + + manifest_version: StrictStr = Field(title="マニフェストバージョン") + name: StrictStr = Field(title="音声ライブラリ名") + version: StrictStr = Field(title="音声ライブラリバージョン") + uuid: StrictStr = Field(title="音声ライブラリのUUID") + brand_name: StrictStr = Field(title="エンジンのブランド名") + engine_name: StrictStr = Field(title="エンジン名") + engine_uuid: StrictStr = Field(title="エンジンのUUID")