2022-09-20 17:49:31 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
|
2022-06-20 17:23:11 +08:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
# NOTICE: Tensorflow 1.15 seems not so compatible with pytorch.
|
|
|
|
|
# A segmentation fault may be raise by pytorch cpp library
|
|
|
|
|
# if 'import tensorflow' in front of 'import torch'.
|
|
|
|
|
# Puting a 'import torch' here can bypass this incompatibility.
|
|
|
|
|
import torch
|
|
|
|
|
from scipy.io.wavfile import write
|
|
|
|
|
|
2022-09-27 22:09:30 +08:00
|
|
|
from modelscope.models import Model
|
2022-07-14 16:25:55 +08:00
|
|
|
from modelscope.outputs import OutputKeys
|
2022-06-20 17:23:11 +08:00
|
|
|
from modelscope.pipelines import pipeline
|
2022-08-06 12:22:17 +08:00
|
|
|
from modelscope.utils.constant import Tasks
|
2022-09-08 14:08:51 +08:00
|
|
|
from modelscope.utils.demo_utils import DemoCompatibilityCheck
|
2022-06-20 17:23:11 +08:00
|
|
|
from modelscope.utils.logger import get_logger
|
2022-06-23 16:55:48 +08:00
|
|
|
from modelscope.utils.test_utils import test_level
|
2022-06-20 17:23:11 +08:00
|
|
|
|
2022-07-06 13:20:04 +08:00
|
|
|
import tensorflow as tf # isort:skip
|
|
|
|
|
|
2022-06-20 17:23:11 +08:00
|
|
|
logger = get_logger()
|
|
|
|
|
|
|
|
|
|
|
2022-09-08 14:08:51 +08:00
|
|
|
class TextToSpeechSambertHifigan16kPipelineTest(unittest.TestCase,
|
|
|
|
|
DemoCompatibilityCheck):
|
|
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
|
self.task = Tasks.text_to_speech
|
2022-10-13 10:16:07 +08:00
|
|
|
self.zhcn_text = '今天北京天气怎么样'
|
|
|
|
|
self.en_text = 'How is the weather in Beijing?'
|
|
|
|
|
self.zhcn_voices = [
|
|
|
|
|
'zhitian_emo', 'zhizhe_emo', 'zhiyan_emo', 'zhibei_emo', 'zhcn'
|
|
|
|
|
]
|
|
|
|
|
self.zhcn_models = [
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_zhitian_emo_zh-cn_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_zhizhe_emo_zh-cn_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_zhiyan_emo_zh-cn_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_zhibei_emo_zh-cn_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_zh-cn_16k'
|
|
|
|
|
]
|
|
|
|
|
self.en_voices = ['luca', 'luna', 'andy', 'annie', 'engb', 'enus']
|
|
|
|
|
self.en_models = [
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_luca_en-gb_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_luna_en-gb_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_andy_en-us_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_annie_en-us_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_en-gb_16k',
|
|
|
|
|
'damo/speech_sambert-hifigan_tts_en-us_16k'
|
|
|
|
|
]
|
2022-06-20 17:23:11 +08:00
|
|
|
|
2022-08-06 12:22:17 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-06-20 17:23:11 +08:00
|
|
|
def test_pipeline(self):
|
2022-10-13 10:16:07 +08:00
|
|
|
for i in range(len(self.zhcn_voices)):
|
|
|
|
|
logger.info('test %s' % self.zhcn_voices[i])
|
2022-10-11 17:22:58 +08:00
|
|
|
model = Model.from_pretrained(
|
2022-10-13 10:16:07 +08:00
|
|
|
model_name_or_path=self.zhcn_models[i], revision='pytorch_am')
|
2022-10-11 17:22:58 +08:00
|
|
|
sambert_hifigan_tts = pipeline(task=self.task, model=model)
|
|
|
|
|
self.assertTrue(sambert_hifigan_tts is not None)
|
2022-10-13 10:16:07 +08:00
|
|
|
output = sambert_hifigan_tts(input=self.zhcn_text)
|
2022-10-11 17:22:58 +08:00
|
|
|
self.assertIsNotNone(output[OutputKeys.OUTPUT_PCM])
|
|
|
|
|
pcm = output[OutputKeys.OUTPUT_PCM]
|
2022-10-13 10:16:07 +08:00
|
|
|
write('output_%s.wav' % self.zhcn_voices[i], 16000, pcm)
|
|
|
|
|
for i in range(len(self.en_voices)):
|
|
|
|
|
logger.info('test %s' % self.en_voices[i])
|
|
|
|
|
model = Model.from_pretrained(
|
|
|
|
|
model_name_or_path=self.en_models[i], revision='pytorch_am')
|
|
|
|
|
sambert_hifigan_tts = pipeline(task=self.task, model=model)
|
|
|
|
|
self.assertTrue(sambert_hifigan_tts is not None)
|
|
|
|
|
output = sambert_hifigan_tts(input=self.en_text)
|
|
|
|
|
self.assertIsNotNone(output[OutputKeys.OUTPUT_PCM])
|
|
|
|
|
pcm = output[OutputKeys.OUTPUT_PCM]
|
|
|
|
|
write('output_%s.wav' % self.en_voices[i], 16000, pcm)
|
2022-06-20 17:23:11 +08:00
|
|
|
|
2022-09-09 14:56:15 +08:00
|
|
|
@unittest.skip('demo compatibility test is only enabled on a needed-basis')
|
2022-09-08 14:08:51 +08:00
|
|
|
def test_demo_compatibility(self):
|
|
|
|
|
self.compatibility_check()
|
|
|
|
|
|
2022-06-20 17:23:11 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|