🎨 Run linter

This commit is contained in:
2024-04-21 21:39:39 +02:00
parent 4cb395d279
commit 0fc86d2661
12 changed files with 77 additions and 56 deletions

View File

@@ -5,5 +5,4 @@ from ..BaseEngine import BaseEngine
class BaseAudioBackgroundEngine(BaseEngine): class BaseAudioBackgroundEngine(BaseEngine):
@abstractmethod @abstractmethod
def get_background(self) -> None: def get_background(self) -> None: ...
...

View File

@@ -12,12 +12,16 @@ from . import BaseAudioBackgroundEngine
class MusicAudioBackgroundEngine(BaseAudioBackgroundEngine): class MusicAudioBackgroundEngine(BaseAudioBackgroundEngine):
name = "Music Audio Background Engine" name = "Music Audio Background Engine"
description = "A basic background engine to set the background audio to a music track." description = (
"A basic background engine to set the background audio to a music track."
)
num_options = 1 num_options = 1
def __init__(self, options: list[str]): def __init__(self, options: list[str]):
assets = self.get_assets(type="bcg_music") assets = self.get_assets(type="bcg_music")
self.background_audio = [asset for asset in assets if asset.data["name"] == options[0]][0] self.background_audio = [
asset for asset in assets if asset.data["name"] == options[0]
][0]
super().__init__() super().__init__()
@classmethod @classmethod
@@ -54,7 +58,7 @@ class MusicAudioBackgroundEngine(BaseAudioBackgroundEngine):
audio = audio.with_effects([afx.AudioFadeOut(1)]) audio = audio.with_effects([afx.AudioFadeOut(1)])
# change volume to 0.5 # change volume to 0.5
audio: mp.AudioFileClip = audio.with_multiply_volume(0.5) audio: mp.AudioFileClip = audio.with_multiply_volume(0.5)
self.ctx.audio.append(audio) return audio
@classmethod @classmethod
def get_settings(cls): def get_settings(cls):

View File

@@ -5,5 +5,4 @@ from src.engines.BaseEngine import BaseEngine
class BaseBackgroundEngine(BaseEngine): class BaseBackgroundEngine(BaseEngine):
@abstractmethod @abstractmethod
def get_background(self) -> None: def get_background(self) -> None: ...
...

View File

@@ -17,7 +17,9 @@ class VideoBackgroundEngine(BaseBackgroundEngine):
def __init__(self, options: list[str]): def __init__(self, options: list[str]):
assets = self.get_assets(type="bcg_video") assets = self.get_assets(type="bcg_video")
self.background_video = [asset for asset in assets if asset.data["name"] == options[0]][0] self.background_video = [
asset for asset in assets if asset.data["name"] == options[0]
][0]
super().__init__() super().__init__()
@classmethod @classmethod
@@ -56,7 +58,7 @@ class VideoBackgroundEngine(BaseBackgroundEngine):
else: else:
clip = clip.with_effects([vfx.Resize(width=self.ctx.width)]) clip = clip.with_effects([vfx.Resize(width=self.ctx.width)])
clip = clip.with_position(("center", "center")) clip = clip.with_position(("center", "center"))
self.ctx.index_0.append(clip) return clip
@classmethod @classmethod
def get_settings(cls): def get_settings(cls):

View File

