WeNet语音识别+Qwen-72B-Chat Bot+Sambert-Hifigan语音合成

WeNet语音识别+Qwen-72B-Chat Bot👾+Sambert-Hifigan语音合成

简介

利用 WeNet 进行语音识别,使用户能够通过语音输入与系统进行交互。接着,Qwen-72B-Chat Bot作为聊天机器人接收用户的语音输入或文本输入,提供响应并与用户进行对话。最后,系统利用 Sambert-Hifigan 进行语音合成,将机器人的响应转换为自然流畅的语音输出,使用户能够以语音方式接收机器人的回复。

特点

  1. 对话记忆功能: 该系统能够记忆和追踪用户和聊天机器人之间的对话历史。这使得用户能够在对话中随时回顾之前的交流内容,从而实现更连贯的对话和更好的交互体验。

  2. 多语音模型切换: 该系统支持多种语音模型的切换。用户可以根据需要选择不同的语音模型进行交互。这种多语音模型切换功能使得系统在不同语境下有更强的适用性和灵活性。

界面

体验一下

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

环境配置

在这里插入图片描述

完整代码

import os
os.system('pip install dashscope')
os.system('pip install modelscope')
import gradio as gr
from http import HTTPStatus
import dashscope
from dashscope import Generation
from dashscope.api_entities.dashscope_response import Role
from typing import List, Optional, Tuple, Dict
from urllib.error import HTTPError
import wenet
from modelscope.outputs import OutputKeys
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks



default_system = 'You are a helpful assistant.'
chs_model = wenet.load_model('chinese')
YOUR_API_TOKEN = os.getenv('YOUR_API_TOKEN')
dashscope.api_key = YOUR_API_TOKEN
History = List[Tuple[str, str]]
Messages = List[Dict[str, str]]



# 加载四个不同的语音合成模型
sambert_hifigan_zh_model_id = 'damo/speech_sambert-hifigan_tts_zh-cn_16k'
sambert_hifigan_zh = pipeline(task=Tasks.text_to_speech, model=sambert_hifigan_zh_model_id)

sambert_hifigan_ch_model_id = 'speech_tts/speech_sambert-hifigan_tts_chuangirl_Sichuan_16k'
sambert_hifigan_ch = pipeline(task=Tasks.text_to_speech, model=sambert_hifigan_ch_model_id)

sambert_hifigan_ca_model_id = 'speech_tts/speech_sambert-hifigan_tts_jiajia_Cantonese_16k'
sambert_hifigan_ca = pipeline(task=Tasks.text_to_speech, model=sambert_hifigan_ca_model_id)

sambert_hifigan_ws_model_id = 'speech_tts/speech_sambert-hifigan_tts_xiaoda_WuuShanghai_16k'
sambert_hifigan_ws = pipeline(task=Tasks.text_to_speech, model=sambert_hifigan_ws_model_id)

    
def clear_session() -> History:
    return []

def modify_system_session(system: str) -> str:
    if system is None or len(system) == 0:
        system = default_system
    return system, system, []

def history_to_messages(history: History, system: str) -> Messages:
    messages = [{'role': Role.SYSTEM, 'content': system}]
    for h in history:
        messages.append({'role': Role.USER, 'content': h[0]})
        messages.append({'role': Role.ASSISTANT, 'content': h[1]})
    return messages


def messages_to_history(messages: Messages) -> Tuple[str, History]:
    assert messages[0]['role'] == Role.SYSTEM
    system = messages[0]['content']
    history = []
    for q, r in zip(messages[1::2], messages[2::2]):
        history.append([q['content'], r['content']])
    return system, history

def model_chat(path:str, history: Optional[History], system: str,model:str,voice:str
) -> Tuple[str, str, History]:
    if path is not None:
        query = chs_model.transcribe(path)['text']
        if query is None:
            query = ''
        if history is None:
            history = []
        messages = history_to_messages(history, system)
        messages.append({'role': Role.USER, 'content': query})
        gen = Generation.call(
            model = "qwen-72b-chat",
            messages=messages,
            result_format='message',
            stream=True
        )
        for response in gen:
            if response.status_code == HTTPStatus.OK:
                role = response.output.choices[0].message.role
                response = response.output.choices[0].message.content
                system, history = messages_to_history(messages + [{'role': role, 'content': response}])
            else:
                raise HTTPError('Request id: %s, Status code: %s, error code: %s, error message: %s' % (
                    response.request_id, response.status_code,
                    response.code, response.message
                ))

        output=None
         # 进行语音合成
        sambert_hifigan_tts_model = {
            '默认': sambert_hifigan_zh,
            '四川话': sambert_hifigan_ch,
            '粤语': sambert_hifigan_ca,
            '上海话': sambert_hifigan_ws
        }

        # 使用对应的语音合成模型进行合成
        sambert_hifigan_tts = sambert_hifigan_tts_model.get(model)
        
        if model == '默认':
            output = sambert_hifigan_tts(input=response, voice=voice)
        else:
            output = sambert_hifigan_tts(input=response)
        
        wav = output[OutputKeys.OUTPUT_WAV]
        path = 'output.wav'
        with open(path, 'wb') as f:
            f.write(wav)
        return history, system, path

