Skip to content

Commit 36f4401

Browse files
committed
num3:test without image and audio
1 parent 0199099 commit 36f4401

File tree

7 files changed

+206
-362
lines changed

7 files changed

+206
-362
lines changed

xinference/model/audio/__init__.py

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -66,35 +66,21 @@ def register_custom_model():
6666

6767

6868
def register_builtin_model():
69-
# Use unified loading function with flatten_model_src + audio-specific defaults
70-
from ..utils import flatten_model_src, load_complete_builtin_models
69+
# Use unified function for audio models
70+
from ..utils import register_builtin_models_unified, flatten_model_src
7171

72-
def convert_audio_with_flatten(model_json):
73-
flattened_list = flatten_model_src(model_json)
74-
if not flattened_list:
75-
return model_json
76-
77-
result = flattened_list[0]
78-
79-
# Add required defaults for audio models
80-
if "multilingual" not in result:
81-
result["multilingual"] = True
82-
if "model_lang" not in result:
83-
result["model_lang"] = ["en", "zh"]
84-
if "version" not in result:
85-
result["version"] = 2
86-
87-
return result
88-
89-
loaded_count = load_complete_builtin_models(
72+
loaded_count = register_builtin_models_unified(
9073
model_type="audio",
91-
builtin_registry=BUILTIN_AUDIO_MODELS,
92-
convert_format_func=convert_audio_with_flatten,
74+
flatten_func=flatten_model_src,
9375
model_class=AudioModelFamilyV2,
76+
builtin_registry=BUILTIN_AUDIO_MODELS,
77+
custom_defaults={
78+
"multilingual": True,
79+
"model_lang": ["en", "zh"],
80+
"version": 2,
81+
}
9482
)
9583

96-
logger.info(f"Successfully loaded {loaded_count} audio models from complete JSON")
97-
9884

9985
def _need_filter(spec: dict):
10086
if (sys.platform != "darwin" or platform.processor() != "arm") and spec.get(

xinference/model/embedding/__init__.py

Lines changed: 22 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -70,92 +70,38 @@ def register_custom_model():
7070

7171

7272
def register_builtin_model():
73-
# Use unified loading function with flatten_quantizations for embedding models
74-
from ..utils import flatten_quantizations, load_complete_builtin_models
73+
# Use unified function for embedding models
74+
from ..utils import register_builtin_models_unified, flatten_quantizations
7575
from .embed_family import BUILTIN_EMBEDDING_MODELS
7676

77-
def convert_embedding_with_quantizations(model_json):
78-
if "model_specs" not in model_json:
79-
return model_json
80-
81-
# Process each model_spec with flatten_quantizations (like builtin embedding loading)
82-
result = model_json.copy()
83-
flattened_specs = []
84-
for spec in result["model_specs"]:
85-
if "model_src" in spec:
86-
flattened_specs.extend(flatten_quantizations(spec))
87-
else:
88-
flattened_specs.append(spec)
89-
result["model_specs"] = flattened_specs
90-
91-
return result
92-
93-
loaded_count = load_complete_builtin_models(
94-
model_type="embedding",
95-
builtin_registry={}, # Temporarily use empty dict, we handle it manually
96-
convert_format_func=convert_embedding_with_quantizations,
97-
model_class=EmbeddingModelFamilyV2,
98-
)
99-
100-
# Manually handle embedding's special registration logic
101-
if loaded_count > 0:
102-
from ...constants import XINFERENCE_MODEL_DIR
77+
def embedding_special_handling(registry, model_type):
78+
"""Handle embedding's special registration logic"""
10379
from ..custom import RegistryManager
10480

105-
registry = RegistryManager.get_registry("embedding")
81+
registry_mgr = RegistryManager.get_registry("embedding")
10682
existing_model_names = {
107-
spec.model_name for spec in registry.get_custom_models()
83+
spec.model_name for spec in registry_mgr.get_custom_models()
10884
}
10985

110-
builtin_embedding_dir = os.path.join(
111-
XINFERENCE_MODEL_DIR, "v2", "builtin", "embedding"
112-
)
113-
complete_json_path = os.path.join(
114-
builtin_embedding_dir, "embedding_models.json"
115-
)
116-
117-
if os.path.exists(complete_json_path):
118-
with codecs.open(complete_json_path, encoding="utf-8") as fd:
119-
model_data = json.load(fd)
120-
121-
models_to_register = []
122-
if isinstance(model_data, list):
123-
models_to_register = model_data
124-
elif isinstance(model_data, dict):
125-
if "model_name" in model_data:
126-
models_to_register = [model_data]
127-
else:
128-
for key, value in model_data.items():
129-
if isinstance(value, dict) and "model_name" in value:
130-
models_to_register.append(value)
131-
132-
for model_data in models_to_register:
86+
for model_name, model_family in BUILTIN_EMBEDDING_MODELS.items():
87+
if model_family.model_name not in existing_model_names:
13388
try:
134-
from ..utils import flatten_quantizations
135-
136-
converted_data = model_data.copy()
137-
if "model_specs" in converted_data:
138-
flattened_specs = []
139-
for spec in converted_data["model_specs"]:
140-
if "model_src" in spec:
141-
flattened_specs.extend(flatten_quantizations(spec))
142-
else:
143-
flattened_specs.append(spec)
144-
converted_data["model_specs"] = flattened_specs
145-
builtin_embedding_family = EmbeddingModelFamilyV2.parse_obj(
146-
converted_data
147-
)
148-
149-
if builtin_embedding_family.model_name not in existing_model_names:
150-
register_embedding(builtin_embedding_family, persist=False)
151-
existing_model_names.add(builtin_embedding_family.model_name)
89+
register_embedding(model_family, persist=False)
90+
existing_model_names.add(model_family.model_name)
91+
except ValueError as e:
92+
# 捕获冲突错误并输出警告,而不是抛出异常
93+
import warnings
94+
warnings.warn(str(e))
15295
except Exception as e:
153-
warnings.warn(
154-
f"Error parsing model {model_data.get('model_name', 'Unknown')}: {e}"
155-
)
96+
import warnings
97+
warnings.warn(f"Error registering embedding model {model_family.model_name}: {e}")
15698

157-
logger.info(
158-
f"Successfully loaded {loaded_count} embedding models from complete JSON"
99+
loaded_count = register_builtin_models_unified(
100+
model_type="embedding",
101+
flatten_func=flatten_quantizations,
102+
model_class=EmbeddingModelFamilyV2,
103+
builtin_registry=BUILTIN_EMBEDDING_MODELS,
104+
special_handling=embedding_special_handling,
159105
)
160106

161107

xinference/model/image/__init__.py

Lines changed: 27 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -62,113 +62,33 @@ def register_custom_model():
6262

6363

6464
def 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

17494
def _install():

0 commit comments

Comments
 (0)