|
| 1 | +package com.openai.example; |
| 2 | + |
| 3 | +import com.openai.client.OpenAIClient; |
| 4 | +import com.openai.client.okhttp.OpenAIOkHttpClient; |
| 5 | +import com.openai.core.MultipartField; |
| 6 | +import com.openai.models.images.ImageEditParams; |
| 7 | +import com.openai.models.images.ImageEditParams.Image; |
| 8 | +import com.openai.models.images.ImageModel; |
| 9 | +import java.io.InputStream; |
| 10 | + |
| 11 | +public final class ImageEditingExample { |
| 12 | + private ImageEditingExample() {} |
| 13 | + |
| 14 | + public static void main(String[] args) { |
| 15 | + // Configures using one of: |
| 16 | + // - The OPENAI_API_KEY environment variable |
| 17 | + // - The OPENAI_BASE_URL and AZURE_OPENAI_KEY environment variables |
| 18 | + OpenAIClient client = OpenAIOkHttpClient.fromEnv(); |
| 19 | + |
| 20 | + ClassLoader classloader = Thread.currentThread().getContextClassLoader(); |
| 21 | + String alohaFilename = "aloha.png"; |
| 22 | + InputStream alohaStream = classloader.getResourceAsStream(alohaFilename); |
| 23 | + String maskFilename = "aloha-mask.png"; |
| 24 | + InputStream maskStream = classloader.getResourceAsStream(maskFilename); |
| 25 | + |
| 26 | + ImageEditParams editParams = ImageEditParams.builder() |
| 27 | + .responseFormat(ImageEditParams.ResponseFormat.URL) |
| 28 | + .image(MultipartField.<Image>builder() |
| 29 | + .value(Image.ofInputStream(alohaStream)) |
| 30 | + .contentType("image/png") |
| 31 | + .filename(alohaFilename) |
| 32 | + .build()) |
| 33 | + .mask(MultipartField.<InputStream>builder() |
| 34 | + .value(maskStream) |
| 35 | + .contentType("image/png") |
| 36 | + .filename(maskFilename) |
| 37 | + .build()) |
| 38 | + .prompt("Fill the mask area with sand.") |
| 39 | + .model(ImageModel.DALL_E_2) |
| 40 | + .n(1) |
| 41 | + .build(); |
| 42 | + |
| 43 | + client.images().edit(editParams).data().orElseThrow().stream() |
| 44 | + .flatMap(image -> image.url().stream()) |
| 45 | + .forEach(System.out::println); |
| 46 | + } |
| 47 | +} |
0 commit comments