当前位置:网站首页>七夕给自己new一个chatRobot当对象
七夕给自己new一个chatRobot当对象
2022-08-05 08:11:00 【蜡笔小旧_】
七夕给自己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,高版本有些库不支持。
安装pygame
、pyaudio
、baidu-sip
、huaweicloudsdkcore
和huaweicloudsdkcbs
库(pip和conda都试一试,哪个行用哪个)
2 设计思路
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 总结
本次的小项目完成的还算成功,但是录音/播放功能属实很拉胯,是固定时间的。有个思路,参照微信用空格键控制语音的输入与输出,感兴趣的朋友可以试试。
点赞收藏加关注,回看不迷路!
边栏推荐
- How Entrepreneurs Attract Venture Capitalists
- The toss of MM before going to the street (interesting)
- Controlling number and letter input in ASP
- egg框架
- The Coolest Kubernetes Network Solution Cilium Getting Started Tutorial
- RedisTemplate: 报错template not initialized; call afterPropertiesSet() before using it
- Redis缓存以及存在的问题--缓存穿透、缓存雪崩、缓存击穿及解决方法
- 【结构体内功修炼】结构体内存对齐(一)
- P1160 队列安排
- moment的使用
猜你喜欢
随机推荐
SVG星球大战样式Toggle切换开关按钮
SVG Star Wars Style Toggle Toggle Button
Spark cluster deployment (third bullet)
星座理想情人
执子之手,与子偕老。你同意么?
The color of life divine
MySQL 数据库 报错 The server quit without updating PID file (/var/lib/mysql/localhost.localdomain.pid)
Controller-----controller
数据库——概述
ps怎么替换颜色,自学ps软件photoshop2022,ps一张图片的一种颜色全部替换成另外一种颜色
[转帖]嫁人一定要嫁工资至少比你高3571.4元的男士
链表专项之环形链表
剑指Offer面试题解总结1-10
学习机赛道加速:请“卷”产品,不要“卷”营销
线性代数对角化
字符串提取 中文、英文、数字
基于多块信息提取和马氏距离的k近邻故障监测
MobileNetV1架构解析
力扣每日一题
Fiddler tool explanation