七夕给自己new一个chatRobot当对象

七夕给自己new一个chatRobot当对象😏

💎本文是博主周末做的小玩意,
🔈大量参考了该博文:``https://blog.csdn.net/NIeson2012/article/details/96476878 
以及华为 https://support.huaweicloud.com/cbs/index.html、
百度 https://cloud.baidu.com/doc/SPEECH/index.html 官方技术文档。
有需要的朋友可以自行查阅。
👀华为后台语料库需自行(tiao)配置(jiao)
https://download.csdn.net/download/m0_66681776/86269857 可直接下载源文件

1 准备工作

  • vscode
  • anaconda
  • 百度账号(已实名)
  • 华为账号(已实名)

在百度云官网 产品->人工智能->语音技术->短文本合成->立即使用->…… 创建一个应用(可以试用,跟着提示走就好)

华为云官网也一样,在产品里找到智能语音交互机器人CBS ,也是申请试用(博主作为学生,属实负担不起原价🥺)

后续的APP_ID之类的也在这些里面找就行,技术文档里有直达链接,很方便🙂。

用anaconda创建一个虚拟环境(对于不同的项目最好相互环境独立),最好是python版本3.6,高版本有些库不支持。

安装pygamepyaudiobaidu-siphuaweicloudsdkcorehuaweicloudsdkcbs库(pip和conda都试一试,哪个行用哪个😆)

2 设计思路

Created with Raphaël 2.3.0 开始程序 录制音频并存储:使用pyaudio库进行录制,wave库进行.wav文件的存储 识别音频文件:上传至百度AI平台SDK鉴权调用API 返回sentence:对返回的内容进行抽取 sentence is not "退出程序" 上传sentence:上传至华为AI平台SDK鉴权调用API 返回response:对返回的内容进行抽取 语音合成response:上传至百度…… 存储音频文件:对返回的内容…… 播放音频文件:使用pygame库进行播放 退出程序 yes no

3 项目源码

这部分反而对博主而言是最简单的,
录音/播放在CSDN查查就有很多,
API的调用官方文档已经恨不得把饭喂我们吃了,
简简单单`CV`一波🉑。

文件夹的架构按博主的来,路径都是相对的。博主是用的VSC,所以在launch.json将functions设置为根目录。_pychae_是在程序运行过程中自己建立起来的。

chatRobot:
    |_>.vscode:
        |_>launch.json
    |_>conclusion:
        |_>conclusion.ipynb
        |_>conclusion.md
    |_>functions:
        |_>__init__.py
        |_>baidu.py
        |_>huawei.py
        |_>recoder.py
    |_>music:
        |_>myvoice.wav
        |_>robotVoice.mp3
    |_>secrets:
        |_>baiduSecrets.csv
        |_>huaweiSecrets.csv
    |_>tests:
        |_>test.py
    |_>main.py

3.1 recoder类

"""
参考博文:
    @author: https://nieson.blog.csdn.net/?type=blog
    @blog: https://blog.csdn.net/NIeson2012/article/details/96476878
"""
import wave
import time 
from pyaudio import PyAudio, paInt16
from pygame import mixer,quit

class recoder():
    
    def __init__(self, filepath="music/myvoice.wav") -> None:
        """_summary_

        Args:
            filepath (str): 文件保存位置
        """
        self.framerate = 16000
        self.num_samples = 2000
        self.channels = 1
        self.sampwidth = 2
        self.FILEPATH = filepath
    
    def save_wave_file(self, filepath: str, data: list) -> None :
        
        """_summary_

        Args:
            filepath (str): 文件保存路径
            data (list): 音频数据
        """
        
        wf = wave.open(filepath, "wb")
        wf.setnchannels(self.channels)
        wf.setsampwidth(self.sampwidth)
        wf.setframerate(self.framerate)
        wf.writeframes(b''.join(data))
        wf.close()
    
    def recordVoice(self) -> None :
        
        """_function_
            记录声音
        """
        
        pa = PyAudio()
        #打开一个新的音频stream
        stream = pa.open(format=paInt16, channels=self.channels,
                        rate=self.framerate, input=True, frames_per_buffer=self.num_samples)
        my_buf = [] #存放录音数据
 
        t = time.time()
        print('正在录音...')
 
        while time.time() < t + 10:  # 设置录音时间(秒)
    	    #循环read,每次read 2000frames
            string_audio_data = stream.read(self.num_samples)
            my_buf.append(string_audio_data)
        print('录音结束.')
        self.save_wave_file(self.FILEPATH, my_buf)
        stream.close()
        
    def playVoice(self, filepath="music/robotVoice.mp3"):
        """_summary_

        Args:
            filepath (_type_): 待播放文件路径
        """
        mixer.init()
        mixer.music.load(filepath)
        mixer.music.play()
        time.sleep(10)
        mixer.music.stop()
        quit()


