Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit dc506d6

Browse files
Add from_config for abstract distance, search, and store (#366)
* [nightly] Increase version to 0.18.0.dev12 * Change import to use TF keras legacy utils to access serialize and deserialize. * Add try/except to handle updated import path in tf 2.13 * Add from_config to abstract classes to support deserialization. * Fix typing on from_config for search and store --------- Co-authored-by: Github Actions Bot <[email protected]>
1 parent ad4f815 commit dc506d6

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

tensorflow_similarity/distances/distance.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,20 @@ def get_config(self) -> dict[str, Any]:
5454
config = {"name": self.name}
5555

5656
return config
57+
58+
@classmethod
59+
def from_config(cls, config: dict[str, Any]) -> Distance:
60+
"""Build a distance from a config.
61+
62+
Args:
63+
config: A Python dict containing the configuration of the distance.
64+
65+
Returns:
66+
A distance instance.
67+
"""
68+
try:
69+
return cls(**config)
70+
except Exception as e:
71+
raise TypeError(
72+
f"Error when deserializing '{cls.__name__}' using" f"config={config}.\n\nException encountered: {e}"
73+
)

tensorflow_similarity/search/search.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,3 +135,20 @@ def get_config(self) -> dict[str, Any]:
135135
def is_built(self):
136136
"Returns whether or not the index is built and ready for querying." ""
137137
return self.built
138+
139+
@classmethod
140+
def from_config(cls, config: dict[str, Any]) -> Search:
141+
"""Build a search from a config.
142+
143+
Args:
144+
config: A Python dict containing the configuration of the search.
145+
146+
Returns:
147+
A distance instance.
148+
"""
149+
try:
150+
return cls(**config)
151+
except Exception as e:
152+
raise TypeError(
153+
f"Error when deserializing '{cls.__name__}' using" f"config={config}.\n\nException encountered: {e}"
154+
)

tensorflow_similarity/stores/store.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,3 +151,20 @@ def get_config(self) -> dict[str, Any]:
151151
}
152152

153153
return config
154+
155+
@classmethod
156+
def from_config(cls, config: dict[str, Any]) -> Store:
157+
"""Build a store from a config.
158+
159+
Args:
160+
config: A Python dict containing the configuration of the store.
161+
162+
Returns:
163+
A distance instance.
164+
"""
165+
try:
166+
return cls(**config)
167+
except Exception as e:
168+
raise TypeError(
169+
f"Error when deserializing '{cls.__name__}' using" f"config={config}.\n\nException encountered: {e}"
170+
)

0 commit comments

Comments
 (0)