Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image description with OpenAI #81

Merged
merged 21 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/wagtail_ai/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def clean_prompt(self):

class DescribeImageApiForm(ApiForm):
image_id = forms.CharField()
maxlength = forms.IntegerField(required=False)
maxlength = forms.IntegerField(required=False, min_value=0, max_value=4096)


class DescribeImageForm(BaseImageForm):
Expand Down
4 changes: 2 additions & 2 deletions src/wagtail_ai/static_src/image_description/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import wandIcon from '../wand_icon.svg';

document.addEventListener('wagtail-ai:image-form', (event) => {
const input = event.target as HTMLInputElement;
const imageId = input.dataset['wagtailaiImageId'];
const imageId = input.dataset.wagtailaiImageId;
if (!imageId) {
throw new Error('The attribute data-wagtailai-image-id is missing.');
}
Expand All @@ -27,7 +27,7 @@ document.addEventListener('wagtail-ai:image-form', (event) => {

const button = document.createElement('button');
button.type = 'button';
button.title = input.dataset['wagtailaiButtonTitle'] || '';
button.title = input.dataset.wagtailaiButtonTitle || '';
button.classList.add('button', 'wagtailai-button');
button.innerHTML = wandIcon;
flexWrapper.appendChild(button);
Expand Down
43 changes: 43 additions & 0 deletions tests/views/test_describe_image.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from http import HTTPStatus
from typing import cast
from unittest.mock import ANY, Mock, call

Expand Down Expand Up @@ -133,3 +134,45 @@ def test_custom_rendition_filter(admin_client, settings):
assert response.json() == {
"message": "This is an echo backend: images/example.max-100x100.jpg"
}


@pytest.mark.parametrize(
"maxlength,expected_status,error_message",
[
(100, HTTPStatus.OK, None),
(
-1,
HTTPStatus.BAD_REQUEST,
"Ensure this value is greater than or equal to 0.",
),
(
10000,
HTTPStatus.BAD_REQUEST,
"Ensure this value is less than or equal to 4096.",
),
],
)
tomusher marked this conversation as resolved.
Show resolved Hide resolved
def test_maxlength_validation(
admin_client, settings, maxlength, expected_status, error_message
):
tomusher marked this conversation as resolved.
Show resolved Hide resolved
settings.WAGTAIL_AI = {
"BACKENDS": {
"echo": {
"CLASS": "wagtail_ai.ai.echo.EchoBackend",
"CONFIG": {
"MODEL_ID": "echo",
"TOKEN_LIMIT": 123123,
},
},
},
"IMAGE_DESCRIPTION_BACKEND": "echo",
}

image = cast(Image, ImageFactory())
response = admin_client.post(
reverse("wagtail_ai:describe_image"),
data={"image_id": image.pk, "maxlength": maxlength},
)
assert response.status_code == expected_status
if error_message is not None:
assert response.json() == {"error": error_message}
Loading