Skip to content

Commit

Permalink
Improve temporary file creation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmezzetti committed Mar 3, 2023
1 parent ee0cc5a commit d758e1f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/python/txtai/cloud/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ def lfstrack(self):
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)
7 changes: 3 additions & 4 deletions test/python/testcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,9 @@ def filedownload(**kwargs):

# 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"]:
Expand Down

0 comments on commit d758e1f

Please sign in to comment.