Azure认知服务学习:徒然学会了抗拒热闹,却还来不及透悟真正的冷清;写个聊天机器人治愈自己吧

网友投稿 574 2022-05-30

写在前面

之前上学使用华为的ModelArts平台做了类似的图像识别之类的小项目,零编码,但是需要自己搞数据集,标注、选择算法、训练模型等,用的话直接调API。

NLP方面之前的一个实习公司有用,对一些类似招股书文件数据进行核查的。那会我作为Java开发做些数据清洗的工作,调NLP的接口去识别一些表格,然后用java写一些逻辑,把数据的按要求分类整理上传。

在之后工作中没有接触过,也没有学习过,但是对这方面蛮感兴趣的。基本算是小白,对NLP之类的算法也不懂,买了一本相关的书籍,也落灰了。看到一个Azure认知服务的活动,学习学习。

博文内容主要是:使用Azure认知服务学习,涉及语言理解、文本翻译、语音转文本

我徒然学会了抗拒热闹,却还来不及透悟真正的冷清。--------张大春

在使用之前我们需要先了解下相关概念

一、关于认知服务的一些基本概念和术语

什么是 Azure 认知服务?:

认知服务

: 提供认知理解(看、听、说、理解,甚至可以决策的认知)功能的服务。

认知服务主要分为四大类:

影像

语音

语言

决策

Azure 认知服务是具有 REST API和客户端库 SDK的基于云的服务,可用于帮助你将认知智能构建到应用程序中。 即使你没有人工智能 (AI) 或数据科学技能,也可向应用程序添加认知功能。Azure 认知服务包含各种 AI 服务,让你能够构建可以看、听、说、理解,甚至可以决策的认知解决方案。

我们要做一个可以和自己聊天的机器人,需要语言理解,即告诉机器人想说什么话,表达什么意思,我们先看看这部分

二、什么是语言理解 (LUIS)?

语言理解 (LUIS) 是一种基于云的对话式 AI 服务,可在用户对话的自然语言文本中应用自定义机器学习智能,以便预测整体含义并提炼出相关的详细信息。 LUIS 通过其自定义门户、API 和 SDK 客户端库提供访问权限。找重点,会提炼出相关信息,换句话讲,就是把要表达的信息转化成机器或者说AI能够识别的信息。

1)、服务构建步骤

########### 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 = '24440ef2829c45f0**61599ee00b496a' # 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}')

这里我们用docker搞一个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 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脚本

2)、本地部署服务,启动和运行

运行 LUIS 的 Docker 容器

┌──[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=24440ef2829c45f0**61599ee00b496a ##这里参数填自己的 ┌──[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=24440ef2829c45f0a**1599ee00b496a ## 启动容器 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.

应用包上传,放到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=24440ef2829c45f0a**1599ee00b496a 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.

随便找一句测试一下:

据悉,该活动由世界休闲组织,杭州市政府主办,中国国际科技促进会三农发展工作委员会等联合会承办。

{ "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": [ "休闲" ] } } ] }

回过头看看语言理解的定义:

语言理解 (LUIS) 是一种基于云的对话式 AI 服务,可在用户对话的自然语言文本中应用自定义机器学习智能,以便预测整体含义并提炼出相关的详细信息。 LUIS 通过其自定义门户、API 和 SDK 客户端库提供访问权限。找重点,会提炼出相关信息,换句话讲,就是把要表达的信息转化成机器或者说AI能够识别的信息。

简单尝试一下,可以把生成的实体组合,作为关键字给类似图灵机器人用,进行简单对话,简单测试了下,发现效果很一般,可能方法不太对,或者我找的机器人太差了,Azure的机器人需要注册绑卡…所以这个以后有时间研究研究

下面看看其他的认知服务,语音合成产品系列,我们现在聊天大都是语音,对于聊天机器人来说,需要把语音转化为文本。然后才是语言理解。

三、什么是语音转文本?

使用语音转文本(也称为语音识别)功能,可将音频流实时听录为文本。 应用程序、工具或设备可以使用、显示和处理此文本即命令输入。

1)、服务构建:

2) JAVA开发环境准备,拉去github的Demo

这里我们使用过java的方式

Git :https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/quickstart/java/jre/from-microphone

3)调用API测试

