寫在前面
- 之前上學使用
華為的ModelArts平臺做了類似的影像識別之類的小專案,零編碼,但是需要自己搞資料集,標注、選擇演算法、訓練模型等,用的話直接調API, NLP方面之前的一個實習公司有用,對一些類似招股書檔案資料進行核查的,那會我作為Java開發做些資料清洗的作業,調NLP的介面去識別一些表格,然后用java寫一些邏輯,把資料的按要求分類整理上傳,- 在之后作業中沒有接觸過,也沒有學習過,但是對這方面蠻感興趣的,基本算是小白,對
NLP之類的演算法也不懂,買了一本相關的書籍,也落灰了,看到這個活動,想學習學習,所以參加了, - 依舊,附上活動鏈接:https://bbs.csdn.net/topics/601636817
活動內容:
使用Azure認知服務免費提供的AI服務(包括語音轉文本、文本轉語音、語音翻譯、文本分析、文本翻譯、語言理解)開發智能應用,并以博文形式分享使用上述服務(至少試用3項服務)教程以及自己的使用心得體驗;
我徒然學會了抗拒熱鬧,卻還來不及透悟真正的冷清,--------張大春
在使用之前我們需要先了解下相關概念
一、關于認知服務的一些基本概念和術語
什么是 Azure 認知服務?:
認知服務: 提供認知理解(看、聽、說、理解,甚至可以決策的認知)功能的服務,
認知服務主要分為四大類:
- 影像
- 語音
- 語言
- 決策
Azure 認知服務是具有 REST API和客戶端庫 SDK的基于云的服務,可用于幫助你將認知智能構建到應用程式中, 即使你沒有人工智能 (AI) 或資料科學技能,也可向應用程式添加認知功能,Azure 認知服務包含各種 AI 服務,讓你能夠構建可以看、聽、說、理解,甚至可以決策的認知解決方案,
我們要做一個可以和自己聊天的機器人,所以需要語言理解.
什么是語言理解 (LUIS)?
語言理解 (LUIS) 是一種基于云的對話式 AI 服務,可在用戶對話的自然語言文本中應用自定義機器學習智能,以便預測整體含義并提煉出相關的詳細資訊, LUIS 通過其自定義門戶、API 和 SDK 客戶端庫提供訪問權限,
二、服務構建步驟
1、首先、我們需要創建一個資源
| 使用Azure門戶創建新資源 |
![]() |
![]() |
![]() |
| 關于Azure 認知服務入門方面的教程,小伙伴可以移步官方檔案 https://docs.azure.cn/zh-cn/cognitive-services/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows |
2、服務應用構建
| 步驟 |
|---|
| 登錄到LUIS門戶 |
![]() |
| 創建新應用 |
![]() |
![]() |
| 生成模型 |
| 添加意向,即你要說些什么話,即語料,這個隨便寫點 |
![]() |
| 添加13-15個 |
![]() |
| 添加物體 |
![]() |
| 使用預構建的模型,這里我們全都選擇了,上面的2步應該就是生成這個,類似模板 |
![]() |
| 訓練模型 |
![]() |
| 發布模型 |
![]() |
| 查看資訊 |
![]() |
3、發布應用服務測驗,使用python腳本呼叫介面
┌──[root@liruilongs.github.io]-[/liruilong]
└─$ docker pull centos/python-36-centos7
┌──[root@liruilongs.github.io]-[/liruilong]
└─$ ls
input luis_run.sh output predict.py
┌──[root@liruilongs.github.io]-[/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}')
4、部署服務,啟動和運行 LUIS環境準備
| 進入資源 |
![]() |
| 按照部署要求一步步構建 |
![]() |
| 拉取鏡像 |
![]() |
| 最好找個鏡像加速器配置一下,要不太慢了 |
拉取鏡像
┌──[root@liruilongs.github.io]-[/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
┌──[root@liruilongs.github.io]-[/etc/docker]
└─$ # 嗯,這個鏡像好似做這個沒啥用,后面用的不是這個,
安裝并運行 LUIS 的 Docker 容器
| 運行步驟 |
|---|
必需的引數:所有認知服務容器都需要三個主要引數, 最終用戶許可協議 (EULA) 的值必須為 accept, 此外,終結點URL和API密鑰都是必需的, |
![]() |
┌──[root@liruilongs.github.io]-[/liruilong]
└─$ mkdir input output
┌──[root@liruilongs.github.io]-[/liruilong]
└─$ ls
input luis_run.sh output
┌──[root@liruilongs.github.io]-[/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 ##這里引數填自己的
┌──[root@liruilongs.github.io]-[/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 root@192.168.26.55:/
949d3538-07df-4149-bee8-83dc7f4e11bd_production.gz 100% 6434KB 19.0MB/s 00:00
放到input檔案夾下,啟動容器
┌──[root@liruilongs.github.io]-[/]
└─$ cp 949d3538-07df-4149-bee8-83dc7f4e11bd_production.gz /liruilong/input/
┌──[root@liruilongs.github.io]-[/]
└─$ cd /liruilong/input/
┌──[root@liruilongs.github.io]-[/liruilong/input]
└─$ ls
949d3538-07df-4149-bee8-83dc7f4e11bd_production.gz
┌──[root@liruilongs.github.io]-[/liruilong/input]
└─$ cd ..
┌──[root@liruilongs.github.io]-[/liruilong]
└─$ pwd
/liruilong
┌──[root@liruilongs.github.io]-[/liruilong]
└─$ # 啟動容器服務
┌──[root@liruilongs.github.io]-[/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測驗一下 |
![]() |
![]() |
![]() |
好吧,唐突了,想的太簡單了,以為會直接對話呢,額,原來只能類似分詞聚義的功能,原來認知是這個意思,即將你要表達的話,提取出關鍵資訊,即轉化為AI機器人能夠處理的資料,
{
"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": [
"休閑"
]
}
}
]
}
簡單嘗試一下,把生成的物體組合,作為關鍵字給一機器人用,然后識別,進行簡單對話,簡單測驗了下,發現效果很一般
額…機器人沒出寫來,唐突了,沒法治愈自己了 ^ _ ^
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/336336.html
標籤:其他
























