Use importlib in favor of deprecated pkg_resources (#5048)

* fix(pkg_resources): use `importlib` in favor of `pkg_resources`

* lint

* import

* removed unnecessary version check

* fixes

* pass lint and format

* fix

* requirements

* fix all typing issues

* fix routes

* fix

* Delete forty-rooms-arrive.md

* add changeset

---------

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
This commit is contained in:
Jayce Slesar 2023-08-01 12:26:02 -04:00 committed by GitHub
parent 75b3a58f14
commit 0b74a1595b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 46 additions and 40 deletions

View File

@ -0,0 +1,5 @@
---
"gradio": minor
---
feat:Use `importlib` in favor of deprecated `pkg_resources`

View File

@ -2,28 +2,37 @@
for the cli command 'gradio environment'
"""
import platform
import pkg_resources
from importlib import metadata
def print_environment_info():
print("Gradio Environment Information:")
print("Operating System: ", platform.system())
print("\n")
print("Gradio Environment Information:\n------------------------------")
print("Operating System:", platform.system())
for package_name in ["gradio", "gradio_client"]:
try:
package_dist = pkg_resources.get_distribution(package_name)
package_version = package_dist.version
print(f"{package_name} version: ", package_version)
print(f"\n{package_name} Dependencies:")
for req in package_dist.requires():
print(
f" {req.project_name}: {pkg_resources.get_distribution(req.project_name).version}"
)
print("\n")
except pkg_resources.DistributionNotFound:
package_version = metadata.version(package_name)
print(f"{package_name} version:", package_version)
except metadata.PackageNotFoundError:
print(f"{package_name} package is not installed.")
print("\n------------------------------------------------")
for package_name in ["gradio", "gradio_client"]:
try:
dist = metadata.distribution(package_name)
print(f"{package_name} dependencies in your environment:\n")
if dist.requires is not None:
for req in dist.requires:
req_base_name = (
req.split(">")[0]
.split("<")[0]
.split("~")[0]
.split("[")[0]
.split("!")[0]
)
try:
print(f"{req_base_name}: {metadata.version(req_base_name)}")
except metadata.PackageNotFoundError:
print(f"{req_base_name} is not installed.")
print("\n")
except metadata.PackageNotFoundError:
print(f"{package_name} package is not installed.")

View File

@ -8,13 +8,11 @@ import time
import uuid
from abc import ABC, abstractmethod
from collections import OrderedDict
from distutils.version import StrictVersion
from pathlib import Path
from typing import TYPE_CHECKING, Any
import filelock
import huggingface_hub
import pkg_resources
from gradio_client import utils as client_utils
from gradio_client.documentation import document, set_documentation_group
@ -242,16 +240,6 @@ class HuggingFaceDatasetSaver(FlaggingCallback):
flagging_dir (str): local directory where the dataset is cloned,
updated, and pushed from.
"""
hh_version = pkg_resources.get_distribution("huggingface_hub").version
try:
if StrictVersion(hh_version) < StrictVersion("0.12.0"):
raise ImportError(
"The `huggingface_hub` package must be version 0.12.0 or higher"
"for HuggingFaceDatasetSaver. Try 'pip install huggingface_hub --upgrade'."
)
except ValueError:
pass
# Setup dataset on the Hub
self.dataset_id = huggingface_hub.create_repo(
repo_id=self.dataset_id,

View File

@ -257,7 +257,7 @@ def _convert(image, dtype, force_copy=False, uniform=False):
dtype_range = {
bool: (False, True),
np.bool_: (False, True),
np.bool8: (False, True),
np.bool8: (False, True), # type: ignore
float: (-1, 1),
np.float_: (-1, 1),
np.float16: (-1, 1),

View File

@ -4,6 +4,12 @@ module use the Optional/Union notation so that they work correctly with pydantic
from __future__ import annotations
import asyncio
import sys
if sys.version_info >= (3, 9):
from importlib.resources import files
else:
from importlib_resources import files
import inspect
import json
import mimetypes
@ -23,7 +29,6 @@ import fastapi
import httpx
import markupsafe
import orjson
import pkg_resources
from fastapi import Depends, FastAPI, File, HTTPException, UploadFile, WebSocket, status
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import (
@ -52,12 +57,10 @@ from gradio.utils import cancel_tasks, run_coro_in_background, set_task_name
mimetypes.init()
STATIC_TEMPLATE_LIB = pkg_resources.resource_filename("gradio", "templates/")
STATIC_PATH_LIB = pkg_resources.resource_filename("gradio", "templates/frontend/static")
BUILD_PATH_LIB = pkg_resources.resource_filename("gradio", "templates/frontend/assets")
VERSION_FILE = pkg_resources.resource_filename("gradio", "version.txt")
with open(VERSION_FILE) as version_file:
VERSION = version_file.read()
STATIC_TEMPLATE_LIB = files("gradio").joinpath("templates").as_posix() # type: ignore
STATIC_PATH_LIB = files("gradio").joinpath("templates", "frontend", "static").as_posix() # type: ignore
BUILD_PATH_LIB = files("gradio").joinpath("templates", "frontend", "assets").as_posix() # type: ignore
VERSION = files("gradio").joinpath("version.txt").read_text()
class ORJSONResponse(JSONResponse):

View File

@ -63,7 +63,7 @@ def colab_check() -> bool:
"""
is_colab = False
try: # Check if running interactively using ipython.
from IPython import get_ipython
from IPython.core.getipython import get_ipython
from_ipynb = get_ipython()
if "google.colab" in str(from_ipynb):
@ -97,7 +97,7 @@ def ipython_check() -> bool:
"""
is_ipython = False
try: # Check if running interactively using ipython.
from IPython import get_ipython
from IPython.core.getipython import get_ipython
if get_ipython() is not None:
is_ipython = True

View File

@ -6,6 +6,7 @@ ffmpy
gradio_client>=0.3.0
httpx
huggingface_hub>=0.14.0
importlib_resources>=1.3,<7.0
Jinja2<4.0
markdown-it-py[linkify]>=2.0.0
mdit-py-plugins<=0.3.3