-
Notifications
You must be signed in to change notification settings - Fork 206
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
整理: 音声ライブラリ機能を API Router でモジュール化 (#1193)
* refactor: 音声ライブラリ API Router へのモジュール化 * fix: lint * まちがえた。 --------- Co-authored-by: Hiroshiba Kazuyuki <[email protected]>
- Loading branch information
Showing
2 changed files
with
99 additions
and
86 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
"""音声ライブラリ機能を提供する API Router""" | ||
|
||
import asyncio | ||
from io import BytesIO | ||
from typing import Annotated | ||
|
||
from fastapi import APIRouter, Depends, HTTPException | ||
from fastapi import Path as FAPath | ||
from fastapi import Request, Response | ||
|
||
from voicevox_engine.engine_manifest.EngineManifest import EngineManifest | ||
from voicevox_engine.library_manager import LibraryManager | ||
from voicevox_engine.model import DownloadableLibraryInfo, InstalledLibraryInfo | ||
|
||
from ..dependencies import check_disabled_mutable_api | ||
|
||
|
||
def generate_router( | ||
engine_manifest_data: EngineManifest, library_manager: LibraryManager | ||
) -> APIRouter: | ||
"""音声ライブラリ API Router を生成する""" | ||
router = APIRouter() | ||
|
||
@router.get( | ||
"/downloadable_libraries", | ||
response_model=list[DownloadableLibraryInfo], | ||
response_description="ダウンロード可能な音声ライブラリの情報リスト", | ||
tags=["音声ライブラリ管理"], | ||
) | ||
def downloadable_libraries() -> list[DownloadableLibraryInfo]: | ||
""" | ||
ダウンロード可能な音声ライブラリの情報を返します。 | ||
""" | ||
if not engine_manifest_data.supported_features.manage_library: | ||
raise HTTPException(status_code=404, detail="この機能は実装されていません") | ||
return library_manager.downloadable_libraries() | ||
|
||
@router.get( | ||
"/installed_libraries", | ||
response_model=dict[str, InstalledLibraryInfo], | ||
response_description="インストールした音声ライブラリの情報", | ||
tags=["音声ライブラリ管理"], | ||
) | ||
def installed_libraries() -> dict[str, InstalledLibraryInfo]: | ||
""" | ||
インストールした音声ライブラリの情報を返します。 | ||
""" | ||
if not engine_manifest_data.supported_features.manage_library: | ||
raise HTTPException(status_code=404, detail="この機能は実装されていません") | ||
return library_manager.installed_libraries() | ||
|
||
@router.post( | ||
"/install_library/{library_uuid}", | ||
status_code=204, | ||
tags=["音声ライブラリ管理"], | ||
dependencies=[Depends(check_disabled_mutable_api)], | ||
) | ||
async def install_library( | ||
library_uuid: Annotated[str, FAPath(description="音声ライブラリのID")], | ||
request: Request, | ||
) -> Response: | ||
""" | ||
音声ライブラリをインストールします。 | ||
音声ライブラリのZIPファイルをリクエストボディとして送信してください。 | ||
""" | ||
if not engine_manifest_data.supported_features.manage_library: | ||
raise HTTPException(status_code=404, detail="この機能は実装されていません") | ||
archive = BytesIO(await request.body()) | ||
loop = asyncio.get_event_loop() | ||
await loop.run_in_executor( | ||
None, library_manager.install_library, library_uuid, archive | ||
) | ||
return Response(status_code=204) | ||
|
||
@router.post( | ||
"/uninstall_library/{library_uuid}", | ||
status_code=204, | ||
tags=["音声ライブラリ管理"], | ||
dependencies=[Depends(check_disabled_mutable_api)], | ||
) | ||
def uninstall_library( | ||
library_uuid: Annotated[str, FAPath(description="音声ライブラリのID")] | ||
) -> Response: | ||
""" | ||
音声ライブラリをアンインストールします。 | ||
""" | ||
if not engine_manifest_data.supported_features.manage_library: | ||
raise HTTPException(status_code=404, detail="この機能は実装されていません") | ||
library_manager.uninstall_library(library_uuid) | ||
return Response(status_code=204) | ||
|
||
return router |