@@ -5,6 +5,7 @@ import moviepy as mp
from . import BaseCaptioningEngine from . import BaseCaptioningEngine
def get_available_fonts(): def get_available_fonts():
# on windows, the fonts are in the C:\Windows\Fonts and C:\Users\Username\AppData\Local\Microsoft\Windows\Fonts # on windows, the fonts are in the C:\Windows\Fonts and C:\Users\Username\AppData\Local\Microsoft\Windows\Fonts
# on linux, the fonts are in the /usr/share/fonts directory # on linux, the fonts are in the /usr/share/fonts directory
@@ -12,7 +13,9 @@ def get_available_fonts():
if platform.system() == "Windows": if platform.system() == "Windows":
font_dirs = [ font_dirs = [
"C:\\Windows\\Fonts", "C:\\Windows\\Fonts",
"C:\\Users\\{}\\AppData\\Local\\Microsoft\\Windows\\Fonts".format(os.getlogin()), "C:\\Users\\{}\\AppData\\Local\\Microsoft\\Windows\\Fonts".format(
os.getlogin()
),
] ]
elif platform.system() == "Linux": elif platform.system() == "Linux":
font_dirs = ["/usr/share/fonts"] font_dirs = ["/usr/share/fonts"]
@@ -69,14 +72,11 @@ class SimpleCaptioningEngine(BaseCaptioningEngine):
punctuations = (".", "?", "!", ",", ":", ";") punctuations = (".", "?", "!", ",", ":", ";")
return text.strip().endswith(tuple(punctuations)) return text.strip().endswith(tuple(punctuations))
def get_captions(self): def get_captions(self, words: list[dict[str, str]] = None) -> list[mp.TextClip]:
# 3 words per 1000 px, we do the math # 3 words per 1000 px, we do the math
max_words = int(self.ctx.width / 1000 * 3) max_words = int(self.ctx.width / 1000 * 3)
clips = [] clips = []
words = (
self.ctx.timed_script.copy()
) # List of dicts with "start", "end", and "text"
current_line = "" current_line = ""
current_start = words[0]["start"] current_start = words[0]["start"]
current_end = words[0]["end"] current_end = words[0]["end"]
@@ -107,8 +107,7 @@ class SimpleCaptioningEngine(BaseCaptioningEngine):
current_line.strip(), current_start, words[-1]["end"] current_line.strip(), current_start, words[-1]["end"]
) )
) )
return clips
self.ctx.index_7.extend(clips)
@classmethod @classmethod
def get_settings(cls): def get_settings(cls):

View File

@@ -103,7 +103,9 @@ class CoquiTTSEngine(BaseTTSEngine):
try: try:
self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2") self.tts = TTS("tts_models/multilingual/multi-dataset/xtts_v2")
except: except:
raise Exception("An error occured when loading thr TTS model. Make sure that you have agreed to the TOS in the TTSEngine tab.") raise Exception(
"An error occured when loading thr TTS model. Make sure that you have agreed to the TOS in the TTSEngine tab."
)
device = "cuda" if torch.cuda.is_available() else "cpu" device = "cuda" if torch.cuda.is_available() else "cpu"
self.tts.to(device) self.tts.to(device)
@@ -124,9 +126,7 @@ class CoquiTTSEngine(BaseTTSEngine):
if self.to_force_duration: if self.to_force_duration:
self.force_duration(float(self.duration), path) self.force_duration(float(self.duration), path)
self.ctx.duration = self.get_audio_duration(path) return self.get_audio_duration(path)
self.ctx.timed_script = self.time_with_whisper(path)
@classmethod @classmethod
def get_options(cls) -> list: def get_options(cls) -> list:
@@ -162,10 +162,12 @@ class CoquiTTSEngine(BaseTTSEngine):
options.append(duration_checkbox) options.append(duration_checkbox)
options.append(duration) options.append(duration)
options.append(gr.Checkbox( options.append(
gr.Checkbox(
label="I agree to the Coqui public mode license", label="I agree to the Coqui public mode license",
info="You must agree to the Coqui TTS terms of service to use this engine: https://coqui.ai/cpml", info="You must agree to the Coqui TTS terms of service to use this engine: https://coqui.ai/cpml",
value=False, value=False,
show_label=True, show_label=True,
)) )
)
return options return options

View File

