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

Improve: spekaer_infoをasync化 #1073

Closed
Show file tree
Hide file tree
Changes from all 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
25 changes: 23 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pyopenjtalk = { git = "https://github.com/VOICEVOX/pyopenjtalk", rev = "b35fc89f
semver = "^3.0.0"
platformdirs = "^3.10.0"
soxr = "^0.3.6"
aiofiles = "^23.2.1"

[tool.poetry.group.dev.dependencies]
pyinstaller = "^5.13"
Expand All @@ -74,6 +75,7 @@ coveralls = "^3.2.0"
poetry = "^1.3.1"
httpx = "^0.25.0" # NOTE: required by fastapi.testclient.TestClient
syrupy = "^4.6.0"
types-aiofiles = "^23.2.0.20240106"
types-pyyaml = "^6.0"

[tool.poetry.group.license.dependencies]
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiofiles==23.2.1 ; python_version >= "3.11" and python_version < "3.12"
altgraph==0.17.3 ; python_version >= "3.11" and python_version < "3.12"
anyio==3.7.1 ; python_version >= "3.11" and python_version < "3.12"
asgiref==3.7.2 ; python_version >= "3.11" and python_version < "3.12"
Expand Down
1 change: 1 addition & 0 deletions requirements-license.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiofiles==23.2.1 ; python_version >= "3.11" and python_version < "3.12"
anyio==3.7.1 ; python_version >= "3.11" and python_version < "3.12"
asgiref==3.7.2 ; python_version >= "3.11" and python_version < "3.12"
cffi==1.15.1 ; python_version >= "3.11" and python_version < "3.12"
Expand Down
2 changes: 2 additions & 0 deletions requirements-test.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiofiles==23.2.1 ; python_version >= "3.11" and python_version < "3.12"
anyio==3.7.1 ; python_version >= "3.11" and python_version < "3.12"
asgiref==3.7.2 ; python_version >= "3.11" and python_version < "3.12"
attrs==23.1.0 ; python_version >= "3.11" and python_version < "3.12"
Expand Down Expand Up @@ -85,6 +86,7 @@ syrupy==4.6.0 ; python_version >= "3.11" and python_version < "3.12"
tomlkit==0.12.1 ; python_version >= "3.11" and python_version < "3.12"
tqdm==4.66.1 ; python_version >= "3.11" and python_version < "3.12"
trove-classifiers==2023.8.7 ; python_version >= "3.11" and python_version < "3.12"
types-aiofiles==23.2.0.20240106 ; python_version >= "3.11" and python_version < "3.12"
types-pyyaml==6.0.12.12 ; python_version >= "3.11" and python_version < "3.12"
typing-extensions==4.7.1 ; python_version >= "3.11" and python_version < "3.12"
unidiff==0.7.5 ; python_version >= "3.11" and python_version < "3.12"
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
aiofiles==23.2.1 ; python_version >= "3.11" and python_version < "3.12"
anyio==3.7.1 ; python_version >= "3.11" and python_version < "3.12"
asgiref==3.7.2 ; python_version >= "3.11" and python_version < "3.12"
cffi==1.15.1 ; python_version >= "3.11" and python_version < "3.12"
Expand Down
37 changes: 21 additions & 16 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from tempfile import NamedTemporaryFile, TemporaryFile
from typing import Annotated, Literal, Optional

import aiofiles
import soundfile
import uvicorn
from fastapi import Depends, FastAPI, Form, HTTPException, Query, Request, Response
Expand Down Expand Up @@ -852,22 +853,22 @@ def speakers(
return filter_speakers_and_styles(speakers, "speaker")

@app.get("/speaker_info", response_model=SpeakerInfo, tags=["その他"])
def speaker_info(
async def speaker_info(
speaker_uuid: str,
core_version: str | None = None,
) -> SpeakerInfo:
"""
指定されたspeaker_uuidに関する情報をjson形式で返します。
画像や音声はbase64エンコードされたものが返されます。
"""
return _speaker_info(
return await _speaker_info(
speaker_uuid=speaker_uuid,
speaker_or_singer="speaker",
core_version=core_version,
)

# FIXME: この関数をどこかに切り出す
def _speaker_info(
async def _speaker_info(
speaker_uuid: str,
speaker_or_singer: Literal["speaker", "singer"],
core_version: str | None,
Expand Down Expand Up @@ -915,29 +916,33 @@ def _speaker_info(
policy = policy_path.read_text("utf-8")
# speaker portrait
portrait_path = speaker_path / "portrait.png"
portrait = b64encode_str(portrait_path.read_bytes())
async with aiofiles.open(portrait_path, "rb") as f:
portrait = b64encode_str(await f.read())
# スタイル情報の取得
style_infos = []
for style in speaker.styles:
id = style.id
# style icon
style_icon_path = speaker_path / "icons" / f"{id}.png"
icon = b64encode_str(style_icon_path.read_bytes())
async with aiofiles.open(style_icon_path, "rb") as f:
icon = b64encode_str(await f.read())
# style portrait
style_portrait_path = speaker_path / "portraits" / f"{id}.png"
style_portrait = None
if style_portrait_path.exists():
style_portrait = b64encode_str(style_portrait_path.read_bytes())
async with aiofiles.open(style_portrait_path, "rb") as f:
style_portrait = b64encode_str(await f.read())
# voice samples
voice_samples = [
b64encode_str(
(
speaker_path
/ "voice_samples/{}_{}.wav".format(id, str(j + 1).zfill(3))
).read_bytes()
voice_samples = []
for j in range(3):
voice_sample_path = (
speaker_path
/ "voice_samples"
/ f"{id}_{str(j + 1).zfill(3)}.wav"
)
for j in range(3)
]
if voice_sample_path.exists():
async with aiofiles.open(voice_sample_path, "rb") as f:
voice_samples.append(b64encode_str(await f.read()))
style_infos.append(
{
"id": id,
Expand Down Expand Up @@ -967,15 +972,15 @@ def singers(
return filter_speakers_and_styles(singers, "singer")

@app.get("/singer_info", response_model=SpeakerInfo, tags=["その他"])
def singer_info(
async def singer_info(
speaker_uuid: str,
core_version: str | None = None,
) -> SpeakerInfo:
"""
指定されたspeaker_uuidに関する情報をjson形式で返します。
画像や音声はbase64エンコードされたものが返されます。
"""
return _speaker_info(
return await _speaker_info(
speaker_uuid=speaker_uuid,
speaker_or_singer="singer",
core_version=core_version,
Expand Down
Loading