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

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

时间:2022-12-12 17:10:03浏览次数:73  
标签:透悟 liruilong github 浅尝 println io Azure root


写在前面


  • 之前上学使用​​华为​​​的​​ModelArts​​​平台做了类似的​​图像识别​​之类的小项目,零编码,但是需要自己搞数据集,标注、选择算法、训练模型等,用的话直接调API。
  • ​NLP​​方面之前的一个实习公司有用,对一些类似招股书文件数据进行核查的。那会我作为Java开发做些数据清洗的工作,调NLP的接口去识别一些表格,然后用java写一些逻辑,把数据的按要求分类整理上传。
  • 在之后工作中没有接触过,也没有学习过,但是对这方面蛮感兴趣的。基本算是小白,对​​NLP​​之类的算法也不懂,买了一本相关的书籍,也落灰了。看到这个活动,想学习学习,所以参加了。
  • 依旧,

活动内容:​​使用Azure认知服务免费提供的AI服务​​(包括语音转文本、文本转语音、语音翻译、文本分析、文本翻译、语言理解)开发智能应用,并以博文形式分享使用上述服务(至少试用3项服务)教程以及自己的使用心得体验;

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


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

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

什么是 Azure 认知服务?:

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

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

  • 影像
  • 语音
  • 语言
  • 决策

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

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

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

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

1)、服务构建步骤

1、创建一个资源

使用Azure门户创建新资源


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



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



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


关于Azure 认知服务入门方面的教程,小伙伴可以移步官方文档​https://docs.azure.cn/zh-cn/cognitive-services/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows​

2、服务应用构建

步骤

登录到LUIS门户


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


创建新应用


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



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


生成模型

添加意向,即你要说些什么话,即语料,这个随便写点


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


添加13-15个


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


添加实体


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


使用预构建的模型,这里我们全都选择了,上面的2步应该就是生成这个,类似模板


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


训练模型


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


发布模型


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


查看信息


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


3、发布应用服务测试,使用python脚本调用接口

########### 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环境,然后执行一下脚本,测试一下

┌──[[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脚本

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

1、 LUIS环境准备

进入资源

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

按照部署要求一步步构建

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

拉取镜像

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

最好找个镜像加速器配置一下,要不太慢了

​2、安装并运行 LUIS 的 Docker 容器​

运行步骤

必需的参数:所有认知服务容器都需要三个主要参数。 ​​最终用户许可协议 (EULA) 的值必须为 accept​​​。 此外,终结点​​URL​​​和​​API​​密钥都是必需的。

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

运行 LUIS 的 Docker 容器

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

测试

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

查看接口API

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

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

应用包上传,放到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=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.

文件名不对,需要改一下

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

用swagger测试一下

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

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

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

随便找一句测试一下:

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

{
"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)、服务构建:

1、创建一个资源

创建一个资源


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



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


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

这里我们使用过java的方式

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

步骤

克隆代码


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



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


导入项目

3)调用API测试

调用API测试

语音文件转文本

找了一首 梁静茹-我还记得 歌曲测试下,发现不行,可能还没这么智能,或者歌词太长了

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

麦克风转文本:​大城小爱​中的一句歌词识别:是妈妈告诉我的哲理 ===》妈妈告诉我,我的心里。应该是歌曲的原因,正常说话可以还是不错的,这里调硬件的代码直接封装好了,特别方便

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

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

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: Reasnotallow=" + 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<SpeechRecognitionResult> 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: Reasnotallow=" + 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<SpeechRecognitionResult> task = recognizer.recognizeOnceAsync();
SpeechRecognitionResult result = task.get();
return result;
//System.out.println("RECOGNIZED: Text=" + result.getText());
}



}

​四、什么是文本翻译​

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

1)、服务构建:

创建资源

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

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

2)Node环境调用API测试

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

$ 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));
})

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

┌──[[email protected]]-[~/translateDemo]
└─$ vim translateDemo.js
┌──[[email protected]]-[~/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认知服务有一些了解,嘻,生活加油 ^ _ ^

标签:透悟,liruilong,github,浅尝,println,io,Azure,root
From: https://blog.51cto.com/u_13474506/5931147

相关文章