2025-02-11 23:13:18 +08:00
|
|
|
import os
|
|
|
|
|
import uuid
|
|
|
|
|
import json
|
|
|
|
|
import base64
|
|
|
|
|
import requests
|
|
|
|
|
from datetime import datetime
|
2025-03-07 18:25:18 +08:00
|
|
|
from core.utils.util import check_model_key
|
2025-02-11 23:13:18 +08:00
|
|
|
from core.providers.tts.base import TTSProviderBase
|
2025-04-12 17:36:04 +08:00
|
|
|
from config.logger import setup_logging
|
|
|
|
|
|
|
|
|
|
TAG = __name__
|
|
|
|
|
logger = setup_logging()
|
2025-02-11 23:13:18 +08:00
|
|
|
|
2025-02-14 00:54:59 +08:00
|
|
|
|
2025-02-11 23:13:18 +08:00
|
|
|
class TTSProvider(TTSProviderBase):
|
|
|
|
|
def __init__(self, config, delete_audio_file):
|
|
|
|
|
super().__init__(config, delete_audio_file)
|
|
|
|
|
self.appid = config.get("appid")
|
|
|
|
|
self.access_token = config.get("access_token")
|
|
|
|
|
self.cluster = config.get("cluster")
|
2025-04-13 19:01:19 +08:00
|
|
|
|
|
|
|
|
if config.get("private_voice"):
|
|
|
|
|
self.voice = config.get("private_voice")
|
|
|
|
|
else:
|
|
|
|
|
self.voice = config.get("voice")
|
|
|
|
|
|
2025-03-02 16:59:25 +08:00
|
|
|
self.api_url = config.get("api_url")
|
|
|
|
|
self.authorization = config.get("authorization")
|
|
|
|
|
self.header = {"Authorization": f"{self.authorization}{self.access_token}"}
|
2025-03-07 18:25:18 +08:00
|
|
|
check_model_key("TTS", self.access_token)
|
2025-02-11 23:13:18 +08:00
|
|
|
|
|
|
|
|
def generate_filename(self, extension=".wav"):
|
2025-04-12 17:36:04 +08:00
|
|
|
return os.path.join(
|
|
|
|
|
self.output_file,
|
|
|
|
|
f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}",
|
|
|
|
|
)
|
2025-02-11 23:13:18 +08:00
|
|
|
|
|
|
|
|
async def text_to_speak(self, text, output_file):
|
|
|
|
|
request_json = {
|
|
|
|
|
"app": {
|
2025-02-23 14:38:21 +08:00
|
|
|
"appid": f"{self.appid}",
|
2025-02-11 23:13:18 +08:00
|
|
|
"token": "access_token",
|
2025-04-12 17:36:04 +08:00
|
|
|
"cluster": self.cluster,
|
2025-02-11 23:13:18 +08:00
|
|
|
},
|
2025-04-12 17:36:04 +08:00
|
|
|
"user": {"uid": "1"},
|
2025-02-11 23:13:18 +08:00
|
|
|
"audio": {
|
|
|
|
|
"voice_type": self.voice,
|
|
|
|
|
"encoding": "wav",
|
|
|
|
|
"speed_ratio": 1.0,
|
|
|
|
|
"volume_ratio": 1.0,
|
|
|
|
|
"pitch_ratio": 1.0,
|
|
|
|
|
},
|
|
|
|
|
"request": {
|
|
|
|
|
"reqid": str(uuid.uuid4()),
|
|
|
|
|
"text": text,
|
|
|
|
|
"text_type": "plain",
|
|
|
|
|
"operation": "query",
|
|
|
|
|
"with_frontend": 1,
|
2025-04-12 17:36:04 +08:00
|
|
|
"frontend_type": "unitTson",
|
|
|
|
|
},
|
2025-02-11 23:13:18 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-23 14:38:21 +08:00
|
|
|
try:
|
2025-04-12 17:36:04 +08:00
|
|
|
resp = requests.post(
|
|
|
|
|
self.api_url, json.dumps(request_json), headers=self.header
|
|
|
|
|
)
|
2025-02-23 14:38:21 +08:00
|
|
|
if "data" in resp.json():
|
|
|
|
|
data = resp.json()["data"]
|
|
|
|
|
file_to_save = open(output_file, "wb")
|
|
|
|
|
file_to_save.write(base64.b64decode(data))
|
|
|
|
|
else:
|
2025-04-12 17:36:04 +08:00
|
|
|
raise Exception(
|
|
|
|
|
f"{__name__} status_code: {resp.status_code} response: {resp.content}"
|
|
|
|
|
)
|
2025-02-23 14:38:21 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
raise Exception(f"{__name__} error: {e}")
|