Fetch models once and cache the results.
()
| 165 | |
| 166 | |
| 167 | def fetch_models_once() -> Dict[str, List[str]]: |
| 168 | """Fetch models once and cache the results.""" |
| 169 | logger.info("Fetching models") |
| 170 | current_time = time.time() |
| 171 | cache_timeout = 300 # 5 minutes |
| 172 | |
| 173 | if ( |
| 174 | st.session_state.cached_models is not None |
| 175 | and current_time - st.session_state.last_model_fetch < cache_timeout |
| 176 | ): |
| 177 | logger.debug("Using cached models") |
| 178 | return st.session_state.cached_models |
| 179 | |
| 180 | logger.debug("Cache expired or not available, fetching new models") |
| 181 | success, stdout, stderr = safe_run_command(["fabric", "--listmodels"]) |
| 182 | if not success: |
| 183 | logger.error(f"Failed to fetch models: {stderr}") |
| 184 | st.error(f"Failed to fetch models: {stderr}") |
| 185 | return {} |
| 186 | |
| 187 | providers = parse_models_output(stdout) |
| 188 | logger.info(f"Found {len(providers)} providers") |
| 189 | st.session_state.cached_models = providers |
| 190 | st.session_state.last_model_fetch = current_time |
| 191 | return providers |
| 192 | |
| 193 | |
| 194 | def get_configured_providers() -> Dict[str, List[str]]: |
no test coverage detected