In vain learned to resist the excitement, but it was too late to understand the real coldness; Write a chat robot to heal yourself!

Write in front

  • Previously, I used Huawei's ModelArts platform to do similar small projects such as image recognition, zero coding, but I need to do my own data sets, labeling, selection algorithms, training models, etc. if you use it, you can directly tune the API.
  • A former internship company in NLP is useful to check the data of some similar Prospectus Documents. I will do some data cleaning as a java developer, call the NLP interface to identify some tables, then write some logic in Java, sort and upload the data according to the requirements.
  • I haven't contacted or studied in my later work, but I'm very interested in this aspect. I'm basically Xiaobai. I don't understand algorithms such as NLP. I bought a related book and got gray. Seeing this activity, I wanted to learn, so I participated.
  • I don't understand anything, but the overall feeling is more cumbersome than Huawei's ModelArts. I don't know why I don't understand.
  • Still, attach the activity link: https://bbs.csdn.net/topics/601636817

Activity content: develop intelligent applications using AI services (including voice to text, text to voice, voice translation, text analysis, text translation and language understanding) provided free of charge by Azure cognitive services, and share tutorials on using the above services (try at least three services) and their own experience in the form of Blog Posts;

I learned to resist the excitement in vain, but I didn't have time to understand the real coldness-------- Da Chun Zhang

Before using, we need to understand the relevant concepts

1, Some basic concepts and terms of cognitive service

What is Azure cognitive service?:

Cognitive services: services that provide cognitive understanding (seeing, listening, speaking, understanding, and even decision-making).

Cognitive services are mainly divided into four categories:

  • image
  • voice
  • language
  • policy decision

Azure cognitive service is a cloud based service with REST API and client library SDK, which can be used to help you build cognitive intelligence into applications. Even if you don't have artificial intelligence (AI) or data science skills, you can add cognitive functions to your applications. Azure cognitive services include various AI services, enabling you to build cognitive solutions that can see, listen, speak, understand, and even make decisions.

We want to be a robot that can chat with ourselves, so we need language understanding

What is language understanding (LUIS)?

Language understanding (LUIS) is a cloud based conversational AI service, which can apply custom machine learning intelligence in the natural language text of user dialogue, so as to predict the overall meaning and extract relevant detailed information. LUIS provides access through its custom portal, API, and SDK client libraries.

2, Service building steps

1. First, we need to create a resource

Create a new resource using Azure portal
For the tutorial on getting started with Azure cognitive services, small partners can move to the official documents https://docs.azure.cn/zh-cn/cognitive-services/cognitive-services-apis-create-account?tabs=multiservice%2Cwindows

2. Service application construction

step
Log in to the LUIS portal
Create new app
Generation model
Add intention, that is, what you want to say, that is, the corpus, and write something casually
Add 13-15
Add entity
We have all selected the pre built model. The above two steps should be to generate this, similar to the template
Training model
Publishing model
see information

Publish the application service test and call the interface with python script

┌──[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': 'How does one live?', 'prediction': {'topIntent': 'Life refueling', 'intents': {'Life refueling': {'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},........

Test success: predict.py script

I feel that there are too few corpora, so basically speaking, the ox head is not right and the horse tail needs a lot of training

########### Python 3.6 #############

#
# This quickstart shows how to predict the intent of an utterance by using the LUIS REST APIs.
# Import module
import requests

try:
    ##########
    # Values to modify.
    # YOUR-APP-ID: The App ID GUID found on the www.luis.ai Application Settings page.
    # Replace with your own 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.
    # What you want to say to him
    utterance = 'How does one live?'
    ##########
    # 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}')

3. Deploy services, start and run LUIS environment preparation

Access resources
Build step by step according to the deployment requirements
Pull image
You'd better find a mirror accelerator to configure it, or it's too slow

Pull image

┌──[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]
└─$ # Well, this image seems to be useless. It's not used in the back.

Install and run the Docker container for LUIS

Running steps
Required parameters: all cognitive service containers require three main parameters. The value of the end user license agreement (EULA) must be accept. In addition, both the endpoint URL and the API key are required.
┌──[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 ## fill in your own parameters here
┌──[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 = 24440ef2829c45f0a061569ee00b496a ## start container


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.

test
View interface API

Well, it's too late. The rest will be written tomorrow

Tags: Python AI

Posted on Sun, 24 Oct 2021 16:38:29 -0400 by makeITfunctional