!5 【解决todo】AI:音乐接入

Merge pull request !5 from 小新/master-jdk21-ai
This commit is contained in:
芋道源码 2024-06-25 13:30:58 +00:00 committed by Gitee
commit 5c73e5e1f4
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
10 changed files with 144 additions and 204 deletions

View File

@ -12,9 +12,8 @@ import lombok.Getter;
@Getter @Getter
public enum AiMusicGenerateEnum { public enum AiMusicGenerateEnum {
// TODO @xin用数字哈项目目前枚举都是数字 LYRIC("1", "歌词模式"),
LYRIC("lyric", "歌词模式"), DESCRIPTION("2", "描述模式");
DESCRIPTION("description", "描述模式");
/** /**
* 模式 * 模式

View File

@ -12,27 +12,18 @@ import lombok.Getter;
@Getter @Getter
public enum AiMusicStatusEnum { public enum AiMusicStatusEnum {
// TODO @xin用数字哈项目目前枚举都是数字
// @xin 文档中无失败这个返回值 // @xin 文档中无失败这个返回值
STREAMING("streaming", "进行中"), STREAMING("10", "进行中"),
COMPLETE("complete", "完成"); COMPLETE("20", "完成");
/** /**
* 状态 * 状态
*/ */
private final String status; private final String status;
/** /**
* 状态名 * 状态名
*/ */
private final String name; private final String name;
public static AiMusicStatusEnum valueOfStatus(String status) {
for (AiMusicStatusEnum statusEnum : AiMusicStatusEnum.values()) {
if (statusEnum.getStatus().equals(status)) {
return statusEnum;
}
}
throw new IllegalArgumentException("未知会话状态: " + status);
}
} }

View File

@ -1,12 +1,12 @@
package cn.iocoder.yudao.module.ai.controller.admin.music; package cn.iocoder.yudao.module.ai.controller.admin.music;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoReqVO; import cn.iocoder.yudao.module.ai.controller.admin.music.vo.AiSunoGenerateReqVO;
import cn.iocoder.yudao.module.ai.service.music.AiMusicService; import cn.iocoder.yudao.module.ai.service.music.AiMusicService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -19,16 +19,15 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - AI 音乐") @Tag(name = "管理后台 - AI 音乐")
@RestController @RestController
@RequestMapping("/ai/music") @RequestMapping("/ai/music")
@RequiredArgsConstructor // TODO @xin通过 @Resource 注入哈
public class AiMusicController { public class AiMusicController {
// TODO @xin变量不用有 ai 前缀 @Resource
private final AiMusicService aiMusicService; private AiMusicService musicService;
@PostMapping("/generate") @PostMapping("/generate")
@Operation(summary = "音乐生成") @Operation(summary = "音乐生成")
public CommonResult<List<Long>> generateMusic(@RequestBody @Valid SunoReqVO sunoReqVO) { public CommonResult<List<Long>> generateMusic(@RequestBody @Valid AiSunoGenerateReqVO reqVO) {
return success(aiMusicService.generateMusic(sunoReqVO)); return success(musicService.generateMusic(reqVO));
} }
} }

View File

@ -0,0 +1,39 @@
package cn.iocoder.yudao.module.ai.controller.admin.music.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.util.List;
/**
* @author xiaoxin
*/
@Schema(description = "管理后台 - 音乐生成 Request VO")
@Data
public class AiSunoGenerateReqVO {
@Schema(description = "用于生成音乐音频的提示", example = "创作一首带有轻松吉他旋律的流行歌曲,[verse] 描述夏日海滩的宁静,[chorus] 节奏加快,表达对自由的向往。")
private String prompt;
@Schema(description = "是否纯音乐", example = "true")
private Boolean makeInstrumental;
@Schema(description = "模型版本, 默认 chirp-v3.5", example = "chirp-v3.5")
private String modelVersion;// 参见 AiModelEnum 枚举
@Schema(description = "音乐风格", example = "[\"pop\",\"jazz\",\"punk\"]")
private List<String> tags;
@Schema(description = "音乐/歌曲名称", example = "夜空中最亮的星")
private String title;
@Schema(description = "平台", requiredMode = Schema.RequiredMode.REQUIRED, example = "Suno")
@NotBlank(message = "平台不能为空")
private String platform;// 参见 AiPlatformEnum 枚举
@Schema(description = "生成模式 1(歌词模式), 2(描述模式)", requiredMode = Schema.RequiredMode.REQUIRED, example = "2")
@NotBlank(message = "生成模式不能为空")
private String generateMode;// 参见 AiMusicGenerateEnum 枚举
}

