From 49b086ad105473a2eb15af59d851e8fd655d51fb Mon Sep 17 00:00:00 2001 From: xupei Date: Mon, 3 Jun 2024 17:13:48 +0800 Subject: [PATCH] =?UTF-8?q?generate=20=E6=B7=BB=E5=8A=A0Logo=E4=B8=8ESloga?= =?UTF-8?q?n?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../da/common/RabbitMQ/GenerateConsumer.java | 124 +++++++------- .../ai/da/common/constant/CommonConstant.java | 6 + .../enums/CollectionLevel2TypeEnum.java | 19 +- .../ai/da/controller/GenerateController.java | 4 +- .../ai/da/mapper/primary/entity/Generate.java | 12 +- .../dto/GenerateThroughImageTextDTO.java | 15 +- .../ai/da/model/dto/GenerateToPythonDTO.java | 5 +- .../java/com/ai/da/python/PythonService.java | 12 +- .../da/service/impl/GenerateServiceImpl.java | 162 +++++++++++++++--- src/main/resources/messages_en.properties | 4 +- 10 files changed, 261 insertions(+), 102 deletions(-) diff --git a/src/main/java/com/ai/da/common/RabbitMQ/GenerateConsumer.java b/src/main/java/com/ai/da/common/RabbitMQ/GenerateConsumer.java index 876dfd2c..ec329d28 100644 --- a/src/main/java/com/ai/da/common/RabbitMQ/GenerateConsumer.java +++ b/src/main/java/com/ai/da/common/RabbitMQ/GenerateConsumer.java @@ -67,7 +67,11 @@ public class GenerateConsumer { // redisUtil.removeFromSet(cancelSetKey, uniqueId); } else { // GenerateCollectionVO generateCollectionVO = generateService.generateThroughImageText(generateThroughImageTextDTO); - generateService.generateThroughImageText(generateThroughImageTextDTO); + try { + generateService.generateThroughImageText(generateThroughImageTextDTO); + }catch (Exception e){ + log.error(e.getMessage()); + } // 将消息从redis排队队列中删除,需保证被消费的消息存储到db之后再从redis删除 redisUtil.removeFromZSet(consumptionOrderKey, uniqueId); /*if (!Objects.isNull(generateCollectionVO)) { @@ -154,63 +158,63 @@ public class GenerateConsumer { } -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer1(Message msg, Channel channel) { -// generate(msg, channel, "consumer 1"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer2(Message msg, Channel channel) { -// generate(msg, channel, "consumer 2"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer3(Message msg, Channel channel) { -// generate(msg, channel, "consumer 3"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer4(Message msg, Channel channel) { -// generate(msg, channel, "consumer 4"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer5(Message msg, Channel channel) { -// generate(msg, channel, "consumer 5"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer6(Message msg, Channel channel) { -// generate(msg, channel, "consumer 6"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer7(Message msg, Channel channel) { -// generate(msg, channel, "consumer 7"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer8(Message msg, Channel channel) { -// generate(msg, channel, "consumer 8"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_QUEUE) -// @RabbitHandler -// public void generateConsumer9(Message msg, Channel channel) { -// generate(msg, channel, "consumer 9"); -// } -// -// @RabbitListener(queues = MQConfig.GENERATE_RESULT_QUEUE) -// @RabbitHandler -// public void getGenerateResult(Message msg, Channel channel) { -// processGenerateResult(msg, channel); -// } + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer1(Message msg, Channel channel) { + generate(msg, channel, "consumer 1"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer2(Message msg, Channel channel) { + generate(msg, channel, "consumer 2"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer3(Message msg, Channel channel) { + generate(msg, channel, "consumer 3"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer4(Message msg, Channel channel) { + generate(msg, channel, "consumer 4"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer5(Message msg, Channel channel) { + generate(msg, channel, "consumer 5"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer6(Message msg, Channel channel) { + generate(msg, channel, "consumer 6"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer7(Message msg, Channel channel) { + generate(msg, channel, "consumer 7"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer8(Message msg, Channel channel) { + generate(msg, channel, "consumer 8"); + } + + @RabbitListener(queues = MQConfig.GENERATE_QUEUE) + @RabbitHandler + public void generateConsumer9(Message msg, Channel channel) { + generate(msg, channel, "consumer 9"); + } + + @RabbitListener(queues = MQConfig.GENERATE_RESULT_QUEUE) + @RabbitHandler + public void getGenerateResult(Message msg, Channel channel) { + processGenerateResult(msg, channel); + } } diff --git a/src/main/java/com/ai/da/common/constant/CommonConstant.java b/src/main/java/com/ai/da/common/constant/CommonConstant.java index 252350c5..0bc1af58 100644 --- a/src/main/java/com/ai/da/common/constant/CommonConstant.java +++ b/src/main/java/com/ai/da/common/constant/CommonConstant.java @@ -17,4 +17,10 @@ public class CommonConstant { public static final Integer NUMBER_10080 = 10080; } + public static final String GENERATE_PATH = "/api/generate_image"; + + public static final String GENERATE_SINGLE_LOGO = "/api/generate_single_logo"; + + public static final String GENERATE_SLOGAN = "/api/slogan"; + } diff --git a/src/main/java/com/ai/da/common/enums/CollectionLevel2TypeEnum.java b/src/main/java/com/ai/da/common/enums/CollectionLevel2TypeEnum.java index cc344c9e..a538f113 100644 --- a/src/main/java/com/ai/da/common/enums/CollectionLevel2TypeEnum.java +++ b/src/main/java/com/ai/da/common/enums/CollectionLevel2TypeEnum.java @@ -1,5 +1,7 @@ package com.ai.da.common.enums; +import java.util.Arrays; +import java.util.List; import java.util.stream.Stream; /** @@ -36,7 +38,18 @@ public enum CollectionLevel2TypeEnum { * 男装下装 */ BOTTOMS("Bottoms"), - ; + /** + * 印花-logo + */ + LOGO("Logo"), + /** + * 印花-slogan + */ + SLOGAN("Slogan"), + /** + * 印花-图案 + */ + Pattern("Pattern"); private String realName; @@ -51,4 +64,8 @@ public enum CollectionLevel2TypeEnum { public static CollectionLevel2TypeEnum of(String realName) { return Stream.of(CollectionLevel2TypeEnum.values()).filter(v -> v.getRealName().equals(realName)).findFirst().orElse(null); } + + public static List printType() { + return Arrays.asList(LOGO.getRealName(), SLOGAN.getRealName(), Pattern.getRealName()); + } } diff --git a/src/main/java/com/ai/da/controller/GenerateController.java b/src/main/java/com/ai/da/controller/GenerateController.java index 4e1566dd..74743856 100644 --- a/src/main/java/com/ai/da/controller/GenerateController.java +++ b/src/main/java/com/ai/da/controller/GenerateController.java @@ -27,11 +27,11 @@ public class GenerateController { @Resource private GenerateService generateService; - @ApiOperation("自动识别sketch的caption 暂时未上") + /*@ApiOperation("自动识别sketch的caption 暂时未上") @PostMapping("/caption") public Response generateCaption(@RequestParam Long sketchElementId) { return Response.success(generateService.generateCaption(sketchElementId)); - } + }*/ /*@ApiOperation("通过文字、图片生成图片") @PostMapping("/sketchAndPrint") diff --git a/src/main/java/com/ai/da/mapper/primary/entity/Generate.java b/src/main/java/com/ai/da/mapper/primary/entity/Generate.java index d58d72ec..6d1148db 100644 --- a/src/main/java/com/ai/da/mapper/primary/entity/Generate.java +++ b/src/main/java/com/ai/da/mapper/primary/entity/Generate.java @@ -33,10 +33,15 @@ public class Generate { private String uniqueId; /** - * Sketchboard Printboard + * Sketchboard Printboard Moodboard */ private String level1Type; + /** + * Slogan Logo Pattern + */ + private String level2Type; + /** * 图片来源 collection | library */ @@ -62,6 +67,11 @@ public class Generate { */ private String modelName; + /** + * Logo 种子 相同种子 相同prompt 输出一定相同 + */ + private String seed; + /** * 创建时间 */ diff --git a/src/main/java/com/ai/da/model/dto/GenerateThroughImageTextDTO.java b/src/main/java/com/ai/da/model/dto/GenerateThroughImageTextDTO.java index 602f1886..398fa9ec 100644 --- a/src/main/java/com/ai/da/model/dto/GenerateThroughImageTextDTO.java +++ b/src/main/java/com/ai/da/model/dto/GenerateThroughImageTextDTO.java @@ -14,24 +14,25 @@ public class GenerateThroughImageTextDTO { @ApiModelProperty("用户id") Long userId; - @ApiModelProperty("caption") + @ApiModelProperty("caption | prompt") String text; @ApiModelProperty("图片在t_collection_element表中的id") Long collectionElementId; - @NotBlank(message = "you have to choose the generate type") + // todo 后续取消这个字段的传输,由后端自行判断相关参数是否有值 +// @NotBlank(message = "you have to choose the generate type") @ApiModelProperty("text image text-image") String generateType; - @ApiModelProperty("图片是update,还是从library中选择") + @ApiModelProperty("图片是update,还是从library中选择 collection || library") String designType; @NotBlank(message = "level1Type cannot be empty!") @ApiModelProperty("Moodboard Printboard Sketchboard MarketingSketch") String level1Type; - @ApiModelProperty("Outwear Dress Blouse Skirt Trousers") + @ApiModelProperty("Outwear Dress Blouse Skirt Trousers || Logo Slogan Pattern") String level2Type; @ApiModelProperty("性别") @@ -50,4 +51,10 @@ public class GenerateThroughImageTextDTO { @NotNull(message = "Please check if the required fields are empty.(isTestUser)") @ApiModelProperty("是否是测试用户") Boolean isTestUser; + + @ApiModelProperty("页面上用户设计的slogan所截的图片") + String sloganBase64; + + @ApiModelProperty("种子 取值范围 0~99999") + String seed; } diff --git a/src/main/java/com/ai/da/model/dto/GenerateToPythonDTO.java b/src/main/java/com/ai/da/model/dto/GenerateToPythonDTO.java index c08a5f90..16fa7988 100644 --- a/src/main/java/com/ai/da/model/dto/GenerateToPythonDTO.java +++ b/src/main/java/com/ai/da/model/dto/GenerateToPythonDTO.java @@ -1,6 +1,5 @@ package com.ai.da.model.dto; -import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @@ -26,6 +25,10 @@ public class GenerateToPythonDTO { // taskId的最后拼接用户id private String tasks_id; + private String seed; + + private String svg; + public GenerateToPythonDTO(String tasks_id, String prompt, String image_url, String mode, String category, String gender) { this.image_url = image_url; this.category = category; diff --git a/src/main/java/com/ai/da/python/PythonService.java b/src/main/java/com/ai/da/python/PythonService.java index 9e9d82b0..3610c7c2 100644 --- a/src/main/java/com/ai/da/python/PythonService.java +++ b/src/main/java/com/ai/da/python/PythonService.java @@ -66,7 +66,7 @@ public class PythonService { private String accessPythonIp; @Value("${access.python.port:''}") private String accessPythonPort; - @Value("${access.python.sr}") + @Value("${access.python.address}") private String srPythonPort; @Value("${minio.bucketName.gradient}") private String gradientBucketName; @@ -2936,7 +2936,7 @@ public class PythonService { throw new BusinessException("system error!"); } - public Boolean generateSketchOrPrint(GenerateToPythonDTO generateToPythonDTO) { + public Boolean generateSketchOrPrint(String params, String servicePath) { //限流校验 // AccessLimitUtils.validate("generateSketchOrPrint", 5); OkHttpClient client = new OkHttpClient().newBuilder() @@ -2946,13 +2946,14 @@ public class PythonService { .writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒) .build(); MediaType mediaType = MediaType.parse("application/json"); - RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue)); +// RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue)); + RequestBody body = RequestBody.create(mediaType, params); Request request = new Request.Builder() // .url("http://18.167.251.121:9992") // .url("http://127.0.0.1:5000/api/diffusion") // .url(accessPythonIp + ":" + accessPythonPort + "/api/diffusion") // .url(accessPythonIp + ":" + accessPythonPort + "/api/generate_image") - .url(srPythonPort + "/api/generate_image") + .url(srPythonPort + servicePath) .method("POST", body) // .addHeader("Authorization", "Basic YWlkbGFiOjEyMw==") .addHeader("Content-Type", "application/json") @@ -2960,7 +2961,8 @@ public class PythonService { Response response = null; String bodyString; try { - log.info("generateSketchOrPrint请求入参content###{}", JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue)); +// log.info("generateSketchOrPrint请求入参content###{}", JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue)); + log.info("generateSketchOrPrint请求入参content###{}", params); response = client.newCall(request).execute(); } catch (IOException ioException) { log.error("PythonService##generateSketchOrPrint异常###{}", ExceptionUtil.getThrowableList(ioException)); diff --git a/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java b/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java index fbe8715d..4033b8aa 100644 --- a/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/GenerateServiceImpl.java @@ -3,6 +3,7 @@ package com.ai.da.service.impl; import com.ai.da.common.config.exception.BusinessException; import com.ai.da.common.constant.CommonConstant; import com.ai.da.common.context.UserContext; +import com.ai.da.common.enums.CollectionLevel2TypeEnum; import com.ai.da.common.enums.GenerateModeEnum; import com.ai.da.common.enums.ModelNameEnum; import com.ai.da.common.utils.*; @@ -21,7 +22,7 @@ import com.ai.da.service.GenerateService; import com.ai.da.service.LibraryService; import com.ai.da.service.RabbitMQService; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; +import com.alibaba.fastjson.serializer.SerializerFeature; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; @@ -32,13 +33,11 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.CollectionUtils; import javax.annotation.Resource; import java.io.IOException; import java.time.LocalDateTime; import java.util.*; -import java.util.stream.Collectors; import static com.ai.da.common.enums.CollectionLevel1TypeEnum.*; @@ -85,6 +84,9 @@ public class GenerateServiceImpl extends ServiceImpl i @Value("${redis.key.generateResult}") private String generateResultKey; + @Value("${minio.bucketName.slogan}") + private String sloganBucket; + @Override public GenerateCaptionVO generateCaption(Long sketchElementId) { CollectionElement collectionElement = collectionElementMapper.selectById(sketchElementId); @@ -110,6 +112,8 @@ public class GenerateServiceImpl extends ServiceImpl i generate.setAccountId(accountId); generate.setUniqueId(generateThroughImageTextDTO.getUniqueId()); generate.setLevel1Type(generateThroughImageTextDTO.getLevel1Type()); + generate.setLevel2Type(generateThroughImageTextDTO.getLevel2Type()); + generate.setSeed(StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getSeed()) ? "" : generateThroughImageTextDTO.getSeed()); // 当level1type是sketchboard时,存数据库需要加上当前性别 generate.setGenerateType(generate.getLevel1Type().equals(SKETCH_BOARD.getRealName()) ? generateType + " (" + generateThroughImageTextDTO.getGender() + ")" : @@ -119,12 +123,15 @@ public class GenerateServiceImpl extends ServiceImpl i generate.setElementSource(StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getDesignType()) ? null : generateThroughImageTextDTO.getDesignType()); String text = generateThroughImageTextDTO.getText(); + generate.setText(text); Long elementId = generateThroughImageTextDTO.getCollectionElementId(); - validateGeneraType(generate, text, elementId, generateType); +// validateGeneraType(generate, text, elementId); if (!StringUtil.isNullOrEmpty(text)) { text = modifyPrompt(text, generate, generateThroughImageTextDTO.getLevel1Type()); } + + // todo 这一步现在还是有必要的吗? // 2.1 sketch或print在t_collection_element表/t_library表中的信息是否需要更新 如 level2Type CollectionElement collectionElement = collectionElementService.editLevel2Type(elementId, generateThroughImageTextDTO.getLevel2Type(), generateThroughImageTextDTO.getDesignType()); @@ -134,15 +141,38 @@ public class GenerateServiceImpl extends ServiceImpl i GenerateModeEnum.TEXT_IMAGE.getType(); String category = generateThroughImageTextDTO.getLevel1Type().equals(SKETCH_BOARD.getRealName()) ? "sketch" : generateThroughImageTextDTO.getLevel1Type().equals(PRINT_BOARD.getRealName()) ? "print" : "moodboard"; -// AsyncCallerUtil asyncCallerUtil = new AsyncCallerUtil(); -// List generatedSketchUrl = asyncCallerUtil.generate(new GenerateToPythonDTO(accountId, Objects.isNull(collectionElement) ? "" : collectionElement.getUrl(), -// category, text, mode, "1", generateThroughImageTextDTO.getGender(), generateThroughImageTextDTO.getUniqueId())); - Boolean requestResult = pythonService.generateSketchOrPrint(new GenerateToPythonDTO(generateThroughImageTextDTO.getUniqueId(), text, Objects.isNull(collectionElement) ? "" : collectionElement.getUrl(), - mode, category, generateThroughImageTextDTO.getGender())); -// log.info("generate 响应 : " + generatedSketchUrl); -// if (CollectionUtils.isEmpty(generatedSketchUrl)) { -// return null; -// } + String path = CommonConstant.GENERATE_PATH; + String jsonString = ""; + HashMap params = new HashMap<>(); + // 3.1 确定不同类型的印花分别调哪个接口 + if (generateThroughImageTextDTO.getLevel1Type().equals(PRINT_BOARD.getRealName())){ + switch(generateThroughImageTextDTO.getLevel2Type()){ + case "Logo": + path = CommonConstant.GENERATE_SINGLE_LOGO; + params.put("tasks_id",generateThroughImageTextDTO.getUniqueId()); + params.put("prompt", text); + params.put("seed", generateThroughImageTextDTO.getSeed()); + jsonString = JSON.toJSONString(params, SerializerFeature.WriteMapNullValue); + break; + case "Slogan": + path = CommonConstant.GENERATE_SLOGAN; + params.put("tasks_id",generateThroughImageTextDTO.getUniqueId()); + params.put("prompt", text); + params.put("svg", collectionElement.getUrl()); + jsonString = JSON.toJSONString(params, SerializerFeature.WriteMapNullValue); + break; + case "Pattern": + GenerateToPythonDTO generateToPythonDTO = new GenerateToPythonDTO(generateThroughImageTextDTO.getUniqueId(), text, Objects.isNull(collectionElement) ? "" : collectionElement.getUrl(), + mode, category, generateThroughImageTextDTO.getGender()); + jsonString = JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue); + } + }else { + GenerateToPythonDTO generateToPythonDTO = new GenerateToPythonDTO(generateThroughImageTextDTO.getUniqueId(), text, Objects.isNull(collectionElement) ? "" : collectionElement.getUrl(), + mode, category, generateThroughImageTextDTO.getGender()); + jsonString = JSON.toJSONString(generateToPythonDTO, SerializerFeature.WriteMapNullValue); + } + + Boolean requestResult = pythonService.generateSketchOrPrint(jsonString, path); // 4、将请求信息落库,将本次generate的请求信息添加到t_generate表中 save(generate); @@ -229,8 +259,24 @@ public class GenerateServiceImpl extends ServiceImpl i redisUtil.addToString(key, new Gson().toJson(generateResultVO), CommonConstant.GENERATE_RESULT_EXPIRE_TIME); } - private void validateGeneraType(Generate generate, String text, Long elementId, String generateType) { - switch (generateType) { + private void validateGeneraType(Generate generate, String text, Long elementId) { + String generateType = ""; + if (StringUtil.isNullOrEmpty(text.trim()) && Objects.isNull(elementId)) { + throw new BusinessException("please.input.the.caption.or.choose.an.image"); + } else if (!StringUtil.isNullOrEmpty(text.trim()) && !Objects.isNull(elementId)) { + generateType = "text-image"; + generate.setText(text); + generate.setElementId(elementId); + } else if (!StringUtil.isNullOrEmpty(text.trim())) { + generateType = "text"; + generate.setText(text); + } else if (!Objects.isNull(elementId)) { + generateType = "image"; + generate.setElementId(elementId); + } + generate.setGenerateType(generateType); + + /*switch (generateType) { case "text": if (StringUtil.isNullOrEmpty(text)) { throw new BusinessException("please.input.the.caption"); @@ -250,7 +296,7 @@ public class GenerateServiceImpl extends ServiceImpl i generate.setText(text); generate.setElementId(elementId); default: - } + }*/ } private String modifyPrompt(String userInput, Generate generate, String level1Type) { @@ -263,13 +309,15 @@ public class GenerateServiceImpl extends ServiceImpl i break; case "Printboard": if (userInput.contains("Painting Style")) { - userInput = "Picasso,increased color saturation,increased glossiness," + translated; + text = "Picasso,increased color saturation,increased glossiness," + translated + ", fabric print, high quality"; } else if (userInput.contains("Illustration Style")) { - userInput = "Flat coating,romantic,soft,pencil strokes,accentuating and widening the depth of pencil strokes,paper patterns,block colors,crayons,reducing image contrast,and hand drawn painting marks," + translated; + text = "Flat coating,romantic,soft,pencil strokes,accentuating and widening the depth of pencil strokes,paper patterns,block colors,crayons,reducing image contrast,and hand drawn painting marks," + translated + ", fabric print, high quality"; } else if (userInput.contains("Real Style")) { - userInput = "Still life photography,hyper realism,3d,deepened projection,increased permutation value,increased concavity and convexity value," + translated; + text = "Still life photography,hyper realism,3d,deepened projection,increased permutation value,increased concavity and convexity value," + translated + ", fabric print, high quality"; + }else { + text = translated; } - text = userInput + ", fabric print, high quality"; +// text = userInput + ", fabric print, high quality"; // generate.setText(text); break; case "Sketchboard": @@ -408,10 +456,10 @@ public class GenerateServiceImpl extends ServiceImpl i if (Objects.isNull(generateThroughImageTextDTO.getUserId())) { throw new BusinessException("userId cannot be empty"); } - String generateType = generateThroughImageTextDTO.getGenerateType(); + /*String generateType = generateThroughImageTextDTO.getGenerateType(); if (!GenerateModeEnum.getGenerateModeList().contains(generateType)) { throw new BusinessException("unknown.generate.type"); - } + }*/ // 判断试用用户是否还有剩余试用机会 int trialsCount = 0; @@ -422,15 +470,75 @@ public class GenerateServiceImpl extends ServiceImpl i } } - String text = generateThroughImageTextDTO.getText(); - Long elementId = generateThroughImageTextDTO.getCollectionElementId(); - validateGeneraType(new Generate(), text, elementId, generateType); + int times = 4; + // 当level1Type为Print_board时,level2Type为pattern时需要确定generateType + if (generateThroughImageTextDTO.getLevel1Type().equals(PRINT_BOARD.getRealName())){ + if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getLevel2Type())){ + throw new BusinessException("level2Type.cannot.be.empty"); + }else if (!CollectionLevel2TypeEnum.printType().contains(generateThroughImageTextDTO.getLevel2Type())){ + throw new BusinessException("unknown.parameter.level2Type"); + } + + // Pattern 参数校验 + if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.Pattern.getRealName())){ + String text = generateThroughImageTextDTO.getText(); + Long elementId = generateThroughImageTextDTO.getCollectionElementId(); + Generate generate = new Generate(); + validateGeneraType(generate, text, elementId); + // 校验后获取 + generateThroughImageTextDTO.setGenerateType(generate.getGenerateType()); + } + // Slogan 参数校验 + if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.SLOGAN.getRealName())){ + if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getSloganBase64())){ + log.error("Printboard-Slogan模式下,slogan image为空"); + throw new BusinessException("Slogan can not be empty!"); + } + + // 将图片上传到图片服务器 + String path = minioUtil.base64Upload(generateThroughImageTextDTO.getSloganBase64(), sloganBucket); + String name = path.substring(path.lastIndexOf("/") + 1, path.lastIndexOf(".")); + // 保存到db,collection-element + CollectionElement collectionElement = new CollectionElement(); + collectionElement.setAccountId(generateThroughImageTextDTO.getUserId()); + collectionElement.setCollectionId(0L); + collectionElement.setLevel1Type(PRINT_BOARD.getRealName()); + collectionElement.setLevel2Type(CollectionLevel2TypeEnum.SLOGAN.getRealName()); + collectionElement.setName(name); + collectionElement.setUrl(path); + collectionElement.setHasPin((byte) 0); + collectionElement.setMd5(MD5Utils.encryptFile(minioUtil.getPresignedUrl(path, 24 * 60), Boolean.FALSE)); + collectionElement.setCreateDate(DateUtil.getByTimeZone(generateThroughImageTextDTO.getTimeZone())); + collectionElementService.save(collectionElement); + + // 将上传后的地址放在指定字段 + generateThroughImageTextDTO.setCollectionElementId(collectionElement.getId()); + generateThroughImageTextDTO.setSloganBase64(null); + generateThroughImageTextDTO.setDesignType("collection"); + } + + // Logo参数校验 + if (generateThroughImageTextDTO.getLevel2Type().equals(CollectionLevel2TypeEnum.LOGO.getRealName())){ + // logo模式下一次只生成一张 + times = 1; + // 校验是否输入内容 + if (StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getText().trim())){ + throw new BusinessException("please.input.the.prompt"); + } + + // 校验seed的取值范围 + int seed = Integer.parseInt(generateThroughImageTextDTO.getSeed()); + if (seed < 0 || seed > 99999){ + throw new BusinessException("the.value.range.of.seed"); + } + } + } // 2、生成唯一id 使用uuid,由于uuid重复的几率很小,故取消对uuid重复性的校验 String uuid = UUID.randomUUID().toString(); ArrayList taskIdList = new ArrayList<>(); - for (int i = 1; i <= 4; i++) { + for (int i = 1; i <= times; i++) { String temp = uuid; temp += "-" + i + "-" + generateThroughImageTextDTO.getUserId(); taskIdList.add(temp); @@ -588,7 +696,7 @@ public class GenerateServiceImpl extends ServiceImpl i String key = generateResultKey + ":" + uniqueId; GenerateResultVO generateResultVO = new Gson().fromJson(redisUtil.getFromString(key), GenerateResultVO.class); // 判断当前task的状态是不是Fail - if (!generateResultVO.getStatus().equals("Fail")){ + if (!generateResultVO.getStatus().equals("Fail")) { // 2、不是,直接发送取消请求到python端 pythonService.cancelGenerateTask(uniqueId); // 3、更改result中当前taskId的状态 diff --git a/src/main/resources/messages_en.properties b/src/main/resources/messages_en.properties index 39096842..6d79efa8 100644 --- a/src/main/resources/messages_en.properties +++ b/src/main/resources/messages_en.properties @@ -54,9 +54,10 @@ save.collection.failed=Save collection failed. save.designItemDetail.failed=Save designItemDetail failed. save.classification.failed=Save classification failed. update.classification.failed=Update classification failed. -please.input.the.caption=Please input the caption. +please.input.the.prompt=Please input the prompt. please.choose.an.image=Please choose an image. please.input.the.caption.and.choose.an.image=Please input the caption and choose an image. +please.input.the.caption.or.choose.an.image=Please input the caption or choose an image. duplicate.likes.are.not.allowed=Duplicate likes are not allowed. layer.information.not.found=Layer information not found. singleOverall.cannot.be.empty=singleOverall cannot be empty. @@ -132,6 +133,7 @@ image.synthesis.failed=image synthesis failed. priority.cannot.be.repeated=priority cannot be repeated. model.not.found=model not found. libraryIdList.cannot.be.empty=libraryIdList cannot be empty. +the.value.range.of.seed=The value range of seed is 0-99999 # 可能会报异常 # Informative: