Search Tutorials


Spring Boot AI + Dall-E 3 Image Generation Example | JavaInUse

Spring Boot AI + Dall-E 3 Image Generation Example

In previous tutorial we had implemented Spring Boot + Azure OpenAI Hello World Example.
OpenaAI
In this tutorial we will be deploying Azure DallE 3 model. Then using a text prompt we will be generating images.
OpenaAI

Implementation

Go to Azure AI Foundry.
OpenaAI
Deploy Dall E 3 model. Copy the API key.
OpenaAI
Modify the application.properties as follows-
azure.openai.api.key=66T7YqY4CaqXB9dsdsdsq1RTz9p45goTXeVAJPpWKERdc5RxRLgJQQJ99BBACYeBjFXJ3w3AAABACOGmCSx
azure.openai.endpoint=https://javainuse-service.openai.azure.com/
azure.openai.dalle.model.id=dall-e-3
Create a class named ImageGenerationController as follows-
package com.azure.ai.openai.usage;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.ImageGenerationOptions;
import com.azure.ai.openai.models.ImageSize;
import com.azure.core.credential.AzureKeyCredential;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

@RestController
public class ImageGenerationController {
	@Value("${azure.openai.api.key}")
	private String azureOpenaiKey;

	@Value("${azure.openai.endpoint}")
	private String endpoint;

	@Value("${azure.openai.dalle.model.id}")
	private String deploymentOrModelId;

	private final ObjectMapper objectMapper = new ObjectMapper();

	@PostMapping("/generate-image")
	public ResponseEntity<ImageResponse> generateImage(@RequestBody ImageRequest imageRequest) {
		try {
			OpenAIClient client = new OpenAIClientBuilder().endpoint(endpoint)
					.credential(new AzureKeyCredential(azureOpenaiKey)).buildClient();

			ImageGenerationOptions imageOptions = new ImageGenerationOptions(imageRequest.getPrompt());

			if (imageRequest.getN() != null) {
				imageOptions.setN(imageRequest.getN());
			} else {
				imageOptions.setN(1);
			}

			if (imageRequest.getSize() != null) {
				try {
					String sizeValue = imageRequest.getSize().toUpperCase();

					if (sizeValue.equals("256X256")) {
						imageOptions.setSize(ImageSize.SIZE256X256);
					} else if (sizeValue.equals("512X512")) {
						imageOptions.setSize(ImageSize.SIZE512X512);
					} else if (sizeValue.equals("1024X1024")) {
						imageOptions.setSize(ImageSize.SIZE1024X1024);
					}
					// If none match, the default size will be used
				} catch (Exception e) {
					// Log error but continue with default size
					System.err.println("Invalid image size specified: " + e.getMessage());
				}
			}

			if (imageRequest.getUserId() != null) {
				imageOptions.setUser(imageRequest.getUserId());
			}

			Object responseObject = client.getImages(imageOptions);

			ImageResponse response = parseImageResponse(responseObject);

			return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(response);
		} catch (Exception ex) {
			ex.printStackTrace();
			return ResponseEntity.internalServerError()
					.body(new ImageResponse("Error generating image: " + ex.getMessage()));
		}
	}

	private ImageResponse parseImageResponse(Object responseObject) {
		ImageResponse response = new ImageResponse();
		List<String> imageUrls = new ArrayList<>();

		try {
			String jsonString = objectMapper.writeValueAsString(responseObject);

			JsonNode rootNode = objectMapper.readTree(jsonString);
			JsonNode dataNode = rootNode.path("data");

			if (dataNode.isArray()) {
				for (JsonNode item : dataNode) {
					JsonNode urlNode = item.path("url");
					if (!urlNode.isMissingNode()) {
						imageUrls.add(urlNode.asText());
					}
				}
			}

			response.setImageUrls(imageUrls);
		} catch (Exception e) {
			e.printStackTrace();
			response.setError("Error parsing response: " + e.getMessage());
		}

		return response;
	}

	public static class ImageRequest {
		private String prompt;
		private Integer n;
		private String size;
		private String userId;

		public String getPrompt() {
			return prompt;
		}

		public void setPrompt(String prompt) {
			this.prompt = prompt;
		}

		public Integer getN() {
			return n;
		}

		public void setN(Integer n) {
			this.n = n;
		}

		public String getSize() {
			return size;
		}

		public void setSize(String size) {
			this.size = size;
		}

		public String getUserId() {
			return userId;
		}

		public void setUserId(String userId) {
			this.userId = userId;
		}
	}

	public static class ImageResponse {
		private List<String> imageUrls = new ArrayList<>();
		private String error;

		public ImageResponse() {
		}

		public ImageResponse(String error) {
			this.error = error;
		}

		public List<String> getImageUrls() {
			return imageUrls;
		}

		public void setImageUrls(List<String> imageUrls) {
			this.imageUrls = imageUrls;
		}

		public String getError() {
			return error;
		}

		public void setError(String error) {
			this.error = error;
		}
	}

}
If we now call the /image url along with the prompt, we get the image url as response.
OpenaAI

OpenaAI

Download Source Code

Download it -
Spring Boot AI + Azure OpenAI + Image Example