From 6fe6c52d990ac90ad4581a5557d5545b02dc7199 Mon Sep 17 00:00:00 2001 From: "ajaysi (aider)" Date: Sat, 14 Sep 2024 17:36:46 +0530 Subject: [PATCH] feat: improve performance by implementing lazy loading, caching, and efficient file handling in alwrity.py and alwrity_utils.py --- alwrity.py | 9 +++++++-- lib/utils/alwrity_utils.py | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/alwrity.py b/alwrity.py index 31569a74..e5f25f02 100644 --- a/alwrity.py +++ b/alwrity.py @@ -2,11 +2,12 @@ import os import json import base64 from datetime import datetime -from dotenv import load_dotenv import streamlit as st # Load .env file -load_dotenv() +def load_environment(): + from dotenv import load_dotenv + load_dotenv() from lib.utils.alwrity_utils import ( blog_from_keyword, ai_agents_team, essay_writer, ai_news_writer, ai_seo_tools, @@ -26,6 +27,7 @@ def process_folder_for_rag(folder_path): st.write(f"This is a placeholder for processing the folder: {folder_path}") +@st.cache_data def check_api_keys(): """ Checks if the required API keys are present in the environment variables. @@ -59,6 +61,7 @@ def check_api_keys(): return True +@st.cache_data def check_llm_environs(): """ Ensures that the LLM provider and corresponding API key are set. @@ -282,6 +285,7 @@ def sidebar_configuration(): # Function to read prompts from the file +@st.cache_data def read_prompts(file_path="prompt_llm.txt"): if os.path.exists(file_path): with open(file_path, "r") as file: @@ -303,6 +307,7 @@ def load_image(image_path): def main(): + load_environment() setup_ui() setup_environment_paths() sidebar_configuration() diff --git a/lib/utils/alwrity_utils.py b/lib/utils/alwrity_utils.py index c9b336b9..0fbc0704 100644 --- a/lib/utils/alwrity_utils.py +++ b/lib/utils/alwrity_utils.py @@ -52,6 +52,7 @@ from lib.content_planning_calender.content_planning_agents_alwrity_crew import a from ..gpt_providers.text_generation.main_text_generation import llm_text_gen +@st.cache_data def record_voice(language="en"): # https://github.com/B4PT0R/streamlit-mic-recorder?tab=readme-ov-file#example state = st.session_state