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

化妆品网站建设描述南京网站建设

化妆品网站建设描述,南京网站建设,辽宁省人民政府网官网,专业网站建设技术大模型场景实战培训,提示词效果调优,大模型应用定制开发,点击咨询 咨询热线:400-920-8999转2 GPT-4的语音对话功能前段时间在网上火了一把,许多人被其强大的自然语言处理能力和流畅的语音交互所吸引。现在,…

大模型场景实战培训,提示词效果调优,大模型应用定制开发,点击咨询
咨询热线:400-920-8999转2

GPT-4的语音对话功能前段时间在网上火了一把,许多人被其强大的自然语言处理能力和流畅的语音交互所吸引。现在,让我们来看看如何使用类似的技术,即基于百度的ERNIE-Bot,来打造自己的语音对话功能。ERNIE-Bot是一种先进的语言理解模型,可以处理复杂的语言任务,包括语音到文本的转换和自然语言理解。

视频演示:
shequ.mov

涉及技术:

  • langchain Memory、Chain
  • Ernie-bot
  • 百度智能云语音识别: https://ai.baidu.com/ai-doc/SPEECH/0lbxfnc9b
  • 百度智能云语音合成: https://ai.baidu.com/ai-doc/SPEECH/plbxhh4be

依赖:

  • ffmpeg

langchain调用Ernie-bot并实现记忆

值得一提的是,在langchain构建ernie-bot多轮对话能力上,社区中已有成员对此进行了深入研究和实践: https://cloud.baidu.com/qianfandev/topic/267682

from langchain.chat_models import ErnieBotChat
from langchain.chains import LLMChain
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
# 定义一个大语言模型对象
llm = ErnieBotChat(ernie_client_id="xxxxx", ernie_client_secret="xxxxxx",model_name='ERNIE-Bot',temperature=0.01) # 定义prompt template    
template = """You are a chatbot having a conversation with a human. Please answer as briefly as possible.{chat_history}
Human: {human_input}
Chatbot:
"""prompt = PromptTemplate(input_variables=["chat_history", "human_input"], template=template
)
# memory
memory = ConversationBufferMemory(llm=llm,memory_key="chat_history",return_messages=True)
# chain
conversation = LLMChain(llm=llm, memory=memory,prompt=prompt)
# 推理
response = conversation.predict(human_input=input)

语音识别部分

目前,百度语音识别服务支持固定的采样率:16000Hz 和 8000Hz。为了确保您的音频文件与这些标准兼容,可能需要对原始音频的采样率进行适当调整。这意味着,如果您的音频文件采样率与上述两个固定值不符,您将需要通过音频处理软件或编程方法,将其转换为这两种支持的采样率之一。这个步骤是确保音频识别准确性的关键,可以显著提高语音识别的效果和准确度。
详情参考: https://ai.baidu.com/ai-doc/SPEECH/0lbxfnc9b

from aip import AipSpeech
import tempfile
import os, uuid
from pydub import AudioSegmentAPP_ID = 'xxx'
API_KEY = 'xxxxx'
SECRET_KEY = 'xxxxxxxxx'client = AipSpeech(APP_ID, API_KEY, SECRET_KEY)def transcribe(audio):with tempfile.TemporaryDirectory() as tempdir:print(f"Temporary directory created at {tempdir}")audio = AudioSegment.from_file(audio)new_frame_rate = 16000new_audio = audio.set_frame_rate(new_frame_rate)random_code = uuid.uuid4()temp_audio_path = f"{tempdir}/output_audio_16000_{random_code}.wav"new_audio.export(temp_audio_path, format="wav")def get_file_content(filePath):with open(filePath, 'rb') as fp:return fp.read()ret = client.asr(get_file_content(temp_audio_path), 'wav', 16000, {'dev_pid': 1537})# 注意:退出 with 块后,tempdir 及其内容会被自动删除return ret.get('result')[0]

语音合成部分

将大型语言模型生成的文本内容转换成语音输出是一个引人入胜的过程。通过这种转换,我们可以将模型的文字回应转化为更加生动、直观的语音形式。这不仅提升了用户体验,还增加了交互的可访问性和便利性。无论是为视觉受限用户提供更加友好的界面,还是为智能助理增添自然的交流方式,这一过程都显得至关重要。
参考语音合成SDK: https://ai.baidu.com/ai-doc/SPEECH/plbxhh4be

from pydub import AudioSegment
from pydub.playback import playdef play_voice(text):result = client.synthesis(text, 'zh', 1, {'vol': 5})# 识别正确返回语音二进制 错误则返回dict 参照下面错误码if not isinstance(result, dict):with tempfile.NamedTemporaryFile(delete=True, suffix='.mp3', mode='wb') as temp_audio_file:temp_audio_file.write(result)temp_audio_file.seek(0)  # 回到文件开头# print(temp_audio_file.name)# 使用 pydub 播放音频audio = AudioSegment.from_file(temp_audio_file.name, format="mp3")play(audio)

gradio交互逻辑

gradio的核心代码框架如下:

def clear_history():# 返回一个空列表来清空聊天记录return [], []with gr.Blocks(css="#chatbot{height:800px} .overflow-y-auto{height:800px}") as demo:chatbot = gr.Chatbot(elem_id="chatbot")state = gr.State([])with gr.Row():txt = gr.Textbox(show_label=False, placeholder="Enter text and press enter")# 录音功能with gr.Row(): # 得到音频文件地址audio = gr.Audio(sources="microphone", type="filepath")with gr.Row():        clear_button = gr.Button("清空聊天记录")# 重要逻辑  txt.submit(predict, [txt, state], [chatbot, txt, state])    audio.change(process_audio, [audio, state], [chatbot, audio, state])clear_button.click(clear_history, [], [chatbot, state])            
# 启动gradio
demo.launch(share=False)

