华为
的ModelArts
平台做了类似的图像识别
之类的小项目,零编码,但是需要自己搞数据集,标注、选择算法、训练模型等,用的话直接调API。NLP
方面之前的一个实习公司有用,对一些类似招股书文件数据进行核查的。那会我作为Java开发做些数据清洗的工作,调NLP的接口去识别一些表格,然后用java写一些逻辑,把数据的按要求分类整理上传。NLP
之类的算法也不懂,买了一本相关的书籍,也落灰了。看到这个活动,想学习学习,所以参加了。活动内容:
使用Azure认知服务免费提供的AI服务
(包括语音转文本、文本转语音、语音翻译、文本分析、文本翻译、语言理解)开发智能应用,并以博文形式分享使用上述服务(至少试用3项服务)教程以及自己的使用心得体验;
我徒然学会了抗拒热闹,却还来不及透悟真正的冷清。--------张大春
在使用之前我们需要先了解下相关概念
认知服务: 提供认知理解(看、听、说、理解,甚至可以决策的认知)功能的服务。
认知服务主要分为四大类:
Azure 认知服务
是具有 REST API
和客户端库 SDK
的基于云的服务
,可用于帮助你将认知智能
构建到应用程序中。 即使你没有人工智能 (AI) 或数据科学技能
,也可向应用程序添加认知功能。Azure 认知服务
包含各种 AI 服务
,让你能够构建可以看、听、说、理解,甚至可以决策的认知解决方案。
我们要做一个可以和自己聊天的机器人,所以需要语言理解.
语言理解 (LUIS) 是一种基于云的对话式 AI 服务
,可在用户对话的自然语言文本中应用自定义机器学习智能,以便预测整体含义并提炼出相关的详细信息
。 LUIS 通过其自定义门户、API 和 SDK 客户端库提供访问权限。找重点,会提炼出相关信息,换句话讲,就是把要表达的信息转化成机器或者说AI能够识别的信息。
使用Azure门户创建新资源 |
关于Azure 认知服务入门方面的教程,小伙伴可以移步官方文档 https://docs.azure.cn/zh-cn/cognitive-services/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows |
步骤 |
---|
登录到LUIS门户 |
创建新应用 |
生成模型 |
添加意向,即你要说些什么话,即语料,这个随便写点 |
添加13-15个 |
添加实体 |
使用预构建的模型,这里我们全都选择了,上面的2步应该就是生成这个,类似模板 |
训练模型 |
发布模型 |
查看信息 |
┌──[[email protected]]-[/liruilong]
└─$ docker pull centos/python-36-centos7
┌──[[email protected]]-[/liruilong]
└─$ ls
input luis_run.sh output predict.py
┌──[[email protected]]-[/liruilong]
└─$ docker run --rm -it --name=chatbot -v $PWD/predict.py:/predict.py centos/python-36-centos7 /bin/bash
(app-root) cd /
(app-root) ls
anaconda-post.log bin boot dev etc help.1 home lib lib64 media mnt opt predict.py proc root run sbin srv sys tmp usr var
(app-root) python predict.py
Traceback (most recent call last):
File "predict.py", line 6, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
(app-root) pip install requests
.........................
Collecting requests
Downloading https:/
.....
(app-root) python predict.py
{
'query': '一个人怎么生活?', 'prediction': {
'topIntent': '生活加油', 'intents': {
'生活加油': {
'score': 0.8969882}, 'Calendar.ShowNext': {
'score': 0.58274937}, 'Calendar.FindCalendarWhen': {
'score': 0.25383785}, 'Places.GetReviews': {
'score': 0.24764298}, 'Utilities.ReadAloud': {
'score': 0.20971665}, 'HomeAutomation.QueryState': {
'score': 0.15509635}, 'Calendar.CheckAvailability': {
'score': 0.12212229}, 'Places.GetPriceRange': {
'score': 0.122063436},........
测试成功 :predict.py脚本
########### Python 3.6 #############
#
# This quickstart shows how to predict the intent of an utterance by using the LUIS REST APIs.
# 导入模块
import requests
try:
##########
# Values to modify.
# YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page.
# 替换为自己的APP-ID
appId = '949d3538-07df-4149-bee8-83dc7f4e11bd'
# YOUR-PREDICTION-KEY: Your LUIS prediction key, 32 character value.
prediction_key = '24440ef2829c45f0a061599ee00b496a'
# YOUR-PREDICTION-ENDPOINT: Replace with your prediction endpoint.
# For example, "https://westus.api.cognitive.microsoft.com/"
prediction_endpoint = 'https://chatbot0.cognitiveservices.azure.cn/'
# The utterance you want to use.
# 你想和他说的话..
utterance = '一个人怎么生活?'
##########
# The headers to use in this REST call.
headers = {
}
# The URL parameters to use in this REST call.
params ={
'query': utterance,
'timezoneOffset': '0',
'verbose': 'true',
'show-all-intents': 'true',
'spellCheck': 'false',
'staging': 'false',
'subscription-key': prediction_key
}
# Make the REST call.
response = requests.get(f'{
prediction_endpoint}luis/prediction/v3.0/apps/{
appId}/slots/production/predict', headers=headers, params=params)
# Display the results on the console.
print(response.json())
except Exception as e:
# Display the error string.
print(f'{
e}')
进入资源 |
按照部署要求一步步构建 |
拉取镜像 |
最好找个镜像加速器配置一下,要不太慢了 |
┌──[[email protected]]-[/etc/docker]
└─$ docker pull mcr.microsoft.com/azure-cognitive-services/luis
Using default tag: latest
latest: Pulling from azure-cognitive-services/luis
b248fa9f6d2a: Pull complete
406741bedf7a: Pull complete
0767c6f1d20a: Pull complete
3687afaf861f: Pull complete
095d858c817b: Pull complete
45c05f18f223: Pull complete
ce065679c887: Pull complete
Digest: sha256:3583c9034a6e8f4ec78b2cf8d880d7eab7d960deebfc5ebd03853cffcdb879f0
Status: Downloaded newer image for mcr.microsoft.com/azure-cognitive-services/luis:latest
mcr.microsoft.com/azure-cognitive-services/luis:latest
┌──[[email protected]]-[/etc/docker]
└─$ # 嗯,这个镜像好似做这个没啥用,后面用的不是这个。
运行步骤 |
---|
必需的参数:所有认知服务容器都需要三个主要参数。 最终用户许可协议 (EULA) 的值必须为 accept 。 此外,终结点URL 和API 密钥都是必需的。 |
┌──[[email protected]]-[/liruilong]
└─$ mkdir input output
┌──[[email protected]]-[/liruilong]
└─$ ls
input luis_run.sh output
┌──[[email protected]]-[/liruilong]
└─$ cat luis_run.sh
docker run --rm -it -p 5000:5000 --memory 4g --cpus 2 -v $PWD/input:/input -v $PWD/output:/output mcr.microsoft.com/azure-cognitive-services/language/luis Eula=accept Billing=https://chatbot0.cognitiveservices.azure.cn/ ApiKey=24440ef2829c45f0a061599ee00b496a ##这里参数填自己的
┌──[[email protected]]-[/liruilong]
└─$ docker run --rm -it -p 5000:5000 --memory 4g --cpus 2 -v $PWD/input:/input -v $PWD/output:/output mcr.microsoft.com/azure-cognitive-services/language/luis Eula=accept Billing=https://chatbot0.cognitiveservices.azure.cn/ ApiKey=24440ef2829c45f0a061599ee00b496a ## 启动容器
EULA Notice: Copyright © Microsoft Corporation 2020. This Cognitive Services Container image is made available to you under the terms [https://go.microsoft.com/fwlink/?linkid=2018657] governing your subscription to Microsoft Azure Services (including the Online Services Terms [https://go.microsoft.com/fwlink/?linkid=2018760]). If you do not have a valid Azure subscription, then you may not use this container.
Using '/input' for reading models and other read-only data.
Using '/output/luis/ff2a18ee78a1' for writing logs and other output data.
Logging to console.
Submitting metering to 'https://chatbot0.cognitiveservices.azure.cn/'.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {
d826e89b-febe-4b93-bad8-db07cb994012} may be persisted to storage in unencrypted form.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Overriding address(es) 'http://+:80'. Binding to endpoints defined in UseKestrel() instead.
Hosting environment: Production
Content root path: /app
Now listening on: http://0.0.0.0:5000
Application started. Press Ctrl+C to shut down.
测试 |
---|
查看接口API |
应用包上传,放到input文件夹下
┌──(liruilong㉿Liruilong)-[/mnt/c/Users/lenovo/Downloads]
└─$ scp ./949d3538-07df-4149-bee8-83dc7f4e11bd_production.gz [email protected]:/
949d3538-07df-4149-bee8-83dc7f4e11bd_production.gz 100% 6434KB 19.0MB/s 00:00
放到input文件夹下,启动容器
┌──[[email protected]]-[/]
└─$ cp 949d3538-07df-4149-bee8-83dc7f4e11bd_production.gz /liruilong/input/
┌──[[email protected]]-[/]
└─$ cd /liruilong/input/
┌──[[email protected]]-[/liruilong/input]
└─$ ls
949d3538-07df-4149-bee8-83dc7f4e11bd_production.gz
┌──[[email protected]]-[/liruilong/input]
└─$ cd ..
┌──[[email protected]]-[/liruilong]
└─$ pwd
/liruilong
┌──[[email protected]]-[/liruilong]
└─$ # 启动容器服务
┌──[[email protected]]-[/liruilong]
└─$ docker run --rm --name=demo -it -p 5000:5000 --memory 4g --cpus 2 -v $PWD/input:/input -v $PWD/output:/output mcr.microsoft.com/azure-cognitive-services/language/luis Eula=accept Billing=https://chatbot0.cognitiveservices.azure.cn/ ApiKey=24440ef2829c45f0a061599ee00b496a
EULA Notice: Copyright © Microsoft Corporation 2020. This Cognitive Services Container image is made available to you under the terms [https://go.microsoft.com/fwlink/?linkid=2018657] governing your subscription to Microsoft Azure Services (including the Online Services Terms [https://go.microsoft.com/fwlink/?linkid=2018760]). If you do not have a valid Azure subscription, then you may not use this container.
Using '/input' for reading models and other read-only data.
Using '/output/luis/da43b9631f9d' for writing logs and other output data.
Logging to console.
Submitting metering to 'https://chatbot0.cognitiveservices.azure.cn/'.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
No XML encryptor configured. Key {
886021ff-6bf5-423d-be12-ba9b52383b9e} may be persisted to storage in unencrypted form.
warn: Microsoft.AspNetCore.Server.Kestrel[0]
Overriding address(es) 'http://+:80'. Binding to endpoints defined in UseKestrel() instead.
Hosting environment: Production
Content root path: /app
Now listening on: http://0.0.0.0:5000
Application started. Press Ctrl+C to shut down.
文件名不对,需要改一下 |
---|
用swagger测试一下 |
{
"query": "据悉,该活动由世界休闲组织,杭州市政府主办,中国国际科技促进会三农发展工作委员会等联合会承办。",
"topScoringIntent": {
"intent": "Places.GetPhoneNumber",
"score": 0.9103095
},
"entities": [
{
"entity": "工作",
"type": "Calendar.Subject",
"startIndex": 35,
"endIndex": 36,
"score": 0.63087225
},
{
"entity": "际科技促进会三农发展工作委员会等联合会承办 。",
"type": "Note.Text",
"startIndex": 25,
"endIndex": 46,
"score": 0.6798551
},
{
"entity": "杭州市",
"type": "Places.AbsoluteLocation",
"startIndex": 14,
"endIndex": 16,
"score": 0.966151536
},
{
"entity": "国际科",
"type": "Places.PlaceName",
"startIndex": 24,
"endIndex": 26,
"score": 0.3605822
},
{
"entity": "工作",
"type": "Calendar.DestinationCalendar",
"startIndex": 35,
"endIndex": 36,
"resolution": {
"values": [
"工作"
]
}
},
{
"entity": "工作",
"type": "Places.OpenStatus",
"startIndex": 35,
"endIndex": 36,
"resolution": {
"values": [
"工作"
]
}
},
{
"entity": "休闲",
"type": "RestaurantReservation.Atmosphere",
"startIndex": 9,
"endIndex": 10,
"resolution": {
"values": [
"休闲"
]
}
}
]
}
简单尝试一下,把生成的实体组合,作为关键字给一机器人用,然后识别。进行简单对话,简单测试了下,发现效果很一般,可能是语料的问题,或者我找的机器人太差了,Azure的机器人需要注册绑卡…所以这个以后有时间研究研究
语音转文本