Skip to content

Commit

Permalink
Tempfiles in windows have exclusive handles.
Browse files Browse the repository at this point in the history
  • Loading branch information
Falven committed Jul 9, 2024
1 parent 4662c4f commit 579c131
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions unstructured_inference/inference/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,8 @@
import pdf2image
from PIL import Image, ImageSequence

from unstructured_inference.inference.elements import (
TextRegion,
)
from unstructured_inference.inference.layoutelement import (
LayoutElement,
)
from unstructured_inference.inference.elements import TextRegion
from unstructured_inference.inference.layoutelement import LayoutElement
from unstructured_inference.logger import logger
from unstructured_inference.models.base import get_model
from unstructured_inference.models.unstructuredmodel import (
Expand Down Expand Up @@ -327,14 +323,21 @@ def process_data_with_model(
) -> DocumentLayout:
"""Processes pdf file in the form of a file handler (supporting a read method) into a
DocumentLayout by using a model identified by model_name."""
with tempfile.NamedTemporaryFile() as tmp_file:

# Create a named temporary file without automatic deletion
with tempfile.NamedTemporaryFile(delete=False, mode="w+b") as tmp_file:
tmp_filename = tmp_file.name
tmp_file.write(data.read())
tmp_file.flush() # Make sure the file is written out

try:
layout = process_file_with_model(
tmp_file.name,
tmp_filename,
model_name,
**kwargs,
)
finally:
os.remove(tmp_filename)

return layout

Expand Down

0 comments on commit 579c131

Please sign in to comment.