View File

@ -1,40 +0,0 @@
package cn.iocoder.yudao.module.ai.controller.admin.music.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import lombok.Data;
import java.util.List;
// TODO @xin1ai 前缀2AiSunoGenerateReqVO要有生成哈3swaggger 缺少的属性也最好加下类似 example类上的 swagger
/**
* @author xiaoxin
*/
@Data
public class SunoReqVO {
@Schema(description = "用于生成音乐音频的提示")
private String prompt;
@Schema(description = "是否纯音乐")
private Boolean makeInstrumental;
// TODO @xin我们自己是不是用 modelVersion还是什么梗精准减少非必要的缩写
@Schema(description = "模型版本")
private String mv;
@Schema(description = "音乐风格")
private List<String> tags;
@Schema(description = "音乐/歌曲名称")
private String title;
@Schema(description = "平台")
@NotBlank(message = "平台不能为空")
private String platform;
@Schema(description = "生成模式 lyric(歌词模式), description(描述模式)")
@NotBlank(message = "生成模式不能为空")
private String generateMode;
}

View File

@ -1,21 +1,21 @@
package cn.iocoder.yudao.module.ai.dal.dataobject.music; package cn.iocoder.yudao.module.ai.dal.dataobject.music;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.ai.enums.music.AiMusicStatusEnum; import cn.iocoder.yudao.module.ai.enums.music.AiMusicStatusEnum;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler; import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data; import lombok.Data;
import java.util.List; import java.util.List;
// TODO @xin注释完善下
/** /**
* @Author xiaoxin * AI 音乐 DO
* @Date 2024/6/5 *
* @author xiaoxin
*/ */
@TableName("ai_music") @TableName("ai_music")
@Data @Data
@ -98,25 +98,11 @@ public class AiMusicDO extends BaseDO {
/** /**
* 音乐风格标签 * 音乐风格标签
*/ */
@TableField(typeHandler = AiMusicTagsHandler.class) @TableField(typeHandler = JacksonTypeHandler.class)
private List<String> tags; private List<String> tags;
/** /**
* 任务编号 * 任务编号
*/ */
private String taskId; private String taskId;
// TODO @xin @TableField(typeHandler = JacksonTypeHandler.class) 替代即可
public static class AiMusicTagsHandler extends AbstractJsonTypeHandler<Object> {
@Override
protected Object parse(String json) {
return JsonUtils.parseArray(json, String.class);
}
@Override
protected String toJson(Object obj) {
return JsonUtils.toJsonString(obj);
}
}
} }

View File

