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 <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
This commit is contained in:
Freddy Boulton 2024-06-03 17:46:45 -04:00 committed by GitHub
parent 3266d2c0f2
commit 83bdf5c447
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 18 additions and 4 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": patch
---
fix:Add support for `numpy~=2.0`

View File

@ -104,6 +104,7 @@ log = logging.getLogger(__name__)
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 _convert(image, dtype, force_copy=False, uniform=False):
# 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

View File

@ -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