【解决todo】AI:音乐接入

This commit is contained in:
xiaoxin 2024-06-24 14:19:09 +08:00
parent 88142ed74c
commit 5ba38dbdaa
10 changed files with 144 additions and 204 deletions

View File

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

View File

@ -12,27 +12,18 @@ import lombok.Getter;
@Getter
public enum AiMusicStatusEnum {
// TODO @xin用数字哈项目目前枚举都是数字
// @xin 文档中无失败这个返回值
STREAMING("streaming", "进行中"),
COMPLETE("complete", "完成");
STREAMING("10", "进行中"),
COMPLETE("20", "完成");
/**
* 状态
*/
private final String status;
/**
* 状态名
*/
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;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@ -19,16 +19,15 @@ import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@Tag(name = "管理后台 - AI 音乐")
@RestController
@RequestMapping("/ai/music")
@RequiredArgsConstructor // TODO @xin通过 @Resource 注入哈
public class AiMusicController {
// TODO @xin变量不用有 ai 前缀
private final AiMusicService aiMusicService;
@Resource
private AiMusicService musicService;
@PostMapping("/generate")
@Operation(summary = "音乐生成")
public CommonResult<List<Long>> generateMusic(@RequestBody @Valid SunoReqVO sunoReqVO) {
return success(aiMusicService.generateMusic(sunoReqVO));
public CommonResult<List<Long>> generateMusic(@RequestBody @Valid AiSunoGenerateReqVO reqVO) {
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;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
import cn.iocoder.yudao.module.ai.enums.music.AiMusicStatusEnum;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.AbstractJsonTypeHandler;
import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler;
import lombok.Data;
import java.util.List;
// TODO @xin注释完善下
/**
* @Author xiaoxin
* @Date 2024/6/5
* AI 音乐 DO
*
* @author xiaoxin
*/
@TableName("ai_music")
@Data
@ -98,25 +98,11 @@ public class AiMusicDO extends BaseDO {
/**
* 音乐风格标签
*/
@TableField(typeHandler = AiMusicTagsHandler.class)
@TableField(typeHandler = JacksonTypeHandler.class)
private List<String> tags;
/**
* 任务编号
*/
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;
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.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 jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
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
*/
@Component
@Slf4j
public class SunoJob implements JobHandler {
@Resource
private SunoApi sunoApi;
@Resource
private AiMusicService musicService;
@Override
public String execute(String param) {
// TODO @xin可以考虑整个逻辑都下沉到 Service 有点类似 AccessLogCleanJob
List<AiMusicDO> unCompletedTask = musicService.getUnCompletedTask();
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(" 个任务!");
Integer count = musicService.syncMusicTask();
log.info("[execute][Suno 同步任务数量 [{}] 个]", count);
return String.format("Suno 同步 - [%s] 个任务", count);
}
}

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;
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 java.util.List;
@ -18,7 +18,7 @@ public interface AiMusicService {
* @param reqVO 请求参数
* @return 生成的音乐ID
*/
List<Long> generateMusic(SunoReqVO reqVO);
List<Long> generateMusic(AiSunoGenerateReqVO reqVO);
/**
* 获取未完成状态的任务
@ -27,6 +27,20 @@ public interface AiMusicService {
*/
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.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.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.mysql.music.AiMusicMapper;
import cn.iocoder.yudao.module.ai.enums.music.AiMusicGenerateEnum;
@ -13,8 +15,7 @@ import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
@ -35,13 +36,22 @@ public class AiMusicServiceImpl implements AiMusicService {
private AiMusicMapper musicMapper;
@Override
public List<Long> generateMusic(SunoReqVO reqVO) {
// TODO @xin是不是可以 if else 调用对应的 API然后 insertMusicData 逻辑
AiMusicGenerateEnum generateEnum = AiMusicGenerateEnum.valueOfMode(reqVO.getGenerateMode());
return switch (generateEnum) {
case DESCRIPTION -> descriptionMode(reqVO);
case LYRIC -> lyricMode(reqVO);
};
public List<Long> generateMusic(AiSunoGenerateReqVO reqVO) {
List<SunoApi.MusicData> musicDataList;
if (Objects.equals(AiMusicGenerateEnum.LYRIC.getMode(), reqVO.getGenerateMode())) {
//歌词模式
SunoApi.MusicGenerateRequest sunoReq = new SunoApi.MusicGenerateRequest(reqVO.getPrompt(), reqVO.getModelVersion(), CollUtil.join(reqVO.getTags(), StrPool.COMMA), reqVO.getTitle());
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
@ -50,36 +60,33 @@ public class AiMusicServiceImpl implements AiMusicService {
}
@Override
public Boolean updateBatch(List<AiMusicDO> aiMusicDOList) {
return musicMapper.updateBatch(aiMusicDOList);
public Integer syncMusicTask() {
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();
}
/**
* 描述模式生成音乐
*
* @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());
@Override
public Boolean updateBatch(List<AiMusicDO> musicDOS) {
return musicMapper.updateBatch(musicDOS);
}
/**
@ -92,7 +99,7 @@ public class AiMusicServiceImpl implements AiMusicService {
if (CollUtil.isEmpty(musicDataList)) {
return Collections.emptyList();
}
List<AiMusicDO> aiMusicDOList = AiMusicConvert.convertFrom(musicDataList).stream()
List<AiMusicDO> aiMusicDOList = buildMusicDOList(musicDataList).stream()
.map(musicDO -> musicDO.setUserId(getLoginUserId())
.setGenerateMode(generateMode)
.setPlatform(platform))
@ -103,4 +110,24 @@ public class AiMusicServiceImpl implements AiMusicService {
.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));
}
}