Spring AI Image Generation from Text Example
In today’s rapidly evolving technological landscape, integrating artificial intelligence (AI) into applications has become a key driver for innovation and efficiency. Spring AI Project is a powerful extension of the Spring framework designed to facilitate the incorporation of AI capabilities into Spring-based applications. Let us delve into understanding how Spring can be utilized in an AI image generation example, exploring the integration of AI capabilities within a Spring application to create dynamic image generation workflows.
1. What is Spring AI?
Spring AI Project is an extension of the widely-used Spring framework that provides tools and libraries to simplify the integration of AI functionalities into Spring applications. It aims to bridge the gap between AI models and business applications by offering seamless integration, enhanced configurability, and robust support for various AI services and frameworks. Whether you’re working with machine learning models, natural language processing, or computer vision, Spring AI provides the necessary components to embed AI capabilities into your application effortlessly.
1.1 Key Features
- Seamless Integration: Spring AI offers a straightforward way to integrate AI services into existing Spring applications, reducing the complexity of managing separate systems.
- Extensive Support: It supports a wide range of AI frameworks and libraries, including TensorFlow, PyTorch, and Apache MXNet, allowing developers to choose the best tools for their needs.
- Easy Configuration: With Spring AI, configuring AI models and services is simplified through Spring’s powerful configuration mechanisms, such as annotations and XML-based configuration.
- Scalability: Built on top of the robust Spring framework, Spring AI ensures that your AI-powered applications can scale seamlessly to handle increased loads and larger datasets.
- Security: Leveraging Spring Security, Spring AI provides secure integration with AI services, ensuring data protection and compliance with security standards.
1.2 Advantages
The integration of Spring AI into your development workflow offers numerous advantages, making it an attractive choice for developers and businesses looking to harness the power of AI:
- Accelerated Development: Spring AI accelerates the development process by providing pre-built components and utilities, reducing the need for custom code and allowing developers to focus on core functionalities.
- Consistency and Reliability: By adhering to Spring’s principles of consistency and reliability, Spring AI ensures that AI functionalities are seamlessly integrated and behave predictably within your application.
- Enhanced Productivity: The intuitive and developer-friendly nature of Spring AI enhances productivity, enabling developers to quickly prototype, test, and deploy AI features.
- Community and Support: Spring AI benefits from the extensive Spring ecosystem and community support, providing access to a wealth of resources, tutorials, and expert advice.
- Future-proofing: As AI technologies continue to evolve, Spring AI is designed to adapt and incorporate new advancements, ensuring that your applications remain up-to-date with the latest AI capabilities.
1.3 Important Classes
- ImageClient: A functional interface that has a method
call()
which returnsImageResponse
. - ImagePrompt: This class is used to construct a prompt for generating an image. It contains the necessary data, such as text and options, that guide the AI in creating the desired image. It has multiple constructors that facilitate the creation of
ImagePrompt
objects by accepting the message along with options represented by another interface,ImageOptions
. - ImageMessage: This class encapsulates the text used for image generation and the weightage that the text should have in influencing the generated image.
- ImageOptions: This interface represents the configurations that can be passed for image generation.
- ImageResponse: This class holds the response of the image generation request to the AI endpoint. It contains
List<ImageGeneration>
where eachImageGeneration
instance contains one of potentially multiple outputs resulting from a single prompt. - ImageGeneration: This class contains the output response and related metadata about this result.
- Image: This class represents the image data generated by the AI model. It encapsulates the properties and methods necessary to handle and manipulate the image output.
- OpenAiImageClient: An implementation of the
ImageClient
interface specifically designed to interact with OpenAI’s image generation service. This class handles the specifics of connecting to and communicating with the OpenAI API. - OpenAiImageOptions: This class provides configuration options for generating images through OpenAI’s service. It allows for the customization of parameters such as image resolution, style, and other generation-related settings.
- OpenAiImageApi: This class acts as the API layer for interacting with OpenAI’s image generation services. It defines the endpoints and methods required to send requests and receive responses from the OpenAI image generation API.
2. How to generate an OpenAI API key?
We’ll use an OpenAI model to generate an image, so let’s first understand how to obtain an OpenAI key.
- Sign Up or Log In: Visit the OpenAI website. Sign up for a new account or log in to your existing account.
- Navigate to API Keys: Once logged in, go to the API Keys section in your account dashboard.
- Create a New API Key: Click on the Create New API Key button. Provide a name or description for the API key for easy identification.
- Generate and Copy the Key: Click on the Generate Key button. Copy the generated API key to a secure location. You will need this key to access OpenAI services.
3. Code example
3.1 Dependencies
Add the following dependencies to your build.grade
file.
plugins { id 'org.springframework.boot' version '3.2.4' id 'io.spring.dependency-management' version '1.1.3' id 'java' } group = 'jcg.springaiimagegeneration' version = '0.0.1-SNAPSHOT' repositories { mavenCentral() maven { url 'https://repo.spring.io/milestone' } } dependencyManagement { imports { mavenBom "org.springframework.ai:spring-ai-bom:0.8.1" } } dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.ai:spring-ai-openai-spring-boot-starter' testImplementation 'org.springframework.boot:spring-boot-starter-test' } ext { set('springAiVersion', "0.8.1") } tasks.withType(Test) { useJUnitPlatform() }
3.2 Update the application properties
Add the following properties to the application.properties
file. Remember to replace the OpenAI key.
# application properties spring.application.name=springaiimagegeneration server.port=8080 spring.main.banner-mode=off # OpenAI API Key spring.ai.openai.api-key=your_openai_api_key
3.3 Create a configuration
Create a configuration class to map the OpenAI key with the Image Client. The value of the OpenAI key will be read from the properties file and injected here with the help of Spring’s @Value
annotation.
package jcg.springaiimagegeneration.configuration; import org.springframework.ai.image.ImageClient; import org.springframework.ai.openai.OpenAiImageClient; import org.springframework.ai.openai.api.OpenAiImageApi; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class OpenAiConfig { @Bean public ImageClient imageClient(@Value("${spring.ai.openai.api-key}") String apiKey) { return new OpenAiImageClient(new OpenAiImageApi(apiKey)); } }
3.4 Creating the Controller
Create a controller to handle the incoming prompt from the user. The prompt will be validated and the image will be generated post-validation based on the user prompt.
package jcg.springaiimagegeneration.controller; import org.apache.commons.lang3.StringUtils; import org.springframework.ai.image.Image; import org.springframework.ai.image.ImageClient; import org.springframework.ai.image.ImagePrompt; import org.springframework.ai.openai.OpenAiImageClient; import org.springframework.ai.openai.OpenAiImageOptions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/v1/image") public class ImageGenerationController { private final ImageClient imageClient; // constructor injection @Autowired public ImageGenerationController(OpenAiImageClient openAiImageClient) { this.imageClient = openAiImageClient; } // request endpoint: http://localhost:8080/api/v1/image/generate?prompt=your_prompt_here @GetMapping("/generate") public Image getImage(@RequestParam("prompt") String prompt) { // input validation if (StringUtils.isEmpty(prompt)) { throw new IllegalArgumentException("Prompt is required"); } // image options OpenAiImageOptions imageOptions = OpenAiImageOptions.builder() .withQuality("standard") .withHeight(1024) .withN(1) .withWidth(1792) .build(); // image prompt ImagePrompt imagePrompt = new ImagePrompt(prompt, imageOptions); // class ep return imageClient.call(imagePrompt).getResult().getOutput(); } }
3.5 Create the Main file
Create a Spring Boot application to initialize the application.
package jcg.springaiimagegeneration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class SpringaiimagegenerationApplication { public static void main(String[] args) { SpringApplication.run(SpringaiimagegenerationApplication.class, args); } }
3.6 Run the application and Demo
Now start the application and open the Postman tool to play with the api. Import the below curl request in the Postman tool. Remember to change the prompt with a valid instruction.
curl -X GET "http://localhost:8080/api/v1/image/generate?prompt=your_prompt_here"
If everything goes well the response containing the image url will be returned from OpenAI and sent to the client as a json response.
{ "url": "generated_image_output_url_based_on_prompt", "b64Json": null }
Please note that the user prompt and generated url are omitted from brevity as it was long text.
4. Conclusion
In conclusion, as AI continues to reshape the technological landscape, tools like Spring AI are essential for seamlessly integrating AI into applications. By leveraging Spring AI, developers can easily add advanced AI capabilities to their Spring-based projects, enabling innovative solutions like generating images from user prompts.
5. Download the source code
You can download the full source code of this example here: Spring AI Example Generate Image from Text
The link to the zip file contains a file without any content