@@ -16,7 +16,9 @@ class YouTubeUploadEngine(BaseUploadEngine):
super().__init__(**kwargs) super().__init__(**kwargs)
self.oauth_name = options[0] self.oauth_name = options[0]
self.oauth = self.retrieve_setting(type="oauth_credentials")[self.oauth_name] self.oauth = self.retrieve_setting(type="oauth_credentials")[self.oauth_name]
self.credentials = self.retrieve_setting(type="youtube_client_secrets")[self.oauth["client_secret"]] self.credentials = self.retrieve_setting(type="youtube_client_secrets")[
self.oauth["client_secret"]
]
self.hashtags = options[1] self.hashtags = options[1]
@@ -35,11 +37,11 @@ class YouTubeUploadEngine(BaseUploadEngine):
result = orjson.loads(result) result = orjson.loads(result)
return result return result
def upload(self): def upload(self, title: str, description: str, path: str):
options = { options = {
"file": self.ctx.get_file_path("final.mp4"), "file": path,
"title": self.ctx.title + " | " + self.hashtags, "title": title + " | " + self.hashtags,
"description": self.ctx.description, "description": description,
"privacyStatus": "private", "privacyStatus": "private",
"category": 28, "category": 28,
} }
@@ -53,7 +55,7 @@ class YouTubeUploadEngine(BaseUploadEngine):
current_oauths = self.retrieve_setting(type="oauth_credentials") or {} current_oauths = self.retrieve_setting(type="oauth_credentials") or {}
current_oauths[self.oauth_name] = { current_oauths[self.oauth_name] = {
"client_secret": self.oauth["client_secret"], "client_secret": self.oauth["client_secret"],
"credentials": new_oauth "credentials": new_oauth,
} }
self.store_setting( self.store_setting(
type="oauth_credentials", type="oauth_credentials",
@@ -68,7 +70,9 @@ class YouTubeUploadEngine(BaseUploadEngine):
choices = list(choices.keys()) choices = list(choices.keys())
return [ return [
gr.Dropdown( gr.Dropdown(
choices=choices, label="Choose Channel", value=choices[0] if choices else "No channels available !" choices=choices,
label="Choose Channel",
value=choices[0] if choices else "No channels available !",
), ),
gr.Textbox(label="Hashtags", value="#shorts", max_lines=1), gr.Textbox(label="Hashtags", value="#shorts", max_lines=1),
] ]
@@ -84,7 +88,9 @@ class YouTubeUploadEngine(BaseUploadEngine):
submit_button = gr.Button("Save") submit_button = gr.Button("Save")
def save(binary, clien_secret_name): def save(binary, clien_secret_name):
current_client_secrets = cls.retrieve_setting(type="youtube_client_secrets") or {} current_client_secrets = (
cls.retrieve_setting(type="youtube_client_secrets") or {}
)
client_secret_json = orjson.loads(binary) client_secret_json = orjson.loads(binary)
current_client_secrets[clien_secret_name] = client_secret_json current_client_secrets[clien_secret_name] = client_secret_json
cls.store_setting( cls.store_setting(
@@ -93,22 +99,32 @@ class YouTubeUploadEngine(BaseUploadEngine):
) )
gr.Info(f"{clien_secret_name} saved successfully !") gr.Info(f"{clien_secret_name} saved successfully !")
submit_button.click(save, inputs=[client_secret_file, clien_secret_name]) submit_button.click(
save, inputs=[client_secret_file, clien_secret_name]
)
with gr.Column() as ytb_oauth: with gr.Column() as ytb_oauth:
possible_client_secrets = cls.retrieve_setting(type="youtube_client_secrets") or {} possible_client_secrets = (
cls.retrieve_setting(type="youtube_client_secrets") or {}
)
possible_client_secrets = list(possible_client_secrets.keys()) possible_client_secrets = list(possible_client_secrets.keys())
choosen_client_secret = gr.Dropdown(label="Login secret", choices=possible_client_secrets) choosen_client_secret = gr.Dropdown(
label="Login secret", choices=possible_client_secrets
)
name = gr.Textbox(label="Name", max_lines=1) name = gr.Textbox(label="Name", max_lines=1)
login_button = gr.Button("Login", variant="primary") login_button = gr.Button("Login", variant="primary")
def login(choosen_client_secret, name): def login(choosen_client_secret, name):
choosen_secret_data = cls.retrieve_setting(type="youtube_client_secrets")[choosen_client_secret] choosen_secret_data = cls.retrieve_setting(
type="youtube_client_secrets"
)[choosen_client_secret]
new_oauth_entry = cls.__oauth(choosen_secret_data) new_oauth_entry = cls.__oauth(choosen_secret_data)
current_oauths = cls.retrieve_setting(type="oauth_credentials") or {} current_oauths = (
cls.retrieve_setting(type="oauth_credentials") or {}
)
current_oauths[name] = { current_oauths[name] = {
"client_secret": choosen_client_secret, "client_secret": choosen_client_secret,
"credentials": new_oauth_entry "credentials": new_oauth_entry,
} }
cls.store_setting( cls.store_setting(
type="oauth_credentials", type="oauth_credentials",