Skip to content

Commit

Permalink
Version 3.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
OpenSourceSimon authored Aug 24, 2023
2 parents 72a1e9d + 57e7b55 commit 99de32e
Show file tree
Hide file tree
Showing 27 changed files with 192 additions and 342 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/autoblack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
name: autoblack
on:
push:
branches: ["master"]
branches: ["develop"]
jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -24,9 +24,9 @@ jobs:
if: failure()
run: |
black . --line-length 101
git config --global user.name 'autoblack'
git config --global user.email 'jasoncameron.all@gmail.com'
git config --global user.name github-actions
git config --global user.email 41898282+github-actions[bot]@users.noreply.github.com
git remote set-url origin https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/$GITHUB_REPOSITORY
git checkout $GITHUB_HEAD_REF
git commit -am "fixup: Format Python code with Black"
git push origin HEAD:master
git push origin HEAD:develop
4 changes: 1 addition & 3 deletions GUI.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,7 @@ def settings():
# Change settings
config = gui.modify_settings(data, config_load, checks)

return render_template(
"settings.html", file="config.toml", data=config, checks=checks
)
return render_template("settings.html", file="config.toml", data=config, checks=checks)


# Make videos.json accessible
Expand Down
4 changes: 1 addition & 3 deletions TTS/TikTok.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ def __init__(self):
"Cookie": f"sessionid={settings.config['settings']['tts']['tiktok_sessionid']}",
}

self.URI_BASE = (
"https://api16-normal-c-useast1a.tiktokv.com/media/api/text/speech/invoke/"
)
self.URI_BASE = "https://api16-normal-c-useast1a.tiktokv.com/media/api/text/speech/invoke/"
self.max_chars = 200

self._session = requests.Session()
Expand Down
4 changes: 1 addition & 3 deletions TTS/aws_polly.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ def run(self, text, filepath, random_voice: bool = False):
raise ValueError(
f"Please set the TOML variable AWS_VOICE to a valid voice. options are: {voices}"
)
voice = str(
settings.config["settings"]["tts"]["aws_polly_voice"]
).capitalize()
voice = str(settings.config["settings"]["tts"]["aws_polly_voice"]).capitalize()
try:
# Request speech synthesis
response = polly.synthesize_speech(
Expand Down
8 changes: 2 additions & 6 deletions TTS/elevenlabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ def run(self, text, filepath, random_voice: bool = False):
if random_voice:
voice = self.randomvoice()
else:
voice = str(
settings.config["settings"]["tts"]["elevenlabs_voice_name"]
).capitalize()
voice = str(settings.config["settings"]["tts"]["elevenlabs_voice_name"]).capitalize()

if settings.config["settings"]["tts"]["elevenlabs_api_key"]:
api_key = settings.config["settings"]["tts"]["elevenlabs_api_key"]
Expand All @@ -37,9 +35,7 @@ def run(self, text, filepath, random_voice: bool = False):
"You didn't set an Elevenlabs API key! Please set the config variable ELEVENLABS_API_KEY to a valid API key."
)

audio = generate(
api_key=api_key, text=text, voice=voice, model="eleven_multilingual_v1"
)
audio = generate(api_key=api_key, text=text, voice=voice, model="eleven_multilingual_v1")
save(audio=audio, filename=filepath)

def randomvoice(self):
Expand Down
23 changes: 8 additions & 15 deletions TTS/engine_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
from utils.voice import sanitize_text


DEFAULT_MAX_LENGTH: int = 50 # Video length variable, edit this on your own risk. It should work, but it's not supported

DEFAULT_MAX_LENGTH: int = (
50 # Video length variable, edit this on your own risk. It should work, but it's not supported
)


class TTSEngine:
Expand Down Expand Up @@ -59,9 +60,7 @@ def add_periods(
comment["comment_body"] = re.sub(regex_urls, " ", comment["comment_body"])
comment["comment_body"] = comment["comment_body"].replace("\n", ". ")
comment["comment_body"] = re.sub(r"\bAI\b", "A.I", comment["comment_body"])
comment["comment_body"] = re.sub(
r"\bAGI\b", "A.G.I", comment["comment_body"]
)
comment["comment_body"] = re.sub(r"\bAGI\b", "A.G.I", comment["comment_body"])
if comment["comment_body"][-1] != ".":
comment["comment_body"] += "."
comment["comment_body"] = comment["comment_body"].replace(". . .", ".")
Expand All @@ -83,17 +82,13 @@ def run(self) -> Tuple[int, int]:
if len(self.reddit_object["thread_post"]) > self.tts_module.max_chars:
self.split_post(self.reddit_object["thread_post"], "postaudio")
else:
self.call_tts(
"postaudio", process_text(self.reddit_object["thread_post"])
)
self.call_tts("postaudio", process_text(self.reddit_object["thread_post"]))
elif settings.config["settings"]["storymodemethod"] == 1:
for idx, text in track(enumerate(self.reddit_object["thread_post"])):
self.call_tts(f"postaudio-{idx}", process_text(text))

else:
for idx, comment in track(
enumerate(self.reddit_object["comments"]), "Saving..."
):
for idx, comment in track(enumerate(self.reddit_object["comments"]), "Saving..."):
# ! Stop creating mp3 files if the length is greater than max length.
if self.length > self.max_length and idx > 1:
self.length -= self.last_clip_length
Expand Down Expand Up @@ -176,16 +171,14 @@ def create_silence_mp3(self):
fps=44100,
)
silence = volumex(silence, 0)
silence.write_audiofile(
f"{self.path}/silence.mp3", fps=44100, verbose=False, logger=None
)
silence.write_audiofile(f"{self.path}/silence.mp3", fps=44100, verbose=False, logger=None)


