Skip to main content

Spring AI ImageModel (Text-to-Image)

Banner Spring AI Icon

๐Ÿ–ผ๏ธ Spring AI: Fun with Image Generationโ€‹

๐ŸŽจ LLMs + AI = Magic Images! ๐Ÿง™โ€โ™‚๏ธโ€‹

Ever wanted to turn your wildest imaginations into reality? Well, sort of! Large Language Models (LLMs) like DALL-E, Stable Diffusion, Midjourney, Imagen, GauGAN, Pixray, and more can generate stunning images from just a simple text prompt. ๐Ÿ–Œ๏ธโœจ

Lucky for us, Spring AI makes it super easy to integrate these magical capabilities into our applications. ๐Ÿš€

๐ŸŒŸ Supported Image Generation Wizardsโ€‹

  • DALL-E (by OpenAI)
  • DALL-E (by Azure OpenAI)
  • Stable Diffusion (by Stability AI)
  • CogView (by ZhiPuAI & QianFan)

This guide will walk you through the Spring AI module for text-to-image generation, with real code examples! ๐ŸŽญ


๐Ÿ› ๏ธ Step 1: Add Dependencies (Because Magic Needs Tools!)โ€‹

To enable OpenAI API support, add the following to your pom.xml:

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

For Stability AIโ€™s API support, add:

<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-stability-ai-spring-boot-starter</artifactId>
</dependency>

By default, these dependencies automatically configure an imageClient bean. Want to disable it? Just set the following in application.properties:

spring.ai.openai.image.enabled=false
spring.ai.stabilityai.image.enabled=false

๐ŸŽญ Step 2: Meet the Image Generation APIโ€‹

Spring AI provides a bunch of cool classes and interfaces to make image generation a breeze. Hereโ€™s the A-list:

Core ClassWhat It Does
ImageModelThe main actor! Calls imagePrompt and returns ImageResponse.
ImageMessageHolds the text and its importance in shaping the image. ๐ŸŽญ
ImagePromptA fancy box containing ImageMessage objects and request options. ๐ŸŽ
ImageOptionsSpecifies extra magic (resolution, quality, etc.). ๐ŸŽจ
ImageResponseThe AI's response, containing one or more generated images. ๐Ÿ–ผ๏ธ
ImageGenerationThe actual image output and related metadata. ๐Ÿ“ธ
ImageGenerationMetadataMetadata describing the generated image. ๐Ÿท๏ธ

โœจ How to Cast an Image Spellโ€‹

@RestController
public class OpenAiImageController {

private final OpenAiImageModel imageModel;

@Autowired
public OpenAiImageController(OpenAiImageModel imageModel) {
this.imageModel = imageModel;
}

@GetMapping("/ask/image")
public ImageResponse generateImage(@RequestParam(value = "message", defaultValue = "Horse running behind tiger") String messgae) {
return imageModel.call(
new ImagePrompt(messgae,
OpenAiImageOptions.builder()
.quality("hd")
.N(4)
.height(1024)
.width(1024).build())

);
}
}

Boom! ๐Ÿฑ๐Ÿ’จ๐Ÿญ


๐Ÿ”ง Step 3: Creating the ImageModelโ€‹

Spring AI provides two classes that implement the ImageModel interface:

  • OpenAiImageModel (for DALL-E ๐Ÿง ๐ŸŽจ)
  • StabilityAiImageModel (for Stable Diffusion ๐ŸŒŠ)

Define an ImageModel bean in your config file:

๐Ÿ“ข Pro Tip: Keep your API keys out of your code! Store them safely in application.properties:

spring.ai.openai.api-key=${OPENAI_API_KEY}
# OR
spring.ai.stability.api-key=${STABILITY_API_KEY}

๐ŸŽฎ Step 4: Creating the Image Generation Controllerโ€‹

Time to roll up our sleeves and make some API magic! Letโ€™s create a simple REST controller to generate images on demand. ๐Ÿง‘โ€๐Ÿ’ป

import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class OpenAiImageController {

private final OpenAiImageModel imageModel;

@Autowired
public OpenAiImageController(OpenAiImageModel imageModel) {
this.imageModel = imageModel;
}

@GetMapping("/ask/image")
public ImageResponse generateImage(@RequestParam(value = "message", defaultValue = "Horse running behind tiger") String messgae) {
return imageModel.call(
new ImagePrompt(messgae,
OpenAiImageOptions.builder()
.quality("hd")
.N(4)
.height(1024)
.width(1024).build())

);
}

@GetMapping("/ask/image/withUrl")
public String generateImageUrl(@RequestParam(value = "message", defaultValue = "Horse running behind tiger") String messgae) {
ImageResponse imageResponse = imageModel.call(new ImagePrompt(messgae, OpenAiImageOptions.builder().quality("hd").N(4).height(1024).width(1024).build()));
String imageUrl = imageResponse.getResult().getOutput().getUrl();
return "Redirect to:" + imageUrl;
}
}

๐Ÿ” This same code works for Stability AI tooโ€”just swap in StabilityAiImageModel. ๐Ÿ”„


๐ŸŒ Step 5: Configuring the Base URLโ€‹

By default, Spring AI selects these URLs for image generation:

spring.ai.openai.image.base-url=api.openai.com
# OR
spring.ai.stabilityai.image.base-url=api.stability.ai/v1

Want to use a custom endpoint? Just override these properties. Easy peasy! ๐Ÿ‹


๐Ÿ› ๏ธ Step 6: Testing Your Image Generation API ๐ŸŽฏโ€‹

Letโ€™s test it! Send a prompt like:

GET /ask/image/withUrl?message=Horse runing behind Tiger

๐ŸŽ‰ Youโ€™ll get back a URL. Copy and paste it into your browser to see the generated image! ๐Ÿ†


๐ŸŽฌ Conclusion: AI Art, Made Easy! ๐Ÿโ€‹

With Spring AI, turning text into images is ridiculously easy and fun! ๐Ÿคฉ We covered: โœ… Adding dependencies ๐Ÿ—๏ธ โœ… Setting up image generation APIs ๐Ÿ–ผ๏ธ โœ… Configuring ImageModel ๐ŸŽจ โœ… Creating a REST Controller ๐Ÿš€ โœ… Generating images with a simple API call! ๐ŸŽญ

Now go aheadโ€”experiment, explore, and bring your AI-powered artistic visions to life! โœจ

Happy Coding & Image Generating! ๐Ÿš€๐ŸŽจ