Skip to content

Commit

Permalink
Add support for numpy~=2.0 (#8440)
Browse files Browse the repository at this point in the history
* Add code

* add changeset

* add changeset

* update requirements.txt

* remove np.bool8

* add changeset

* type check

---------

Co-authored-by: gradio-pr-bot <[email protected]>
Co-authored-by: Abubakar Abid <[email protected]>
  • Loading branch information
3 people authored Jun 3, 2024
1 parent 3266d2c commit 83bdf5c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/ripe-ears-tease.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"gradio": patch
---

fix:Add support for `numpy~=2.0`
15 changes: 12 additions & 3 deletions gradio/processing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ async def handle_async_request(
if TYPE_CHECKING:
from gradio.blocks import Block


#########################
# GENERAL
#########################
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 83bdf5c

Please sign in to comment.