💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows
Python agentsaiai-agentsembeddingsinformation-retrievallanguage-modellarge-language-modelsllmnlppythonragretrieval-augmented-generationsearchsearch-enginesemantic-searchsentence-embeddingstransformerstxtaivector-databasevector-search
Update translation pipeline to use hf_hub_download for language detection, closes #803
davidmezzetti4a268041b71a
diff --git a/.github/workflows/build.yml b/.github/workflows/build.ymlindex 72dd5fc7..7dc9b5b7 100644--- a/.github/workflows/build.yml+++ b/.github/workflows/build.yml@@ -47,7 +47,7 @@ jobs:- name: Buildrun: |pip install -U wheel- pip install .[all,dev] fasttext==0.9.2 pillow==10.4.0 huggingface-hub==0.25.2+ pip install .[all,dev] fasttext==0.9.2 pillow==10.4.0python -c "import nltk; nltk.download(['punkt', 'punkt_tab', 'averaged_perceptron_tagger_eng'])"python --versionmake data coveragediff --git a/src/python/txtai/pipeline/text/translation.py b/src/python/txtai/pipeline/text/translation.pyindex 1b68b484..d15e0a58 100644--- a/src/python/txtai/pipeline/text/translation.py+++ b/src/python/txtai/pipeline/text/translation.py@@ -2,6 +2,8 @@Translation module"""+import os+# Conditional importtry:import fasttext@@ -10,7 +12,7 @@ try:except ImportError:FASTTEXT = False-from huggingface_hub import cached_download+from huggingface_hub import hf_hub_downloadfrom huggingface_hub.hf_api import HfApifrom transformers import AutoModelForSeq2SeqLM, AutoTokenizer@@ -24,7 +26,7 @@ class Translation(HFModel):"""# Default language detection model- DEFAULT_LANG_DETECT = "https://dl.fbaipublicfiles.com/fasttext/supervised-models/lid.176.ftz"+ DEFAULT_LANG_DETECT = "julien-c/fasttext-language-id/lid.176.ftz"def __init__(self, path=None, quantize=False, gpu=True, batch=64, langdetect=None, findmodels=True):"""@@ -155,7 +157,7 @@ class Translation(HFModel):path = self.langdetect if self.langdetect else Translation.DEFAULT_LANG_DETECT# Load language detection model- path = cached_download(path, legacy_cache_layout=True)+ path = path if os.path.exists(path) else self.download(path)self.detector = fasttext.load_model(path)# Transform texts to format expected by language detection model@@ -163,6 +165,26 @@ class Translation(HFModel):return [x[0].split("__")[-1] for x in self.detector.predict(texts)[0]]+ def download(self, path):+ """+ Downloads path from the Hugging Face Hub.++ Args:+ path: full model path++ Returns:+ local cached model path+ """++ # Split into parts+ parts = path.split("/")++ # Calculate repo id split+ repo = 2 if len(parts) > 2 else 1++ # Download and cache file+ return hf_hub_download(repo_id="/".join(parts[:repo]), filename="/".join(parts[repo:]))+def translate(self, texts, source, target, showmodels=False):"""Translates text from source to target language.