From 1bf9326604377504a519fbeaf27662b8aaae6677 Mon Sep 17 00:00:00 2001 From: Will Miao <13051207myq@gmail.com> Date: Fri, 22 Aug 2025 11:13:37 +0800 Subject: [PATCH] feat: Enhance download path template handling to support JSON strings and ensure defaults --- py/services/settings_manager.py | 37 +++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/py/services/settings_manager.py b/py/services/settings_manager.py index a6a6e24a..1540904c 100644 --- a/py/services/settings_manager.py +++ b/py/services/settings_manager.py @@ -110,6 +110,43 @@ class SettingsManager: Template string for the model type, defaults to '{base_model}/{first_tag}' """ templates = self.settings.get('download_path_templates', {}) + + # Handle edge case where templates might be stored as JSON string + if isinstance(templates, str): + try: + # Try to parse JSON string + parsed_templates = json.loads(templates) + if isinstance(parsed_templates, dict): + # Update settings with parsed dictionary + self.settings['download_path_templates'] = parsed_templates + self._save_settings() + templates = parsed_templates + logger.info("Successfully parsed download_path_templates from JSON string") + else: + raise ValueError("Parsed JSON is not a dictionary") + except (json.JSONDecodeError, ValueError) as e: + # If parsing fails, set default values + logger.warning(f"Failed to parse download_path_templates JSON string: {e}. Setting default values.") + default_template = '{base_model}/{first_tag}' + templates = { + 'lora': default_template, + 'checkpoint': default_template, + 'embedding': default_template + } + self.settings['download_path_templates'] = templates + self._save_settings() + + # Ensure templates is a dictionary + if not isinstance(templates, dict): + default_template = '{base_model}/{first_tag}' + templates = { + 'lora': default_template, + 'checkpoint': default_template, + 'embedding': default_template + } + self.settings['download_path_templates'] = templates + self._save_settings() + return templates.get(model_type, '{base_model}/{first_tag}') settings = SettingsManager()