From 83bdf5c4478c0c6138cc639818dd5e403ea712a0 Mon Sep 17 00:00:00 2001 From: Freddy Boulton Date: Mon, 3 Jun 2024 17:46:45 -0400 Subject: [PATCH] Add support for `numpy~=2.0` (#8440) * Add code * add changeset * add changeset * update requirements.txt * remove np.bool8 * add changeset * type check --------- Co-authored-by: gradio-pr-bot Co-authored-by: Abubakar Abid --- .changeset/ripe-ears-tease.md | 5 +++++ gradio/processing_utils.py | 15 ++++++++++++--- requirements.txt | 2 +- 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 .changeset/ripe-ears-tease.md diff --git a/.changeset/ripe-ears-tease.md b/.changeset/ripe-ears-tease.md new file mode 100644 index 0000000000000..e43e3488e6ace --- /dev/null +++ b/.changeset/ripe-ears-tease.md @@ -0,0 +1,5 @@ +--- +"gradio": patch +--- + +fix:Add support for `numpy~=2.0` diff --git a/gradio/processing_utils.py b/gradio/processing_utils.py index 3c0d4b7bd5c86..5b658842ee12c 100644 --- a/gradio/processing_utils.py +++ b/gradio/processing_utils.py @@ -104,6 +104,7 @@ async def handle_async_request( if TYPE_CHECKING: from gradio.blocks import Block + ######################### # GENERAL ######################### @@ -660,14 +661,17 @@ def _convert(image, dtype, force_copy=False, uniform=False): dtype_range = { bool: (False, True), np.bool_: (False, True), - np.bool8: (False, True), # type: ignore float: (-1, 1), - np.float_: (-1, 1), np.float16: (-1, 1), np.float32: (-1, 1), np.float64: (-1, 1), } + if hasattr(np, "float_"): + dtype_range[np.float_] = dtype_range[float] # type: ignore + if hasattr(np, "bool8"): + dtype_range[np.bool8] = dtype_range[np.bool_] # type: ignore + def _dtype_itemsize(itemsize, *dtypes): """Return first of `dtypes` with itemsize greater than `itemsize` Parameters @@ -786,7 +790,12 @@ def _scale(a, n, m, copy=True): # is a subclass of that type (e.g. `np.floating` will allow # `float32` and `float64` arrays through) - if np.issubdtype(dtype_in, np.obj2sctype(dtype)): + if hasattr(np, "obj2sctype"): + is_subdtype = np.issubdtype(dtype_in, np.obj2sctype(dtype)) + else: + is_subdtype = np.issubdtype(dtype_in, dtypeobj_out.type) + + if is_subdtype: if force_copy: image = image.copy() return image diff --git a/requirements.txt b/requirements.txt index 777c6c5fc90a0..2f6c6d2c746f6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -9,7 +9,7 @@ importlib_resources>=1.3,<7.0 Jinja2<4.0 markupsafe~=2.0 matplotlib~=3.0 -numpy~=1.0 +numpy>=1.0,<3.0 orjson~=3.0 packaging pandas>=1.0,<3.0