From a93a710395b2bcbf04ae4097d2a349ff4329b908 Mon Sep 17 00:00:00 2001 From: Francisco G P Date: Wed, 12 Mar 2025 00:36:03 -0700 Subject: [PATCH 1/3] Corrected deprecated comments and made few adjustments regarding best practices. --- src/async_request.py | 4 ++-- src/main.py | 28 ++++++++++------------------ src/scene_split.py | 13 ++++++++++--- src/slideshow.py | 5 ++--- src/text_to_img.py | 8 ++------ 5 files changed, 26 insertions(+), 32 deletions(-) diff --git a/src/async_request.py b/src/async_request.py index ba48716..31e6cca 100644 --- a/src/async_request.py +++ b/src/async_request.py @@ -9,7 +9,7 @@ async def send_request(session: aiohttp.ClientSession, url: str, data: Dict[str, async def periodic_requests(url: str, data: Dict[str, Any], interval: int, count: int) -> None: async with aiohttp.ClientSession() as session: for _ in range(count): - asyncio.create_task(send_request(session, url, data)) # Fire and forget + asyncio.create_task(send_request(session, url, data)) await asyncio.sleep(interval) # Wait before sending the next request url = "https://api.example.com/endpoint" @@ -17,4 +17,4 @@ async def periodic_requests(url: str, data: Dict[str, Any], interval: int, count interval = 5 # Seconds between requests count = 10 # Number of requests -asyncio.run(periodic_requests(url, data, interval, count)) \ No newline at end of file +asyncio.run(periodic_requests(url, data, interval, count)) diff --git a/src/main.py b/src/main.py index df445d5..b8c646b 100644 --- a/src/main.py +++ b/src/main.py @@ -1,28 +1,24 @@ import timeit -start = timeit.default_timer() import json import os import scene_split import text_to_img import shutil +import slideshow + +start = timeit.default_timer() # Execution time begins with open("story.txt", "r", encoding="utf-8") as f: story = f.read() -def clear_story_folder(folder_path: str = "story") -> None: +def clear_story_folder(folder_path: str = "story") -> None: # Folder path generation (if not exists) if os.path.exists(folder_path): shutil.rmtree(folder_path) os.makedirs(folder_path) -clear_story_folder() -scenes = scene_split.main(story,0.7) +clear_story_folder() # Messages for splitted scenes and quantity +scenes = scene_split.main(story, 0.7) print("Scenes splitted successfully!") -# print(scenes) -# for i in scenes: -# print(i[0]) - -# Scenes to prompt -# print(scenes) number_of_scenes = len(scenes) print(f"Number of scenes: {number_of_scenes}") @@ -30,18 +26,14 @@ def clear_story_folder(folder_path: str = "story") -> None: image_type = input("Enter the type of image you want to generate (realistic, cartoon, abstract): ") print("\n") -for i, scene in enumerate(scenes, 1): +for i, scene in enumerate(scenes, 1): # Scenes to prompt prompt = f"Make a {image_type} image of" + scene - # print(prompt) text_to_img.main(prompt, f"story/image-{i}") -# Folder and File name generation -story_dict = {f"story/image-{i}.png": line for i, line in enumerate(scenes, 1)} +story_dict = {f"story/image-{i}.png": line for i, line in enumerate(scenes, 1)} # Folder and file name generation with open("story.json", "w") as f: json.dump(story_dict, f, indent=4) -end = timeit.default_timer() -print(f"Time taken: {end-start} seconds") - -import slideshow \ No newline at end of file +end = timeit.default_timer() # Execution time ends +print(f"Time taken: {end-start} seconds") # Total execution time diff --git a/src/scene_split.py b/src/scene_split.py index 8ea4ead..bb61fe3 100644 --- a/src/scene_split.py +++ b/src/scene_split.py @@ -7,7 +7,15 @@ import re from typing import List -story_text: str = """John walked into the forest. He heard rustling behind him. The trees loomed tall as he pressed forward, his heart pounding. Later that night, he found a small cabin. It looked abandoned, but the door creaked open when he pushed it. The wind howled outside as he stepped in. Inside the cabin, an old man sat by the fire. He wore a long cloak and stared at John as if expecting him. In the morning, John woke up to find the man missing. The fire had gone cold. He stepped outside and saw footprints leading into the misty woods. With no other choice, he followed the footprints. The deeper he went, the more uneasy he felt, as if someone—or something—was watching him.""" +story_text: str = '''John walked into the forest. He heard rustling behind him. +The trees loomed tall as he pressed forward, his heart pounding. Later that +night, he found a small cabin. It looked abandoned, but the door creaked open +when he pushed it. The wind howled outside as he stepped in. Inside the cabin, +an old man sat by the fire. He wore a long cloak and stared at John as if +expecting him. In the morning, John woke up to find the man missing. The fire +had gone cold. He stepped outside and saw footprints leading into the misty +woods. With no other choice, he followed the footprints. The deeper he went, +the more uneasy he felt, as if someone—or something—was watching him.''' # Example story text following multiline best practices in Python def split_into_sentences(text: str) -> List[str]: return re.findall(r"[^.!?]+", text) @@ -43,8 +51,7 @@ def main(story_text: str, threshold: float = 0.5) -> List[str]: i += 1 merged_sentences.append(sentences[-1]) - # print("Similarity array:", similarity_array) return merged_sentences if __name__ == "__main__": - print(main(story_text)) \ No newline at end of file + print(main(story_text)) diff --git a/src/slideshow.py b/src/slideshow.py index c8c676d..0c82f16 100644 --- a/src/slideshow.py +++ b/src/slideshow.py @@ -7,7 +7,6 @@ image_texts = json.load(file) image_paths: List[str] = list(image_texts.keys()) -# print(image_texts) root = tk.Tk() root.title("Image Slideshow with Text") @@ -40,7 +39,7 @@ def update_image() -> None: idx = (idx + 1) % len(image_texts) root.after(5000, update_image) -def toggle_pause() -> None: +def toggle_pause() -> None: # Commands for controlling the images global paused paused = not paused if not paused: @@ -56,7 +55,7 @@ def prev_image() -> None: idx = (idx - 1) % len(image_texts) update_image() -btn_frame = tk.Frame(root) +btn_frame = tk.Frame(root) # Buttons for controlling the images through commands btn_frame.pack() btn_prev = tk.Button(btn_frame, text="<< Previous", command=prev_image) diff --git a/src/text_to_img.py b/src/text_to_img.py index d6afb87..392d7ac 100644 --- a/src/text_to_img.py +++ b/src/text_to_img.py @@ -3,14 +3,13 @@ import time import os from dotenv import load_dotenv -from PIL import Image load_dotenv() TOGETHER_API_KEY = os.getenv("TOGETHER_AI_API_KEY") client = Together(api_key=TOGETHER_API_KEY) -def main(myprompt: str, img_file_name: str): +def main(myprompt: str, img_file_name: str): # Images main format response = client.images.generate( prompt=myprompt, model="black-forest-labs/FLUX.1-schnell-Free", @@ -20,14 +19,11 @@ def main(myprompt: str, img_file_name: str): n=1, response_format="b64_json", ) - # print(response.data[0].b64_json) imgstring: str = response.data[0].b64_json imgdata: bytes = base64.b64decode(imgstring) filename: str = f'{img_file_name}.png' with open(filename, 'wb') as f: f.write(imgdata) - # image = Image.open(filename) - # image.show() if __name__=="__main__": - main("Cat eating burger", "burger-cat") \ No newline at end of file + main("Cat eating burger", "burger-cat") From a87d1270a41e6cffebe82f208dd9b7eee13da988 Mon Sep 17 00:00:00 2001 From: Francisco G P Date: Wed, 12 Mar 2025 09:02:31 -0700 Subject: [PATCH 2/3] Applied requested changes to main.py. --- src/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main.py b/src/main.py index b8c646b..5a3d0d4 100644 --- a/src/main.py +++ b/src/main.py @@ -1,4 +1,5 @@ import timeit +start = timeit.default_timer() # Execution time begins import json import os import scene_split @@ -6,8 +7,6 @@ import shutil import slideshow -start = timeit.default_timer() # Execution time begins - with open("story.txt", "r", encoding="utf-8") as f: story = f.read() From 22fd894bbc64a77cb04d229a85317345fe672c2b Mon Sep 17 00:00:00 2001 From: Francisco G P Date: Wed, 12 Mar 2025 09:43:14 -0700 Subject: [PATCH 3/3] Applied requested change to main.py. --- src/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/main.py b/src/main.py index 5a3d0d4..d06fca7 100644 --- a/src/main.py +++ b/src/main.py @@ -5,7 +5,6 @@ import scene_split import text_to_img import shutil -import slideshow with open("story.txt", "r", encoding="utf-8") as f: story = f.read() @@ -36,3 +35,5 @@ def clear_story_folder(folder_path: str = "story") -> None: # Folder path genera end = timeit.default_timer() # Execution time ends print(f"Time taken: {end-start} seconds") # Total execution time + +import slideshow # Slideshow is played