generate功能相关代码优化

This commit is contained in:
徐佩
2023-08-31 12:59:23 +08:00
parent 89b237215a
commit 2d924ca1e6
8 changed files with 98 additions and 50 deletions

View File

@@ -18,4 +18,13 @@ public class GenerateCollectionVO {
@ApiModelProperty("生成的图片信息")
private List<GenerateCollectionItemVO> generatedCollectionItems;
public GenerateCollectionVO(Long generateId, Long collectionId, List<GenerateCollectionItemVO> generatedCollectionItems) {
this.generateId = generateId;
this.collectionId = collectionId;
this.generatedCollectionItems = generatedCollectionItems;
}
public GenerateCollectionVO() {
}
}

View File

@@ -23,10 +23,10 @@ public class PantoneVO {
@ApiModelProperty("r")
private Integer r;
@ApiModelProperty("r")
@ApiModelProperty("g")
private Integer g;
@ApiModelProperty("r")
@ApiModelProperty("b")
private Integer b;
@ApiModelProperty("h")

View File

@@ -1,6 +1,5 @@
package com.ai.da.model.vo;
import com.ai.da.model.dto.CollectionColorDTO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

View File

@@ -1,17 +1,13 @@
package com.ai.da.model.vo;
import com.ai.da.common.enums.CurrentDesignPrintPictureTypeEnum;
import com.ai.da.mapper.entity.CollectionElement;
import com.ai.da.mapper.entity.Library;
import com.ai.da.model.dto.CollectionColorDTO;
import com.ai.da.python.vo.DesignPythonItemPrint;
import com.google.common.collect.Lists;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.List;
import java.util.Objects;
@Data
@ApiModel("校验element响应")

View File

@@ -1457,7 +1457,7 @@ public class PythonService {
public String generateSketchOrPrint(String url, String text, int mode, String modelName) {
//限流校验
AccessLimitUtils.validate("generateSketch",5);
AccessLimitUtils.validate("generateSketchOrPrint",5);
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
@@ -1501,4 +1501,32 @@ public class PythonService {
//生成失败
throw new BusinessException("generateSketchOrPrint exception!");
}
public String sendPostToModel(Map<String,Object> content,String portAndRoute,String functionName){
OkHttpClient client = new OkHttpClient().newBuilder()
.connectTimeout(30, TimeUnit.SECONDS)
.pingInterval(5, TimeUnit.SECONDS)//websocket轮训间隔(单位:秒)
.readTimeout(60, TimeUnit.SECONDS)//读取超时(单位:秒)
.writeTimeout(60, TimeUnit.SECONDS)//写入超时(单位:秒)
.build();
MediaType mediaType = MediaType.parse("application/json");
RequestBody body = RequestBody.create(mediaType, JSON.toJSONString(content));
Request request = new Request.Builder()
.url(accessPythonIp + ":" + portAndRoute)
.method("POST", body)
.addHeader("Authorization", "Basic YWlkbGFiOjEyMw==")
.addHeader("Content-Type", "application/json")
.build();
Response response = null;
String bodyString = null;
try {
log.info(functionName + "请求入参content###{}", JSON.toJSONString(content));
response = client.newCall(request).execute();
bodyString = response.body().string();
} catch (IOException ioException) {
log.error("PythonService##"+ functionName +"异常###{}", ExceptionUtil.getThrowableList(ioException));
}
return bodyString;
}
}

View File

@@ -115,4 +115,12 @@ public interface CollectionElementService extends IService<CollectionElement> {
*/
void refreshHistoryData() ;
/**
* 当level2Type发生改变时修改levelType
* @param elementId
* @param level2Type
* @return
*/
CollectionElement editLevel2Type(Long elementId,String level2Type);
}

View File

@@ -24,6 +24,7 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import io.netty.util.internal.StringUtil;
import javafx.scene.chart.ValueAxis;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.LICENSE;
@@ -569,4 +570,17 @@ public class CollectionElementServiceImpl extends ServiceImpl<CollectionElementM
}
return Boolean.TRUE;
}
@Override
public CollectionElement editLevel2Type(Long elementId, String level2Type){
CollectionElement collectionElement = null;
if(!Objects.isNull(elementId)){
collectionElement = collectionElementMapper.selectById(elementId);
if (StringUtil.isNullOrEmpty(collectionElement.getLevel2Type()) || !(collectionElement.getLevel2Type()).equals(level2Type)){
collectionElement.setLevel2Type(level2Type);
updateById(collectionElement);
}
}
return collectionElement;
}
}

View File

