refactor: tts engines now use toml config

pull/861/head
Callum Leslie 2 years ago
parent 3e1b95e708
commit 436266033e
No known key found for this signature in database
GPG Key ID: D382C4AFEECEAA90

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import random import random
import os from utils import settings
from gtts import gTTS from gtts import gTTS
max_chars = 0 max_chars = 0
@ -12,7 +12,7 @@ class GTTS:
self.voices = [] self.voices = []
def run(self, text, filepath): def run(self, text, filepath):
tts = gTTS(text=text, lang=os.getenv("POSTLANG") or "en", slow=False) tts = gTTS(text=text, lang=settings.config["reddit"]["thread"]["post_lang"] or "en", slow=False)
tts.save(filepath) tts.save(filepath)
def randomvoice(self): def randomvoice(self):

@ -1,5 +1,5 @@
import base64 import base64
import os from utils import settings
import random import random
import requests import requests
from requests.adapters import HTTPAdapter, Retry from requests.adapters import HTTPAdapter, Retry
@ -75,7 +75,7 @@ class TikTok: # TikTok Text-to-Speech Wrapper
voice = ( voice = (
self.randomvoice() self.randomvoice()
if random_voice if random_voice
else (os.getenv("TIKTOK_VOICE") or random.choice(self.voices["human"])) else (settings.config["settings"]["tts"]["tiktok_voice"] or random.choice(self.voices["human"]))
) )
try: try:
r = requests.post(f"{self.URI_BASE}{voice}&req_text={text}&speaker_map_type=0") r = requests.post(f"{self.URI_BASE}{voice}&req_text={text}&speaker_map_type=0")

@ -2,7 +2,7 @@
from boto3 import Session from boto3 import Session
from botocore.exceptions import BotoCoreError, ClientError from botocore.exceptions import BotoCoreError, ClientError
import sys import sys
import os from utils import settings
import random import random
voices = [ voices = [
@ -35,11 +35,11 @@ class AWSPolly:
if random_voice: if random_voice:
voice = self.randomvoice() voice = self.randomvoice()
else: else:
if not os.getenv("AWS_VOICE"): if not settings.config["settings"]["tts"]["aws_polly_voice"]:
return ValueError( return ValueError(
f"Please set the environment variable AWS_VOICE to a valid voice. options are: {voices}" f"Please set the environment variable AWS_VOICE to a valid voice. options are: {voices}"
) )
voice = str(os.getenv("AWS_VOICE")).capitalize() voice = str(settings.config["settings"]["tts"]["aws_polly_voice"]).capitalize()
try: try:
# Request speech synthesis # Request speech synthesis
response = polly.synthesize_speech( response = polly.synthesize_speech(

@ -2,7 +2,6 @@
from pathlib import Path from pathlib import Path
from typing import Tuple from typing import Tuple
import re import re
from os import getenv
# import sox # import sox
# from mutagen import MutagenError # from mutagen import MutagenError
@ -12,6 +11,7 @@ from rich.progress import track
from moviepy.editor import AudioFileClip, CompositeAudioClip, concatenate_audioclips from moviepy.editor import AudioFileClip, CompositeAudioClip, concatenate_audioclips
from utils.console import print_step, print_substep from utils.console import print_step, print_substep
from utils.voice import sanitize_text from utils.voice import sanitize_text
from utils import settings
DEFUALT_MAX_LENGTH: int = 50 # video length variable DEFUALT_MAX_LENGTH: int = 50 # video length variable
@ -56,7 +56,7 @@ class TTSEngine:
print_step("Saving Text to MP3 files...") print_step("Saving Text to MP3 files...")
self.call_tts("title", self.reddit_object["thread_title"]) self.call_tts("title", self.reddit_object["thread_title"])
if self.reddit_object["thread_post"] != "" and getenv("STORYMODE", "").casefold() == "true": if self.reddit_object["thread_post"] != "" and settings.config["settings"]["storymode"] == True:
self.call_tts("posttext", self.reddit_object["thread_post"]) self.call_tts("posttext", self.reddit_object["thread_post"])
idx = None idx = None
@ -109,7 +109,7 @@ class TTSEngine:
clip.close() clip.close()
def process_text(text: str): def process_text(text: str):
lang = getenv("POSTLANG", "") lang = settings.config["reddit"]["thread"]["post_lang"]
new_text = sanitize_text(text) new_text = sanitize_text(text)
if lang: if lang:
print_substep("Translating Text...") print_substep("Translating Text...")

@ -1,7 +1,7 @@
import random import random
import os
import requests import requests
from requests.exceptions import JSONDecodeError from requests.exceptions import JSONDecodeError
from utils import settings
voices = [ voices = [
"Brian", "Brian",
@ -35,11 +35,11 @@ class StreamlabsPolly:
if random_voice: if random_voice:
voice = self.randomvoice() voice = self.randomvoice()
else: else:
if not os.getenv("STREAMLABS_VOICE"): if not settings.config["settings"]["tts"]["streamlabs_polly_voice"]:
return ValueError( return ValueError(
f"Please set the environment variable STREAMLABS_VOICE to a valid voice. options are: {voices}" f"Please set the environment variable STREAMLABS_VOICE to a valid voice. options are: {voices}"
) )
voice = str(os.getenv("STREAMLABS_VOICE")).capitalize() voice = str(settings.config["settings"]["tts"]["streamlabs_polly_voice"]).capitalize()
body = {"voice": voice, "text": text, "service": "polly"} body = {"voice": voice, "text": text, "service": "polly"}
response = requests.post(self.url, data=body) response = requests.post(self.url, data=body)
try: try:
@ -56,5 +56,3 @@ class StreamlabsPolly:
def randomvoice(self): def randomvoice(self):
return random.choice(self.voices) return random.choice(self.voices)
# StreamlabsPolly().run(text=str('hi hi ' * 92)[1:], filepath='hello.mp3', random_voice=True)

Loading…
Cancel
Save