Skip to content

armgit / txtai

Public Imported from GitHub
0

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

Improve temporary file creation logic

davidmezzettid758e1fa761e
diff --git a/src/python/txtai/cloud/hub.py b/src/python/txtai/cloud/hub.py
index ea817f88..ba0dea06 100644
--- a/src/python/txtai/cloud/hub.py
+++ b/src/python/txtai/cloud/hub.py
@@ -88,10 +88,14 @@ class HuggingFaceHub(Cloud):
content += "embeddings filter=lfs diff=lfs merge=lfs -text\n"
# pylint: disable=R1732
- tmp = tempfile.NamedTemporaryFile()
- with open(tmp.name, "w", encoding="utf-8") as f:
- f.write(content)
+ with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
+ tmp.write(content)
+ attributes = tmp.name
+ # Upload file
huggingface_hub.upload_file(
- repo_id=self.config["container"], token=self.config.get("token"), path_or_fileobj=tmp.name, path_in_repo=os.path.basename(path)
+ repo_id=self.config["container"], token=self.config.get("token"), path_or_fileobj=attributes, path_in_repo=os.path.basename(path)
)
+
+ # Remove temporary file
+ os.remove(attributes)
diff --git a/test/python/testcloud.py b/test/python/testcloud.py
index 2c182ebc..e748c2c2 100644
--- a/test/python/testcloud.py
+++ b/test/python/testcloud.py
@@ -125,10 +125,9 @@ class TestCloud(unittest.TestCase):
# Initialize attributes file
# pylint: disable=R1732
- tmp = tempfile.NamedTemporaryFile()
- with open(tmp.name, "w", encoding="utf-8") as f:
- f.write("*.bin filter=lfs diff=lfs merge=lfs -text\n")
- attributes = tmp.name
+ with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp:
+ tmp.write("*.bin filter=lfs diff=lfs merge=lfs -text\n")
+ attributes = tmp.name
# Run tests with uncompressed and compressed index
for path in [f"cloud.{provider}", f"cloud.{provider}.tar.gz"]: