@@ -50,6 +50,22 @@ def _set_api_embeddings(self, result: TextEncodingResult | None) -> None:
5050 def clear_api_embeddings (self ) -> None :
5151 self ._set_api_embeddings (None )
5252
53+ def should_use_local_encoding (self ) -> bool :
54+ """Decide whether to use local text encoding based on availability.
55+
56+ The user's ``use_local_text_encoder`` setting acts as a tiebreaker only
57+ when **both** the API key and the local encoder are available. When only
58+ one option exists, that option is used regardless of the setting.
59+ """
60+ settings = self .state .app_settings .model_copy (deep = True )
61+ api_available = bool (settings .ltx_api_key )
62+ text_encoder_dir = self ._config .model_path ("text_encoder" )
63+ local_available = text_encoder_dir .exists () and any (text_encoder_dir .iterdir ())
64+
65+ if api_available and local_available :
66+ return settings .use_local_text_encoder # setting is tiebreaker
67+ return local_available # use whichever is available
68+
5369 def prepare_text_encoding (self , prompt : str , enhance_prompt : bool ) -> None :
5470 """Validate settings and prepare text embeddings for a generation run.
5571
@@ -58,42 +74,39 @@ def prepare_text_encoding(self, prompt: str, enhance_prompt: bool) -> None:
5874 with no local fallback.
5975 """
6076 settings = self .state .app_settings .model_copy (deep = True )
77+ api_available = bool (settings .ltx_api_key )
78+ text_encoder_dir = self ._config .model_path ("text_encoder" )
79+ local_available = text_encoder_dir .exists () and any (text_encoder_dir .iterdir ())
6180
62- if not settings . use_local_text_encoder and not settings . ltx_api_key :
81+ if not api_available and not local_available :
6382 raise RuntimeError (
6483 "TEXT_ENCODING_NOT_CONFIGURED: To generate videos, you need to configure text encoding. "
6584 "Either enter an LTX API Key in Settings, or enable the Local Text Encoder."
6685 )
6786
68- if settings .use_local_text_encoder :
69- text_encoder_path = self ._config .model_path ("text_encoder" )
70- if not text_encoder_path .exists () or not any (text_encoder_path .iterdir ()):
71- raise RuntimeError (
72- "TEXT_ENCODER_NOT_DOWNLOADED: Local text encoder is enabled but not downloaded. "
73- "Please download it from Settings (~25 GB), or switch to using the LTX API."
74- )
75-
87+ use_local = self .should_use_local_encoding ()
7688 gemma_root = self .resolve_gemma_root ()
7789 embeddings = self ._prepare_api_embeddings (prompt , enhance_prompt )
7890
79- if settings . ltx_api_key and not settings . use_local_text_encoder and embeddings is None and gemma_root is None :
91+ if not use_local and embeddings is None and gemma_root is None :
8092 raise RuntimeError (
8193 "LTX API text encoding failed and local text encoder is not available. "
8294 "Please download the text encoder from Settings or check your API key."
8395 )
8496
8597 def resolve_gemma_root (self ) -> str | None :
86- settings = self .state .app_settings .model_copy (deep = True )
98+ if not self .should_use_local_encoding ():
99+ return None
87100 text_encoder_dir = self ._config .model_path ("text_encoder" )
88- text_encoder_available = text_encoder_dir .exists () and any (text_encoder_dir .iterdir ())
89-
90- if (settings .use_local_text_encoder or not settings .ltx_api_key ) and text_encoder_available :
91- return str (text_encoder_dir )
92- return None
101+ return str (text_encoder_dir )
93102
94103 def _prepare_api_embeddings (self , prompt : str , enhance_prompt : bool ) -> TextEncodingResult | None :
104+ if self .should_use_local_encoding ():
105+ self .clear_api_embeddings ()
106+ return None
107+
95108 settings = self .state .app_settings .model_copy (deep = True )
96- if not settings .ltx_api_key or settings . use_local_text_encoder :
109+ if not settings .ltx_api_key :
97110 self .clear_api_embeddings ()
98111 return None
99112
0 commit comments