Skip to content

Commit 579c131

Browse files
committed
Tempfiles in windows have exclusive handles.
1 parent 4662c4f commit 579c131

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

unstructured_inference/inference/layout.py

+11-8
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,8 @@
99
import pdf2image
1010
from PIL import Image, ImageSequence
1111

12-
from unstructured_inference.inference.elements import (
13-
TextRegion,
14-
)
15-
from unstructured_inference.inference.layoutelement import (
16-
LayoutElement,
17-
)
12+
from unstructured_inference.inference.elements import TextRegion
13+
from unstructured_inference.inference.layoutelement import LayoutElement
1814
from unstructured_inference.logger import logger
1915
from unstructured_inference.models.base import get_model
2016
from unstructured_inference.models.unstructuredmodel import (
@@ -327,14 +323,21 @@ def process_data_with_model(
327323
) -> DocumentLayout:
328324
"""Processes pdf file in the form of a file handler (supporting a read method) into a
329325
DocumentLayout by using a model identified by model_name."""
330-
with tempfile.NamedTemporaryFile() as tmp_file:
326+
327+
# Create a named temporary file without automatic deletion
328+
with tempfile.NamedTemporaryFile(delete=False, mode="w+b") as tmp_file:
329+
tmp_filename = tmp_file.name
331330
tmp_file.write(data.read())
332331
tmp_file.flush() # Make sure the file is written out
332+
333+
try:
333334
layout = process_file_with_model(
334-
tmp_file.name,
335+
tmp_filename,
335336
model_name,
336337
**kwargs,
337338
)
339+
finally:
340+
os.remove(tmp_filename)
338341

339342
return layout
340343

0 commit comments

Comments
 (0)