unstructured 对于大型PDF文件的错误/bug

kyks70gy  于 2个月前  发布在  其他
关注(0)|答案(1)|浏览(88)

Bug描述:

处理大型PDF文件时出现错误,当文件大小约为5000页41毫克时,会出现以下错误。

2024-07-01 12:02:22 File "/app/doc_processing.py", line 113, in __extractPdfElements"
2024-07-01 12:02:22 return partition_pdf(
2024-07-01 12:02:22 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2024-07-01 12:02:22 File "/usr/lib/python3.11/site-packages/unstructured/documents/elements.py", line 593, in wrapper"
2024-07-01 12:02:22 elements = func(*args, **kwargs)"
2024-07-01 12:02:22 ^^^^^^^^^^^^^^^^^^^^^^->"
2024-07-01 12:02:22 File "/usr/lib/python3.11/site-packages/unstructured/file_utils/filetype.py", line 626, in wrapper"
2024-07-01 12:02:22 elements = func(*args, **kwargs)"
2024-07-01 12:02:22 ^^^^^^^^^->"

错误信息:

在处理大型PDF文件时,出现了截断的图像文件错误。具体来说,当尝试加载一个大小约为5000页、41毫克的PDF文件时,出现了以下错误。

OSError: image file is truncated (2327 bytes not processed)

此外,还发生了另一个异常。

Traceback (most recent call last):
File "/usr/lib/python3.11/site-packages/uvicorn/protocols/http/httptools_impl.py", line 411, in run_asgi"
result = await app( # type: ignore[func-returns-value]"

要解决这个问题,您需要将OSError对象转换为可以序列化为JSON的对象。您可以通过在引发异常时添加一个自定义消息来实现这一点。以下是修改后的代码:

import json
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
from starlette.exceptions import HTTPException
from starlette.status import HTTP_400_BAD_REQUEST

app = FastAPI()

@app.exception_handler(OSError)
async def handle_oserror(request: Request, exc: OSError):
    return JSONResponse(content={"detail": str(exc)}, status_code=HTTP_400_BAD_REQUEST)

@app.get("/")
async def read_root():
    try:
        # Your large pdf file processing code here
        pass
    except OSError as e:
        raise HTTPException(status_code=400, detail="An error occurred while processing the PDF file") from e

这样,当OSError异常发生时,它将被捕获并返回一个包含自定义错误消息的JSON响应。

qgzx9mmu

qgzx9mmu1#

在stackoverflow上的

from PIL import ImageFile
ImageFile.LOAD_TRUNCATED_IMAGES = True

答案中添加的代码片段是否有帮助?我遇到了类似的问题,这个解决方案解决了它,但自从使用这个解决方案以来,我还没有能够重现这个问题。

相关问题