Add: test code for style-bert-vits2 as a library

By executing "hatch run test:test", you can check whether the test passes in all Python 3.9 to 3.12 environments.
This commit is contained in:
tsukumi
2024-03-10 04:18:39 +00:00
parent d2fd378b56
commit afff154da4
4 changed files with 56 additions and 6 deletions

View File

@@ -52,24 +52,28 @@ Source = "https://github.com/litagin02/Style-Bert-VITS2"
[tool.hatch.version]
path = "style_bert_vits2/constants.py"
[tool.hatch.envs.default]
[tool.hatch.envs.test]
dependencies = [
"coverage[toml]>=6.5",
"pytest",
]
[tool.hatch.envs.default.scripts]
[tool.hatch.envs.test.scripts]
# Usage: `hatch run test:test`
test = "pytest {args:tests}"
# Usage: `hatch run test:coverage`
test-cov = "coverage run -m pytest {args:tests}"
# Usage: `hatch run test:cov-report`
cov-report = [
"- coverage combine",
"coverage report",
]
# Usage: `hatch run test:cov`
cov = [
"test-cov",
"cov-report",
]
[[tool.hatch.envs.all.matrix]]
[[tool.hatch.envs.test.matrix]]
python = ["3.9", "3.10", "3.11", "3.12"]
[tool.coverage.run]

View File

@@ -1,6 +1,6 @@
import warnings
from pathlib import Path
from typing import Any, Optional, Union
from typing import Any, Optional, Union, TypedDict
import gradio as gr
import numpy as np
@@ -263,6 +263,13 @@ class TTSModel:
return (self.hyper_parameters.data.sampling_rate, audio)
class TTSModelInfo(TypedDict):
name: str
files: list[str]
styles: list[str]
speakers: list[str]
class TTSModelHolder:
"""
Style-Bert-Vits2 の音声合成モデルを管理するクラス。
@@ -297,8 +304,7 @@ class TTSModelHolder:
self.model_files_dict: dict[str, list[Path]] = {}
self.current_model: Optional[TTSModel] = None
self.model_names: list[str] = []
self.models: list[TTSModel] = []
self.models_info: list[dict[str, Union[str, list[str]]]] = []
self.models_info: list[TTSModelInfo] = []
self.refresh()

1
tests/.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
*.wav

39
tests/test_main.py Normal file
View File

@@ -0,0 +1,39 @@
import pytest
from scipy.io import wavfile
from style_bert_vits2.constants import BASE_DIR
from style_bert_vits2.tts_model import TTSModelHolder
def synthesize(device: str = 'cpu'):
# モデル一覧を取得
model_holder = TTSModelHolder(BASE_DIR / 'model_assets', device)
# モデルが存在する場合、音声合成を実行
if len(model_holder.models_info) > 0:
# jvnv-F1-jp モデルを探す
for model_info in model_holder.models_info:
if model_info['name'] == 'jvnv-F1-jp':
# 音声合成を実行
model = model_holder.get_model(model_info['name'], model_info['files'][0])
model.load()
sample_rate, audio_data = model.infer("あらゆる現実を、すべて自分のほうへねじ曲げたのだ。")
# 音声データを保存
with open(BASE_DIR / 'tests/test.wav', mode='wb') as f:
wavfile.write(f, sample_rate, audio_data)
else:
pytest.skip("音声合成モデルが見つかりませんでした。")
def test_synthesize_cpu():
synthesize(device='cpu')
assert (BASE_DIR / 'tests/test.wav').exists()
def test_synthesize_cuda():
synthesize(device='cuda')
assert (BASE_DIR / 'tests/test.wav').exists()