Skip to content

Commit

Permalink
fix: Windows 11 failing to auto-delete tmp file (#1260)
Browse files Browse the repository at this point in the history
  • Loading branch information
pabloogc authored Nov 17, 2023
1 parent 4197ada commit 0d52002
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions private_gpt/server/ingest/ingest_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,17 @@ def ingest(self, file_name: str, file_data: AnyStr | Path) -> list[IngestedDoc]:
else:
# llama-index mainly supports reading from files, so
# we have to create a tmp file to read for it to work
with tempfile.NamedTemporaryFile() as tmp:
path_to_tmp = Path(tmp.name)
if isinstance(file_data, bytes):
path_to_tmp.write_bytes(file_data)
else:
path_to_tmp.write_text(str(file_data))
documents = reader.load_data(path_to_tmp)
# delete=False to avoid a Windows 11 permission error.
with tempfile.NamedTemporaryFile(delete=False) as tmp:
try:
path_to_tmp = Path(tmp.name)
if isinstance(file_data, bytes):
path_to_tmp.write_bytes(file_data)
else:
path_to_tmp.write_text(str(file_data))
documents = reader.load_data(path_to_tmp)
finally:
path_to_tmp.unlink()
logger.info(
"Transformed file=%s into count=%s documents", file_name, len(documents)
)
Expand Down

0 comments on commit 0d52002

Please sign in to comment.