Node.js调用Qwen3-TTS-12Hz-1.7B-VoiceDesign:实时语音聊天机器人开发
Node.js调用Qwen3-TTS-12Hz-1.7B-VoiceDesign实时语音聊天机器人开发1. 引言想不想让你的聊天机器人不仅能打字回复还能用各种声音跟你对话比如让AI用温柔的女声说你好呀或者用搞怪的卡通音调讲个笑话今天我就带你用Node.js和Qwen3-TTS模型从零开始搭建一个会说话的智能聊天机器人。这个教程特别适合前端开发者和Node.js爱好者不需要深厚的AI背景只要会写JavaScript就能跟着做。我们会用到最新的Qwen3-TTS语音合成技术让你的机器人不仅能说话还能根据你的指令变换不同的声音风格——甜美萝莉、沉稳大叔、甚至外星人音效都没问题学完这篇教程你将掌握WebSocket实时通信、语音流处理、多轮对话保持等实用技能这些都是开发现代AI应用的必备能力。准备好了吗让我们开始吧2. 环境准备与快速部署2.1 系统要求与依赖安装首先确保你的开发环境满足以下要求Node.js 18.0 或更高版本Python 3.8用于语音模型推理至少8GB内存推荐16GB支持CUDA的GPU可选但能大幅提升速度创建项目目录并初始化mkdir voice-chatbot cd voice-chatbot npm init -y安装核心依赖# Node.js 相关依赖 npm install express socket.io axios multer npm install --save-dev nodemon # Python 环境依赖推荐使用conda conda create -n qwen-tts python3.10 conda activate qwen-tts pip install torch torchaudio transformers soundfile pip install qwen3-tts2.2 项目结构设计建议的项目结构如下voice-chatbot/ ├── server/ # Node.js后端 │ ├── app.js # Express服务器 │ ├── socket.js # WebSocket处理 │ └── tts-service/ # Python语音服务 ├── public/ # 前端静态文件 │ ├── index.html │ ├── style.css │ └── script.js └── package.json3. 核心功能实现3.1 WebSocket实时通信搭建我们先建立一个双向通信通道让浏览器和服务器能实时交换消息// server/socket.js const socketIO require(socket.io); function setupSocket(server) { const io socketIO(server, { cors: { origin: *, methods: [GET, POST] } }); // 存储用户对话上下文 const userContexts new Map(); io.on(connection, (socket) { console.log(用户连接:, socket.id); // 初始化用户上下文 userContexts.set(socket.id, { messages: [], voiceStyle: 友好自然的助手声音 }); // 处理文本消息 socket.on(text-message, async (data) { try { const { text } data; const context userContexts.get(socket.id); // 保存用户消息到上下文 context.messages.push({ role: user, content: text }); // 调用AI生成回复这里简化处理 const aiResponse await generateAIResponse(context.messages); // 保存AI回复到上下文 context.messages.push({ role: assistant, content: aiResponse }); // 生成语音 const audioData await generateSpeech(aiResponse, context.voiceStyle); // 发送回复给客户端 socket.emit(voice-response, { text: aiResponse, audio: audioData }); } catch (error) { console.error(处理消息错误:, error); socket.emit(error, { message: 处理消息时出错 }); } }); // 处理语音风格设置 socket.on(set-voice-style, (style) { const context userContexts.get(socket.id); if (context) { context.voiceStyle style; socket.emit(voice-style-updated, { style }); } }); socket.on(disconnect, () { console.log(用户断开连接:, socket.id); userContexts.delete(socket.id); }); }); return io; } // 简化的AI回复生成 async function generateAIResponse(messages) { // 这里可以接入任何AI聊天模型 // 简化示例固定回复 const responses [ 你好我是你的语音助手很高兴为你服务。, 这个问题很有意思让我想想怎么回答..., 我已经记录下你的需求会尽快处理。, 今天的天气真不错适合出去走走呢 ]; return responses[Math.floor(Math.random() * responses.length)]; } module.exports { setupSocket };3.2 Qwen3-TTS语音生成服务现在实现Python语音服务这是整个项目的核心# server/tts-service/tts_handler.py import torch import soundfile as sf import base64 import io from qwen_tts import Qwen3TTSModel class TTSService: def __init__(self): self.model None self.is_loaded False def load_model(self): 加载语音模型 if not self.is_loaded: try: print(正在加载Qwen3-TTS模型...) self.model Qwen3TTSModel.from_pretrained( Qwen/Qwen3-TTS-12Hz-1.7B-VoiceDesign, device_mapauto, torch_dtypetorch.float16 ) self.is_loaded True print(模型加载完成!) except Exception as e: print(f模型加载失败: {e}) raise def generate_speech(self, text, voice_style): 生成语音并返回base64编码的音频数据 if not self.is_loaded: self.load_model() try: # 生成语音 wavs, sample_rate self.model.generate_voice_design( texttext, languageChinese, instructvoice_style ) # 将音频数据保存到内存缓冲区 buffer io.BytesIO() sf.write(buffer, wavs[0], sample_rate, formatWAV) buffer.seek(0) # 转换为base64 audio_base64 base64.b64encode(buffer.read()).decode(utf-8) return audio_base64 except Exception as e: print(f语音生成错误: {e}) return None # 创建全局服务实例 tts_service TTSService()创建Flask接口供Node.js调用# server/tts-service/app.py from flask import Flask, request, jsonify from flask_cors import CORS from tts_handler import tts_service app Flask(__name__) CORS(app) app.route(/generate-speech, methods[POST]) def generate_speech(): try: data request.json text data.get(text, ) voice_style data.get(voice_style, 友好自然的助手声音) if not text: return jsonify({error: 缺少文本参数}), 400 audio_data tts_service.generate_speech(text, voice_style) if audio_data: return jsonify({ audio: audio_data, format: audio/wav }) else: return jsonify({error: 语音生成失败}), 500 except Exception as e: return jsonify({error: str(e)}), 500 if __name__ __main__: tts_service.load_model() # 预加载模型 app.run(port5000, debugTrue)3.3 Node.js与Python服务集成在Node.js中调用Python语音服务// server/tts-service/node-integration.js const axios require(axios); class TTSService { constructor() { this.baseURL http://localhost:5000; } async generateSpeech(text, voiceStyle 友好自然的助手声音) { try { const response await axios.post(${this.baseURL}/generate-speech, { text, voice_style: voiceStyle }); return response.data.audio; } catch (error) { console.error(调用TTS服务失败:, error.message); throw new Error(语音生成服务暂时不可用); } } // 语音风格预设库 getVoicePresets() { return { friendly: 温暖友好的助手声音语速适中带有微笑的语气, professional: 专业沉稳的商务声音清晰准确语速平稳, energetic: 充满活力的年轻声音语速稍快热情洋溢, calm: 平静舒缓的治疗师声音语速缓慢让人放松, storybook: 讲故事的老爷爷声音语速有起伏富有表现力 }; } } module.exports TTSService;4. 前端界面与交互实现创建一个简单但功能完整的前端界面!-- public/index.html -- !DOCTYPE html html head title语音聊天机器人/title link relstylesheet hrefstyle.css /head body div classcontainer h1 智能语音助手/h1 div classvoice-controls label选择语音风格:/label select idvoiceStyle option valuefriendly友好助手/option option valueprofessional专业商务/option option valueenergetic活力青年/option option valuecalm平静舒缓/option option valuestorybook讲故事模式/option /select button idtestVoice测试语音/button /div div classchat-container div idmessageList classmessage-list/div div classinput-area input typetext idmessageInput placeholder输入你的消息... button idsendButton发送/button button idvoiceButton/button /div /div audio idaudioPlayer hidden/audio /div script src/socket.io/socket.io.js/script script srcscript.js/script /body /html添加样式美化界面/* public/style.css */ body { font-family: Arial, sans-serif; background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); height: 100vh; margin: 0; display: flex; justify-content: center; align-items: center; } .container { background: white; border-radius: 15px; padding: 20px; width: 400px; box-shadow: 0 10px 30px rgba(0,0,0,0.2); } .voice-controls { margin-bottom: 20px; display: flex; gap: 10px; align-items: center; } .chat-container { border: 1px solid #ddd; border-radius: 10px; overflow: hidden; } .message-list { height: 300px; overflow-y: auto; padding: 15px; background: #f9f9f9; } .message { margin: 10px 0; padding: 10px; border-radius: 10px; max-width: 80%; } .message.user { background: #007bff; color: white; margin-left: auto; } .message.assistant { background: #e9ecef; color: #333; } .input-area { display: flex; padding: 10px; background: white; border-top: 1px solid #ddd; } input, button, select { padding: 10px; border: 1px solid #ddd; border-radius: 5px; } input { flex: 1; margin-right: 10px; } button { background: #007bff; color: white; cursor: pointer; border: none; } button:hover { background: #0056b3; }实现前端交互逻辑// public/script.js document.addEventListener(DOMContentLoaded, function() { const socket io(); const messageInput document.getElementById(messageInput); const sendButton document.getElementById(sendButton); const voiceButton document.getElementById(voiceButton); const messageList document.getElementById(messageList); const voiceStyleSelect document.getElementById(voiceStyle); const testVoiceButton document.getElementById(testVoice); const audioPlayer document.getElementById(audioPlayer); const voicePresets { friendly: 温暖友好的助手声音语速适中带有微笑的语气, professional: 专业沉稳的商务声音清晰准确语速平稳, energetic: 充满活力的年轻声音语速稍快热情洋溢, calm: 平静舒缓的治疗师声音语速缓慢让人放松, storybook: 讲故事的老爷爷声音语速有起伏富有表现力 }; // 发送文本消息 function sendMessage() { const text messageInput.value.trim(); if (text) { addMessage(user, text); socket.emit(text-message, { text }); messageInput.value ; } } // 添加消息到聊天界面 function addMessage(role, text) { const messageDiv document.createElement(div); messageDiv.className message ${role}; messageDiv.textContent text; messageList.appendChild(messageList); messageList.scrollTop messageList.scrollHeight; } // 播放语音 function playAudio(audioData) { audioPlayer.src data:audio/wav;base64,${audioData}; audioPlayer.play(); } // 事件监听 sendButton.addEventListener(click, sendMessage); messageInput.addEventListener(keypress, (e) { if (e.key Enter) sendMessage(); }); voiceButton.addEventListener(click, () { // 这里可以添加语音识别功能 alert(语音输入功能需要浏览器麦克风权限); }); testVoiceButton.addEventListener(click, () { const styleKey voiceStyleSelect.value; socket.emit(set-voice-style, voicePresets[styleKey]); // 发送测试消息 socket.emit(text-message, { text: 你好这是当前语音风格的测试 }); }); // Socket事件监听 socket.on(voice-response, (data) { addMessage(assistant, data.text); playAudio(data.audio); }); socket.on(voice-style-updated, (data) { alert(语音风格已更新); }); socket.on(error, (data) { alert(错误: ${data.message}); }); });5. 完整服务器集成最后将所有的组件集成到主服务器文件中// server/app.js const express require(express); const http require(http); const path require(path); const { setupSocket } require(./socket); const TTSService require(./tts-service/node-integration); const app express(); const server http.createServer(app); const io setupSocket(server); const ttsService new TTSService(); // 提供静态文件 app.use(express.static(path.join(__dirname, ../public))); // API路由 app.use(express.json()); app.post(/api/generate-speech, async (req, res) { try { const { text, voiceStyle } req.body; const audioData await ttsService.generateSpeech(text, voiceStyle); res.json({ audio: audioData }); } catch (error) { res.status(500).json({ error: error.message }); } }); app.get(/api/voice-presets, (req, res) { res.json(ttsService.getVoicePresets()); }); const PORT process.env.PORT || 3000; server.listen(PORT, () { console.log(服务器运行在 http://localhost:${PORT}); console.log(请确保Python TTS服务也在运行: http://localhost:5000); }); // 导出用于测试 module.exports { app, server, io };6. 运行与测试6.1 启动服务首先启动Python TTS服务cd server/tts-service python app.py然后启动Node.js服务器npm run dev访问 http://localhost:3000 即可开始使用你的语音聊天机器人6.2 测试不同语音风格尝试发送以下消息并切换不同的语音风格来体验效果你好介绍一下你自己讲一个简短的笑话用不同的情绪说今天天气真好7. 总结通过这个教程我们成功构建了一个功能完整的实时语音聊天机器人。关键实现点包括WebSocket双向通信、Qwen3-TTS语音合成、多轮对话上下文维护以及前后端的协同工作。实际使用中语音生成质量相当不错特别是能够通过自然语言描述来控制声音风格这为创建多样化的人机交互体验提供了很大空间。延迟方面在本地网络环境下基本可以做到实时响应用户体验流畅。你可以在这个基础上继续扩展很多功能比如添加语音识别输入、支持更多语言、实现情感分析来自动选择语音风格或者集成更强大的AI聊天模型。最重要的是这个项目展示了如何将先进的AI语音技术与熟悉的Web开发栈相结合创造出真正有吸引力的交互体验。希望这个教程能为你打开语音交互开发的大门获取更多AI镜像想探索更多AI镜像和应用场景访问 CSDN星图镜像广场提供丰富的预置镜像覆盖大模型推理、图像生成、视频生成、模型微调等多个领域支持一键部署。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2435883.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!