Repository: huggingface/huggingface_hub
Pull request #1511 (2023-06-14 16:45:54)

Refactor InferenceClient internals to prepare for TGI
Still a very early draft

HuggingFaceDocBuilderDev: The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/huggingface_hub/pr_1511). All of your documentation changes will be reflected on that endpoint.

Patch:
diff --git a/docs/source/package_reference/inference_client.mdx b/docs/source/package_reference/inference_client.mdx
index ea9f1eb8..1c16aff0 100644
--- a/docs/source/package_reference/inference_client.mdx
+++ b/docs/source/package_reference/inference_client.mdx
@@ -24,13 +24,13 @@ for more information on how to use it.
 
 For most tasks, the return value has a built-in type (string, list, image...). Here is a list for the more complex types.
 
-[[autodoc]] huggingface_hub._inference_types.ClassificationOutput
+[[autodoc]] huggingface_hub.inference._types.ClassificationOutput
 
-[[autodoc]] huggingface_hub._inference_types.ConversationalOutputConversation
+[[autodoc]] huggingface_hub.inference._types.ConversationalOutputConversation
 
-[[autodoc]] huggingface_hub._inference_types.ConversationalOutput
+[[autodoc]] huggingface_hub.inference._types.ConversationalOutput
 
-[[autodoc]] huggingface_hub._inference_types.ImageSegmentationOutput
+[[autodoc]] huggingface_hub.inference._types.ImageSegmentationOutput
 
 ## InferenceAPI
 
diff --git a/src/huggingface_hub/__init__.py b/src/huggingface_hub/__init__.py
index 0f24a83e..48ea73f9 100644
--- a/src/huggingface_hub/__init__.py
+++ b/src/huggingface_hub/__init__.py
@@ -55,10 +55,6 @@ _SUBMOD_ATTRS = {
     "_commit_scheduler": [
         "CommitScheduler",
     ],
-    "_inference": [
-        "InferenceClient",
-        "InferenceTimeoutError",
-    ],
     "_login": [
         "interpreter_login",
         "login",
@@ -206,6 +202,10 @@ _SUBMOD_ATTRS = {
         "ModelHubMixin",
         "PyTorchModelHubMixin",
     ],
+    "inference._client": [
+        "InferenceClient",
+        "InferenceTimeoutError",
+    ],
     "inference_api": [
         "InferenceApi",
     ],
@@ -354,10 +354,6 @@ __getattr__, __dir__, __all__ = _attach(__name__, submodules=[], submod_attrs=_S
 # ```
 if TYPE_CHECKING:  # pragma: no cover
     from ._commit_scheduler import CommitScheduler  # noqa: F401
-    from ._inference import (
-        InferenceClient,  # noqa: F401
-        InferenceTimeoutError,  # noqa: F401
-    )
     from ._login import (
         interpreter_login,  # noqa: F401
         login,  # noqa: F401
@@ -501,6 +497,10 @@ if TYPE_CHECKING:  # pragma: no cover
         ModelHubMixin,  # noqa: F401
         PyTorchModelHubMixin,  # noqa: F401
     )
+    from .inference._client import (
+        InferenceClient,  # noqa: F401
+        InferenceTimeoutError,  # noqa: F401
+    )
     from .inference_api import InferenceApi  # noqa: F401
     from .keras_mixin import (
         KerasModelHubMixin,  # noqa: F401
diff --git a/src/huggingface_hub/inference/__init__.py b/src/huggingface_hub/inference/__init__.py
new file mode 100644
index 00000000..e69de29b
diff --git a/src/huggingface_hub/_inference.py b/src/huggingface_hub/inference/_client.py
similarity index 99%
rename from src/huggingface_hub/_inference.py
rename to src/huggingface_hub/inference/_client.py
index 5fa3a471..55030bcc 100644
--- a/src/huggingface_hub/_inference.py
+++ b/src/huggingface_hub/inference/_client.py
@@ -1,5 +1,5 @@
 # coding=utf-8
-# Copyright 2019-present, the HuggingFace Inc. team.
+# Copyright 2023-present, the HuggingFace Inc. team.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -46,10 +46,10 @@ from typing import TYPE_CHECKING, Any, BinaryIO, ContextManager, Dict, Generator
 from requests import HTTPError, Response
 from requests.structures import CaseInsensitiveDict
 
-from ._inference_types import ClassificationOutput, ConversationalOutput, ImageSegmentationOutput
-from .constants import INFERENCE_ENDPOINT
-from .utils import build_hf_headers, get_session, hf_raise_for_status, is_numpy_available, is_pillow_available
-from .utils._typing import Literal
+from ..constants import INFERENCE_ENDPOINT
+from ..utils import build_hf_headers, get_session, hf_raise_for_status, is_numpy_available, is_pillow_available
+from ..utils._typing import Literal
+from ._types import ClassificationOutput, ConversationalOutput, ImageSegmentationOutput
 
 
 if TYPE_CHECKING:
diff --git a/src/huggingface_hub/_inference_types.py b/src/huggingface_hub/inference/_types.py
similarity index 75%
rename from src/huggingface_hub/_inference_types.py
rename to src/huggingface_hub/inference/_types.py
index 2af27a68..b4691a8c 100644
--- a/src/huggingface_hub/_inference_types.py
+++ b/src/huggingface_hub/inference/_types.py
@@ -1,6 +1,20 @@
+# coding=utf-8
+# Copyright 2023-present, the HuggingFace Inc. team.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
 from typing import TYPE_CHECKING, List
 
-from .utils._typing import TypedDict
+from ..utils._typing import TypedDict
 
 
 if TYPE_CHECKING:


Test patch:
diff --git a/tests/test_inference_client.py b/tests/test_inference_client.py
index 8b0b45b7..efd1e9c7 100644
--- a/tests/test_inference_client.py
+++ b/tests/test_inference_client.py
@@ -21,7 +21,7 @@ import pytest
 from PIL import Image
 
 from huggingface_hub import InferenceClient, hf_hub_download
-from huggingface_hub._inference import _open_as_binary
+from huggingface_hub.inference._client import _open_as_binary
 from huggingface_hub.utils import build_hf_headers
 
 from .testing_utils import with_production_testing
@@ -271,7 +271,7 @@ class TestHeadersAndCookies(unittest.TestCase):
         # Case-insensitive overwrite
         self.assertEqual(InferenceClient(headers={"USER-agent": "bar"}).headers["user-agent"], "bar")
 
-    @patch("huggingface_hub._inference.get_session")
+    @patch("huggingface_hub.inference._client.get_session")
     def test_mocked_post(self, get_session_mock: MagicMock) -> None:
         """Test that headers and cookies are correctly passed to the request."""
         client = InferenceClient(headers={"X-My-Header": "foo"}, cookies={"my-cookie": "bar"})