From 80a31bd05cd5df01ac70c88b20e067acbe483b74 Mon Sep 17 00:00:00 2001 From: Volker Stampa Date: Tue, 4 Oct 2022 17:10:27 +0200 Subject: [PATCH 1/3] Raise if image download fails Raise if image cannot be loaded from url due to non-okay status code. --- aleph_alpha_client/image.py | 35 ++++++++++++++++++++++------------- tests/test_image.py | 16 ++++++++++++++++ 2 files changed, 38 insertions(+), 13 deletions(-) create mode 100644 tests/test_image.py diff --git a/aleph_alpha_client/image.py b/aleph_alpha_client/image.py index db42c6c..7f0f423 100644 --- a/aleph_alpha_client/image.py +++ b/aleph_alpha_client/image.py @@ -7,6 +7,7 @@ class Cropping: """ Describes a quadratic crop of the file. """ + def __init__(self, upper_left_x: int, upper_left_y: int, size: int): self.upper_left_x = upper_left_x self.upper_left_y = upper_left_y @@ -18,6 +19,7 @@ class ImagePrompt: An image send as part of a prompt to a model. The image is represented as base64. """ + def __init__( self, base_64: str, @@ -39,20 +41,20 @@ def from_url(cls, url: str): Downloads a file and prepare it to be used in a prompt. The image will be [center cropped](https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.CenterCrop) """ - bytes = requests.get(url).content - return cls.from_bytes(bytes) + return cls.from_bytes(cls._get_url(url)) @classmethod - def from_url_with_cropping(cls, url: str, upper_left_x: int, - upper_left_y: int, crop_size: int): + def from_url_with_cropping( + cls, url: str, upper_left_x: int, upper_left_y: int, crop_size: int + ): """ Downloads a file and prepare it to be used in a prompt. upper_left_x, upper_left_y and crop_size are used to crop the image. """ - cropping = Cropping(upper_left_x=upper_left_x, - upper_left_y=upper_left_y, - size=crop_size) - bytes = requests.get(url).content + cropping = Cropping( + upper_left_x=upper_left_x, upper_left_y=upper_left_y, size=crop_size + ) + bytes = cls._get_url(url) return cls.from_bytes(bytes, cropping=cropping) @classmethod @@ -66,19 +68,26 @@ def from_file(cls, path: str): return cls.from_bytes(image) @classmethod - def from_file_with_cropping(cls, path: str, upper_left_x: int, - upper_left_y: int, crop_size: int): + def from_file_with_cropping( + cls, path: str, upper_left_x: int, upper_left_y: int, crop_size: int + ): """ Load an image from disk and prepare it to be used in a prompt upper_left_x, upper_left_y and crop_size are used to crop the image. """ - cropping = Cropping(upper_left_x=upper_left_x, - upper_left_y=upper_left_y, - size=crop_size) + cropping = Cropping( + upper_left_x=upper_left_x, upper_left_y=upper_left_y, size=crop_size + ) with open(path, "rb") as f: bytes = f.read() return cls.from_bytes(bytes, cropping=cropping) + @classmethod + def _get_url(cls, url: str) -> bytes: + response = requests.get(url) + response.raise_for_status() + return response.content + def _to_prompt_item(self) -> Dict[str, Any]: """ A dict if serialized to JSON is suitable as a prompt element diff --git a/tests/test_image.py b/tests/test_image.py new file mode 100644 index 0000000..55eec61 --- /dev/null +++ b/tests/test_image.py @@ -0,0 +1,16 @@ +from http import HTTPStatus +from pytest import raises +from pytest_httpserver import HTTPServer +from requests import RequestException + +from aleph_alpha_client.image import ImagePrompt + + +def test_from_url_with_non_OK_response(httpserver: HTTPServer): + path = "/image" + httpserver.expect_request(path).respond_with_data( + "html", status=HTTPStatus.FORBIDDEN + ) + + with raises(RequestException) as e: + ImagePrompt.from_url(httpserver.url_for(path)) From 6795da93f4c758c279f182c94c72de308bf9d275 Mon Sep 17 00:00:00 2001 From: Volker Stampa Date: Tue, 4 Oct 2022 17:14:15 +0200 Subject: [PATCH 2/3] Add .env.example for documentation --- .env.example | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..66f773b --- /dev/null +++ b/.env.example @@ -0,0 +1,6 @@ +TEST_API_URL=https://test.api.aleph-alpha.com +TEST_MODEL=luminous-extended +TEST_TOKEN= +TEST_CHECKPOINT= +TEST_CHECKPOINT_QA= +TEST_CHECKPOINT_SUMMARIZATION= \ No newline at end of file From 263b6a4eeb7e13f9cff96301de58614ca13089a0 Mon Sep 17 00:00:00 2001 From: Volker Stampa Date: Tue, 4 Oct 2022 17:17:26 +0200 Subject: [PATCH 3/3] Bump version & update changelog --- Changelog.md | 4 ++++ aleph_alpha_client/version.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Changelog.md b/Changelog.md index 88e0249..267e2dc 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,5 +1,9 @@ # Changelog +## 2.4.4 + +- Fix: ImagePrompt.from_url raises if status-code not OK + ## 2.4.3 - Fix: Dependency `urllib` is specified to be at least of version `1.26`. diff --git a/aleph_alpha_client/version.py b/aleph_alpha_client/version.py index 5a8e098..afe4dee 100644 --- a/aleph_alpha_client/version.py +++ b/aleph_alpha_client/version.py @@ -1 +1 @@ -__version__ = "2.4.3" +__version__ = "2.4.4"