当前位置: 首页 > ds >正文

Spring AI Alibaba 调用文生语音模型(CosyVoice)

语音合成

        语音合成,又称文本转语音(Text-to-Speech,TTS),是将文本转换为自然语音的技术。该技术基于机器学习算法,通过学习大量语音样本,掌握语言的韵律、语调和发音规则,从而在接收到文本输入时生成真人般自然的语音内容。

CosyVoice

        CosyVoice 是阿里开源的一款创新的多语言、情感丰富的语音生成大模型,旨在通过先进的 AI 技术生成自然且富有情感的语音。无论是在语音生成的质量,还是在细节控制的精准度上,CosyVoice 都表现卓越,能够应对零样本语音生成、跨语言声音合成以及指令执行等多种任务。

SpringAI   

访问地址:Spring AI

‌        Spring AI‌是一个面向人工智能工程的应用框架,由Spring团队推出,旨在将AI能力集成到Java应用中。Spring AI的核心是解决AI集成的根本挑战,即将企业数据和API与AI模型连接起来‌。

Spring AI Alibaba

Spring AI Alibaba‌是一个基于Spring AI构建的开源项目,专门针对阿里云的大模型服务进行适配,是阿里云通义系列模型及服务在Java AI应用开发领域的最佳实践。该项目旨在帮助Java开发者快速构建AI应用,降低技术门槛和成本‌

开发案例实现

本例演示使用Spring AI Alibaba 调用文生语音模型(CosyVoice)

1、准备工作

环境配置要求

  • JDK 17
  • SpringBoot  3.4.2

登录阿里云百炼申请API-Key

复制下自己创建的API-KEY。

​2、创建Springboot项目配置pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.4.2</version><relativePath/></parent><artifactId>alibaba-dashscope-tts-cosyvoice</artifactId><properties><maven.compiler.source>17</maven.compiler.source><maven.compiler.target>17</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.alibaba.cloud.ai</groupId><artifactId>spring-ai-alibaba-starter</artifactId><version>1.0.0-M6.1</version></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.8.20</version></dependency></dependencies></project>

3、创建业务服务类Service和ServiceImpl

CosyVoiceService 

public interface CosyVoiceService {/*** 文生语音,生成语音文件(mp3格式)* @param text*/public void genVoiceFile(String text);
}

CosyVoiceServiceImpl 

@Slf4j
@Service
public class CosyVoiceServiceImpl implements CosyVoiceService {//模型名称 龙飞private static String model = "cosyvoice-v1";//音色名称private static String voice = "longfei";@Autowiredprivate SpeechSynthesisModel synthesisModel;@Overridepublic void genVoiceFile(String text) {DashScopeSpeechSynthesisOptions scopeSpeechSynthesisOptions = DashScopeSpeechSynthesisOptions.builder().withModel(model).withVoice(voice).build();ByteBuffer byteBuffer = synthesisModel.call(new SpeechSynthesisPrompt("你好啊,今天是小满节气,是中国二十四节气之一。",scopeSpeechSynthesisOptions)).getResult().getOutput().getAudio();byte[] bytes = byteBuffer.array();File file = new File("D:\\audio\\" + UUID.randomUUID().toString(Boolean.TRUE) + ".mp3");if (!file.getParentFile().exists()){FileUtil.mkParentDirs(file);}try {FileOutputStream fileOutputStream = new FileOutputStream(file);fileOutputStream.write(bytes);fileOutputStream.flush();log.info("创建成功:{}", file.getAbsoluteFile());} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}
}

4、配置application.yml

server:port: 9021
spring:ai:dashscope:api-key: sk-{your-spi-key}chat:options:#模型名称model: cosyvoice-v1

5、运行服务并进行测试验证

6、验证生成的mp3文件

http://www.xdnf.cn/news/8276.html

相关文章:

  • 基于labview的声音采集与存储分析系统
  • 深入浅出DDD:从理论到落地的关键
  • 海南藏族自治州政府门户网站集约化建设实践与动易解决方案应用
  • Java集合框架入门指南:从小白到基础掌握
  • 聚水潭ERP(奇门)集成用友ERP(用友U8、U9、NC、BIP、畅捷通T+、好业财)
  • 位图算法——判断唯一字符
  • 百度智能云千帆AppBuilder RAG流程技术文档
  • 佰力博科技与您探讨半导体电阻测试常用的一些方法
  • Qt 布局管理器的层级关系
  • 【I2C】高效实现I2C寄存器读取函数
  • 如何免费申请SSL证书并无限续期!
  • 使用Node开发需要知道的背景知识
  • 基于机器学习的策略开发和Backtrader回测
  • “2025香港国际法律服务大会探讨“跨法域 链全球”新格局”
  • 基于LangManus深入理解系统提示设计
  • Origin绘制多因子柱状点线图
  • 0x90属性中的属性名$I30和Scb->AttributeName的关系
  • day19-20-四剑客-find-grep-sed-awk
  • OpenCV CUDA 模块图像过滤-----创建一个计算图像导数的滤波器函数createDerivFilter()
  • 深入剖析小红书笔记详情接口:技术原理与实战应用
  • 技术篇-2.1.C\C++应用场景及开发工具安装
  • Python训练营打卡——DAY33(2025.5.22)
  • 并发编程之异步线程池
  • 【运维实战】Python打造基于免费API的IP地址归属地批量查询工具!
  • 5.0.6 TreeView的使用介绍
  • find-chomd-read-ls和ll的区别
  • Crawl4AI:高效的AI数据抓取工具
  • Spring概念问题详解
  • [C++面试] 基础题
  • springboot 集成kerberos 用户认证 获取域账号