Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Update vectors package to standardize fallback logic, closes #1221

davidmezzetti0eafc0580f60
diff --git a/src/python/txtai/vectors/dense/litert.py b/src/python/txtai/vectors/dense/litert.py
index 26c06e22..765dd13e 100644
--- a/src/python/txtai/vectors/dense/litert.py
+++ b/src/python/txtai/vectors/dense/litert.py
@@ -51,12 +51,14 @@ class LiteRT(Vectors):
# Check if this is a local path, otherwise download from the HF Hub
model = path if os.path.exists(path) else Download()(path)
- # Also local tokenizer file
- tokenizer = os.path.dirname(path) + "/" + "tokenizer.json"
- tokenizer = tokenizer if os.path.exists(tokenizer) else Download()(tokenizer)
+ # Derive tokenizer file path
+ tokenizer = self.config.get("tokenizer")
+ if not tokenizer:
+ tokenizer = os.path.dirname(path) + "/" + "tokenizer.json"
+ tokenizer = tokenizer if os.path.exists(tokenizer) else Download()(tokenizer)
# Load tokenizer and model
- tokenizer = Tokenizer.from_file(tokenizer)
+ tokenizer = Tokenizer.from_file(tokenizer) if os.path.exists(tokenizer) else Tokenizer.from_pretrained(tokenizer)
model = CompiledModel.from_file(
model,
HardwareAccelerator.GPU | HardwareAccelerator.NPU | HardwareAccelerator.CPU if self.config.get("gpu", True) else HardwareAccelerator.CPU,
diff --git a/src/python/txtai/vectors/dense/m2v.py b/src/python/txtai/vectors/dense/m2v.py
index 20bfaf2d..f13753b6 100644
--- a/src/python/txtai/vectors/dense/m2v.py
+++ b/src/python/txtai/vectors/dense/m2v.py
@@ -41,8 +41,8 @@ class Model2Vec(Vectors):
config = json.load(f)
return config.get("model_type") == "model2vec"
- # Ignore this error - invalid repo or directory
- except DownloadError:
+ # Ignore invalid repo/directory and when HF Hub is not available
+ except (DownloadError, ImportError):
pass
return False
diff --git a/src/python/txtai/vectors/dense/onnx.py b/src/python/txtai/vectors/dense/onnx.py
index d5e8aa63..0487484c 100644
--- a/src/python/txtai/vectors/dense/onnx.py
+++ b/src/python/txtai/vectors/dense/onnx.py
@@ -77,6 +77,7 @@ class ONNX(Vectors):
Tokenizer
"""
+ # Derive tokenizer file path
tokenizer = self.config.get("tokenizer")
if not tokenizer:
tokenizer = os.path.dirname(path) + "/" + "tokenizer.json"
diff --git a/src/python/txtai/vectors/dense/words.py b/src/python/txtai/vectors/dense/words.py
index 0f2bdb7f..55833f0e 100644
--- a/src/python/txtai/vectors/dense/words.py
+++ b/src/python/txtai/vectors/dense/words.py
@@ -97,8 +97,8 @@ class WordVectors(Vectors):
config = json.load(f)
return config.get("model_type") == "staticvectors"
- # Ignore this error - invalid repo or directory
- except DownloadError:
+ # Ignore invalid repo/directory and when HF Hub is not available
+ except (DownloadError, ImportError):
pass
return False