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

@@ -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