@@ -15,6 +15,7 @@ import com.ai.da.model.dto.GenerateLikeDTO;
import com.ai.da.model.dto.GenerateThroughImageTextDTO;
import com.ai.da.model.vo.*;
import com.ai.da.python.PythonService;
import com.ai.da.service.CollectionElementService;
import com.ai.da.service.GenerateService;
import com.ai.da.service.LibraryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
@@ -40,13 +41,16 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper,Generate> im
@Resource
private PythonService pythonService;
@Resource
private CollectionElementService collectionElementService;
@Override
public GenerateCaptionVO generateCaption(Long sketchElementId) {
CollectionElement collectionElement = collectionElementMapper.selectById(sketchElementId);
Assert.notNull(collectionElement,"System error!Please reselect the sketch");
Assert.isTrue("Sketchboard".equals(collectionElement.getLevel1Type()) && !StringUtil.isNullOrEmpty(collectionElement.getUrl())
,"System error!Please reselect the sketch");
// String url = collectionElement.getUrl();
String url = collectionElement.getUrl();
// String caption = pythonService.generateSketchCaption(url);
GenerateCaptionVO recognized_caption = new GenerateCaptionVO("recognized caption");
@@ -60,63 +64,33 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper,Generate> im
Long accountId = userHolder.getId();
// 2、判断必须入参是否为非空
String generateType = generateThroughImageTextDTO.getGenerateType();
String text = generateThroughImageTextDTO.getText();
Long elementId = generateThroughImageTextDTO.getCollectionElementId();
String modelName = generateThroughImageTextDTO.getVersion();
Generate generate = new Generate();
generate.setAccountId(accountId);
generate.setGenerateType(generateType);
generate.setModelName(StringUtil.isNullOrEmpty(modelName) ? ModelNameEnum.MODEL_0.getCode() : modelName);
generate.setGenerateType(generateThroughImageTextDTO.getGenerateType());
generate.setModelName(StringUtil.isNullOrEmpty(generateThroughImageTextDTO.getVersion()) ? ModelNameEnum.MODEL_0.getCode() : generateThroughImageTextDTO.getVersion());
generate.setCreateDate(DateUtil.getByTimeZone(generateThroughImageTextDTO.getTimeZone()));
generate.setLevel1Type(generateThroughImageTextDTO.getLevel1Type());
int mode = GenerateTypeEnum.TEXT_IMAGE.getCode();
switch(generateType){
case "text":
Assert.notNull(text,"Please input the caption");
generate.setText(text);
mode = GenerateTypeEnum.TEXT.getCode();
break;
case "image":
Assert.notNull(elementId,"Please choose a image");
generate.setCollectionElementId(elementId);
break;
case "text-image":
Assert.isTrue(!StringUtil.isNullOrEmpty(text) && Objects.nonNull(elementId),
"Please input the caption and choose a image");
generate.setText(text);
generate.setCollectionElementId(elementId);
default:
}
String text = generateThroughImageTextDTO.getText();
Long elementId = generateThroughImageTextDTO.getCollectionElementId();
validateGeneraType(generate,text,elementId);
// 3、将请求信息落库
// 3.1 sketch或print在t_collection_element表中的信息是否需要更新 如 level2Type
CollectionElement collectionElement = null;
if(!Objects.isNull(elementId)){
collectionElement = collectionElementMapper.selectById(elementId);
if (StringUtil.isNullOrEmpty(collectionElement.getLevel2Type()) || !(collectionElement.getLevel2Type()).equals(generateThroughImageTextDTO.getLevel2Type()) ){
collectionElement.setLevel2Type(generateThroughImageTextDTO.getLevel2Type());
collectionElementMapper.updateById(collectionElement);
}
}
CollectionElement collectionElement = collectionElementService.editLevel2Type(elementId, generateThroughImageTextDTO.getLevel2Type());
// 3.2 将本次generate的请求信息添加到t_generate表中
save(generate);
// 4、向模型发起请求
// String generatedSketchUrl = pythonService.generateSketchOrPrint(collectionElement.getUrl(),text
// ,mode,generateThroughImageTextDTO.getVersion());
int mode = GenerateTypeEnum.TEXT.getValue().equals(generate.getGenerateType()) ? GenerateTypeEnum.TEXT.getCode() : GenerateTypeEnum.TEXT_IMAGE.getCode();
// String generatedSketchUrl = pythonService.generateSketchOrPrint(collectionElement.getUrl(),text,mode,generateThroughImageTextDTO.getVersion());
List<String> generatedSketchUrl = Arrays.asList("testUrl1","testUrl2","testUrl3","testUrl4");
GenerateCollectionVO generateCollectionVO = new GenerateCollectionVO();
List<GenerateCollectionItemVO> generatedCollectionItems = new ArrayList<>();
generateCollectionVO.setGenerateId(generate.getId());
generateCollectionVO.setCollectionId(Objects.isNull(collectionElement) ? null : collectionElement.getCollectionId());
generateCollectionVO.setGeneratedCollectionItems(generatedCollectionItems);
// 5、处理模型返回的数据
// 5.1 将相应的url保存到数据库
List<GenerateCollectionItemVO> generatedCollectionItems = new ArrayList<>();
generatedSketchUrl.forEach(item -> {
GenerateDetail generateDetail = new GenerateDetail();
generateDetail.setUrl(item);
@@ -131,7 +105,27 @@ public class GenerateServiceImpl extends ServiceImpl<GenerateMapper,Generate> im
});
// 6、将模型返回的图片地址返回给前端
return generateCollectionVO;
Long collectionId = Objects.isNull(collectionElement) ? null : collectionElement.getCollectionId();
return new GenerateCollectionVO(generate.getId(),collectionId,generatedCollectionItems);
}
private void validateGeneraType(Generate generate,String text,Long elementId){
switch(generate.getGenerateType()){
case "text":
Assert.notNull(text,"Please input the caption");
generate.setText(text);
break;
case "image":
Assert.notNull(elementId,"Please choose a image");
generate.setCollectionElementId(elementId);
break;
case "text-image":
Assert.isTrue(!StringUtil.isNullOrEmpty(text) && Objects.nonNull(elementId),
"Please input the caption and choose a image");
generate.setText(text);
generate.setCollectionElementId(elementId);
default:
}
}
@Override