代码18/19行重点介绍下

  1. txt.submit(predict, [txt, state], [chatbot, txt, state])

    • txt.submit: 这是Gradio中Textbox组件的一个方法,用于绑定一个函数(在本例中是predict函数)到文本框的输入动作上。当用户在文本框中输入文本并提交(通常是按下回车键)时,绑定的predict函数会被触发。
    • predict: 这是绑定到文本框提交动作的函数。这个函数会在用户提交文本后执行。
    • [txt, state]: 这是传递给predict函数的参数列表。在这里,txt代表文本框的内容,state是一个State组件,用于在Gradio界面中维持状态。
    • [chatbot, txt, state]: 这是predict函数执行后,其输出将被传递的组件列表。在此,函数的输出将被用来更新Chatbot组件(显示对话内容)、清空Textbox组件的文本,以及更新State组件的状态。
  2. audio.change(process_audio, [audio, state], [chatbot, audio, state])

    • audio.change: 这是Gradio中Audio组件的一个方法,用于将一个函数(在这个例子中是process_audio函数)绑定到音频输入的变化上。当用户通过音频组件提供新的音频输入时,绑定的process_audio函数会被触发。
    • process_audio: 这是绑定到音频组件变化的函数。当音频输入改变时,这个函数会执行。
    • [audio, state]: 这是传递给process_audio函数的参数列表。在这里,audio代表音频组件的输入,state仍然是用于维持状态的State组件。
    • [chatbot, audio, state]: 这是process_audio函数执行后,其输出将被传递的组件列表。这意味着函数的输出将用于更新Chatbot组件、重置Audio组件的输入,以及更新State组件的状态。

总的来说,这两行代码将用户界面的交互(文本输入和音频输入)与相应的处理函数(predictprocess_audio)关联起来,并定义了这些函数执行后如何更新界面组件。

两个函数的实现逻辑:

def play_voice(text):result = client.synthesis(text, 'zh', 1, {'vol': 5})# 识别正确返回语音二进制 错误则返回dict 参照下面错误码if not isinstance(result, dict):with tempfile.NamedTemporaryFile(delete=True, suffix='.mp3', mode='wb') as temp_audio_file:temp_audio_file.write(result)temp_audio_file.seek(0)  # 回到文件开头# print(temp_audio_file.name)# 使用 pydub 播放音频audio = AudioSegment.from_file(temp_audio_file.name, format="mp3")play(audio)# 录音文件转文本的过程
def process_audio(audio, history=[]):if audio:text = transcribe(audio)  # print(text)if text is None:text="你好"responses, clear, updated_history = predict(text, history)# print(f"====={responses}")# print(f"++++{updated_history}")# 返回处理结果和 None 来清空音频输入return responses, None, updated_history    else:       # print(f"------{history}")return [(u,b) for u,b in zip(history[::2], history[1::2])], None, history# 调用ernie-bot对话功能
def predict(input, history=[]):    history.append(input)    response = conversation.predict(human_input=input)history.append(response)# history[::2] 切片语法,每隔两个元素提取一个元素,即提取出所有的输入,# history[1::2]表示从历史记录中每隔2个元素提取一个元素,即提取出所有的输出# zip函数把两个列表元素打包为元组的列表的方式play_voice(response)responses = [(u,b) for u,b in zip(history[::2], history[1::2])]print("==取出输入:",history[::2])print("==取出输出:",history[1::2])print("组合元组:",responses)return responses, "", history

通过本文的介绍,我们探索了如何利用ERNIE-Bot来打造一个高效的语音对话功能。从设置开发环境、处理音频文件,到利用ERNIE-Bot进行语言理解和生成语音回应,每一步都是构建这一创新交互体验的关键部分。

http://www.rdtb.cn/news/18633.html

相关文章:

  • 网站建设的可研设计报告如何将网站的关键词排名优化
  • WordPress 云 memcachewindows优化大师是系统软件吗
  • 专门做拼团的网站网络营销官网
  • 湖南住房和城乡建设网门户网站静态网站开发
  • 免费小说网站怎么做商丘seo博客
  • 用腾讯云做淘宝客网站视频下载培训机构营业执照如何办理
  • 那种广告式网站怎么做小广告
  • 深圳网站建设fantodo营销团队公司
  • 如何查网站开发商我们公司在做网站推广
  • 设计师 必备的网站阿里云域名注册
  • 模特公司网站源码网站关键词排名优化系统
  • 夏邑网站建设响应式模版移动优化
  • 太原做网站公司运营网络优化报告
  • 湖州 网站建设公司连云港seo公司
  • 做网站产生的流量费怎么算网站建站价格
  • 如何制作营销网站模板下载百度热搜排名
  • 保定建网站公司排名优化哪家专业
  • 网站的系统建设方式有哪些广州新闻发布
  • asp网站发邮件网络营销服务商有哪些
  • 什么都可以看的浏览器百度系优化
  • phpok做网站教程做游戏推广怎么找客户
  • 北京企业网站推广搜索引擎营销的四种方式
  • 百度一下手机版网页北京网络排名优化
  • 什么网站做简历好南京seo网站管理
  • 建e网站策划公司排行榜
  • 网站开发最重要的技巧百度词条
  • 地方网站还有得做吗哈尔滨优化网站公司
  • 济宁网站建设吊装seo排名关键词
  • 北京网站建设官网百度网盘免费下载
  • 赤峰做网站的公司许昌网络推广公司