2023-09-15 00:33:18 +08:00
|
|
|
|
|
|
|
|
import os
|
|
|
|
|
import asyncio
|
|
|
|
|
from asyncio import Queue
|
|
|
|
|
import logging
|
2023-09-22 13:38:44 +08:00
|
|
|
from typing import Optional
|
2023-09-15 00:33:18 +08:00
|
|
|
|
|
|
|
|
from google.cloud import texttospeech
|
|
|
|
|
|
2023-09-22 13:38:44 +08:00
|
|
|
from .storage import AIStorage
|
2023-09-15 00:33:18 +08:00
|
|
|
from .compute_task import ComputeTask, ComputeTaskResult, ComputeTaskState, ComputeTaskType
|
|
|
|
|
from .compute_node import ComputeNode
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
You need to set the GOOGLE_APPLICATION_CREDENTIALS environment variable when using it.
|
|
|
|
|
see:https://cloud.google.com/text-to-speech/docs/before-you-begin
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class GoogleTextToSpeechNode(ComputeNode):
|
|
|
|
|
_instance = None
|
|
|
|
|
|
2023-09-21 15:52:56 +08:00
|
|
|
@classmethod
|
|
|
|
|
def get_instance(cls):
|
2023-09-15 00:33:18 +08:00
|
|
|
if cls._instance is None:
|
2023-09-21 15:52:56 +08:00
|
|
|
cls._instance = cls()
|
2023-09-15 00:33:18 +08:00
|
|
|
return cls._instance
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
super().__init__()
|
|
|
|
|
self.node_id = "google_text_to_speech_node"
|
|
|
|
|
self.task_queue = Queue()
|
2023-09-22 13:38:44 +08:00
|
|
|
self.client: Optional[texttospeech.TextToSpeechClient] = None
|
2023-09-15 00:33:18 +08:00
|
|
|
|
2023-09-21 15:52:56 +08:00
|
|
|
self.language_list = {
|
|
|
|
|
"cnm-CN": {
|
|
|
|
|
"female": ["cmn-CN-Standard-A",
|
|
|
|
|
"cmn-CN-Standard-D",
|
|
|
|
|
"cmn-CN-Wavenet-A",
|
|
|
|
|
"cmn-CN-Wavenet-D",
|
|
|
|
|
"cmn-TW-Standard-A",
|
|
|
|
|
"cmn-TW-Wavenet-A"],
|
|
|
|
|
"man": ["cmn-CN-Standard-B",
|
|
|
|
|
"cmn-CN-Standard-C",
|
|
|
|
|
"cmn-CN-Wavenet-B",
|
|
|
|
|
"cmn-CN-Wavenet-C",
|
|
|
|
|
"cmn-TW-Standard-B",
|
|
|
|
|
"cmn-TW-Standard-C",
|
|
|
|
|
"cmn-TW-Wavenet-B",
|
|
|
|
|
"cmn-TW-Wavenet-C"]
|
|
|
|
|
},
|
|
|
|
|
"en-US": {
|
|
|
|
|
"female": ["en-US-Neural2-C",
|
|
|
|
|
"en-US-Neural2-E",
|
|
|
|
|
"en-US-Neural2-F",
|
|
|
|
|
"en-US-Neural2-G",
|
|
|
|
|
"en-US-Neural2-H",
|
|
|
|
|
"en-US-News-K",
|
|
|
|
|
"en-US-News-L",
|
|
|
|
|
"en-US-Standard-C",
|
|
|
|
|
"en-US-Standard-E",
|
|
|
|
|
"en-US-Standard-F",
|
|
|
|
|
"en-US-Standard-G",
|
|
|
|
|
"en-US-Standard-H",
|
|
|
|
|
"en-US-Studio-O",
|
|
|
|
|
"en-US-Wavenet-C",
|
|
|
|
|
"en-US-Wavenet-E",
|
|
|
|
|
"en-US-Wavenet-F",
|
|
|
|
|
"en-US-Wavenet-G",
|
|
|
|
|
"en-US-Wavenet-H"],
|
|
|
|
|
"man": ["en-US-Polyglot-1",
|
|
|
|
|
"en-US-Standard-A",
|
|
|
|
|
"en-US-Standard-B",
|
|
|
|
|
"en-US-Standard-D",
|
|
|
|
|
"en-US-Standard-I",
|
|
|
|
|
"en-US-Standard-J",
|
|
|
|
|
"en-US-Studio-M",
|
|
|
|
|
"en-US-Wavenet-A",
|
|
|
|
|
"en-US-Wavenet-B",
|
|
|
|
|
"en-US-Wavenet-D",
|
|
|
|
|
"en-US-Wavenet-I",
|
|
|
|
|
"en-US-Wavenet-J"]
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-15 00:33:18 +08:00
|
|
|
self.start()
|
|
|
|
|
|
2023-09-22 13:38:44 +08:00
|
|
|
def init(self):
|
|
|
|
|
user_config = AIStorage.get_instance().get_user_config()
|
|
|
|
|
google_application_credentials = user_config.get_value("google_application_credentials")
|
|
|
|
|
if google_application_credentials is None:
|
|
|
|
|
raise Exception("google_application_credentials is None!")
|
|
|
|
|
os.environ["GOOGLE_APPLICATION_CREDENTIALS"] = google_application_credentials
|
|
|
|
|
self.client = texttospeech.TextToSpeechClient()
|
|
|
|
|
|
2023-09-15 00:33:18 +08:00
|
|
|
def start(self):
|
|
|
|
|
async def _run_task_loop():
|
|
|
|
|
while True:
|
|
|
|
|
task = await self.task_queue.get()
|
|
|
|
|
try:
|
|
|
|
|
result = self._run_task(task)
|
|
|
|
|
if result is not None:
|
|
|
|
|
task.state = ComputeTaskState.DONE
|
|
|
|
|
task.result = result
|
|
|
|
|
except Exception as e:
|
|
|
|
|
logger.error(f"google_text_to_speech_node run task error: {e}")
|
|
|
|
|
task.state = ComputeTaskState.ERROR
|
|
|
|
|
task.result = ComputeTaskResult()
|
|
|
|
|
task.result.set_from_task(task)
|
|
|
|
|
task.result.worker_id = self.node_id
|
|
|
|
|
task.result.result_str = str(e)
|
|
|
|
|
|
|
|
|
|
asyncio.create_task(_run_task_loop())
|
|
|
|
|
|
|
|
|
|
def _run_task(self, task: ComputeTask):
|
|
|
|
|
task.state = ComputeTaskState.RUNNING
|
|
|
|
|
language_code = task.params["language_code"]
|
|
|
|
|
text = task.params["text"]
|
2023-09-21 15:52:56 +08:00
|
|
|
voice_name = task.params["voice_name"]
|
|
|
|
|
gender = task.params["gender"]
|
|
|
|
|
age = task.params["age"]
|
|
|
|
|
|
|
|
|
|
if language_code == "zh":
|
|
|
|
|
language_code = "cnm-CN"
|
|
|
|
|
elif language_code == "en":
|
|
|
|
|
language_code = "en-US"
|
|
|
|
|
else:
|
|
|
|
|
raise Exception(f"language_code {language_code} not support")
|
|
|
|
|
|
|
|
|
|
lang_list = self.language_list[language_code][gender]
|
|
|
|
|
voice = lang_list[hash(voice_name) % len(lang_list)]
|
2023-09-15 00:33:18 +08:00
|
|
|
|
|
|
|
|
synthesis_input = texttospeech.SynthesisInput(text=text)
|
|
|
|
|
voice = texttospeech.VoiceSelectionParams(language_code=language_code,
|
2023-09-21 15:52:56 +08:00
|
|
|
ssml_gender=texttospeech.SsmlVoiceGender.NEUTRAL,
|
|
|
|
|
name=voice)
|
2023-09-15 00:33:18 +08:00
|
|
|
|
|
|
|
|
audio_config = texttospeech.AudioConfig(audio_encoding=texttospeech.AudioEncoding.MP3)
|
|
|
|
|
|
|
|
|
|
response = self.client.synthesize_speech(input=synthesis_input, voice=voice, audio_config=audio_config)
|
|
|
|
|
|
|
|
|
|
result = ComputeTaskResult()
|
|
|
|
|
result.set_from_task(task)
|
|
|
|
|
result.worker_id = self.node_id
|
|
|
|
|
result.result = response.audio_content
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
async def push_task(self, task: ComputeTask, proiority: int = 0):
|
|
|
|
|
logger.info(f"google_text_to_speech_node push task: {task.display()}")
|
|
|
|
|
self.task_queue.put_nowait(task)
|
|
|
|
|
|
|
|
|
|
async def remove_task(self, task_id: str):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def get_task_state(self, task_id: str):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def display(self) -> str:
|
|
|
|
|
return f"GoogleTextToSpeechNode: {self.node_id}"
|
|
|
|
|
|
|
|
|
|
def get_capacity(self):
|
|
|
|
|
return 0
|
|
|
|
|
|
2023-09-21 15:52:56 +08:00
|
|
|
def is_support(self, task: ComputeTask) -> bool:
|
|
|
|
|
if task.task_type == ComputeTaskType.TEXT_2_VOICE:
|
2023-09-15 00:33:18 +08:00
|
|
|
return True
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
def is_local(self) -> bool:
|
|
|
|
|
return False
|
2023-09-22 13:38:44 +08:00
|
|
|
|
|
|
|
|
def declare_user_config(self):
|
|
|
|
|
if os.getenv("GOOGLE_APPLICATION_CREDENTIALS") is None:
|
|
|
|
|
user_config = AIStorage.get_instance().get_user_config()
|
|
|
|
|
user_config.add_user_config("google_application_credentials",
|
|
|
|
|
"google application credentials, please visit:https://cloud.google.com/text-to-speech/docs/before-you-begin",
|
|
|
|
|
False,
|
|
|
|
|
None)
|