mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-18 19:20:05 +08:00
commit
5fabb0b757
@ -0,0 +1,37 @@
|
||||
package cn.iocoder.yudao.module.ai.enums;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @Author xiaoxin
|
||||
* @Date 2024/6/5
|
||||
*/
|
||||
@AllArgsConstructor
|
||||
@Getter
|
||||
public enum AiMusicStatusEnum {
|
||||
|
||||
SUBMITTED("submitted", "已提交"),
|
||||
QUEUED("queued", "排队中"),
|
||||
STREAMING("streaming", "进行中"),
|
||||
COMPLETE("complete", "完成");
|
||||
|
||||
/**
|
||||
* 状态
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -49,6 +49,11 @@ public enum AiModelEnum {
|
||||
XING_HUO_2_0("星火大模型2.0", "generalv2", "/v2.1/chat"),
|
||||
XING_HUO_3_0("星火大模型3.0", "generalv3", "/v3.1/chat"),
|
||||
XING_HUO_3_5("星火大模型3.5", "generalv3.5", "/v3.5/chat"),
|
||||
|
||||
//Suno
|
||||
SUNO_2( "SUNO-2", "chirp-v2-xxl-alpha",null),
|
||||
SUNO_3_0( "SUNO-3.0", "chirp-v3-0",null),
|
||||
SUNO_3_5( "SUNO-3.5", "chirp-v3.5",null),
|
||||
;
|
||||
|
||||
/**
|
||||
|
@ -1,8 +1,8 @@
|
||||
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.SunoLyricModeVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoReqVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoRespVO;
|
||||
import cn.iocoder.yudao.module.ai.service.music.MusicService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
@ -13,6 +13,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
|
||||
@Tag(name = "管理后台 - AI 音乐生成")
|
||||
@ -23,9 +25,16 @@ public class MusicController {
|
||||
|
||||
private final MusicService musicService;
|
||||
|
||||
@PostMapping("/suno-gen")
|
||||
@Operation(summary = "音乐生成")
|
||||
public CommonResult<SunoRespVO> musicGen(@RequestBody @Valid SunoReqVO sunoReqVO) {
|
||||
return success(musicService.musicGen(sunoReqVO));
|
||||
@PostMapping("generate/description-mode")
|
||||
@Operation(summary = "音乐生成-描述模式")
|
||||
public CommonResult<List<Long>> descriptionMode(@RequestBody @Valid SunoReqVO sunoReqVO) {
|
||||
return success(musicService.descriptionMode(sunoReqVO));
|
||||
}
|
||||
|
||||
@PostMapping("generate/lyric-mode")
|
||||
@Operation(summary = "音乐生成-歌词模式")
|
||||
public CommonResult<List<Long>> lyricMode(@RequestBody @Valid SunoLyricModeVO sunoLyricModeVO) {
|
||||
return success(musicService.lyricMode(sunoLyricModeVO));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,85 +0,0 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.music.vo;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 表示单个音乐数据的类
|
||||
*/
|
||||
@Data
|
||||
public class MusicDataVO {
|
||||
/**
|
||||
* 音乐数据的 ID
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/**
|
||||
* 音乐音频的标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 音乐音频的图片 URL
|
||||
*/
|
||||
@JsonProperty("image_url")
|
||||
private String imageUrl;
|
||||
|
||||
/**
|
||||
* 音乐音频的歌词
|
||||
*/
|
||||
private String lyric;
|
||||
|
||||
/**
|
||||
* 音乐音频的 URL
|
||||
*/
|
||||
@JsonProperty("audio_url")
|
||||
private String audioUrl;
|
||||
|
||||
/**
|
||||
* 音乐视频的 URL
|
||||
*/
|
||||
@JsonProperty("video_url")
|
||||
private String videoUrl;
|
||||
|
||||
/**
|
||||
* 音乐音频的创建时间
|
||||
*/
|
||||
@JsonProperty("created_at")
|
||||
private String createdAt;
|
||||
|
||||
/**
|
||||
* 使用的模型名称
|
||||
*/
|
||||
private String model;
|
||||
|
||||
/**
|
||||
* 生成音乐音频的提示
|
||||
*/
|
||||
private String prompt;
|
||||
|
||||
/**
|
||||
* 音乐音频的风格
|
||||
*/
|
||||
private String style;
|
||||
|
||||
public static List<MusicDataVO> convertFrom(List<SunoApi.SunoResp.MusicData> musicDataList) {
|
||||
return musicDataList.stream().map(musicData -> {
|
||||
MusicDataVO musicDataVO = new MusicDataVO();
|
||||
musicDataVO.setId(musicData.id());
|
||||
musicDataVO.setTitle(musicData.title());
|
||||
musicDataVO.setImageUrl(musicData.imageUrl());
|
||||
musicDataVO.setLyric(musicData.lyric());
|
||||
musicDataVO.setAudioUrl(musicData.audioUrl());
|
||||
musicDataVO.setVideoUrl(musicData.videoUrl());
|
||||
musicDataVO.setCreatedAt(musicData.createdAt());
|
||||
musicDataVO.setModel(musicData.model());
|
||||
musicDataVO.setPrompt(musicData.prompt());
|
||||
musicDataVO.setStyle(musicData.style());
|
||||
return musicDataVO;
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
}
|
@ -0,0 +1,22 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.music.vo;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* @Author jxli@quant360.com
|
||||
* @Date 2024/6/7
|
||||
*/
|
||||
@Data
|
||||
public class SunoLyricModeVO extends SunoReqVO {
|
||||
|
||||
/**
|
||||
* 标签/音乐风格
|
||||
*/
|
||||
private String tags;
|
||||
|
||||
/**
|
||||
* 音乐名称
|
||||
*/
|
||||
private String title;
|
||||
|
||||
}
|
@ -2,39 +2,21 @@ package cn.iocoder.yudao.module.ai.controller.admin.music.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.Data;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
@Data
|
||||
@Accessors(chain = true)
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public class SunoReqVO {
|
||||
/**
|
||||
* 用于生成音乐音频的提示
|
||||
*/
|
||||
private String prompt;
|
||||
|
||||
/**
|
||||
* 用于生成音乐音频的歌词
|
||||
* 是否纯音乐
|
||||
*/
|
||||
private String lyric;
|
||||
|
||||
private boolean makeInstrumental;
|
||||
/**
|
||||
* 指示音乐音频是否为定制,如果为 true,则从歌词生成,否则从提示生成
|
||||
* //todo 首次请求返回的模型是对的,后续更新音频返回的模型又变成v3.5了
|
||||
* 模型版本 {@link cn.iocoder.yudao.module.ai.enums.AiModelEnum} Suno
|
||||
*/
|
||||
private boolean custom;
|
||||
|
||||
/**
|
||||
* 音乐音频的标题
|
||||
*/
|
||||
private String title;
|
||||
|
||||
/**
|
||||
* 音乐音频的风格
|
||||
*/
|
||||
private String style;
|
||||
|
||||
/**
|
||||
* 音乐音频生成后回调的 URL
|
||||
*/
|
||||
private String callbackUrl;
|
||||
private String mv;
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
package cn.iocoder.yudao.module.ai.controller.admin.music.vo;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* API 响应的数据
|
||||
*/
|
||||
@Data
|
||||
public class SunoRespVO {
|
||||
/**
|
||||
* 表示请求是否成功
|
||||
*/
|
||||
private boolean success;
|
||||
|
||||
/**
|
||||
* 任务 ID
|
||||
*/
|
||||
@JsonProperty("task_id")
|
||||
private String taskId;
|
||||
|
||||
/**
|
||||
* 音乐数据列表
|
||||
*/
|
||||
private List<MusicDataVO> data;
|
||||
|
||||
|
||||
//把 SunoResp转为本vo类
|
||||
public static SunoRespVO convertFrom(SunoApi.SunoResp sunoResp) {
|
||||
SunoRespVO sunoRespVO = new SunoRespVO();
|
||||
sunoRespVO.setSuccess(sunoResp.success());
|
||||
sunoRespVO.setTaskId(sunoResp.taskId());
|
||||
sunoRespVO.setData(MusicDataVO.convertFrom(sunoResp.data()));
|
||||
return sunoRespVO;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
package cn.iocoder.yudao.module.ai.dal.dataobject.music;
|
||||
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO;
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* @Author xiaoxin
|
||||
* @Date 2024/6/5
|
||||
*/
|
||||
@TableName("ai_music")
|
||||
@Data
|
||||
public class AiMusicDO extends BaseDO {
|
||||
@TableId(type = IdType.AUTO)
|
||||
@Schema(description = "编号")
|
||||
private Long id;
|
||||
|
||||
@Schema(description = "用户编号")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "音乐名称")
|
||||
private String title;
|
||||
|
||||
@Schema(description = "图片地址")
|
||||
private String imageUrl;
|
||||
|
||||
@Schema(description = "歌词")
|
||||
private String lyric;
|
||||
|
||||
@Schema(description = "音频地址")
|
||||
private String audioUrl;
|
||||
|
||||
@Schema(description = "视频地址")
|
||||
private String videoUrl;
|
||||
|
||||
@Schema(description = "音乐状态")
|
||||
private String status;
|
||||
|
||||
@Schema(description = "描述词")
|
||||
private String gptDescriptionPrompt;
|
||||
|
||||
@Schema(description = "提示词")
|
||||
private String prompt;
|
||||
|
||||
@Schema(description = "模型")
|
||||
private String model;
|
||||
|
||||
@Schema(description = "错误信息")
|
||||
private String errorMessage;
|
||||
|
||||
@Schema(description = "音乐风格标签")
|
||||
private String tags;
|
||||
|
||||
@Schema(description = "任务id")
|
||||
private String taskId;
|
||||
|
||||
|
||||
|
||||
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(musicData.tags());
|
||||
}
|
||||
|
||||
public static List<AiMusicDO> convertFrom(List<SunoApi.MusicData> musicDataList) {
|
||||
return musicDataList.stream()
|
||||
.map(AiMusicDO::convertFrom)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package cn.iocoder.yudao.module.ai.dal.mysql.music;
|
||||
|
||||
import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
|
||||
import cn.iocoder.yudao.module.ai.dal.dataobject.music.AiMusicDO;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* @Author xiaoxin
|
||||
* @Date 2024/6/5
|
||||
*/
|
||||
@Mapper
|
||||
public interface AiMusicMapper extends BaseMapperX<AiMusicDO> {
|
||||
|
||||
}
|
@ -1,7 +1,9 @@
|
||||
package cn.iocoder.yudao.module.ai.service.music;
|
||||
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoLyricModeVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoReqVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoRespVO;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author xiaoxin
|
||||
@ -10,10 +12,13 @@ import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoRespVO;
|
||||
public interface MusicService {
|
||||
|
||||
/**
|
||||
* 音乐生成
|
||||
*
|
||||
* @param sunoReqVO 请求实体
|
||||
* @return 响应实体
|
||||
* 音乐生成-描述模式
|
||||
*/
|
||||
SunoRespVO musicGen(SunoReqVO sunoReqVO);
|
||||
List<Long> descriptionMode(SunoReqVO reqVO);
|
||||
|
||||
|
||||
/**
|
||||
* 音乐生成-歌词模式
|
||||
**/
|
||||
List<Long> lyricMode(SunoLyricModeVO reqVO);
|
||||
}
|
||||
|
@ -1,10 +1,26 @@
|
||||
package cn.iocoder.yudao.module.ai.service.music;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.text.StrPool;
|
||||
import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoLyricModeVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoReqVO;
|
||||
import cn.iocoder.yudao.module.ai.controller.admin.music.vo.SunoRespVO;
|
||||
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.AiMusicStatusEnum;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
|
||||
|
||||
/**
|
||||
* @Author xiaoxin
|
||||
@ -12,20 +28,70 @@ import org.springframework.stereotype.Service;
|
||||
*/
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public class MusicServiceImpl implements MusicService {
|
||||
|
||||
private final SunoApi sunoApi;
|
||||
private final AiMusicMapper musicMapper;
|
||||
|
||||
private final Queue<String> taskQueue = new ConcurrentLinkedQueue<>();
|
||||
|
||||
|
||||
@Override
|
||||
public SunoRespVO musicGen(SunoReqVO sunoReqVO) {
|
||||
SunoApi.SunoResp sunoResp = sunoApi.musicGen(new SunoApi.SunoReq(
|
||||
sunoReqVO.getPrompt(),
|
||||
sunoReqVO.getLyric(),
|
||||
sunoReqVO.isCustom(),
|
||||
sunoReqVO.getTitle(),
|
||||
sunoReqVO.getStyle(),
|
||||
sunoReqVO.getCallbackUrl()
|
||||
));
|
||||
return SunoRespVO.convertFrom(sunoResp);
|
||||
public List<Long> descriptionMode(SunoReqVO reqVO) {
|
||||
SunoApi.SunoReq sunoReq = new SunoApi.SunoReq(reqVO.getPrompt(), reqVO.getMv(), reqVO.isMakeInstrumental());
|
||||
//默认异步
|
||||
List<SunoApi.MusicData> musicDataList = sunoApi.generate(sunoReq);
|
||||
return insertMusicData(musicDataList);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<Long> lyricMode(SunoLyricModeVO reqVO) {
|
||||
SunoApi.SunoReq sunoReq = new SunoApi.SunoReq(reqVO.getPrompt(), reqVO.getMv(), reqVO.getTags(), reqVO.getTitle());
|
||||
//默认异步
|
||||
List<SunoApi.MusicData> musicDataList = sunoApi.customGenerate(sunoReq);
|
||||
return insertMusicData(musicDataList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 新增音乐数据并提交 suno任务
|
||||
*
|
||||
* @param musicDataList 音乐数据列表
|
||||
* @return 音乐id集合
|
||||
*/
|
||||
private List<Long> insertMusicData(List<SunoApi.MusicData> musicDataList) {
|
||||
if (CollUtil.isEmpty(musicDataList)) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
return AiMusicDO.convertFrom(musicDataList).stream()
|
||||
.peek(musicDO -> musicMapper.insert(musicDO.setUserId(getLoginUserId())))
|
||||
.peek(e -> Optional.of(e.getTaskId()).ifPresent(taskQueue::add))
|
||||
.map(AiMusicDO::getId)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelay = 5, timeUnit = TimeUnit.SECONDS)
|
||||
@Transactional
|
||||
public void flushSunoTask() {
|
||||
if (CollUtil.isEmpty(taskQueue)) {
|
||||
return;
|
||||
}
|
||||
CollUtil.split(taskQueue, 5).
|
||||
stream().map(chunk -> CollUtil.join(chunk, StrPool.COMMA))
|
||||
.forEach(taskIds -> {
|
||||
List<SunoApi.MusicData> musicData = sunoApi.selectById(taskIds);
|
||||
musicData.stream()
|
||||
.map(AiMusicDO::convertFrom)
|
||||
.forEach(musicDO -> {
|
||||
//更新音乐生成结果
|
||||
musicMapper.update(musicDO, Wrappers.<AiMusicDO>lambdaUpdate().eq(AiMusicDO::getTaskId, musicDO.getTaskId()));
|
||||
//完成后剔除任务
|
||||
if (Objects.equals(AiMusicStatusEnum.COMPLETE.getStatus(), musicDO.getStatus())) {
|
||||
taskQueue.remove(musicDO.getTaskId());
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -151,7 +151,7 @@ public class YudaoAiAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnProperty(value = "yudao.ai.suno.enable", havingValue = "true")
|
||||
public SunoApi sunoApi(YudaoAiProperties yudaoAiProperties) {
|
||||
return new SunoApi(new SunoConfig(yudaoAiProperties.getSuno().getToken()));
|
||||
return new SunoApi(new SunoConfig(yudaoAiProperties.getSuno().getBaseUrl()));
|
||||
}
|
||||
|
||||
private static @NotNull MidjourneyConfig getMidjourneyConfig(ApplicationContext applicationContext,
|
||||
|
@ -119,9 +119,9 @@ public class YudaoAiProperties {
|
||||
|
||||
private boolean enable = false;
|
||||
/**
|
||||
* token
|
||||
* suno-api 服务的基本地址
|
||||
*/
|
||||
private String token;
|
||||
private String baseUrl;
|
||||
|
||||
}
|
||||
|
||||
|
@ -13,7 +13,7 @@ import lombok.NoArgsConstructor;
|
||||
@AllArgsConstructor
|
||||
public class SunoConfig {
|
||||
/**
|
||||
* token信息
|
||||
* suno-api服务的基本路径
|
||||
*/
|
||||
private String token;
|
||||
private String baseUrl;
|
||||
}
|
||||
|
@ -4,112 +4,207 @@ import cn.iocoder.yudao.framework.ai.core.model.suno.SunoConfig;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.ai.openai.api.ApiUtils;
|
||||
import org.springframework.core.ParameterizedTypeReference;
|
||||
import org.springframework.http.HttpStatusCode;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.reactive.function.client.ClientResponse;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Suno API
|
||||
* <br>
|
||||
* 文档地址:https://platform.acedata.cloud/documents/d016ee3f-421b-4b6e-989a-8beba8701701
|
||||
* 文档地址:https://github.com/status2xx/suno-api/blob/main/README_CN.md
|
||||
*
|
||||
* @Author xiaoxin
|
||||
* @Date 2024/5/27
|
||||
* @Date 2024/6/3
|
||||
*/
|
||||
@Slf4j
|
||||
public class SunoApi {
|
||||
|
||||
public static final String DEFAULT_BASE_URL = "https://api.acedata.cloud/suno";
|
||||
private final WebClient webClient;
|
||||
private final Predicate<HttpStatusCode> STATUS_PREDICATE = status -> !status.is2xxSuccessful();
|
||||
private final Function<ClientResponse, Mono<? extends Throwable>> EXCEPTION_FUNCTION = response -> response.bodyToMono(String.class)
|
||||
.handle((respBody, sink) -> {
|
||||
log.error("【suno-api】调用失败!resp: 【{}】", respBody);
|
||||
sink.error(new IllegalStateException("【suno-api】调用失败!"));
|
||||
});
|
||||
|
||||
|
||||
public SunoApi(SunoConfig config) {
|
||||
this.webClient = WebClient.builder()
|
||||
.baseUrl(DEFAULT_BASE_URL)
|
||||
.defaultHeaders(ApiUtils.getJsonContentHeaders(config.getToken()))
|
||||
.baseUrl(config.getBaseUrl())
|
||||
.defaultHeaders((headers) -> headers.setContentType(MediaType.APPLICATION_JSON))
|
||||
.build();
|
||||
}
|
||||
|
||||
// TODO @芋艿:方法名,要考虑下;
|
||||
public SunoResp musicGen(SunoReq sunReq) {
|
||||
public List<MusicData> generate(SunoApi.SunoReq sunReq) {
|
||||
return this.webClient.post()
|
||||
.uri("/audios")
|
||||
.body(Mono.just(sunReq), SunoReq.class)
|
||||
.uri("/api/generate")
|
||||
.body(Mono.just(sunReq), SunoApi.SunoReq.class)
|
||||
.retrieve()
|
||||
.onStatus(status -> !status.is2xxSuccessful(),
|
||||
response -> response.bodyToMono(String.class)
|
||||
.handle((respBody, sink) -> {
|
||||
log.error("【Suno】调用失败!resp: 【{}】", respBody);
|
||||
sink.error(new IllegalStateException("【Suno】调用失败!"));
|
||||
}))
|
||||
.bodyToMono(SunoResp.class)
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION)
|
||||
.bodyToMono(new ParameterizedTypeReference<List<MusicData>>() {
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
public List<MusicData> customGenerate(SunoApi.SunoReq sunReq) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/custom_generate")
|
||||
.body(Mono.just(sunReq), SunoApi.SunoReq.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION)
|
||||
.bodyToMono(new ParameterizedTypeReference<List<MusicData>>() {
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
public List<MusicData> doChatCompletion(String prompt) {
|
||||
return this.webClient.post()
|
||||
.uri("/v1/chat/completions")
|
||||
.body(Mono.just(new SunoReq(prompt)), SunoApi.SunoReq.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION)
|
||||
.bodyToMono(new ParameterizedTypeReference<List<MusicData>>() {
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
public LyricsData generateLyrics(String prompt) {
|
||||
return this.webClient.post()
|
||||
.uri("/api/generate_lyrics")
|
||||
.body(Mono.just(new SunoReq(prompt)), SunoApi.SunoReq.class)
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION)
|
||||
.bodyToMono(LyricsData.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
public List<MusicData> selectById(String ids) {
|
||||
return this.webClient.get()
|
||||
.uri(uriBuilder -> uriBuilder
|
||||
.path("/api/get")
|
||||
.queryParam("ids", ids)
|
||||
.build())
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION)
|
||||
.bodyToMono(new ParameterizedTypeReference<List<MusicData>>() {
|
||||
})
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
public LimitData selectLimit() {
|
||||
return this.webClient.get()
|
||||
.uri("/api/get_limit")
|
||||
.retrieve()
|
||||
.onStatus(STATUS_PREDICATE, EXCEPTION_FUNCTION)
|
||||
.bodyToMono(LimitData.class)
|
||||
.block();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 请求数据对象,用于生成音乐音频。
|
||||
* 根据提示生成音频
|
||||
*
|
||||
* @param prompt 用于生成音乐音频的提示
|
||||
* @param lyric 用于生成音乐音频的歌词
|
||||
* @param custom 指示音乐音频是否为定制,如果为 true,则从歌词生成,否则从提示生成
|
||||
* @param title 音乐音频的标题
|
||||
* @param style 音乐音频的风格
|
||||
* @param callbackUrl 音乐音频生成后回调的 URL
|
||||
* @param prompt 用于生成音乐音频的提示
|
||||
* @param tags 音乐风格
|
||||
* @param title 音乐名称
|
||||
* @param mv 模型
|
||||
* @param waitAudio false表示后台模式,仅返回音频任务信息,需要调用get API获取详细的音频信息。
|
||||
* true表示同步模式,API最多等待100s,音频生成完毕后直接返回音频链接等信息,建议在GPT等agent中使用。
|
||||
* @param makeInstrumental 指示音乐音频是否为定制,如果为 true,则从歌词生成,否则从提示生成
|
||||
*/
|
||||
@JsonInclude(value = JsonInclude.Include.NON_NULL)
|
||||
public record SunoReq(
|
||||
String prompt,
|
||||
String lyric,
|
||||
boolean custom,
|
||||
String tags,
|
||||
String title,
|
||||
String style,
|
||||
String callbackUrl
|
||||
String mv,
|
||||
@JsonProperty("wait_audio") boolean waitAudio,
|
||||
@JsonProperty("make_instrumental") boolean makeInstrumental
|
||||
) {
|
||||
public SunoReq(String prompt) {
|
||||
this(prompt, null, false, null, null, null);
|
||||
this(prompt, null, null, null, false, false);
|
||||
}
|
||||
|
||||
public SunoReq(String prompt, String mv, boolean makeInstrumental) {
|
||||
this(prompt, null, null, mv, false, makeInstrumental);
|
||||
}
|
||||
|
||||
|
||||
public SunoReq(String prompt, String mv, String tags, String title) {
|
||||
this(prompt, tags, title, mv, false, false);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* SunoAPI 响应的数据。
|
||||
* SunoAPI 响应的音频数据。
|
||||
*
|
||||
* @param success 表示请求是否成功
|
||||
* @param taskId 任务 ID
|
||||
* @param data 音乐数据列表
|
||||
* @param id 音乐数据的 ID
|
||||
* @param title 音乐音频的标题
|
||||
* @param imageUrl 音乐音频的图片 URL
|
||||
* @param lyric 音乐音频的歌词
|
||||
* @param audioUrl 音乐音频的 URL
|
||||
* @param videoUrl 音乐视频的 URL
|
||||
* @param createdAt 音乐音频的创建时间
|
||||
* @param modelName
|
||||
* @param status submitted、queued、streaming、complete
|
||||
* @param gptDescriptionPrompt
|
||||
* @param prompt 生成音乐音频的提示
|
||||
* @param type
|
||||
* @param tags
|
||||
*/
|
||||
public record SunoResp(
|
||||
boolean success,
|
||||
@JsonProperty("task_id") String taskId,
|
||||
List<MusicData> data
|
||||
public record MusicData(
|
||||
String id,
|
||||
String title,
|
||||
@JsonProperty("image_url") String imageUrl,
|
||||
String lyric,
|
||||
@JsonProperty("audio_url") String audioUrl,
|
||||
@JsonProperty("video_url") String videoUrl,
|
||||
@JsonProperty("created_at") String createdAt,
|
||||
@JsonProperty("model_name") String modelName,
|
||||
String status,
|
||||
@JsonProperty("gpt_description_prompt") String gptDescriptionPrompt,
|
||||
String prompt,
|
||||
String type,
|
||||
String tags
|
||||
) {
|
||||
/**
|
||||
* 单个音乐数据。
|
||||
*
|
||||
* @param id 音乐数据的 ID
|
||||
* @param title 音乐音频的标题
|
||||
* @param imageUrl 音乐音频的图片 URL
|
||||
* @param lyric 音乐音频的歌词
|
||||
* @param audioUrl 音乐音频的 URL
|
||||
* @param videoUrl 音乐视频的 URL
|
||||
* @param createdAt 音乐音频的创建时间
|
||||
* @param model 使用的模型名称
|
||||
* @param prompt 生成音乐音频的提示
|
||||
* @param style 音乐音频的风格
|
||||
*/
|
||||
public record MusicData(
|
||||
String id,
|
||||
String title,
|
||||
@JsonProperty("image_url") String imageUrl,
|
||||
String lyric,
|
||||
@JsonProperty("audio_url") String audioUrl,
|
||||
@JsonProperty("video_url") String videoUrl,
|
||||
@JsonProperty("created_at") String createdAt,
|
||||
String model,
|
||||
String prompt,
|
||||
String style
|
||||
) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* SunoAPI 响应的歌词数据。
|
||||
*
|
||||
* @param text 歌词
|
||||
* @param title 标题
|
||||
* @param status 状态
|
||||
*/
|
||||
public record LyricsData(
|
||||
String text,
|
||||
String title,
|
||||
String status
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* SunoAPI 响应的限额数据,目前每日免费50
|
||||
*/
|
||||
public record LimitData(
|
||||
@JsonProperty("credits_left") Long creditsLeft,
|
||||
String period,
|
||||
@JsonProperty("monthly_limit") Long monthlyLimit,
|
||||
@JsonProperty("monthly_usage") Long monthlyUsage
|
||||
) {
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -5,27 +5,54 @@ import cn.iocoder.yudao.framework.ai.core.model.suno.api.SunoApi;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @Author xiaoxin
|
||||
* @Date 2024/5/27
|
||||
*/
|
||||
public class SunoTests {
|
||||
|
||||
private SunoConfig sunoConfig;
|
||||
SunoApi sunoApi;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
String token = "16b4356581984d538652354b60d69ff0";
|
||||
this.sunoConfig = new SunoConfig(token);
|
||||
String url = "https://suno-ix9nve79x-status2xxs-projects.vercel.app";
|
||||
this.sunoApi = new SunoApi(new SunoConfig(url));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateMusic() {
|
||||
SunoApi sunoApi = new SunoApi(sunoConfig);
|
||||
SunoApi.SunoReq sunoReq = new SunoApi.SunoReq("创作一首带有轻松吉他旋律的流行歌曲,[verse] 描述夏日海滩的宁静,[chorus] 节奏加快,表达对自由的向往。");
|
||||
public void selectById() {
|
||||
System.out.println(sunoApi.selectById("d460ddda-7c87-4f34-b751-419b08a590ca,ff90ea66-49cd-4fd2-b44c-44267dfd5551"));
|
||||
|
||||
SunoApi.SunoResp sunoResp = sunoApi.musicGen(sunoReq);
|
||||
System.out.println(sunoResp);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generate() {
|
||||
List<SunoApi.MusicData> generate = sunoApi.generate(new SunoApi.SunoReq("创作一首带有轻松吉他旋律的流行歌曲,[verse] 描述夏日海滩的宁静,[chorus] 节奏加快,表达对自由的向往。"));
|
||||
System.out.println(generate);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void doChatCompletion() {
|
||||
List<SunoApi.MusicData> generate = sunoApi.doChatCompletion("创作一首带有轻松吉他旋律的流行歌曲,[verse] 描述夏日海滩的宁静,[chorus] 节奏加快,表达对自由的向往。");
|
||||
System.out.println(generate);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void generateLyrics() {
|
||||
SunoApi.LyricsData lyricsData = sunoApi.generateLyrics("A soothing lullaby");
|
||||
System.out.println(lyricsData);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void selectLimit() {
|
||||
SunoApi.LimitData limitData = sunoApi.selectLimit();
|
||||
System.out.println(limitData);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ yudao.ai:
|
||||
channel-id: 1237948819677904960
|
||||
suno:
|
||||
enable: true
|
||||
token: 16b4356581984d538652354b60d69ff0
|
||||
base-url: https://suno-ix9nve79x-status2xxs-projects.vercel.app
|
||||
|
||||
--- #################### 芋道相关配置 ####################
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user