2025-03-07 18:25:18 +08:00
|
|
|
import os
|
|
|
|
|
import uuid
|
|
|
|
|
import json
|
|
|
|
|
import requests
|
|
|
|
|
import shutil
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from core.providers.tts.base import TTSProviderBase
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class TTSProvider(TTSProviderBase):
|
|
|
|
|
def __init__(self, config, delete_audio_file):
|
|
|
|
|
super().__init__(config, delete_audio_file)
|
2025-04-13 19:01:19 +08:00
|
|
|
self.url = config.get(
|
|
|
|
|
"url",
|
|
|
|
|
"https://u95167-bd74-2aef8085.westx.seetacloud.com:8443/flashsummary/tts?token=",
|
|
|
|
|
)
|
|
|
|
|
if config.get("private_voice"):
|
|
|
|
|
self.voice_id = int(config.get("private_voice"))
|
|
|
|
|
else:
|
|
|
|
|
self.voice_id = int(config.get("voice_id", 1695))
|
2025-03-07 18:25:18 +08:00
|
|
|
self.token = config.get("token")
|
|
|
|
|
self.to_lang = config.get("to_lang")
|
2025-04-14 18:30:58 +08:00
|
|
|
self.volume_change_dB = int(config.get("volume_change_dB", 0))
|
|
|
|
|
self.speed_factor = int(config.get("speed_factor", 1))
|
2025-04-16 22:55:13 +08:00
|
|
|
self.stream = str(config.get("stream", False)).lower() in ("true", "1", "yes")
|
2025-03-18 13:25:34 +08:00
|
|
|
self.output_file = config.get("output_dir")
|
2025-04-14 18:30:58 +08:00
|
|
|
self.pitch_factor = int(config.get("pitch_factor", 0))
|
2025-03-07 18:25:18 +08:00
|
|
|
self.format = config.get("format", "mp3")
|
2025-04-14 18:30:58 +08:00
|
|
|
self.emotion = int(config.get("emotion", 1))
|
2025-04-13 19:01:19 +08:00
|
|
|
self.header = {"Content-Type": "application/json"}
|
2025-03-07 18:25:18 +08:00
|
|
|
|
|
|
|
|
def generate_filename(self, extension=".mp3"):
|
2025-04-13 19:01:19 +08:00
|
|
|
return os.path.join(
|
|
|
|
|
self.output_file,
|
|
|
|
|
f"tts-{datetime.now().date()}@{uuid.uuid4().hex}{extension}",
|
|
|
|
|
)
|
2025-03-07 18:25:18 +08:00
|
|
|
|
|
|
|
|
async def text_to_speak(self, text, output_file):
|
2025-04-13 19:01:19 +08:00
|
|
|
url = f"{self.url}{self.token}"
|
2025-03-07 18:25:18 +08:00
|
|
|
result = "firefly"
|
2025-04-13 19:01:19 +08:00
|
|
|
payload = json.dumps(
|
|
|
|
|
{
|
|
|
|
|
"to_lang": self.to_lang,
|
|
|
|
|
"text": text,
|
|
|
|
|
"emotion": self.emotion,
|
|
|
|
|
"format": self.format,
|
|
|
|
|
"volume_change_dB": self.volume_change_dB,
|
|
|
|
|
"voice_id": self.voice_id,
|
|
|
|
|
"pitch_factor": self.pitch_factor,
|
|
|
|
|
"speed_factor": self.speed_factor,
|
|
|
|
|
"token": self.token,
|
|
|
|
|
}
|
|
|
|
|
)
|
2025-03-07 18:25:18 +08:00
|
|
|
|
|
|
|
|
resp = requests.request("POST", url, data=payload)
|
|
|
|
|
if resp.status_code != 200:
|
|
|
|
|
return None
|
|
|
|
|
resp_json = resp.json()
|
|
|
|
|
try:
|
2025-04-13 19:01:19 +08:00
|
|
|
result = (
|
|
|
|
|
resp_json["url"]
|
|
|
|
|
+ ":"
|
|
|
|
|
+ str(resp_json["port"])
|
|
|
|
|
+ "/flashsummary/retrieveFileData?stream=True&token="
|
|
|
|
|
+ self.token
|
|
|
|
|
+ "&voice_audio_path="
|
|
|
|
|
+ resp_json["voice_path"]
|
|
|
|
|
)
|
2025-03-07 18:25:18 +08:00
|
|
|
except Exception as e:
|
|
|
|
|
print("error:", e)
|
|
|
|
|
|
|
|
|
|
audio_content = requests.get(result)
|
|
|
|
|
with open(output_file, "wb") as f:
|
|
|
|
|
f.write(audio_content.content)
|
|
|
|
|
return True
|
|
|
|
|
voice_path = resp_json.get("voice_path")
|
|
|
|
|
des_path = output_file
|
|
|
|
|
shutil.move(voice_path, des_path)
|