Skip to content

Commit

Permalink
mypy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bendsouza2 committed Dec 9, 2024
1 parent bf93716 commit 3c71718
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 18 deletions.
8 changes: 7 additions & 1 deletion python/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ RUN yum update -y && \
aspell-esp \
aspell-es \
hunspell-es \
make && \
make \
liberation-sans-fonts \
ImageMagick && \
yum clean all


RUN fc-cache -f -v
RUN fc-list | grep LiberationSans

RUN mkdir -p /usr/share/hunspell && \
curl -o /usr/share/hunspell/es_ES.dic https://cgit.freedesktop.org/libreoffice/dictionaries/plain/es/es_ES.dic && \
curl -o /usr/share/hunspell/es_ES.aff https://cgit.freedesktop.org/libreoffice/dictionaries/plain/es/es_ES.aff
Expand Down
3 changes: 2 additions & 1 deletion python/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,5 @@ class Paths:
VIDEO_DIR_PATH = "video"
GOOGLE_CREDS_PATH = "google_creds.json"
YT_TOKEN_PATH = "python/token.json"
PYTHON_ENV_FILE = ".env"
PYTHON_ENV_FILE = ".env"
FONT_PATH = "/usr/share/fonts/liberation/LiberationSans-Regular.ttf"
7 changes: 6 additions & 1 deletion python/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ def process_video_and_upload(db_write_function: Optional[Callable[[Dict[str, str
prompt = Prompts.IMAGE_GENERATOR + audio_generator.sentence
image_generator = ImageGenerator(prompts=prompt, cloud_storage=True)

if audio_generator.cloud_storage is True:
audio_file = audio_generator.audio_cloud_path
else:
audio_file = audio_generator.audio_path

video_generator = VideoGenerator(
word=audio_generator.word,
sentence=audio_generator.sentence,
translated_sentence=audio_generator.translated_sentence,
image_paths=image_generator.image_paths,
audio_filepath=audio_generator.audio_path,
audio_filepath=audio_file,
cloud_storage=True,
)

Expand Down
33 changes: 18 additions & 15 deletions python/word_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def __init__(self,
self.translated_sentence = self.google_translate(
source_language=self.language_to_learn, target_language=self.native_language
)
self.audio_duration = None
self.audio_path = self.text_to_speech(language=self.language_to_learn)
self.audio_duration: Optional[float] = None
self.audio_path, self.audio_cloud_path = self.text_to_speech(language=self.language_to_learn)
self.sub_filepath = None

@property
Expand All @@ -74,7 +74,7 @@ def word_list_path(self, word_list_path):
else:
self._word_list_path = f"{base_config.BASE_DIR}/{word_list_path}"

def text_to_speech(self, language: str, filepath: Optional[str] = None) -> str:
def text_to_speech(self, language: str, filepath: Optional[str] = None) -> Tuple[str | None, str | None]:
"""
Generate an audio file
:param language: The language that the audio should be generated in
Expand All @@ -95,9 +95,11 @@ def text_to_speech(self, language: str, filepath: Optional[str] = None) -> str:
s3_key = f"{Paths.AUDIO_DIR_PATH}/{dt}.wav"
s3_bucket = BucketSort(bucket=BUCKET_NAME)
s3_path = s3_bucket.push_object_to_s3(audio_buffer.read(), s3_key)
else:
s3_path = None

tts.save(filepath)
return filepath
return filepath, s3_path

def get_audio_duration(self) -> float:
"""
Expand Down Expand Up @@ -186,14 +188,14 @@ def echogarden_generate_subtitles(self, sentence: str) -> str:
file_to_execute = f"{base_config.BASE_DIR}/{Paths.NODE_SUBS_FILE_PATH}"
for log_path in [file_to_execute, self.audio_path]:
no_path = []
if not os.path.exists(log_path):
if log_path is not None and not os.path.exists(log_path):
no_path.append(log_path)
if len(no_path) > 0:
raise FileNotFoundError(f"paths {no_path} do not exist")

command = ["node", file_to_execute, self.audio_path, sentence, output_file_path]
try:
result = subprocess.run(command, check=True, capture_output=True, text=True)
result = subprocess.run(command, check=True, capture_output=True, text=True) # type: ignore[arg-type]
except subprocess.CalledProcessError as e:
raise subprocess.CalledProcessError(
e.returncode, e.cmd, stderr=f"Command failed with exit code {e.returncode}. stderr {e.stderr}"
Expand Down Expand Up @@ -492,7 +494,7 @@ def create_subtitle_clip(
colour: str = "white",
background_opacity: float = 0.7,
text_pos: Tuple[str, str] | Tuple[int, int] | Tuple[float, float] = ("center", "center"),
font: str = "Courier",
font: str = Paths.FONT_PATH,
padding: int = 60
) -> CompositeVideoClip:
"""
Expand Down Expand Up @@ -523,7 +525,7 @@ def create_translated_subtitle_clip(
audio_duration: float,
font_size: int = 50,
colour: str = "white",
font: str = "Courier",
font: str = Paths.FONT_PATH,
padding: int = 60,
text_pos: Tuple[str, str] = ("center", "top")
) -> CompositeVideoClip:
Expand Down Expand Up @@ -577,10 +579,10 @@ def create_translated_subtitles_file(
:return: Path to the created subtitles file
"""
if words is None:
words = self.translated_sentence.split()
words_list = self.translated_sentence.split()
else:
words = words.split()
word_groups = [words[i:i + 3] for i in range(0, len(words), 3)]
words_list = words.split()
word_groups = [words_list[i:i + 3] for i in range(0, len(words_list), 3)]

group_count = len(word_groups)
display_duration = audio_duration / group_count
Expand All @@ -605,7 +607,7 @@ def create_translated_subtitles_file(
def create_fancy_word_clip(
word: str,
font_size: int = 80,
font: str = "Toppan-Bunkyu-Gothic-Demibold",
font: str = Paths.FONT_PATH,
duration: float = 1.0,
stroke_colour: str = "green",
style: str = "bounce"
Expand Down Expand Up @@ -662,7 +664,7 @@ def create_fancy_word_clip(

return final_clip

def generate_video(self, output_filepath: Optional[str] = None, word_font: str = "Courier") -> str:
def generate_video(self, output_filepath: Optional[str] = None, word_font: str = Paths.FONT_PATH) -> str:
"""
Combine audio, images, word overlay and subtitles to generate and save a video
:param output_filepath: the absolute path to store the generated video
Expand Down Expand Up @@ -732,12 +734,13 @@ def generate_video(self, output_filepath: Optional[str] = None, word_font: str =

s3_path = None
if self.cloud_storage is True:
with tempfile.NamedTemporaryFile(suffix=".mp4", delete=True) as temp_video:
with tempfile.NamedTemporaryFile(suffix=".mp4", dir="/tmp", delete=True) as temp_video:
final_video.write_videofile(
temp_video.name,
fps=24,
codec="libx264",
audio_codec="aac"
audio_codec="aac",
temp_audiofile=f"/tmp/temp_audiofile.m4a",
)
temp_video.seek(0)

Expand Down

0 comments on commit 3c71718

Please sign in to comment.