|
| 1 | +import com.alibaba.dashscope.aigc.imagegeneration.*; |
| 2 | +import com.alibaba.dashscope.common.Role; |
| 3 | +import com.alibaba.dashscope.exception.NoApiKeyException; |
| 4 | +import com.alibaba.dashscope.exception.UploadFileException; |
| 5 | +import com.alibaba.dashscope.task.AsyncTaskListParam; |
| 6 | +import io.reactivex.Flowable; |
| 7 | + |
| 8 | +import java.util.Arrays; |
| 9 | +import java.util.Collections; |
| 10 | + |
| 11 | +public class ImageGenerationUsage { |
| 12 | + |
| 13 | + private static final String DASHSCOPE_API_KEY = System.getenv("DASHSCOPE_API_KEY"); |
| 14 | + |
| 15 | + static ImageGenerationMessage t2iMessage = ImageGenerationMessage.builder() |
| 16 | + .role(Role.USER.getValue()) |
| 17 | + .content(Collections.singletonList( |
| 18 | + Collections.singletonMap("text", "一间有着精致窗户的花店,漂亮的木质门,摆放着花朵") |
| 19 | + )).build(); |
| 20 | + |
| 21 | + public static void t2iUsage() throws NoApiKeyException, UploadFileException{ |
| 22 | + ImageGenerationParam param = ImageGenerationParam.builder() |
| 23 | + .apiKey(DASHSCOPE_API_KEY) |
| 24 | + .model(ImageGeneration.Models.WanX2_6_T2I) |
| 25 | + .n(3) |
| 26 | + .messages(Collections.singletonList(t2iMessage)) |
| 27 | + .build(); |
| 28 | + |
| 29 | + ImageGeneration ig = new ImageGeneration(); |
| 30 | + ImageGenerationResult res =ig.call(param); |
| 31 | + System.out.println(res); |
| 32 | + } |
| 33 | + |
| 34 | + public static void imageUsage() throws NoApiKeyException, UploadFileException{ |
| 35 | + ImageGenerationMessage userMessage = ImageGenerationMessage.builder() |
| 36 | + .role(Role.USER.getValue()) |
| 37 | + .content(Arrays.asList( |
| 38 | + // --------------- |
| 39 | + // image 支持本地文件 |
| 40 | + // --------------- |
| 41 | + Collections.singletonMap("text", "参考图1的风格和图2的背景,生成番茄炒蛋"), |
| 42 | + Collections.singletonMap("image", "https://cdn.wanx.aliyuncs.com/tmp/pressure/umbrella1.png"), |
| 43 | + Collections.singletonMap("image", "https://img.alicdn.com/imgextra/i3/O1CN01SfG4J41UYn9WNt4X1_!!6000000002530-49-tps-1696-960.webp") |
| 44 | + )).build(); |
| 45 | + ImageGenerationParam param = ImageGenerationParam.builder() |
| 46 | + .apiKey(DASHSCOPE_API_KEY) |
| 47 | + .model(ImageGeneration.Models.WanX2_6_IMAGE) |
| 48 | + .n(1) |
| 49 | + .messages(Collections.singletonList(userMessage)) |
| 50 | + .build(); |
| 51 | + |
| 52 | + ImageGeneration ig = new ImageGeneration(); |
| 53 | + ImageGenerationResult res =ig.call(param); |
| 54 | + System.out.println(res); |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + public static void t2iUsageAsync() throws NoApiKeyException, UploadFileException{ |
| 59 | + ImageGenerationParam param = ImageGenerationParam.builder() |
| 60 | + .apiKey(DASHSCOPE_API_KEY) |
| 61 | + .model(ImageGeneration.Models.WanX2_6_T2I) |
| 62 | + .n(1) |
| 63 | + .messages(Collections.singletonList(t2iMessage)) |
| 64 | + .build(); |
| 65 | + |
| 66 | + ImageGeneration ig = new ImageGeneration(); |
| 67 | + ImageGenerationResult res = ig.asyncCall(param); |
| 68 | + System.out.println(res); |
| 69 | + |
| 70 | + String taskId = res.getOutput().getTaskId(); |
| 71 | + testAsyncTask(taskId); |
| 72 | + } |
| 73 | + |
| 74 | + public static void testAsyncTask(String taskId) throws NoApiKeyException { |
| 75 | + ImageGeneration ig = new ImageGeneration(); |
| 76 | + System.out.println(); |
| 77 | + System.out.println(); |
| 78 | + System.out.println("-----------async-t2i-fetch-res-----------"); |
| 79 | + ImageGenerationResult res = ig.fetch(taskId, DASHSCOPE_API_KEY); |
| 80 | + System.out.println(res); |
| 81 | + |
| 82 | + try { |
| 83 | + System.out.println(); |
| 84 | + System.out.println(); |
| 85 | + System.out.println("-----------async-t2i-cancel-res-----------"); |
| 86 | + res = ig.cancel(taskId, DASHSCOPE_API_KEY); |
| 87 | + System.out.println(res); |
| 88 | + }catch (Exception e){ |
| 89 | + System.out.println(e.getMessage()); |
| 90 | + } |
| 91 | + |
| 92 | + System.out.println(); |
| 93 | + System.out.println(); |
| 94 | + System.out.println("-----------async-t2i-wait-res-----------"); |
| 95 | + res = ig.wait(taskId, DASHSCOPE_API_KEY); |
| 96 | + System.out.println(res); |
| 97 | + |
| 98 | + AsyncTaskListParam param = AsyncTaskListParam.builder().build(); |
| 99 | + System.out.println(); |
| 100 | + System.out.println(); |
| 101 | + System.out.println("-----------async-task-list-res-----------"); |
| 102 | + ImageGenerationListResult res2 = ig.list(param); |
| 103 | + System.out.println(res2); |
| 104 | + } |
| 105 | + |
| 106 | + public static void imageUsageStream() throws NoApiKeyException, UploadFileException{ |
| 107 | + ImageGenerationMessage userMessage = ImageGenerationMessage.builder() |
| 108 | + .role(Role.USER.getValue()) |
| 109 | + .content(Collections.singletonList( |
| 110 | + Collections.singletonMap("text", "给我一个1张图辣椒炒肉教程") |
| 111 | + )).build(); |
| 112 | + ImageGenerationParam param = ImageGenerationParam.builder() |
| 113 | + .apiKey(DASHSCOPE_API_KEY) |
| 114 | + .model(ImageGeneration.Models.WanX2_6_IMAGE) |
| 115 | + .messages(Collections.singletonList(userMessage)) |
| 116 | + .stream(true) |
| 117 | + .enableInterleave(true) |
| 118 | + .maxImages(1) |
| 119 | + .build(); |
| 120 | + |
| 121 | + ImageGeneration ig = new ImageGeneration(); |
| 122 | + Flowable<ImageGenerationResult> res = ig.streamCall(param); |
| 123 | + res.blockingForEach(System.out::println); |
| 124 | + } |
| 125 | + |
| 126 | + public static void main(String[] args) { |
| 127 | + try { |
| 128 | + t2iUsage(); |
| 129 | +// imageUsage(); |
| 130 | +// t2iUsageAsync(); |
| 131 | +// imageUsageStream(); |
| 132 | + }catch (NoApiKeyException | UploadFileException e){ |
| 133 | + System.out.println(e.getMessage()); |
| 134 | + } |
| 135 | + System.exit(0); |
| 136 | + } |
| 137 | + |
| 138 | +} |
0 commit comments