✨ 基于 CosyVoice 的智能语音交互扩展项目
[简体中文]|English
本系统通过集成多模态AI能力,实现:
- 🗣️ 智能对话生成
- 📚 知识库语义检索
- 🔊 情感化语音合成
- 🌐 用户友好的WebUI
- 部署 CosyVoice 语音合成服务
- Python 3.10+ 环境
- 有效的 大模型API 密钥
功能模块 | 支持技术 | 特性描述 |
---|---|---|
智能对话生成 | DeepSeek-R1/Qwen2.5/Others | 🧠 多轮情景对话 |
语义检索 | BGE-M3 嵌入模型 + FAISS | 🔍 知识库快速匹配 |
语音合成 | CosyVoice API | 🎵 情感化语音生成 |
网页分析 | Selenium + BeautifulSoup | 🌐 动态网页内容抓取 |
图形界面 | Gradio | 🖥️ 用户友好的WebUI |
推荐使用webui
# 大模型 API配置
api_url = "https://api.siliconflow.cn/v1/chat/completions"
headers = {
"Authorization": "Bearer sk-jgxgrpjdrxmmtghsjmplqkdclxcjegasofsrfbfcwkyiaekc",
"Content-Type": "application/json"
}
# 修改ai_chat.py中以下的全局变量
chat_model = "deepseek-ai/DeepSeek-R1"
text_model = "Qwen/Qwen2.5-72B-Instruct"
embed_model = "BAAI/bge-m3"
text_api_url = "https://api.siliconflow.cn/v1/chat/completions"
url_embedding = "https://api.siliconflow.cn/v1/embeddings"
vector_db = VectorDB() # 创建向量数据库实例
vector_db.add_texts(texts) # 将文字添加进知识库
batch_analyze_webpages(webpage_urls, vector_db) # 将网页内容添加到知识库
运行一次构建成功后请注释添加代码以加快运行速度
#修改webui.py(关闭流式传输--必须)
audio_output = gr.Audio(label="合成音频", autoplay=True, streaming=False)
#修改cosyvoice\cli\frontend.py(关闭切片--非必需)
def text_normalize(self, text, split=True, text_frontend=True):
if isinstance(text, Generator):
logging.info('get tts_text generator, will skip text_normalize!')
return [text]
if text_frontend is False:
return [text] if split is True else text
text = text.strip()
if self.use_ttsfrd:
texts = [i["text"] for i in json.loads(self.frd.do_voicegen_frd(text))["sentences"]]
text = ''.join(texts)
else:
if contains_chinese(text):
text = self.zh_tn_model.normalize(text)
text = text.replace("\n", "")
text = replace_blank(text)
text = replace_corner_mark(text)
text = text.replace(".", "。")
text = text.replace(" - ", ",")
text = remove_bracket(text)
text = re.sub(r'[,,、]+$', '。', text)
else:
text = self.en_tn_model.normalize(text)
text = spell_out_number(text, self.inflect_parser)
# 移除了split_paragraph切片处理,直接返回整个文本
return [text] if split is True else text
# 先启动CosyVoice
python webui.py --port 50000 --model_dir pretrained_models/models_name
# 再运行ai_chat.py
python ai_chat.py
#修改webui.py(关闭流式传输--必须)
audio_output = gr.Audio(label="合成音频", autoplay=True, streaming=False)
#修改cosyvoice\cli\frontend.py(关闭切片--非必需)
def text_normalize(self, text, split=True, text_frontend=True):
if isinstance(text, Generator):
logging.info('get tts_text generator, will skip text_normalize!')
return [text]
if text_frontend is False:
return [text] if split is True else text
text = text.strip()
if self.use_ttsfrd:
texts = [i["text"] for i in json.loads(self.frd.do_voicegen_frd(text))["sentences"]]
text = ''.join(texts)
else:
if contains_chinese(text):
text = self.zh_tn_model.normalize(text)
text = text.replace("\n", "")
text = replace_blank(text)
text = replace_corner_mark(text)
text = text.replace(".", "。")
text = text.replace(" - ", ",")
text = remove_bracket(text)
text = re.sub(r'[,,、]+$', '。', text)
else:
text = self.en_tn_model.normalize(text)
text = spell_out_number(text, self.inflect_parser)
# 移除了split_paragraph切片处理,直接返回整个文本
return [text] if split is True else text
# 先启动CosyVoice
python webui.py --port 50000 --model_dir pretrained_models/models_name
# 再运行app.py
python app.py
本项目基于以下优秀的开源项目开发:
感谢B站UP主TinyLight微光小明分享的爱莉希雅参考音频
- 确保 CosyVoice 服务已正确部署并运行在 50000 端口(可自行修改端口号)
- 参考音频文件需按格式命名:
【情感】语音内容.wav
欢迎通过 Issue 或 PR 提交改进建议!
本项目采用 Apache License 2.0