Fix TextToImage integration tests

* Create StyleEnum for StabilityAI prompt options.
* Add copyright header and javadoc to StabilityAI classes
This commit is contained in:
Mark Pollack
2024-01-24 15:41:22 -05:00
parent c5bca45007
commit 593d7f23df
11 changed files with 220 additions and 5 deletions

View File

@@ -32,9 +32,12 @@ public class OpenAiImageClientIT extends AbstractIT {
@Test
void imageAsUrlTest() {
var options = ImageOptionsBuilder.builder().withHeight(256).withWidth(256).build();
var options = ImageOptionsBuilder.builder().withHeight(1024).withWidth(1024).build();
ImagePrompt imagePrompt = new ImagePrompt("Create an image of a mini golden doodle dog.", options);
var instructions = """
A light cream colored mini golden doodle with a sign that contains the message "I'm on my way to BARCADE!".""";
ImagePrompt imagePrompt = new ImagePrompt(instructions, options);
ImageResponse imageResponse = openaiImageClient.call(imagePrompt);
@@ -46,6 +49,7 @@ public class OpenAiImageClientIT extends AbstractIT {
var generation = imageResponse.getResult();
Image image = generation.getOutput();
assertThat(image.getUrl()).isNotEmpty();
// System.out.println(image.getUrl());
assertThat(image.getB64Json()).isNull();
var imageGenerationMetadata = generation.getMetadata();

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2023-2023 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai;
import org.slf4j.Logger;
@@ -13,6 +28,10 @@ import org.springframework.util.Assert;
import java.util.List;
import java.util.stream.Collectors;
/**
* StabilityAiImageClient is a class that implements the ImageClient interface. It
* provides a client for calling the StabilityAI image generation API.
*/
public class StabilityAiImageClient implements ImageClient {
private final Logger logger = LoggerFactory.getLogger(getClass());

View File

@@ -1,9 +1,28 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai;
import org.springframework.ai.image.ImageGenerationMetadata;
import java.util.Objects;
/**
* Represents metadata associated with the image generation process in the StabilityAI
* framework.
*/
public class StabilityAiImageGenerationMetadata implements ImageGenerationMetadata {
private String finishReason;

View File

@@ -0,0 +1,40 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai;
/**
* Enum representing different styles for images.
*/
public enum StyleEnum {
THREE_D_MODEL("3d-model"), ANALOG_FILM("analog-film"), ANIME("anime"), CINEMATIC("cinematic"),
COMIC_BOOK("comic-book"), DIGITAL_ART("digital-art"), ENHANCE("enhance"), FANTASY_ART("fantasy-art"),
ISOMETRIC("isometric"), LINE_ART("line-art"), LOW_POLY("low-poly"), MODELING_COMPOUND("modeling-compound"),
NEON_PUNK("neon-punk"), ORIGAMI("origami"), PHOTOGRAPHIC("photographic"), PIXEL_ART("pixel-art"),
TILE_TEXTURE("tile-texture");
private final String text;
StyleEnum(final String text) {
this.text = text;
}
@Override
public String toString() {
return text;
}
}

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai.api;
import com.fasterxml.jackson.annotation.JsonInclude;
@@ -14,6 +29,9 @@ import java.io.IOException;
import java.util.List;
import java.util.function.Consumer;
/**
* Represents the StabilityAI API.
*/
public class StabilityAiApi {
public static final String DEFAULT_IMAGE_MODEL = "stable-diffusion-v1-6";

View File

@@ -1,7 +1,26 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai.api;
import org.springframework.ai.image.ImageOptions;
/**
* StabilityAiImageOptions is an interface that extends ImageOptions. It provides
* additional stability AI specific image options.
*/
public interface StabilityAiImageOptions extends ImageOptions {
Float getCfgScale();

View File

@@ -1,5 +1,27 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai.api;
import org.springframework.ai.stabilityai.StyleEnum;
/**
* The StabilityAiImageOptionsBuilder class provides a convenient way to construct an
* instance of StabilityAiImageOptions. by allowing you to chain multiple method calls to
* set the desired options.
*/
public class StabilityAiImageOptionsBuilder {
private StabilityAiImageOptionsImpl options;
@@ -72,6 +94,11 @@ public class StabilityAiImageOptionsBuilder {
return this;
}
public StabilityAiImageOptionsBuilder withStylePreset(StyleEnum styleEnum) {
options.setStylePreset(styleEnum.toString());
return this;
}
public StabilityAiImageOptions build() {
return options;
}

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai.api;
public class StabilityAiImageOptionsImpl implements StabilityAiImageOptions {

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai;
import org.junit.jupiter.api.Test;

View File

@@ -1,9 +1,26 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.image.*;
import org.springframework.ai.stabilityai.api.StabilityAiApi;
import org.springframework.ai.stabilityai.api.StabilityAiImageOptions;
import org.springframework.ai.stabilityai.api.StabilityAiImageOptionsBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@@ -24,8 +41,15 @@ public class StabilityAiImageClientIT {
@Test
void imageAsBase64Test() throws IOException {
ImagePrompt imagePrompt = new ImagePrompt(
"A light cream colored mini golden doodle holding a sign that says 'I want to go with you on vacation!'");
StabilityAiImageOptions imageOptions = StabilityAiImageOptionsBuilder.builder()
.withStylePreset(StyleEnum.PHOTOGRAPHIC)
.build();
var instructions = """
A light cream colored mini golden doodle with a sign that contains the message "I'm on my way to BARCADE!".""";
ImagePrompt imagePrompt = new ImagePrompt(instructions, imageOptions);
ImageResponse imageResponse = this.stabilityAiImageClient.call(imagePrompt);
@@ -34,7 +58,7 @@ public class StabilityAiImageClientIT {
assertThat(image.getB64Json()).isNotEmpty();
writeFile(image);
// writeFile(image);
}
private static void writeFile(Image image) throws IOException {

View File

@@ -1,3 +1,18 @@
/*
* Copyright 2023-2024 the original author or authors.
*
* 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
*
* https://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.
*/
package org.springframework.ai.stabilityai;
import org.springframework.ai.stabilityai.api.StabilityAiApi;