@ -1,61 +1,28 @@
package cn.iocoder.yudao.module.ai.job; package cn.iocoder.yudao.module.ai.job;
import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler; import cn.iocoder.yudao.framework.quartz.core.handler.JobHandler;
import cn.iocoder.yudao.module.ai.dal.dataobject.music.AiMusicDO;
import cn.iocoder.yudao.module.ai.service.music.AiMusicConvert;
import cn.iocoder.yudao.module.ai.service.music.AiMusicService; import cn.iocoder.yudao.module.ai.service.music.AiMusicService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
// TODO @xin不要直接叫这个名字哈要有它的目的
/** /**
* 处理 Suno Job * 同步 Suno 任务状态以及回写对应的音乐信息 Job
*
* @author xiaoxin * @author xiaoxin
*/ */
@Component @Component
@Slf4j @Slf4j
public class SunoJob implements JobHandler { public class SunoJob implements JobHandler {
@Resource
private SunoApi sunoApi;
@Resource @Resource
private AiMusicService musicService; private AiMusicService musicService;
@Override @Override
public String execute(String param) { public String execute(String param) {
// TODO @xin可以考虑整个逻辑都下沉到 Service 有点类似 AccessLogCleanJob Integer count = musicService.syncMusicTask();
List<AiMusicDO> unCompletedTask = musicService.getUnCompletedTask(); log.info("[execute][Suno 同步任务数量 [{}] 个]", count);
return String.format("Suno 同步 - [%s] 个任务", count);
if (CollUtil.isEmpty(unCompletedTask)) {
log.info("Suno 无进行中任务需要更新!");
return "Suno 无进行中任务需要更新!";
}
log.info("Suno 开始同步, 共 [{}] 个任务!", unCompletedTask.size());
//GET 请求为避免参数过长分批次处理
CollUtil.split(unCompletedTask, 4)
.forEach(chunk -> {
Map<String, Long> taskIdMap = CollUtil.toMap(chunk, new HashMap<>(), AiMusicDO::getTaskId, AiMusicDO::getId);
List<SunoApi.MusicData> musicTaskList = sunoApi.getMusicList(new ArrayList<>(taskIdMap.keySet()));
if (CollUtil.isNotEmpty(musicTaskList)) {
List<AiMusicDO> aiMusicDOS = AiMusicConvert.convertFrom(musicTaskList);
//回填id
aiMusicDOS.forEach(aiMusicDO -> aiMusicDO.setId(taskIdMap.get(aiMusicDO.getTaskId())));
musicService.updateBatch(aiMusicDOS);
} else {
log.warn("Suno 任务同步失败, 任务ID: [{}]", taskIdMap.keySet());
}
});
return "Suno 同步 - ".concat(String.valueOf(unCompletedTask.size())).concat(" 个任务!");
} }
} }

View File

@ -1,42 +0,0 @@
package cn.iocoder.yudao.module.ai.service.music;
import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
import cn.iocoder.yudao.module.ai.dal.dataobject.music.AiMusicDO;
import java.util.List;
import java.util.stream.Collectors;
/**
* AI 音乐 Convert
*
* @author xiaoxin
*/
public class AiMusicConvert {
public static AiMusicDO convertFrom(SunoApi.MusicData musicData) {
return new AiMusicDO()
.setTaskId(musicData.id())
.setPrompt(musicData.prompt())
.setGptDescriptionPrompt(musicData.gptDescriptionPrompt())
.setAudioUrl(musicData.audioUrl())
.setVideoUrl(musicData.videoUrl())
.setImageUrl(musicData.imageUrl())
.setLyric(musicData.lyric())
.setTitle(musicData.title())
.setStatus(musicData.status())
.setModel(musicData.modelName())
.setTags(StrUtil.isNotBlank(musicData.tags()) ? List.of(musicData.tags().split(StrPool.COMMA)) : null);
}
// TODO @xin一般情况下不用 convert直接逻辑里 convert 就好啦
public static List<AiMusicDO> convertFrom(List<SunoApi.MusicData> list) {
// TODO @xin可以使用 CollectionUtils.convertList 简洁一点
return list.stream()
.map(AiMusicConvert::convertFrom)
.collect(Collectors.toList());
}
}

View File

@ -1,6 +1,6 @@
package cn.iocoder.yudao.module.ai.service.music; package cn.iocoder.yudao.module.ai.service.music;
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoReqVO; import cn.iocoder.yudao.module.ai.controller.admin.music.vo.AiSunoGenerateReqVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.music.AiMusicDO; import cn.iocoder.yudao.module.ai.dal.dataobject.music.AiMusicDO;
import java.util.List; import java.util.List;
@ -18,7 +18,7 @@ public interface AiMusicService {
* @param reqVO 请求参数 * @param reqVO 请求参数
* @return 生成的音乐ID * @return 生成的音乐ID
*/ */
List<Long> generateMusic(SunoReqVO reqVO); List<Long> generateMusic(AiSunoGenerateReqVO reqVO);
/** /**
* 获取未完成状态的任务 * 获取未完成状态的任务
@ -27,6 +27,20 @@ public interface AiMusicService {
*/ */
List<AiMusicDO> getUnCompletedTask(); List<AiMusicDO> getUnCompletedTask();
Boolean updateBatch(List<AiMusicDO> aiMusicDOList);
/**
* 同步音乐任务
*
* @return 同步数量
*/
Integer syncMusicTask();
/**
* 批量更新音乐信息
*
* @param musicDOS 音乐信息
* @return 是否成功
*/
Boolean updateBatch(List<AiMusicDO> musicDOS);
} }