def process_text(text: str, clean: bool = True):
lang = settings.config["reddit"]["thread"]["post_lang"]
new_text = sanitize_text(text) if clean else text
if lang:
print_substep("Translating Text...")
translated_text = translators.google(text, to_language=lang)
translated_text = translators.translate_text(text, translator="google", to_language=lang)
new_text = sanitize_text(translated_text)
return new_text
4 changes: 1 addition & 3 deletions TTS/pyttsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ def run(
if voice_id == "" or voice_num == "":
voice_id = 2
voice_num = 3
raise ValueError(
"set pyttsx values to a valid value, switching to defaults"
)
raise ValueError("set pyttsx values to a valid value, switching to defaults")
else:
voice_id = int(voice_id)
voice_num = int(voice_num)
Expand Down
4 changes: 1 addition & 3 deletions TTS/streamlabs_polly.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ def run(self, text, filepath, random_voice: bool = False):
raise ValueError(
f"Please set the config variable STREAMLABS_POLLY_VOICE to a valid voice. options are: {voices}"
)
voice = str(
settings.config["settings"]["tts"]["streamlabs_polly_voice"]
).capitalize()
voice = str(settings.config["settings"]["tts"]["streamlabs_polly_voice"]).capitalize()
body = {"voice": voice, "text": text, "service": "polly"}
response = requests.post(self.url, data=body)
if not check_ratelimit(response):
Expand Down
14 changes: 7 additions & 7 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from video_creation.voices import save_text_to_mp3
from utils.ffmpeg_install import ffmpeg_install

__VERSION__ = "3.2"
__VERSION__ = "3.2.1"

print(
"""
Expand Down Expand Up @@ -74,22 +74,24 @@ def shutdown() -> NoReturn:
if "redditid" in globals():
print_markdown("## Clearing temp files")
cleanup(redditid)

print("Exiting...")
sys.exit()


if __name__ == "__main__":
if sys.version_info.major != 3 or sys.version_info.minor != 10:
print("Hey! Congratulations, you've made it so far (which is pretty rare with no Python 3.10). Unfortunately, this program only works on Python 3.10. Please install Python 3.10 and try again.")
print(
"Hey! Congratulations, you've made it so far (which is pretty rare with no Python 3.10). Unfortunately, this program only works on Python 3.10. Please install Python 3.10 and try again."
)
sys.exit()
ffmpeg_install()
directory = Path().absolute()
config = settings.check_toml(
f"{directory}/utils/.config.template.toml", f"{directory}/config.toml"
)
config is False and sys.exit()

if (
not settings.config["settings"]["tts"]["tiktok_sessionid"]
or settings.config["settings"]["tts"]["tiktok_sessionid"] == ""
Expand All @@ -101,9 +103,7 @@ def shutdown() -> NoReturn:
sys.exit()
try:
if config["reddit"]["thread"]["post_id"]:
for index, post_id in enumerate(
config["reddit"]["thread"]["post_id"].split("+")
):
for index, post_id in enumerate(config["reddit"]["thread"]["post_id"].split("+")):
index += 1
print_step(
f'on the {index}{("st" if index % 10 == 1 else ("nd" if index % 10 == 2 else ("rd" if index % 10 == 3 else "th")))} post of {len(config["reddit"]["thread"]["post_id"].split("+"))}'
Expand Down
25 changes: 6 additions & 19 deletions reddit/subreddit.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ def get_subreddit_threads(POST_ID: str):

content = {}
if settings.config["reddit"]["creds"]["2fa"]:
print(
"\nEnter your two-factor authentication code from your authenticator app.\n"
)
print("\nEnter your two-factor authentication code from your authenticator app.\n")
code = input("> ")
print()
pw = settings.config["reddit"]["creds"]["password"]
Expand Down Expand Up @@ -59,9 +57,7 @@ def get_subreddit_threads(POST_ID: str):
]: # note to user. you can have multiple subreddits via reddit.subreddit("redditdev+learnpython")
try:
subreddit = reddit.subreddit(
re.sub(
r"r\/", "", input("What subreddit would you like to pull from? ")
)
re.sub(r"r\/", "", input("What subreddit would you like to pull from? "))
# removes the r/ from the input
)
except ValueError:
Expand All @@ -71,9 +67,7 @@ def get_subreddit_threads(POST_ID: str):
sub = settings.config["reddit"]["thread"]["subreddit"]
print_substep(f"Using subreddit: r/{sub} from TOML config")
subreddit_choice = sub
if (
str(subreddit_choice).casefold().startswith("r/")
): # removes the r/ from the input
if str(subreddit_choice).casefold().startswith("r/"): # removes the r/ from the input
subreddit_choice = subreddit_choice[2:]
subreddit = reddit.subreddit(subreddit_choice)

Expand All @@ -84,12 +78,8 @@ def get_subreddit_threads(POST_ID: str):
settings.config["reddit"]["thread"]["post_id"]
and len(str(settings.config["reddit"]["thread"]["post_id"]).split("+")) == 1
):
submission = reddit.submission(
id=settings.config["reddit"]["thread"]["post_id"]
)
elif settings.config["ai"][
"ai_similarity_enabled"
]: # ai sorting based on comparison
submission = reddit.submission(id=settings.config["reddit"]["thread"]["post_id"])
elif settings.config["ai"]["ai_similarity_enabled"]: # ai sorting based on comparison
threads = subreddit.hot(limit=50)
keywords = settings.config["ai"]["ai_similarity_keywords"].split(",")
keywords = [keyword.strip() for keyword in keywords]
Expand All @@ -107,10 +97,7 @@ def get_subreddit_threads(POST_ID: str):
if submission is None:
return get_subreddit_threads(POST_ID) # submission already done. rerun

elif (
not submission.num_comments
and settings.config["settings"]["storymode"] == "false"
):
elif not submission.num_comments and settings.config["settings"]["storymode"] == "false":
print_substep("No comments found. Skipping.")
exit()

Expand Down
8 changes: 4 additions & 4 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ playwright==1.34.0
praw==7.7.0
prawcore~=2.3.0
requests==2.31.0
rich==13.3.5
rich==13.4.1
toml==0.10.2
translators==5.7.6
pyttsx3==2.90
Pillow==9.5.0
tomlkit==0.11.8
Flask==2.3.2
Flask==2.3.3
clean-text==0.6.0
unidecode==1.3.6
spacy==3.5.3
torch==2.0.1
transformers==4.29.2
ffmpeg-python==0.2.0
elevenlabs==0.2.16
yt-dlp==2023.3.4
elevenlabs==0.2.17
yt-dlp==2023.7.6
20 changes: 5 additions & 15 deletions utils/ai_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@

# Mean Pooling - Take attention mask into account for correct averaging
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[
0
] # First element of model_output contains all token embeddings
input_mask_expanded = (
attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
)
token_embeddings = model_output[0] # First element of model_output contains all token embeddings
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(
input_mask_expanded.sum(1), min=1e-9
)
Expand All @@ -36,19 +32,13 @@ def sort_by_similarity(thread_objects, keywords):
)
with torch.no_grad():
threads_embeddings = model(**encoded_threads)
threads_embeddings = mean_pooling(
threads_embeddings, encoded_threads["attention_mask"]
)
threads_embeddings = mean_pooling(threads_embeddings, encoded_threads["attention_mask"])

# Keywords inference
encoded_keywords = tokenizer(
keywords, padding=True, truncation=True, return_tensors="pt"
)
encoded_keywords = tokenizer(keywords, padding=True, truncation=True, return_tensors="pt")
with torch.no_grad():
keywords_embeddings = model(**encoded_keywords)
keywords_embeddings = mean_pooling(
keywords_embeddings, encoded_keywords["attention_mask"]
)
keywords_embeddings = mean_pooling(keywords_embeddings, encoded_keywords["attention_mask"])

# Compare every keyword w/ every thread embedding
threads_embeddings_tensor = torch.tensor(threads_embeddings)
Expand Down
17 changes: 3 additions & 14 deletions utils/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,7 @@ def handle_input(
optional=False,
):
if optional:
console.print(
message
+ "\n[green]This is an optional value. Do you want to skip it? (y/n)"
)
console.print(message + "\n[green]This is an optional value. Do you want to skip it? (y/n)")
if input().casefold().startswith("y"):
return default if default is not NotImplemented else ""
if default is not NotImplemented:
Expand Down Expand Up @@ -86,11 +83,7 @@ def handle_input(
console.print("[red]" + err_message)
continue
elif match != "" and re.match(match, user_input) is None:
console.print(
"[red]"
+ err_message
+ "\nAre you absolutely sure it's correct?(y/n)"
)
console.print("[red]" + err_message + "\nAre you absolutely sure it's correct?(y/n)")
if input().casefold().startswith("y"):
break
continue
Expand Down Expand Up @@ -123,9 +116,5 @@ def handle_input(
if user_input in options:
return user_input
console.print(
"[red bold]"
+ err_message
+ "\nValid options are: "
+ ", ".join(map(str, options))
+ "."
"[red bold]" + err_message + "\nValid options are: " + ", ".join(map(str, options)) + "."
)
Loading

0 comments on commit 99de32e

Please sign in to comment.