Spaces:
Sleeping
Sleeping
Download test/pipeline/services/model_loader_test.py from yetrun/general-deep-learning: direct link, hf CLI and curl.
- Browser
- Download file 2.1 kB
-
https://huggingface.co/spaces/yetrun/general-deep-learning/resolve/main/test/pipeline/services/model_loader_test.py
- Command line
-
hf download hf://spaces/yetrun/general-deep-learning/test/pipeline/services/model_loader_test.py
-
curl -L -o model_loader_test.py https://huggingface.co/spaces/yetrun/general-deep-learning/resolve/main/test/pipeline/services/model_loader_test.py
2.1 kB
| import pathlib | |
| import keras | |
| import pytest | |
| from deep_learning.models.spec import ModelArtifact | |
| from deep_learning.pipeline.services.model_loader import load_inference_artifact | |
| def _create_model() -> keras.Model: | |
| inputs = keras.Input(shape=(3,)) | |
| outputs = keras.layers.Dense(1)(inputs) | |
| return keras.Model(inputs, outputs, name="demo_model") | |
| def test_load_deployment_inference_artifact_loads_keras_model(tmp_path): | |
| """验证部署加载工具会读取完整 .keras 模型并返回包装后的推理产物。""" | |
| model_path = tmp_path / "model_epoch_001.keras" | |
| _create_model().save(model_path) | |
| resource = object() | |
| def load_model_artifact(path): | |
| assert path == model_path | |
| return ModelArtifact(model=keras.models.load_model(str(path))) | |
| artifact, loaded_resource = load_inference_artifact( | |
| checkpoint_rule={ | |
| "path": model_path | |
| }, | |
| load_inference_artifact_fn=load_model_artifact, | |
| resource_factory=lambda: resource | |
| ) | |
| assert artifact.model.name == "demo_model" | |
| assert loaded_resource is resource | |
| def test_load_deployment_inference_artifact_requires_existing_model(tmp_path): | |
| """验证找不到部署模型时会报出查找信息。""" | |
| with pytest.raises(FileNotFoundError, match="未找到任何部署模型文件"): | |
| load_inference_artifact( | |
| checkpoint_rule={ | |
| "dirs": [tmp_path] | |
| }, | |
| load_inference_artifact_fn=lambda path: ModelArtifact(model=object()) | |
| ) | |
| def test_load_deployment_inference_artifact_rejects_weights_file(tmp_path): | |
| """验证部署推理只接受完整 .keras 模型。""" | |
| weights_path = tmp_path / "model_epoch_001.weights.h5" | |
| weights_path.write_text("stub", encoding="utf-8") | |
| with pytest.raises(ValueError, match="部署推理只支持完整 .keras 模型"): | |
| load_inference_artifact( | |
| checkpoint_rule={ | |
| "path": pathlib.Path(weights_path) | |
| }, | |
| load_inference_artifact_fn=lambda path: ModelArtifact(model=object()) | |
| ) | |