【智控万物】codelab对接图灵机器人(实现语音聊天)
【演示视频】https://v.youku.com/v_show/id_XNDY1NzY3MzA0OA==.html
视频结尾处出问题在于,图灵对话机器人调用量没了(免费版适用于个人或测试使用,图灵机器人平台每天会赠送免费版用户100次调用量),每个账号可创建5个机器人。
1、下载CodeLab
https://www.codelab.club/blog/codelab-download/下载页,下载后解压,运行程序codelab-adapter-win-3_1_0.exe,会在默认浏览器中打开运行页面“CodeLab Adapter v3 Web UI”
2、下载extensio_python_exec。
看见此项,安装完成,下面修改程序代码。
用记事本或其它Python文本编辑工具
将以下代码覆盖原有代码。
from io import StringIO
import contextlib
import sys
import time
# import subprocess
# import webbrowser
from codelab_adapter.core_extension import Extension
from codelab_adapter.utils import verify_token
import json
import urllib.request
class PythonKernelExtension(Extension):
def __init__(self):
super().__init__()
self.NODE_ID = self.generate_node_id(__file__)
@contextlib.contextmanager
def stdoutIO(self, stdout=None):
old = sys.stdout
if stdout is None:
stdout = StringIO()
sys.stdout = stdout
yield stdout
sys.stdout = old
def tuling(self, code):
api_url = "http://openapi.tuling123.com/openapi/api/v2"
text_input = code
req = {
"perception":
{
"inputText":
{
"text": text_input
},
"selfInfo":
{
"location":
{
"city": "张家口",
"province": "河北",
"street": "涿鹿"
}
}
},
"userInfo":
{
"apiKey": "06c515993ed74e8e9ae1aa63a80ab9ac",
"userId": "OnlyUseAlphabet"
}
}
# print(req)
# 将字典格式的req编码为utf8
req = json.dumps(req).encode('utf8')
# print(req)
http_post = urllib.request.Request(api_url, data=req, headers={'content-type': 'application/json'})
response = urllib.request.urlopen(http_post)
response_str = response.read().decode('utf8')
# print(response_str)
response_dic = json.loads(response_str)
# print(response_dic)
intent_code = response_dic['intent']['code']
results_text = response_dic['results']['values']['text']
return('图灵的回答:' + results_text)
def run_python_code(self, code):
try:
with self.stdoutIO() as s:
# 'hello world'[::-1]
# import re; print(re.search('just4fun', 'blog.just4fun.site').span())
# exec(object[, globals[, locals]])
exec(code)
output = s.getvalue()
except Exception as e:
output = e
return output
@verify_token
def extension_message_handle(self, topic, payload):
'''
所有可能运行代码的地方,都加上验证,确认payload中代码风险和token
test: import webbrowser;webbrowser.open("https://www.codelab.club")
'''
self.logger.info(f'python code: {payload["content"]}')
message_id = payload.get("message_id")
python_code = payload["content"]
#output = self.run_python_code(python_code)
output =self.tuling(python_code)
payload["content"] = str(output)
message = {"payload": payload}
self.publish(message)
def run(self):
"避免插件结束退出"
while self._running:
time.sleep(0.5)
export = PythonKernelExtension
这"apiKey"要改成自己的。不想要“图灵的回答”,可把return('图灵的回答:' + results_text),改成return( results_text)。修改后记得重新启动extension_python_exec。
点击运行。
3、打开https://scratch3v3.codelab.club/
这段程序要一直跟图灵说话,5秒一次,中间有一次不说,谈话就中止。
修正1:使用标识判断,解决语音为空时(不说话的时候)。因使用默认的5秒(一句话在5秒说完,差不多),可以自己修改。
还存在两个问题,一是:不能实现静音识别——不说了作为中止,然后开始识别;二是:没有唤醒词功能,像“小度小度”那种。这两个问题都可以用App Inventor2在手机上解决。
修正2:
此段程序实现,语音对话开始后,如果连续有超三次不说话(静音),将进入休眠状态,直到响度大于50后,再重新启动语音对话。
【修正后演示视频】
https://v.youku.com/v_show/id_XNDY1NzcyMjg0NA==.html
很深奥,要好好学习
页:
[1]