View File

@ -2,8 +2,10 @@ package cn.iocoder.yudao.module.ai.service.music;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.text.StrPool; import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi; import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoReqVO; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.AiSunoGenerateReqVO;
import cn.iocoder.yudao.module.ai.dal.dataobject.music.AiMusicDO; import cn.iocoder.yudao.module.ai.dal.dataobject.music.AiMusicDO;
import cn.iocoder.yudao.module.ai.dal.mysql.music.AiMusicMapper; import cn.iocoder.yudao.module.ai.dal.mysql.music.AiMusicMapper;
import cn.iocoder.yudao.module.ai.enums.music.AiMusicGenerateEnum; import cn.iocoder.yudao.module.ai.enums.music.AiMusicGenerateEnum;
@ -13,8 +15,7 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Collections; import java.util.*;
import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@ -35,13 +36,22 @@ public class AiMusicServiceImpl implements AiMusicService {
private AiMusicMapper musicMapper; private AiMusicMapper musicMapper;
@Override @Override
public List<Long> generateMusic(SunoReqVO reqVO) { public List<Long> generateMusic(AiSunoGenerateReqVO reqVO) {
// TODO @xin是不是可以 if else 调用对应的 API然后 insertMusicData 逻辑 List<SunoApi.MusicData> musicDataList;
AiMusicGenerateEnum generateEnum = AiMusicGenerateEnum.valueOfMode(reqVO.getGenerateMode()); if (Objects.equals(AiMusicGenerateEnum.LYRIC.getMode(), reqVO.getGenerateMode())) {
return switch (generateEnum) { //歌词模式
case DESCRIPTION -> descriptionMode(reqVO); SunoApi.MusicGenerateRequest sunoReq = new SunoApi.MusicGenerateRequest(reqVO.getPrompt(), reqVO.getModelVersion(), CollUtil.join(reqVO.getTags(), StrPool.COMMA), reqVO.getTitle());
case LYRIC -> lyricMode(reqVO); musicDataList = sunoApi.customGenerate(sunoReq);
}; } else if (Objects.equals(AiMusicGenerateEnum.DESCRIPTION.getMode(), reqVO.getGenerateMode())) {
//描述模式
SunoApi.MusicGenerateRequest sunoReq = new SunoApi.MusicGenerateRequest(reqVO.getPrompt(), reqVO.getModelVersion(), reqVO.getMakeInstrumental());
musicDataList = sunoApi.generate(sunoReq);
} else {
log.error("未知的生成模式:{}", reqVO.getGenerateMode());
throw new IllegalArgumentException("未知的生成模式");
}
// 2. 插入数据库
return insertMusicData(musicDataList, reqVO.getGenerateMode(), reqVO.getPlatform());
} }
@Override @Override
@ -50,36 +60,33 @@ public class AiMusicServiceImpl implements AiMusicService {
} }
@Override @Override
public Boolean updateBatch(List<AiMusicDO> aiMusicDOList) { public Integer syncMusicTask() {
return musicMapper.updateBatch(aiMusicDOList); List<AiMusicDO> unCompletedTask = this.getUnCompletedTask();
if (CollUtil.isEmpty(unCompletedTask)) {
log.info("Suno 无进行中任务需要更新!");
return 0;
}
log.info("Suno 开始同步, 共 [{}] 个任务!", unCompletedTask.size());
//GET 请求为避免参数过长分批次处理
CollUtil.split(unCompletedTask, 4)
.forEach(chunk -> {
Map<String, Long> taskIdMap = CollUtil.toMap(chunk, new HashMap<>(), AiMusicDO::getTaskId, AiMusicDO::getId);
List<SunoApi.MusicData> musicTaskList = sunoApi.getMusicList(new ArrayList<>(taskIdMap.keySet()));
if (CollUtil.isNotEmpty(musicTaskList)) {
List<AiMusicDO> aiMusicDOS = buildMusicDOList(musicTaskList);
//回填id
aiMusicDOS.forEach(aiMusicDO -> aiMusicDO.setId(taskIdMap.get(aiMusicDO.getTaskId())));
this.updateBatch(aiMusicDOS);
} else {
log.warn("Suno 任务同步失败, 任务ID: [{}]", taskIdMap.keySet());
}
});
return unCompletedTask.size();
} }
/** @Override
* 描述模式生成音乐 public Boolean updateBatch(List<AiMusicDO> musicDOS) {
* return musicMapper.updateBatch(musicDOS);
* @param reqVO 请求参数
* @return 生成的音乐ID集合
*/
public List<Long> descriptionMode(SunoReqVO reqVO) {
// 1. 异步生成
SunoApi.MusicGenerateRequest sunoReq = new SunoApi.MusicGenerateRequest(reqVO.getPrompt(), reqVO.getMv(), reqVO.getMakeInstrumental());
List<SunoApi.MusicData> musicDataList = sunoApi.generate(sunoReq);
// 2. 插入数据库
return insertMusicData(musicDataList, reqVO.getGenerateMode(), reqVO.getPlatform());
}
/**
* 歌词模式生成音乐
*
* @param reqVO 请求参数
* @return 生成的音乐ID集合
*/
public List<Long> lyricMode(SunoReqVO reqVO) {
// 1. 异步生成
SunoApi.MusicGenerateRequest sunoReq = new SunoApi.MusicGenerateRequest(reqVO.getPrompt(), reqVO.getMv(), CollUtil.join(reqVO.getTags(), StrPool.COMMA), reqVO.getTitle());
List<SunoApi.MusicData> musicDataList = sunoApi.customGenerate(sunoReq);
// 2. 插入数据库
return insertMusicData(musicDataList, reqVO.getGenerateMode(), reqVO.getPlatform());
} }
/** /**
@ -92,7 +99,7 @@ public class AiMusicServiceImpl implements AiMusicService {
if (CollUtil.isEmpty(musicDataList)) { if (CollUtil.isEmpty(musicDataList)) {
return Collections.emptyList(); return Collections.emptyList();
} }
List<AiMusicDO> aiMusicDOList = AiMusicConvert.convertFrom(musicDataList).stream() List<AiMusicDO> aiMusicDOList = buildMusicDOList(musicDataList).stream()
.map(musicDO -> musicDO.setUserId(getLoginUserId()) .map(musicDO -> musicDO.setUserId(getLoginUserId())
.setGenerateMode(generateMode) .setGenerateMode(generateMode)
.setPlatform(platform)) .setPlatform(platform))
@ -103,4 +110,24 @@ public class AiMusicServiceImpl implements AiMusicService {
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
/**
* 构建 AiMusicDO 集合
*
* @param musicTaskList suno 音乐任务列表
* @return AiMusicDO 集合
*/
private static List<AiMusicDO> buildMusicDOList(List<SunoApi.MusicData> musicTaskList) {
return CollectionUtils.convertList(musicTaskList, musicData -> new AiMusicDO()
.setTaskId(musicData.id())
.setPrompt(musicData.prompt())
.setGptDescriptionPrompt(musicData.gptDescriptionPrompt())
.setAudioUrl(musicData.audioUrl())
.setVideoUrl(musicData.videoUrl())
.setImageUrl(musicData.imageUrl())
.setLyric(musicData.lyric())
.setTitle(musicData.title())
.setStatus(Objects.equals("complete", musicData.status()) ? AiMusicStatusEnum.COMPLETE.getStatus() : AiMusicStatusEnum.STREAMING.getStatus())
.setModel(musicData.modelName())
.setTags(StrUtil.isNotBlank(musicData.tags()) ? List.of(musicData.tags().split(StrPool.COMMA)) : null));
}
} }