From 92b1562194338778219ce0ca9261e5380fce4abb Mon Sep 17 00:00:00 2001 From: Lucain Pouget Date: Mon, 25 Sep 2023 09:38:02 +0200 Subject: [PATCH 1/2] Respect HTTPError spec --- src/huggingface_hub/hf_api.py | 4 +++- src/huggingface_hub/inference/_client.py | 6 ++++-- .../inference/_generated/_async_client.py | 6 ++++-- src/huggingface_hub/inference/_text_generation.py | 10 ++++++---- src/huggingface_hub/utils/_errors.py | 3 ++- utils/generate_async_inference_client.py | 6 ++++-- 6 files changed, 23 insertions(+), 12 deletions(-) diff --git a/src/huggingface_hub/hf_api.py b/src/huggingface_hub/hf_api.py index f4eb4b5961..e9e4349d4a 100644 --- a/src/huggingface_hub/hf_api.py +++ b/src/huggingface_hub/hf_api.py @@ -956,7 +956,9 @@ def whoami(self, token: Optional[str] = None) -> Dict: raise HTTPError( "Invalid user token. If you didn't pass a user token, make sure you " "are properly logged in by executing `huggingface-cli login`, and " - "if you did pass a user token, double-check it's correct." + "if you did pass a user token, double-check it's correct.", + request=e.request, + response=e.response, ) from e return r.json() diff --git a/src/huggingface_hub/inference/_client.py b/src/huggingface_hub/inference/_client.py index cc4a8fb1d9..b060a77510 100644 --- a/src/huggingface_hub/inference/_client.py +++ b/src/huggingface_hub/inference/_client.py @@ -232,7 +232,7 @@ def post( ) except TimeoutError as error: # Convert any `TimeoutError` to a `InferenceTimeoutError` - raise InferenceTimeoutError(f"Inference call timed out: {url}") from error + raise InferenceTimeoutError(f"Inference call timed out: {url}") from error # type: ignore try: hf_raise_for_status(response) @@ -243,7 +243,9 @@ def post( if timeout is not None and time.time() - t0 > timeout: raise InferenceTimeoutError( f"Model not loaded on the server: {url}. Please retry with a higher timeout (current:" - f" {self.timeout})." + f" {self.timeout}).", + request=error.request, + response=error.response, ) from error # ...or wait 1s and retry logger.info(f"Waiting for model to be loaded on the server: {error}") diff --git a/src/huggingface_hub/inference/_generated/_async_client.py b/src/huggingface_hub/inference/_generated/_async_client.py index ad068c6aba..3ab4faf436 100644 --- a/src/huggingface_hub/inference/_generated/_async_client.py +++ b/src/huggingface_hub/inference/_generated/_async_client.py @@ -231,7 +231,7 @@ async def post( except asyncio.TimeoutError as error: await client.close() # Convert any `TimeoutError` to a `InferenceTimeoutError` - raise InferenceTimeoutError(f"Inference call timed out: {url}") from error + raise InferenceTimeoutError(f"Inference call timed out: {url}") from error # type: ignore except aiohttp.ClientResponseError as error: error.response_error_payload = response_error_payload await client.close() @@ -240,7 +240,9 @@ async def post( if timeout is not None and time.time() - t0 > timeout: raise InferenceTimeoutError( f"Model not loaded on the server: {url}. Please retry with a higher timeout" - f" (current: {self.timeout})." + f" (current: {self.timeout}).", + request=error.request, + response=error.response, ) from error # ...or wait 1s and retry logger.info(f"Waiting for model to be loaded on the server: {error}") diff --git a/src/huggingface_hub/inference/_text_generation.py b/src/huggingface_hub/inference/_text_generation.py index 7837eb75b5..b373f8db80 100644 --- a/src/huggingface_hub/inference/_text_generation.py +++ b/src/huggingface_hub/inference/_text_generation.py @@ -468,13 +468,15 @@ def raise_text_generation_error(http_error: HTTPError) -> NoReturn: # If error_type => more information than `hf_raise_for_status` if error_type is not None: if error_type == "generation": - raise GenerationError(message) from http_error + raise GenerationError(message, request=http_error.request, response=http_error.response) from http_error if error_type == "incomplete_generation": - raise IncompleteGenerationError(message) from http_error + raise IncompleteGenerationError( + message, request=http_error.request, response=http_error.response + ) from http_error if error_type == "overloaded": - raise OverloadedError(message) from http_error + raise OverloadedError(message, request=http_error.request, response=http_error.response) from http_error if error_type == "validation": - raise ValidationError(message) from http_error + raise ValidationError(message, request=http_error.request, response=http_error.response) from http_error # Otherwise, fallback to default error raise http_error diff --git a/src/huggingface_hub/utils/_errors.py b/src/huggingface_hub/utils/_errors.py index 47f66d8b4c..c458dba19f 100644 --- a/src/huggingface_hub/utils/_errors.py +++ b/src/huggingface_hub/utils/_errors.py @@ -85,7 +85,8 @@ def __init__(self, message: str, response: Optional[Response] = None): request_id=self.request_id, server_message=self.server_message, ), - response=response, + response=response, # type: ignore + request=response.request if response is not None else None, # type: ignore ) def append_to_message(self, additional_message: str) -> None: diff --git a/utils/generate_async_inference_client.py b/utils/generate_async_inference_client.py index f24ecc5827..967ec92927 100644 --- a/utils/generate_async_inference_client.py +++ b/utils/generate_async_inference_client.py @@ -207,7 +207,7 @@ def _rename_to_AsyncInferenceClient(code: str) -> str: except asyncio.TimeoutError as error: await client.close() # Convert any `TimeoutError` to a `InferenceTimeoutError` - raise InferenceTimeoutError(f"Inference call timed out: {url}") from error + raise InferenceTimeoutError(f"Inference call timed out: {url}") from error # type: ignore except aiohttp.ClientResponseError as error: error.response_error_payload = response_error_payload await client.close() @@ -216,7 +216,9 @@ def _rename_to_AsyncInferenceClient(code: str) -> str: if timeout is not None and time.time() - t0 > timeout: raise InferenceTimeoutError( f"Model not loaded on the server: {url}. Please retry with a higher timeout" - f" (current: {self.timeout})." + f" (current: {self.timeout}).", + request=error.request, + response=error.response, ) from error # ...or wait 1s and retry logger.info(f"Waiting for model to be loaded on the server: {error}") From f0e6a96f63476ac3ffbf8134cb96167c95044fe9 Mon Sep 17 00:00:00 2001 From: Lucain Pouget Date: Mon, 25 Sep 2023 10:16:24 +0200 Subject: [PATCH 2/2] revert --- src/huggingface_hub/inference/_text_generation.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/huggingface_hub/inference/_text_generation.py b/src/huggingface_hub/inference/_text_generation.py index b373f8db80..aba07834bb 100644 --- a/src/huggingface_hub/inference/_text_generation.py +++ b/src/huggingface_hub/inference/_text_generation.py @@ -468,15 +468,13 @@ def raise_text_generation_error(http_error: HTTPError) -> NoReturn: # If error_type => more information than `hf_raise_for_status` if error_type is not None: if error_type == "generation": - raise GenerationError(message, request=http_error.request, response=http_error.response) from http_error + raise GenerationError(message) from http_error # type: ignore if error_type == "incomplete_generation": - raise IncompleteGenerationError( - message, request=http_error.request, response=http_error.response - ) from http_error + raise IncompleteGenerationError(message) from http_error # type: ignore if error_type == "overloaded": - raise OverloadedError(message, request=http_error.request, response=http_error.response) from http_error + raise OverloadedError(message) from http_error # type: ignore if error_type == "validation": - raise ValidationError(message, request=http_error.request, response=http_error.response) from http_error + raise ValidationError(message) from http_error # type: ignore # Otherwise, fallback to default error raise http_error