def update_dropdowns(model,voice):   
    if model == "默认":  
        voice=gr.Dropdown(choices=['zhitian_emo', 'zhiyan_emo', 'zhizhe_emo', 'zhibei_emo'], value='zhitian_emo',label="声音",visible=True) 
    else: 
        voice=gr.Dropdown(choices=['zhitian_emo', 'zhiyan_emo', 'zhizhe_emo', 'zhibei_emo'], value='zhitian_emo',label="声音",visible=False)
    return voice
with gr.Blocks() as demo:
    gr.Markdown("""<p align="center"><img src="https://modelscope.cn/api/v1/models/qwen/Qwen-VL-Chat/repo?Revision=master&FilePath=assets/logo.jpg&View=true" style="height: 80px"/><p>""")
    gr.Markdown("""<center><font size=4>WeNet语音识别+Qwen-72B-Chat Bot👾+Sambert-Hifigan语音合成</center>""")

    textbox = gr.Microphone(type="filepath",label='录音')
    with gr.Row():
        with gr.Column(scale=3):
            system_input = gr.Textbox(value=default_system, lines=1, label='System', visible=False)
        with gr.Column(scale=1):
            modify_system = gr.Button("🛠️ 设置system并清除历史对话", scale=2, visible=False)
        system_state = gr.Textbox(value=default_system, visible=False)
    chatbot = gr.Chatbot(label='Qwen-72B-Chat', visible=False)
    model=gr.Dropdown(choices=['默认', '四川话', '粤语', '上海话'], value='默认',label="声音模型")
    voice = gr.Dropdown(choices=['zhitian_emo', 'zhiyan_emo', 'zhizhe_emo', 'zhibei_emo'], value='zhitian_emo',label="声音")
    
    audio_output = gr.Audio(type="filepath",label='输出音频',autoPlay=True)

    with gr.Row():
        clear_history = gr.Button("🎲 清除记忆")
        sumbit = gr.Button("🚀 发送")

    model.change(update_dropdowns,inputs=[model,voice],outputs=[voice])

    sumbit.click(model_chat,
                 inputs=[textbox, chatbot, system_state,model,voice],
                 outputs=[chatbot, system_input,audio_output],
                 concurrency_limit=10)
    clear_history.click(fn=clear_session,
                        inputs=[],
                        outputs=[chatbot],
                        concurrency_limit=10)
    modify_system.click(fn=modify_system_session,
                        inputs=[system_input],
                        outputs=[system_state, system_input, chatbot],
                        concurrency_limit=10)
demo.queue(api_open=False).launch(height=800, share=False)

在这里插入图片描述

### Qwen-VL-72B 和 Qwen-VL-Max 的特性比较 #### 参数规模与模型架构 Qwen-VL-72B 是一款拥有 720亿参数的大规模多模态预训练模型,专为处理复杂的视觉和语言联合任务而设计[^1]。相比之下,Qwen-VL-Max 虽然具体参数量未公开,但从名称推测其可能代表了一个更为优化或改进版本,在某些特定场景下具备更优性能。 #### 处理能力对比 对于建模方面的挑战——即面对差异很大的视觉任务(包括不同类型输入的任务、不同粒度的任务以及格式各异的输出),Qwen-VL-72B 凭借庞大的参数量能够更好地捕捉复杂模式并适应多种类型的输入数据。然而,Qwen-VL-Max 可能在效率上有所提升,通过算法上的创新减少计算资源消耗的同时保持甚至提高准确性。 #### 数据需求分析 考虑到标注成本差异大且粒度和语义各异的数据集特点,Qwen-VL-72B 需要依赖大量高质量标记样本进行有效学习;而对于收集图像成本高且数量有限的情况,则显示出一定局限性。相反,Qwen-VL-Max 或者引入了新的机制来缓解这些题,比如增强自监督学习的能力或是利用迁移学习技术从其他领域获取有用信息补充当前任务所需的知识。 ```python # Python伪代码展示如何加载这两个模型 from transformers import AutoModelForVisionToText, AutoTokenizer def load_model(model_name): tokenizer = AutoTokenizer.from_pretrained(model_name) model = AutoModelForVisionToText.from_pretrained(model_name) return model, tokenizer qwen_vl_72b_model, qwen_vl_72b_tokenizer = load_model('qwen-vl-72b') qwen_vl_max_model, qwen_vl_max_tokenizer = load_model('qwen-vl-max') ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈增林

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值