package speechsdk.quickstart; import com.microsoft.cognitiveservices.speech.*; import com.microsoft.cognitiveservices.speech.audio.AudioConfig; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.Semaphore; import static com.microsoft.cognitiveservices.speech.ResultReason.*; /** * @Classname Program * @Description TODO * @Date 2021/10/30 13:58 * @Created LiRuilong */ public class Program { private static Semaphore stopTranslationWithFileSemaphore; public static void main(String[] args) throws InterruptedException, ExecutionException { SpeechConfig speechConfig = SpeechConfig.fromSubscription("eb48f918a90448fe***53633d6301ebb", "chinanorth2"); speechConfig.setSpeechRecognitionLanguage("zh-cn"); // 从文件中识别 // SpeechRecognitionResult result = fromFile(speechConfig); // 从麦克风识别 SpeechRecognitionResult result = fromMic(speechConfig); switch (result.getReason()) { case RecognizedSpeech: System.out.println("We recognized: " + result.getText()); break; case NoMatch: System.out.println("NOMATCH: 无法识别语音."); break; case Canceled: { CancellationDetails cancellation = CancellationDetails.fromResult(result); System.out.println("CANCELED: Reason=" + cancellation.getReason()); if (cancellation.getReason() == CancellationReason.Error) { System.out.println("CANCELED: ErrorCode=" + cancellation.getErrorCode()); System.out.println("CANCELED: ErrorDetails=" + cancellation.getErrorDetails()); System.out.println("CANCELED: Did you update the subscription info?"); } } default:{}; break; } } /** * @param speechConfig: * @return: void * @Description 从麦克风识别 * @author LiRui long * @date 2021/11/1 18:30 **/ public static SpeechRecognitionResult fromMic(SpeechConfig speechConfig) throws InterruptedException, ExecutionException { AudioConfig audioConfig = AudioConfig.fromDefaultMicrophoneInput(); SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, audioConfig); System.out.println("对这麦克风说话."); Future task = recognizer.recognizeOnceAsync(); SpeechRecognitionResult result = task.get(); return result; } /** * @param speechConfig: * @return: com.microsoft.cognitiveservices.speech.SpeechRecognitionResult * @Description 从文件中识别 * @author LiRui long * @date 2021/11/1 18:29 **/ public static SpeechRecognitionResult fromFile(SpeechConfig speechConfig) throws InterruptedException, ExecutionException { AudioConfig audioConfig = AudioConfig.fromWavFileInput("E:\docker\cognitive-services-speech-sdk\quickstart\java\jre\translate-speech-to-text\src\speechsdk\quickstart\梁静茹-我还记得.wav"); SpeechRecognizer recognizer = new SpeechRecognizer(speechConfig, audioConfig); // First initialize the semaphore. stopTranslationWithFileSemaphore = new Semaphore(0); recognizer.recognizing.addEventListener((s, e) -> { System.out.println("RECOGNIZING: Text=" + e.getResult().getText()); }); recognizer.recognized.addEventListener((s, e) -> { if (e.getResult().getReason() == RecognizedSpeech) { System.out.println("RECOGNIZED: Text=" + e.getResult().getText()); } else if (e.getResult().getReason() == NoMatch) { System.out.println("NOMATCH: Speech could not be recognized."); } }); recognizer.canceled.addEventListener((s, e) -> { System.out.println("CANCELED: Reason=" + e.getReason()); if (e.getReason() == CancellationReason.Error) { System.out.println("CANCELED: ErrorCode=" + e.getErrorCode()); System.out.println("CANCELED: ErrorDetails=" + e.getErrorDetails()); System.out.println("CANCELED: Did you update the subscription info?"); } stopTranslationWithFileSemaphore.release(); }); recognizer.sessionStopped.addEventListener((s, e) -> { System.out.println("\n Session stopped event."); stopTranslationWithFileSemaphore.release(); }); Future task = recognizer.recognizeOnceAsync(); SpeechRecognitionResult result = task.get(); return result; //System.out.println("RECOGNIZED: Text=" + result.getText()); } }

四、什么是文本翻译

文本翻译是一种基于云的机器翻译服务,是 Azure 认知服务系列 REST API 的一部分。 翻译器可用于任何操作系统

1)、服务构建:

2)Node环境调用API测试

这里我们用node环境来测试一下;

Azure认知服务学习:徒然学会了抗拒热闹,却还来不及透悟真正的冷清;写个聊天机器人治愈自己吧

$ docker pull node $ mkdir translateDemo;cd translateDemo $ vim translateDemo.js

测试js准备

const axios = require('axios').default; const { v4: uuidv4 } = require('uuid'); let subscriptionKey = "3c6588c7026b41a4**7f81551cb4a737"; let endpoint = "https://api.translator.azure.cn/"; let location = "chinanorth"; axios({ baseURL: endpoint, url: '/translate', method: 'post', headers: { 'Ocp-Apim-Subscription-Key': subscriptionKey, 'Ocp-Apim-Subscription-Region': location, 'Content-type': 'application/json', 'X-ClientTraceId': uuidv4().toString() }, params: { 'api-version': '3.0', 'from': 'zh-Hans', 'to': ['zh-Hant', 'en'] }, data: [{ 'text': '我徒然学会了抗拒热闹,却还来不及透悟真正的冷清。--------张大春' }], responseType: 'json' }).then(function(response){ console.log(JSON.stringify(response.data, null, 4)); })

这里测试将 【我徒然学会了抗拒热闹,却还来不及透悟真正的冷清。--------张大春】中文简体翻译为中文繁体,英语。

┌──[root@liruilongs.github.io]-[~/translateDemo] └─$ vim translateDemo.js ┌──[root@liruilongs.github.io]-[~/translateDemo] └─$ docker run -it --rm --name=translateDemo -v $PWD/translateDemo.js:/root/translateDemo.js node:latest /bin/bash root@35376c1c05d8:/# pwd / root@35376c1c05d8:/# cd root/;npm install axios uuid added 3 packages, and audited 4 packages in 7s 1 package is looking for funding run `npm fund` for details found 0 vulnerabilities root@35376c1c05d8:~# node translateDemo.js [ { "translations": [ { "text": "我徒然學會了抗拒熱鬧,卻還來不及透悟真正的冷清。 --------張大春", "to": "zh-Hant" }, { "text": "I learned in vain to resist the excitement, but it is too late to understand the real cold. -------- Zhang Dachun", "to": "en" } ] } ] root@35376c1c05d8:~#

时间原因,关于Azure认知服务免费提供的AI服务就尝试到这里,聊天机器人没有写出来哈,但是对Azure认知服务有一些了解,嘻,生活加油 ^ _ ^

AI平台 API

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:软件测试人员必读的经典书籍(附电子书),前阿里大佬给我推荐...
下一篇:Atlas 500 Docker镜像安装ffmpeg+OpenCV环境(atlascopco空压机)
相关文章