Changes: python assets path to locate assets after bundling

This commit is contained in:
sauravniraula 2025-05-12 03:02:05 +05:45
parent 574748f4f8
commit 997e50e15b
No known key found for this signature in database
GPG key ID: 60FCC1B5A5E83326
4 changed files with 17 additions and 6 deletions

View file

@ -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:

View file

@ -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 = []

View file

@ -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"]]

View file

@ -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())