@@ -62,113 +62,33 @@ def register_custom_model():
6262
6363
6464def register_builtin_model ():
65- import json
66-
67- from ...constants import XINFERENCE_MODEL_DIR
68- from ..custom import RegistryManager
69-
70- registry = RegistryManager .get_registry ("image" )
71- existing_model_names = {spec .model_name for spec in registry .get_custom_models ()}
72-
73- builtin_image_dir = os .path .join (XINFERENCE_MODEL_DIR , "v2" , "builtin" , "image" )
74- if os .path .isdir (builtin_image_dir ):
75- # First, try to load from the complete JSON file
76- complete_json_path = os .path .join (builtin_image_dir , "image_models.json" )
77- if os .path .exists (complete_json_path ):
78- try :
79- with codecs .open (complete_json_path , encoding = "utf-8" ) as fd :
80- model_data = json .load (fd )
81-
82- # Handle different formats
83- models_to_register = []
84- if isinstance (model_data , list ):
85- # Multiple models in a list
86- models_to_register = model_data
87- elif isinstance (model_data , dict ):
88- # Single model
89- if "model_name" in model_data :
90- models_to_register = [model_data ]
91- else :
92- # Models dict - extract models
93- for key , value in model_data .items ():
94- if isinstance (value , dict ) and "model_name" in value :
95- models_to_register .append (value )
96-
97- # Register all models from the complete JSON
98- for model_data in models_to_register :
99- try :
100- # Convert format using flatten_model_src
101- from ..utils import flatten_model_src
102-
103- flattened_list = flatten_model_src (model_data )
104- converted_data = (
105- flattened_list [0 ] if flattened_list else model_data
106- )
107- builtin_image_family = ImageModelFamilyV2 .parse_obj (
108- converted_data
109- )
110-
111- # Only register if model doesn't already exist
112- if builtin_image_family .model_name not in existing_model_names :
113- # Add to BUILTIN_IMAGE_MODELS directly for proper builtin registration
114- if (
115- builtin_image_family .model_name
116- not in BUILTIN_IMAGE_MODELS
117- ):
118- BUILTIN_IMAGE_MODELS [
119- builtin_image_family .model_name
120- ] = []
121- BUILTIN_IMAGE_MODELS [
122- builtin_image_family .model_name
123- ].append (builtin_image_family )
124- # Update model descriptions for the new builtin model
125- IMAGE_MODEL_DESCRIPTIONS .update (
126- generate_image_description (builtin_image_family )
127- )
128- existing_model_names .add (builtin_image_family .model_name )
129- except Exception as e :
130- warnings .warn (
131- f"Error parsing image model { model_data .get ('model_name' , 'Unknown' )} : { e } "
132- )
133-
134- logger .info (
135- f"Successfully registered { len (models_to_register )} image models from complete JSON"
136- )
137-
138- except Exception as e :
139- warnings .warn (
140- f"Error loading complete JSON file { complete_json_path } : { e } "
141- )
142- # Fall back to individual files if complete JSON loading fails
143-
144- # Fall back: load individual JSON files (backward compatibility)
145- individual_files = [
146- f
147- for f in os .listdir (builtin_image_dir )
148- if f .endswith (".json" ) and f != "image_models.json"
149- ]
150- for f in individual_files :
151- try :
152- with codecs .open (
153- os .path .join (builtin_image_dir , f ), encoding = "utf-8"
154- ) as fd :
155- builtin_image_family = ImageModelFamilyV2 .parse_obj (json .load (fd ))
156-
157- # Only register if model doesn't already exist
158- if builtin_image_family .model_name not in existing_model_names :
159- # Add to BUILTIN_IMAGE_MODELS directly for proper builtin registration
160- if builtin_image_family .model_name not in BUILTIN_IMAGE_MODELS :
161- BUILTIN_IMAGE_MODELS [builtin_image_family .model_name ] = []
162- BUILTIN_IMAGE_MODELS [builtin_image_family .model_name ].append (
163- builtin_image_family
164- )
165- # Update model descriptions for the new builtin model
166- IMAGE_MODEL_DESCRIPTIONS .update (
167- generate_image_description (builtin_image_family )
168- )
169- existing_model_names .add (builtin_image_family .model_name )
170- except Exception as e :
171- warnings .warn (f"{ builtin_image_dir } /{ f } has error, { e } " )
65+ # Use unified function for image models
66+ from ..utils import register_builtin_models_unified , flatten_model_src
67+
68+ def image_special_handling (registry , model_type ):
69+ """Handle image's special registration logic"""
70+ from ...constants import XINFERENCE_MODEL_DIR
71+ from ..custom import RegistryManager
72+
73+ registry_mgr = RegistryManager .get_registry ("image" )
74+ existing_model_names = {spec .model_name for spec in registry_mgr .get_custom_models ()}
75+
76+ for model_name , model_families in BUILTIN_IMAGE_MODELS .items ():
77+ for model_family in model_families :
78+ if model_family .model_name not in existing_model_names :
79+ # Update model descriptions for the new builtin model
80+ IMAGE_MODEL_DESCRIPTIONS .update (
81+ generate_image_description (model_family )
82+ )
83+ existing_model_names .add (model_family .model_name )
84+
85+ loaded_count = register_builtin_models_unified (
86+ model_type = "image" ,
87+ flatten_func = flatten_model_src ,
88+ model_class = ImageModelFamilyV2 ,
89+ builtin_registry = BUILTIN_IMAGE_MODELS ,
90+ special_handling = image_special_handling ,
91+ )
17292
17393
17494def _install ():
0 commit comments