Toll Free:

1800 889 7020

How to Generate Images from Text with SpringAI

With technology, artificial intelligence (AI) integration in applications has become innovative and efficient. The Spring AI Project is an influential add-on framework based on Spring, making it easy to bring AI capabilities to Spring-based applications. Java application development services have embraced this trend, leveraging AI to enhance application functionality and user experiences. In this article, we take a brief look at Spring AI and guide you through creating an image from a user prompt.

1. What is Spring AI?

The Spring AI Project was an extension project to the Spring framework where additional capabilities in the form of tools and libraries that assist in the integration of AI into Spring boot application development were introduced. It connects AI models with end-user programs ensuring easy use and maximum flexibility and offers rich support for many AI services and frameworks. Whether one is working with machine learning models, natural language processing, or computer vision, the Spring AI will help embed all the other AI capabilities into applications in the most effortless manner possible.

2. How to generate an OpenAI API key?

We will now understand how to obtain an OpenAI key. We’ll use an OpenAI model to generate an image.

  • Create an Account or Sign In: Go to the OpenAI website and either sign up for a new account or log in to your existing one.
  • Access API Keys: After logging in, head to the API Keys section within your account dashboard.
  • Generate a New API Key: Select the option to create a new API key. Assign a name or description to the key for easy reference.
  • Generate and Save the Key: Click the Generate Key button. Make sure to copy the newly created API key and store it in a safe place, as you will need it to access OpenAI’s 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 = 'com.spring.ai.imageclient'
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=spring-ai-image-generation
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 to the Image Client. The OpenAI key’s value will be read from the properties file and injected using Spring’s @Value annotation.

package com.spring.ai.imageclient.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) {
        System.out.println("API Key: " + apiKey);
        System.out.println("Creating OpenAI Image Client");
        return new OpenAiImageClient(new OpenAiImageApi(apiKey));
    }
}

3.4 Creating the Controller

The ImageGenerationController class is a REST controller in a Spring Boot application, responsible for handling HTTP requests related to image generation. The PromptRequest and PromptResponse are the DTO classes to map the request and response.

package com.spring.ai.imageclient.controller;

import com.spring.ai.imageclient.dto.PromptRequest;
import com.spring.ai.imageclient.dto.PromptResponse;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
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;
    }

    /*
     * Generate an image based on the prompt
     * @param request - prompt request
     * @return prompt response
     */
    @PostMapping("/generate")
    public PromptResponse getImage(@RequestBody PromptRequest request) {
        final String prompt = request.getPrompt();

        // input validation
        if (StringUtils.isEmpty(prompt)) {
            System.out.println("Prompt is required");
            throw new IllegalArgumentException("Prompt is required");
        }

        System.out.println("Prompt: " + prompt);
        System.out.println("Generating image...");

        // image options
        OpenAiImageOptions imageOptions = OpenAiImageOptions.builder()
                .withQuality("standard")
                .withHeight(1024)
                .withN(1)
                .withWidth(1792)
                .build();

        // image prompt
        ImagePrompt imagePrompt = new ImagePrompt(prompt, imageOptions);

        System.out.println("Calling image client...");
        // class ep
        Image img = imageClient.call(imagePrompt).getResult().getOutput();

        System.out.println("Image generated successfully");
        return new PromptResponse(img.getUrl());
    }
}

3.5 Create the Main file

Create a Spring Boot application to initialize the application.

package com.spring.ai.imageclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
        System.out.println("App started successfully");
    }
}

3.6 Run the application and Demo

Now start the application and open the Postman tool to hit the api endpoint. Import the below curl POST request in the Postman tool. Remember to change the prompt with a valid instruction.

curl 'http://localhost:8080/api/v1/image/generate' -H 'Content-Type: application/json' -d '{
    "prompt": "a white punk lion with futuristic background"
}'

If everything goes well the response containing the image url will be returned from OpenAI and sent to the client as a json response.

{
  "generatedUrl": "https://oaidalleapiprodscus.blob.core.windows.net/private/org-9Ys0ZRqaTWzWMNLv1XefTZvC/user-YJ5pfUPFbA4XmjGWBKadjply/img-nHbvdQQAgcu39Fi2APtj3yO2.png?st=2024-08-25T02%3A34%3A58Z&se=2024-08-25T04%3A34%3A58Z&sp=r&sv=2024-08-04&sr=b&rscd=inline&rsct=image/png&skoid=d505667d-d6c1-4a0a-bac7-5c84a87759f8&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2024-08-24T23%3A11%3A39Z&ske=2024-08-25T23%3A11%3A39Z&sks=b&skv=2024-08-04&sig=Zz4PHhKvcGlu4F28yFg1aa/qX6f3%2BYpqM7eMJW/Y54U%3D"
}

// Fig. 1: Demo image

springai generateimagefromtext demooutput

4. Conclusion

In conclusion, as Generative AI Development Services continues to reshape the technological landscape, tools like Spring AI are essential for seamlessly integrating AI into applications. By leveraging Spring AI, Spring developers can easily add advanced AI capabilities to their Spring-based projects, enabling innovative solutions like generating images from user prompts.

5. Download the code

You can download the full code here: Download

Cleveland

Scroll to Top