diff --git a/servers/fastapi/api/utils.py b/servers/fastapi/api/utils.py index 31646834..aeaf3ee7 100644 --- a/servers/fastapi/api/utils.py +++ b/servers/fastapi/api/utils.py @@ -1,6 +1,7 @@ import asyncio import json import os +import sys import traceback from typing import List, Optional @@ -47,6 +48,11 @@ def update_env_with_user_config(): os.environ["GOOGLE_API_KEY"] = user_config.GOOGLE_API_KEY +def get_resource(relative_path): + base_path = getattr(sys, "_MEIPASS", os.path.dirname(os.path.abspath(__file__))) + return os.path.join(base_path, relative_path) + + def replace_file_name(old_name: str, new_name: str) -> str: splitted = old_name.split(".") if len(splitted) < 1: diff --git a/servers/fastapi/image_processor/icons_finder.py b/servers/fastapi/image_processor/icons_finder.py index ac04f8b6..82cc0c1b 100644 --- a/servers/fastapi/image_processor/icons_finder.py +++ b/servers/fastapi/image_processor/icons_finder.py @@ -1,6 +1,7 @@ import os from typing import List, Optional +from api.utils import get_resource from ppt_generator.models.query_and_prompt_models import ( IconCategoryEnum, IconQueryCollectionWithData, @@ -21,11 +22,11 @@ async def get_icon( with open(output_path, "wb") as f_a: try: - with open(f"assets/icons/bold/{icon_name}.png", "rb") as f_b: + with open(get_resource(f"assets/icons/bold/{icon_name}.png"), "rb") as f_b: f_a.write(f_b.read()) except Exception as e: print("Error finding icon: ", e) - with open(f"assets/icons/placeholder.png", "rb") as f_b: + with open(get_resource("assets/icons/placeholder.png"), "rb") as f_b: f_a.write(f_b.read()) @@ -42,7 +43,9 @@ async def get_icons( icon_names = [result.page_content for result in results] - icon_paths = [f"assets/icons/bold/{each}-bold.png" for each in icon_names] + icon_paths = [ + get_resource(f"assets/icons/bold/{each}-bold.png") for each in icon_names + ] icon_temp_paths = [] diff --git a/servers/fastapi/image_processor/icons_vectorstore_utils.py b/servers/fastapi/image_processor/icons_vectorstore_utils.py index d41ed5ae..1eaf7119 100644 --- a/servers/fastapi/image_processor/icons_vectorstore_utils.py +++ b/servers/fastapi/image_processor/icons_vectorstore_utils.py @@ -4,10 +4,11 @@ from langchain_core.vectorstores import InMemoryVectorStore from langchain_huggingface import HuggingFaceEmbeddings from langchain_core.documents import Document +from api.utils import get_resource def get_icons_vectorstore(): - vector_store_path = "assets/icons_vectorstore.json" + vector_store_path = get_resource("assets/icons_vectorstore.json") embeddings = HuggingFaceEmbeddings(model_name="all-MiniLM-L6-v2") @@ -17,7 +18,7 @@ def get_icons_vectorstore(): vector_store = InMemoryVectorStore(embeddings) - with open("assets/icons.json", "r") as f: + with open(get_resource("assets/icons.json"), "r") as f: icons = json.load(f) icon_names = [icon["name"] for icon in icons["icons"]] diff --git a/servers/fastapi/image_processor/images_finder.py b/servers/fastapi/image_processor/images_finder.py index 6eae412b..bec1cc76 100644 --- a/servers/fastapi/image_processor/images_finder.py +++ b/servers/fastapi/image_processor/images_finder.py @@ -6,6 +6,7 @@ from openai import OpenAI from ppt_generator.models.query_and_prompt_models import ( ImagePromptWithThemeAndAspectRatio, ) +from api.utils import get_resource async def generate_image( @@ -25,7 +26,7 @@ async def generate_image( await image_gen_func(image_prompt, output_path) except Exception as e: print(f"Error generating image: {e}") - with open("assets/images/placeholder.jpg", "rb") as f_a: + with open(get_resource("assets/images/placeholder.jpg"), "rb") as f_a: with open(output_path, "wb") as f_b: f_b.write(f_a.read())