From 74fe4bb63703ee225c135d0a80df6d21caa8cd93 Mon Sep 17 00:00:00 2001 From: dazedanon Date: Sat, 8 Nov 2025 12:46:37 -0600 Subject: [PATCH] Fix GUI so that its properly caching during estimtaes --- gui/translation_tab.py | 4 ++++ modules/main.py | 4 ++++ util/translation.py | 21 +++++++++++++++++---- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/gui/translation_tab.py b/gui/translation_tab.py index 16e5d95..a047cd1 100644 --- a/gui/translation_tab.py +++ b/gui/translation_tab.py @@ -270,6 +270,10 @@ class TranslationWorker(QThread): # Load environment variables load_dotenv() + # Clear the translation cache at the start of the run + from util.translation import clear_cache + clear_cache() + # Check for required environment variables required_envs = ["api", "key", "organization", "model", "language", "timeout", "fileThreads", "threads", "width", "listWidth"] env_missing = False diff --git a/modules/main.py b/modules/main.py index 6da382c..3575c54 100644 --- a/modules/main.py +++ b/modules/main.py @@ -87,6 +87,10 @@ tqdm.write( def main(): + # Clear the translation cache at the start of the run + from util.translation import clear_cache + clear_cache() + estimate = "" speaker_parse = False # Deferred until after engine select while estimate == "": diff --git a/util/translation.py b/util/translation.py index 3f74a0b..c089d4b 100644 --- a/util/translation.py +++ b/util/translation.py @@ -145,6 +145,12 @@ def clear_cache(): global _cache with CACHE_LOCK: _cache = {} + # Also clear the cache file on disk + try: + if CACHE_FILE.exists(): + CACHE_FILE.unlink() + except Exception: + pass def load_cache(): """Load the translation cache from disk""" @@ -156,8 +162,16 @@ def load_cache(): if _cache is not None: # Double-check after acquiring lock return _cache - # Start with empty cache each run + # Try to load existing cache from disk _cache = {} + try: + if CACHE_FILE.exists(): + with open(CACHE_FILE, "r", encoding="utf-8") as f: + _cache = json.load(f) + except Exception: + # If cache file is corrupted or unreadable, start fresh + _cache = {} + return _cache def save_cache(): @@ -1061,9 +1075,8 @@ def translateAI(text, history, fullPromptFlag, config, filename=None, pbar=None, if tList and isinstance(tList[0], list): tList = [t for sublist in tList for t in sublist] - # Save cache after processing - if not config.estimateMode: - save_cache() + # Save cache after processing (for both estimate and translation modes) + save_cache() # Return result if formatType == "json":