bentoml / BentoML

The easiest way to serve AI apps and models - Build reliable Inference APIs, LLM apps, Multi-model chains, RAG service, and much more!

Home Page:https://bentoml.com

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

bug: Pillow cannot identify image file.

decadance-dance opened this issue · comments

Describe the bug

When I upload PNG images (via Python client or Swagger UI) I get this error:

File "/home/dmytrodronov/miniconda3/envs/ocr2/lib/python3.10/site-packages/pydantic/main.py", line 503, in model_validate
    return cls.__pydantic_validator__.validate_python(
File "/home/dmytrodronov/miniconda3/envs/ocr2/lib/python3.10/site-packages/_bentoml_sdk/validators.py", line 70, in decode
    return PILImage.open(obj.file, formats=formats)
File "/home/dmytrodronov/miniconda3/envs/ocr2/lib/python3.10/site-packages/PIL/Image.py", line 3309, in open
    raise UnidentifiedImageError(msg)
PIL.UnidentifiedImageError: cannot identify image file <tempfile.SpooledTemporaryFile object at 0x7feb53719db0>

To reproduce

# example.py (service) 
from __future__ import annotations
import bentoml
from PIL.Image import Image as PILImage


@bentoml.service(
    resources={"cpu": "4"},
    traffic={"timeout": 10},
)
class TestService:
    def __init__(self) -> None:
        ...

    @bentoml.api(batchable=True,
        batch_dim=(0, 0),
        max_batch_size=16,
        max_latency_ms=1000)
    def predict(self, images: list[PILImage]) -> int:
        return 0
# client.py
from httpx import request


files = [('images', open('./2.png', 'rb'))]
response = request('POST', 'http://localhost:3000/predict', files=files)
print(response)
  1. Run the bentoml bentoml serve example:TestService
  2. Run the client.py.

Image:
2

Expected behavior

I expect that the endpoint will work no matter what image file extension is used and that the request will end with a 200 status.

Environment

Environment variable

BENTOML_DEBUG=''
BENTOML_QUIET=''
BENTOML_BUNDLE_LOCAL_BUILD=''
BENTOML_DO_NOT_TRACK=''
BENTOML_CONFIG=''
BENTOML_CONFIG_OPTIONS=''
BENTOML_PORT=''
BENTOML_HOST=''
BENTOML_API_WORKERS=''

System information

bentoml: 1.2.9
python: 3.10.13
platform: Linux-5.4.0-169-generic-x86_64-with-glibc2.31
uid_gid: 1003:1003
conda: 23.11.0
in_conda_env: True

conda_packages
name: ocr2
channels:
  - conda-forge
  - defaults
dependencies:
  - _libgcc_mutex=0.1=conda_forge
  - _openmp_mutex=4.5=2_gnu
  - asttokens=2.4.1=pyhd8ed1ab_0
  - brotli-bin=1.0.9=h5eee18b_7
  - bzip2=1.0.8=h7b6447c_0
  - ca-certificates=2023.12.12=h06a4308_0
  - cairo=1.16.0=hb05425b_5
  - cffi=1.16.0=py310h5eee18b_0
  - comm=0.2.1=pyhd8ed1ab_0
  - debugpy=1.6.7=py310h6a678d5_0
  - decorator=5.1.1=pyhd8ed1ab_0
  - exceptiongroup=1.2.0=pyhd8ed1ab_2
  - executing=2.0.1=pyhd8ed1ab_0
  - expat=2.5.0=h6a678d5_0
  - fontconfig=2.14.1=h4c34cd2_2
  - freetype=2.12.1=h4a9f257_0
  - fribidi=1.0.10=h7b6447c_0
  - giflib=5.2.1=h5eee18b_3
  - glib=2.69.1=he621ea3_2
  - graphite2=1.3.14=h295c915_1
  - harfbuzz=4.3.0=hf52aaf7_2
  - html5lib=1.1=pyhd3eb1b0_0
  - icu=73.1=h6a678d5_0
  - importlib_metadata=7.0.1=hd8ed1ab_0
  - ipykernel=6.29.0=pyhd33586a_0
  - ipython=8.20.0=pyh707e725_0
  - jedi=0.19.1=pyhd8ed1ab_0
  - jpeg=9e=h5eee18b_1
  - jupyter_client=8.6.0=pyhd8ed1ab_0
  - jupyter_core=5.7.1=py310hff52083_0
  - lcms2=2.12=h3be6417_0
  - ld_impl_linux-64=2.38=h1181459_1
  - lerc=3.0=h295c915_0
  - libbrotlicommon=1.0.9=h5eee18b_7
  - libbrotlidec=1.0.9=h5eee18b_7
  - libbrotlienc=1.0.9=h5eee18b_7
  - libdeflate=1.17=h5eee18b_1
  - libffi=3.4.4=h6a678d5_0
  - libgcc-ng=13.2.0=h807b86a_4
  - libgomp=13.2.0=h807b86a_4
  - libpng=1.6.39=h5eee18b_0
  - libsodium=1.0.18=h36c2ea0_1
  - libstdcxx-ng=11.2.0=h1234567_1
  - libtiff=4.5.1=h6a678d5_0
  - libuuid=1.41.5=h5eee18b_0
  - libwebp=1.3.2=h11a3e52_0
  - libwebp-base=1.3.2=h5eee18b_0
  - libxcb=1.15=h7f8727e_0
  - libxml2=2.10.4=hf1b16e4_1
  - lz4-c=1.9.4=h6a678d5_0
  - matplotlib-inline=0.1.6=pyhd8ed1ab_0
  - munkres=1.1.4=py_0
  - ncurses=6.4=h6a678d5_0
  - nest-asyncio=1.6.0=pyhd8ed1ab_0
  - openjpeg=2.4.0=h3ad879b_0
  - openssl=3.2.0=hd590300_1
  - packaging=23.2=pyhd8ed1ab_0
  - pango=1.50.7=h05da053_0
  - parso=0.8.3=pyhd8ed1ab_0
  - pcre=8.45=h295c915_0
  - pexpect=4.9.0=pyhd8ed1ab_0
  - pickleshare=0.7.5=py_1003
  - pip=23.3.1=py310h06a4308_0
  - pixman=0.40.0=h7f8727e_1
  - platformdirs=4.1.0=pyhd8ed1ab_0
  - prompt-toolkit=3.0.42=pyha770c72_0
  - psutil=5.9.8=py310h2372a71_0
  - ptyprocess=0.7.0=pyhd3deb0d_0
  - pure_eval=0.2.2=pyhd8ed1ab_0
  - pycparser=2.21=pyhd3eb1b0_0
  - pydyf=0.8.0=pyhd8ed1ab_0
  - pygments=2.17.2=pyhd8ed1ab_0
  - pyphen=0.14.0=py310h06a4308_0
  - python=3.10.13=h955ad1f_0
  - python-dateutil=2.8.2=pyhd8ed1ab_0
  - python_abi=3.10=2_cp310
  - pyzmq=25.1.2=py310h6a678d5_0
  - readline=8.2=h5eee18b_0
  - setuptools=68.2.2=py310h06a4308_0
  - six=1.16.0=pyhd3eb1b0_1
  - sqlite=3.41.2=h5eee18b_0
  - stack_data=0.6.2=pyhd8ed1ab_0
  - tinycss2=1.2.1=py310h06a4308_0
  - tk=8.6.12=h1ccaba5_0
  - traitlets=5.14.1=pyhd8ed1ab_0
  - typing_extensions=4.9.0=pyha770c72_0
  - wcwidth=0.2.13=pyhd8ed1ab_0
  - weasyprint=60.2=pyhd8ed1ab_0
  - wheel=0.41.2=py310h06a4308_0
  - xz=5.4.5=h5eee18b_0
  - zeromq=4.3.5=h6a678d5_0
  - zipp=3.17.0=pyhd8ed1ab_0
  - zlib=1.2.13=h5eee18b_0
  - zstd=1.5.5=hc292b87_0
  - pip:
      - aiohttp==3.9.3
      - aiosignal==1.3.1
      - altair==5.2.0
      - annotated-types==0.6.0
      - anyascii==0.3.2
      - anyio==4.2.0
      - appdirs==1.4.4
      - asgiref==3.8.1
      - async-timeout==4.0.3
      - attrs==23.2.0
      - bentoml==1.2.9
      - blinker==1.7.0
      - brotli==1.1.0
      - build==1.1.1
      - cachetools==5.3.2
      - cattrs==23.1.2
      - certifi==2023.11.17
      - charset-normalizer==3.3.2
      - circus==0.18.0
      - click==8.1.7
      - click-option-group==0.5.6
      - cloudpickle==3.0.0
      - coloredlogs==15.0.1
      - contextlib2==21.6.0
      - contourpy==1.2.0
      - cssselect2==0.7.0
      - cycler==0.12.1
      - deepmerge==1.1.1
      - defusedxml==0.7.1
      - deprecated==1.2.14
      - deskew==1.5.1
      - docx2txt==0.8
      - fastapi==0.110.0
      - filelock==3.13.1
      - flatbuffers==23.5.26
      - fonttools==4.47.2
      - frozenlist==1.4.1
      - fs==2.4.16
      - fsspec==2023.12.2
      - gitdb==4.0.11
      - gitpython==3.1.41
      - h11==0.14.0
      - h5py==3.10.0
      - httpcore==1.0.2
      - httpx==0.26.0
      - huggingface-hub==0.20.3
      - humanfriendly==10.0
      - idna==3.6
      - imageio==2.33.1
      - importlib-metadata==6.11.0
      - imutils==0.5.4
      - inflection==0.5.1
      - jinja2==3.1.3
      - joblib==1.3.2
      - jsonschema==4.21.1
      - jsonschema-specifications==2023.12.1
      - kiwisolver==1.4.5
      - langdetect==1.0.9
      - lazy-loader==0.3
      - lightning-utilities==0.10.1
      - lxml==5.1.0
      - markdown-it-py==3.0.0
      - markupsafe==2.1.4
      - matplotlib==3.8.2
      - mdurl==0.1.2
      - mplcursors==0.5.3
      - mpmath==1.3.0
      - multidict==6.0.5
      - networkx==3.2.1
      - numpy==1.26.3
      - nvidia-cublas-cu12==12.1.3.1
      - nvidia-cuda-cupti-cu12==12.1.105
      - nvidia-cuda-nvrtc-cu12==12.1.105
      - nvidia-cuda-runtime-cu12==12.1.105
      - nvidia-cudnn-cu12==8.9.2.26
      - nvidia-cufft-cu12==11.0.2.54
      - nvidia-curand-cu12==10.3.2.106
      - nvidia-cusolver-cu12==11.4.5.107
      - nvidia-cusparse-cu12==12.1.0.106
      - nvidia-ml-py==11.525.150
      - nvidia-nccl-cu12==2.18.1
      - nvidia-nvjitlink-cu12==12.3.101
      - nvidia-nvtx-cu12==12.1.105
      - onnx==1.15.0
      - onnxruntime==1.17.0
      - opencv-python==4.9.0.80
      - opentelemetry-api==1.20.0
      - opentelemetry-instrumentation==0.41b0
      - opentelemetry-instrumentation-aiohttp-client==0.41b0
      - opentelemetry-instrumentation-asgi==0.41b0
      - opentelemetry-sdk==1.20.0
      - opentelemetry-semantic-conventions==0.41b0
      - opentelemetry-util-http==0.41b0
      - orjson==3.9.13
      - pandas==2.2.0
      - pathspec==0.12.1
      - pillow==10.2.0
      - pip-requirements-parser==32.0.1
      - pip-tools==7.4.1
      - prettytable==3.9.0
      - prometheus-client==0.20.0
      - protobuf==4.25.2
      - pyarrow==15.0.0
      - pyclipper==1.3.0.post5
      - pydantic==2.5.3
      - pydantic-core==2.14.6
      - pydeck==0.8.1b0
      - pympler==1.0.1
      - pyparsing==3.1.1
      - pypdfium2==4.26.0
      - pyproject-hooks==1.0.0
      - python-doctr==0.9.0a0
      - python-docx==1.1.0
      - python-json-logger==2.0.7
      - python-multipart==0.0.6
      - pytz==2023.3.post1
      - pytz-deprecation-shim==0.1.0.post0
      - pyyaml==6.0.1
      - rapidfuzz==3.6.1
      - referencing==0.32.1
      - regex==2023.12.25
      - requests==2.31.0
      - rich==13.7.0
      - rpds-py==0.17.1
      - safetensors==0.4.2
      - schema==0.7.5
      - scikit-image==0.22.0
      - scikit-learn==1.4.0
      - scipy==1.12.0
      - shapely==2.0.2
      - simple-di==0.1.5
      - smmap==5.0.1
      - sniffio==1.3.0
      - starlette==0.36.3
      - streamlit==1.25.0
      - sympy==1.12
      - tabulate==0.9.0
      - tenacity==8.2.3
      - threadpoolctl==3.2.0
      - tifffile==2024.1.30
      - tokenizers==0.15.2
      - toml==0.10.2
      - tomli==2.0.1
      - toolz==0.12.1
      - torch==2.1.2
      - torchmetrics==1.3.2
      - torchvision==0.16.2
      - tornado==6.4
      - tqdm==4.66.1
      - transformers==4.39.1
      - triton==2.1.0
      - tzdata==2023.4
      - tzlocal==4.3.1
      - unidecode==1.3.8
      - urllib3==2.1.0
      - uvicorn==0.27.0.post1
      - validators==0.22.0
      - wand==0.6.13
      - watchdog==3.0.0
      - watchfiles==0.21.0
      - webencodings==0.5.1
      - wrapt==1.16.0
      - yarl==1.9.4
      - zopfli==0.2.3
prefix: /home/dmytrodronov/miniconda3/envs/ocr2
pip_packages
aiohttp==3.9.3
aiosignal==1.3.1
alabaster==0.7.16
altair==5.2.0
annotated-types==0.6.0
anyascii==0.3.2
anyio==4.2.0
appdirs==1.4.4
asgiref==3.8.1
asttokens @ file:///home/conda/feedstock_root/build_artifacts/asttokens_1698341106958/work
async-timeout==4.0.3
attrs==23.2.0
Babel==2.14.0
bentoml==1.2.9
blinker==1.7.0
Brotli==1.1.0
build==1.1.1
cachetools==5.3.2
cattrs==23.1.2
certifi==2023.11.17
cffi @ file:///croot/cffi_1700254295673/work
charset-normalizer==3.3.2
circus==0.18.0
click==8.1.7
click-option-group==0.5.6
cloudpickle==3.0.0
coloredlogs==15.0.1
comm @ file:///home/conda/feedstock_root/build_artifacts/comm_1704278392174/work
contextlib2==21.6.0
contourpy==1.2.0
cssselect2==0.2.1
cycler==0.12.1
debugpy @ file:///croot/debugpy_1690905042057/work
decorator @ file:///home/conda/feedstock_root/build_artifacts/decorator_1641555617451/work
deepmerge==1.1.1
defusedxml==0.7.1
Deprecated==1.2.14
deskew==1.5.1
docutils==0.20.1
docx2txt==0.8
exceptiongroup==1.2.0
executing @ file:///home/conda/feedstock_root/build_artifacts/executing_1698579936712/work
fastapi==0.110.0
filelock==3.13.1
flatbuffers==23.5.26
fonttools==4.25.0
frozenlist==1.4.1
fs==2.4.16
fsspec==2023.12.2
gitdb==4.0.11
GitPython==3.1.41
h11==0.14.0
h5py==3.10.0
html5lib @ file:///Users/ktietz/demo/mc3/conda-bld/html5lib_1629144453894/work
httpcore==1.0.2
httpx==0.26.0
huggingface-hub==0.20.3
humanfriendly==10.0
idna==3.6
imageio==2.33.1
imagesize==1.4.1
importlib-metadata==6.11.0
imutils==0.5.4
inflection==0.5.1
ipykernel @ file:///home/conda/feedstock_root/build_artifacts/ipykernel_1705417941265/work
ipython @ file:///home/conda/feedstock_root/build_artifacts/ipython_1704718870316/work
jedi @ file:///home/conda/feedstock_root/build_artifacts/jedi_1696326070614/work
Jinja2==3.1.3
joblib==1.3.2
jsonschema==4.21.1
jsonschema-specifications==2023.12.1
jupyter_client @ file:///home/conda/feedstock_root/build_artifacts/jupyter_client_1699283905679/work
jupyter_core @ file:///home/conda/feedstock_root/build_artifacts/jupyter_core_1704727030956/work
kiwisolver==1.4.5
langdetect==1.0.9
lazy_loader==0.3
lightning-utilities==0.10.1
lxml==5.1.0
markdown-it-py==3.0.0
MarkupSafe==2.1.4
matplotlib==3.8.2
matplotlib-inline @ file:///home/conda/feedstock_root/build_artifacts/matplotlib-inline_1660814786464/work
mdurl==0.1.2
meson==1.3.2
mplcursors==0.5.3
mpmath==1.3.0
multidict==6.0.5
munkres==1.1.4
nest_asyncio @ file:///home/conda/feedstock_root/build_artifacts/nest-asyncio_1705850609492/work
networkx==3.2.1
numpy==1.26.3
nvidia-cublas-cu12==12.1.3.1
nvidia-cuda-cupti-cu12==12.1.105
nvidia-cuda-nvrtc-cu12==12.1.105
nvidia-cuda-runtime-cu12==12.1.105
nvidia-cudnn-cu12==8.9.2.26
nvidia-cufft-cu12==11.0.2.54
nvidia-curand-cu12==10.3.2.106
nvidia-cusolver-cu12==11.4.5.107
nvidia-cusparse-cu12==12.1.0.106
nvidia-ml-py==11.525.150
nvidia-nccl-cu12==2.18.1
nvidia-nvjitlink-cu12==12.3.101
nvidia-nvtx-cu12==12.1.105
onnx==1.15.0
onnxruntime==1.17.0
opencv-python==4.9.0.80
opentelemetry-api==1.20.0
opentelemetry-instrumentation==0.41b0
opentelemetry-instrumentation-aiohttp-client==0.41b0
opentelemetry-instrumentation-asgi==0.41b0
opentelemetry-sdk==1.20.0
opentelemetry-semantic-conventions==0.41b0
opentelemetry-util-http==0.41b0
orjson==3.9.13
packaging @ file:///home/conda/feedstock_root/build_artifacts/packaging_1696202382185/work
pandas==2.2.0
parso @ file:///home/conda/feedstock_root/build_artifacts/parso_1638334955874/work
pathspec==0.12.1
pexpect @ file:///home/conda/feedstock_root/build_artifacts/pexpect_1706113125309/work
pickleshare @ file:///home/conda/feedstock_root/build_artifacts/pickleshare_1602536217715/work
pillow==10.2.0
pip-requirements-parser==32.0.1
pip-tools==7.4.1
platformdirs @ file:///home/conda/feedstock_root/build_artifacts/platformdirs_1701708255999/work
prettytable==3.9.0
prometheus_client==0.20.0
prompt-toolkit @ file:///home/conda/feedstock_root/build_artifacts/prompt-toolkit_1702399386289/work
protobuf==4.25.2
psutil @ file:///home/conda/feedstock_root/build_artifacts/psutil_1705722392846/work
ptyprocess @ file:///home/conda/feedstock_root/build_artifacts/ptyprocess_1609419310487/work/dist/ptyprocess-0.7.0-py2.py3-none-any.whl
pure-eval @ file:///home/conda/feedstock_root/build_artifacts/pure_eval_1642875951954/work
pyarrow==15.0.0
pyclipper==1.3.0.post5
pycparser @ file:///tmp/build/80754af9/pycparser_1636541352034/work
pydantic==2.5.3
pydantic_core==2.14.6
pydeck==0.8.1b0
pydyf @ file:///home/conda/feedstock_root/build_artifacts/pydyf_1695654182853/work
Pygments==2.17.2
Pympler==1.0.1
pyparsing==3.1.1
pypdfium2==4.26.0
pyphen @ file:///croot/pyphen_1696417729457/work
pyproject_hooks==1.0.0
python-dateutil @ file:///home/conda/feedstock_root/build_artifacts/python-dateutil_1626286286081/work
-e git+https://github.com/mindee/doctr.git@bb8e32d268fdf2fe0d2804f6b8076fa91d19b8dc#egg=python_doctr
python-docx==1.1.0
python-json-logger==2.0.7
python-multipart==0.0.6
pytz==2023.3.post1
pytz-deprecation-shim==0.1.0.post0
PyYAML==6.0.1
pyzmq @ file:///croot/pyzmq_1705605076900/work
rapidfuzz==3.6.1
referencing==0.32.1
regex==2023.12.25
requests==2.31.0
rich==13.7.0
rpds-py==0.17.1
safetensors==0.4.2
schema==0.7.5
scikit-image==0.22.0
scikit-learn==1.4.0
scipy==1.12.0
shapely==2.0.2
simple-di==0.1.5
six @ file:///tmp/build/80754af9/six_1644875935023/work
smmap==5.0.1
sniffio==1.3.0
snowballstemmer==2.2.0
Sphinx==7.2.6
sphinxcontrib-applehelp==1.0.8
sphinxcontrib-devhelp==1.0.6
sphinxcontrib-htmlhelp==2.0.5
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.7
sphinxcontrib-serializinghtml==1.1.10
stack-data @ file:///home/conda/feedstock_root/build_artifacts/stack_data_1669632077133/work
starlette==0.36.3
streamlit==1.25.0
sympy==1.12
tabulate==0.9.0
tenacity==8.2.3
threadpoolctl==3.2.0
tifffile==2024.1.30
tinycss2 @ file:///croot/tinycss2_1668168815555/work
tokenizers==0.15.2
toml==0.10.2
tomli==2.0.1
toolz==0.12.1
torch==2.1.2
torchmetrics==1.3.2
torchvision==0.16.2
tornado @ file:///home/conda/feedstock_root/build_artifacts/tornado_1695373560918/work
tqdm==4.66.1
traitlets @ file:///home/conda/feedstock_root/build_artifacts/traitlets_1704212992681/work
transformers==4.39.1
triton==2.1.0
typing_extensions==4.9.0
tzdata==2023.4
tzlocal==4.3.1
Unidecode==1.3.8
urllib3==2.1.0
uvicorn==0.27.0.post1
validators==0.22.0
Wand==0.6.13
watchdog==3.0.0
watchfiles==0.21.0
wcwidth @ file:///home/conda/feedstock_root/build_artifacts/wcwidth_1704731205417/work
weasyprint @ file:///home/conda/feedstock_root/build_artifacts/weasyprint_1702301929204/work/weasyprint-60.2-py3-none-any.whl#sha256=3e98eedcc1c5a14cb310c293c6d59a479f59a13f0d705ff07106482827fa5705
webencodings==0.5.1
wrapt==1.16.0
yarl==1.9.4
zipp @ file:///home/conda/feedstock_root/build_artifacts/zipp_1695255097490/work
zopfli==0.2.3

I tested with your image file. Looks like it is your Image format does not match.
Your image file is a ".png", but actually it is not a PNG format, Maybe a "JPEG".