if __name__ == "__main__":
    recoder = recoder("music/myvoice.wav")
    recoder.recordVoice()
    recoder.playVoice("music/myvoice.wav")

3.2 huawei类

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkcore.http.http_config import HttpConfig
# 导入CBS服务库huaweicloudsdkcbs
from huaweicloudsdkcbs.v1.region.cbs_region import CbsRegion
from huaweicloudsdkcbs.v1 import *

import codecs
import csv

class huawei():
    
    def __init__(self) -> None:
        
        """_function_
        
            读取secrets内的各类密匙
        """

        with codecs.open('secrets/huaweiSecrets.csv', encoding='utf-8-sig') as f:
            for row in csv.DictReader(f, skipinitialspace=True):
                self.project_id = row["Project ID"]
                self.qabot_id = row["qabot_id"]
                self.ak = row["Access Key Id"]
                self.sk = row["Secret Access Key"]
    
    def getResponse(self, sentence: str) -> str:
        
        """_summary_

        Args:
            sentence (str): 待回复的语句

        Returns:
            str: 后台预设回复
        """
        
        # 使用默认配置
        config = HttpConfig.get_default_config()
    
        # 初始化客户端认证信息,使用当前客户端初始化方式可不填 project_id/domain_id,以BasicCredentials为例
        basic_credentials = BasicCredentials(self.ak, self.sk, self.project_id)

        # 初始化指定云服务的客户端 {Service}Client ,以初始化 Region 级服务CBS的 CbsClient 为例
        client = CbsClient.new_builder() \
            .with_http_config(config) \
            .with_credentials(basic_credentials) \
            .with_region(CbsRegion.value_of("cn-north-4")) \
            .build()

        # 以调用问答机器人会话接口ExecuteQaChat为例
        request = ExecuteQaChatRequest()
        # qabot_id获取方法参考本章节“准备工作”
        request.qabot_id = self.qabot_id
        request.body = PostRequestsReq(
            # session_id首轮不需要传入或可传入任意值,第二轮开始使用上一轮返回的session_id
            session_id="ad7a5010-3817...",
            question=sentence,
        )
        response = client.execute_qa_chat(request)
    
        # 异常处理
        try:
            request = ExecuteQaChatRequest()
            response = client.execute_qa_chat(request)
            print(response)
        except exceptions.ClientRequestException as e:
            pass
            # print(e.status_code)
            # print(e.request_id)
            # print(e.error_code)
            # print(e.error_msg)
    
        return response.qabot_answers.answers[0].answer[3:-4]
    
                
if __name__ == "__main__":
    huawei = huawei()
    print(huawei.project_id)
    print(huawei.getResponse("早上好"))

3.3 baidu类

from aip import AipSpeech
import codecs
import csv

class baidu():
    
    def __init__(self) -> None:
        
        """_function_
        
            读取secrets.csv内的各类密匙
        """
        
        with codecs.open('secrets/baiduSecrets.csv', encoding='utf-8-sig') as f:
            for row in csv.DictReader(f, skipinitialspace=True):
                self.APP_ID = row["APP_ID"]
                self.API_KEY = row["API_KEY"]
                self.SECRET_KEY = row["SECRET_KEY"]
        self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)
    
    
        
    def getWords(self, filepath="music/myvoice.wav") -> str:
        
        """_summary_

        Args:
            filepath (str): 待识别语音

        Returns:
            str: 识别后字符串
        """
        
        def get_file_content(filePath: str):
            with open(filePath, 'rb') as fp:
                return fp.read()
        
        result = self.client.asr(get_file_content(filepath), 'pcm', 16000, {
            'dev_pid': 1537,})
        return result["result"][0]
    
    def getVoice(self, sentence: str):
        
        filePath = "music/robotVoice.mp3"
        result  = self.client.synthesis(sentence, 'zh', 1, {
        'vol': 5, 'per': 111,
        })

        # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
        if not isinstance(result, dict):
            with open(filePath, 'wb') as f:
                f.write(result)
                f.close()
                        
if __name__ == "__main__":
    baidu = baidu()
    print(baidu.APP_ID)
    response = baidu.getWords("music/myvoice.wav")
    print(response)
    baidu.getVoice("早上好")

3.4 CSV文件

IAM Name,IAM ID,Project ID,qabot_id,User Name,Access Key Id,Secret Access Key
******,******,******,******,******,******,******
APP_ID,API_KEY,SECRET_KEY
******,******,******

4 总结

本次的小项目完成的还算成功,但是录音/播放功能属实很拉胯,是固定时间的。有个思路,参照微信用空格键控制语音的输入与输出,感兴趣的朋友可以试试💡。

点赞👍收藏🔸加关注💖,回看不迷路!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

糠帅傅蓝烧牛肉面

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

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

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

打赏作者

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

抵扣说明:

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

余额充值