From 8bdb63bcf4af08857ec892b32fb6e316cb42be71 Mon Sep 17 00:00:00 2001 From: tarepan Date: Thu, 16 May 2024 00:02:55 +0000 Subject: [PATCH 1/3] =?UTF-8?q?refactor:=20device=20=E3=81=AB=E9=96=A2?= =?UTF-8?q?=E3=81=97=E3=81=A6=20API=20=E3=81=A8=E9=9F=B3=E5=A3=B0=E3=83=A9?= =?UTF-8?q?=E3=82=A4=E3=83=96=E3=83=A9=E3=83=AA=E3=82=92=E5=88=86=E9=9B=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voicevox_engine/app/routers/engine_info.py | 27 ++++++++++++++++++---- voicevox_engine/core/core_adapter.py | 25 ++++++++++++++++---- voicevox_engine/model.py | 10 -------- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/voicevox_engine/app/routers/engine_info.py b/voicevox_engine/app/routers/engine_info.py index 6ad6ce768..e9fadbcac 100644 --- a/voicevox_engine/app/routers/engine_info.py +++ b/voicevox_engine/app/routers/engine_info.py @@ -1,14 +1,33 @@ """エンジンの情報機能を提供する API Router""" import json -from typing import Callable +from typing import Callable, Self from fastapi import APIRouter, HTTPException, Response +from pydantic import BaseModel, Field from voicevox_engine import __version__ -from voicevox_engine.core.core_adapter import CoreAdapter +from voicevox_engine.core.core_adapter import CoreAdapter, DeviceSupport from voicevox_engine.engine_manifest.EngineManifest import EngineManifest -from voicevox_engine.model import SupportedDevicesInfo + + +class SupportedDevicesInfo(BaseModel): + """ + 対応しているデバイスの情報 + """ + + cpu: bool = Field(title="CPUに対応しているか") + cuda: bool = Field(title="CUDA(Nvidia GPU)に対応しているか") + dml: bool = Field(title="DirectML(Nvidia GPU/Radeon GPU等)に対応しているか") + + @classmethod + def generate_from(cls, device_support: DeviceSupport) -> Self: + """`DeviceSupport` インスタンスから本インスタンスを生成する。""" + return cls( + cpu=device_support.cpu, + cuda=device_support.cuda, + dml=device_support.dml, + ) def generate_engine_info_router( @@ -41,7 +60,7 @@ def supported_devices(core_version: str | None = None) -> Response: if supported_devices is None: raise HTTPException(status_code=422, detail="非対応の機能です。") return Response( - content=supported_devices, + content=SupportedDevicesInfo.generate_from(supported_devices), media_type="application/json", ) diff --git a/voicevox_engine/core/core_adapter.py b/voicevox_engine/core/core_adapter.py index ba3c24908..61981c089 100644 --- a/voicevox_engine/core/core_adapter.py +++ b/voicevox_engine/core/core_adapter.py @@ -1,4 +1,6 @@ +import json import threading +from dataclasses import dataclass import numpy as np from numpy.typing import NDArray @@ -7,6 +9,15 @@ from .core_wrapper import CoreWrapper, OldCoreError +@dataclass(frozen=True) +class DeviceSupport: + """音声ライブラリのデバイス利用可否""" + + cpu: bool + cuda: bool # CUDA (Nvidia GPU) + dml: bool # DirectML (Nvidia GPU/Radeon GPU等) + + class CoreAdapter: """ コアのアダプター。 @@ -28,13 +39,19 @@ def speakers(self) -> str: return self.core.metas() @property - def supported_devices(self) -> str | None: + def supported_devices(self) -> DeviceSupport | None: """デバイスサポート情報(None: 情報無し)""" try: - supported_devices = self.core.supported_devices() + supported_devices = json.loads(self.core.supported_devices()) + assert isinstance(supported_devices, dict) + device_support = DeviceSupport( + cpu=supported_devices["cpu"], + cuda=supported_devices["cuda"], + dml=supported_devices["dml"], + ) except OldCoreError: - supported_devices = None - return supported_devices + device_support = None + return device_support def initialize_style_id_synthesis( self, style_id: StyleId, skip_reinit: bool diff --git a/voicevox_engine/model.py b/voicevox_engine/model.py index 2249b34bc..beb4b4098 100644 --- a/voicevox_engine/model.py +++ b/voicevox_engine/model.py @@ -326,16 +326,6 @@ class WordTypes(str, Enum): SUFFIX = "SUFFIX" -class SupportedDevicesInfo(BaseModel): - """ - 対応しているデバイスの情報 - """ - - cpu: bool = Field(title="CPUに対応しているか") - cuda: bool = Field(title="CUDA(Nvidia GPU)に対応しているか") - dml: bool = Field(title="DirectML(Nvidia GPU/Radeon GPU等)に対応しているか") - - class SupportedFeaturesInfo(BaseModel): """ エンジンの機能の情報 From 331ce8864f5a1eb3a2e48514f4db01f99a8a39c2 Mon Sep 17 00:00:00 2001 From: tarepan Date: Thu, 16 May 2024 00:31:00 +0000 Subject: [PATCH 2/3] =?UTF-8?q?fix:=20=E6=88=BB=E3=82=8A=E5=80=A4=E3=82=92?= =?UTF-8?q?=E7=9B=B4=E6=8E=A5=E8=BF=94=E3=81=99=E3=82=88=E3=81=86=E3=81=AB?= =?UTF-8?q?=E5=A4=89=E6=9B=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- voicevox_engine/app/routers/engine_info.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/voicevox_engine/app/routers/engine_info.py b/voicevox_engine/app/routers/engine_info.py index e9fadbcac..3390a45e2 100644 --- a/voicevox_engine/app/routers/engine_info.py +++ b/voicevox_engine/app/routers/engine_info.py @@ -54,15 +54,12 @@ async def core_versions() -> Response: @router.get( "/supported_devices", response_model=SupportedDevicesInfo, tags=["その他"] ) - def supported_devices(core_version: str | None = None) -> Response: + def supported_devices(core_version: str | None = None) -> SupportedDevicesInfo: """対応デバイスの一覧を取得します。""" supported_devices = get_core(core_version).supported_devices if supported_devices is None: raise HTTPException(status_code=422, detail="非対応の機能です。") - return Response( - content=SupportedDevicesInfo.generate_from(supported_devices), - media_type="application/json", - ) + return SupportedDevicesInfo.generate_from(supported_devices) @router.get("/engine_manifest", response_model=EngineManifest, tags=["その他"]) async def engine_manifest() -> EngineManifest: From 2ea950b7516258e55e1aa7bfdf12ae6d72d903c1 Mon Sep 17 00:00:00 2001 From: Hiroshiba Date: Sat, 25 May 2024 19:18:42 +0900 Subject: [PATCH 3/3] Apply suggestions from code review --- voicevox_engine/app/routers/engine_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/voicevox_engine/app/routers/engine_info.py b/voicevox_engine/app/routers/engine_info.py index dccadf0bf..4299e1581 100644 --- a/voicevox_engine/app/routers/engine_info.py +++ b/voicevox_engine/app/routers/engine_info.py @@ -23,7 +23,7 @@ class SupportedDevicesInfo(BaseModel): @classmethod def generate_from(cls, device_support: DeviceSupport) -> Self: - """`DeviceSupport` インスタンスから本インスタンスを生成する。""" + """`DeviceSupport` インスタンスからこのインスタンスを生成する。""" return cls( cpu=device_support.cpu, cuda=device_support.cuda,