The implementation of Python to make a simple intelligent chat robot

2022-04-20 839 Reading

Tips: This article has exceeded seven hundred and seventy-five No update in days, please note whether relevant content is still available!

In the last two days, we need to build a python applet, which is to realize the dialogue function between human and intelligent robot (intelligent dialogue interface). At present, we just tested it and it can be realized, It is to realize the intelligent dialogue (voice communication) between individuals and machines.

General idea

You can imagine that if you want to realize the intelligent dialogue between people and machines, there must be the following steps:

  • The computer receives the user's voice input

  • Convert the voice input entered by the user into text information

  • Call the intelligent dialogue interface, send the request text information, and obtain the intelligent answer text information returned by the interface

  • Convert the response text information into voice format output

Here you can install many ready-made library functions to assist our system implementation.

Environment to be prepared

The following are some python dependency packages that need to be installed

  • Pip install pyaudio to install the pyaudio dependency package, which is used for recording and generating wav files

  • Pip install baidu aip Install Baidu AI's sdk, call the voice technology interface to recognize audio as text data and return

  • Pip install pyttsx3 Install the pyttsx3 dependency package and play the text information in audio format

Next, I will gradually implement each of the above functions and finally combine them.

Receive the user's voice input and save it as an audio file

 import time import wave from pyaudio import PyAudio, paInt16 Frame=16000 # sampling rate Num_samples=2000 # sampling point Channels=1 # channel Sampwidth=2 # sample width 2bytes FILEPATH = '../ Voice/myvoice. wav '# The file directory must exist #It is used to receive voice input from users and generate wav audio files (the difference between wav, pcm and mp3 can be detailed in Baidu) class Speak(): #Save audio data to wav file     def save_wave_file(self, filepath, data):         wf = wave.open(filepath, 'wb')         wf.setnchannels(channels)         wf.setsampwidth(sampwidth)         wf.setframerate(framerate)         wf.writeframes(b''.join(data))         wf.close() #Perform voice recording     def my_record(self):         pa = PyAudio() #Open a new audio stream         stream = pa.open(format=paInt16, channels=channels,                          rate=framerate, input=True, frames_per_buffer=num_samples) My_buf=[] # Store recording data         t = time.time() Print ('Speaking... ') While time. time()<t+5: # Set recording time (seconds) #Loop read, each time read 2000frames             string_audio_data = stream.read(num_samples)             my_buf.append(string_audio_data) Print ('End of speech ') Self. save_wave_file (FILEPATH, my_buf) # Save the recording data         stream.close()

Call Baidu AI interface to identify audio files and return them as text information

I have used Baidu AI's interface several times before, and my graduation project "Abnormal Behavior and Analysis of Online Classroom Students" also uses Baidu's intelligent platform, There are many free products for personal debugging. In general, Baidu has done quite well in the field of artificial intelligence.

Before calling Baidu AI interface, you need to first enter Baidu AI open platform to search for voice recognition

Click Use Now. If you don't have an account, you can first create an account and then get free resources to use

I have created one before. Suppose I click Create again

The system will automatically check the voice recognition interface and directly create an application. Then there will be AppID API Key、Secret Key, Then call Baidu interface directly,

You can view the interface document and perform specific interface operations

When the prelude is ready, you can directly call the interface for speech recognition

 from aip import AipSpeech "" "Your APPID AK SK" " APP_ID = '25990397' API_KEY = 'iS91n0uEOujkMIlsOTLxiVOc' SECRET_KEY='' # Fill in your own key here "" "Call interface, call BaiDu AI interface for recording and speech recognition" "" client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) class ReadWav(): #Read file     def get_file_content(self, filePath):         with open(filePath, 'rb') as fp:             return fp.read()     def predict(self): #Call Baidu AI interface to identify local files          return client.asr(self.get_file_content('../voices/myvoices.wav'), 'wav', 16000, {             'dev_pid': 1537,         })                            ReadWav=ReadWav() # Instance method Print (readWav. predict()) # Call the identification method and output

Execution result (the recording of the audio file is: What's your name?)

 {'corpus_no ':' 708788408348433929 ',' err_msg ':' success. ',' err_no ': 0,' result ': ['What's your name?'], 'sn': '255158586831650276613'}

Request intelligent robot, send text message and return intelligent chat content

Previously, our teacher recommended me to use Turing robot for intelligent chat, but later found that the authentication has been unable to pass and requires payment

Then refer to the reply of Zhihu Big Brother

Found a free Qingyun guest intelligent robot that can chat only by sending a get request without registering, and directly called the interface

The codes are as follows:

 def talkWithRobot(msg):     url = ' http://api.qingyunke.com/api.php?key=free&appid=0&msg= {}'.format(urllib.parse.quote(msg))     html = requests.get(url)     return html.json()["content"] Print (talkWithRobot ("Hello!")

output

 Yo~All right, all right

Convert the answer information into a voice file and output it

Here you need to import the pyttsx3 package. The specific code is as follows

 import pyttsx3 class RobotSay():     def __init__(self): #Initialize voice Self. engine=pyttsx3. init() # Initialize the voice library #Set speech speed         self.rate = self.engine.getProperty('rate')         self.engine.setProperty('rate', self.rate - 50)     def say(self, msg): #Output voice Self. engine. say (msg) # Synthetic speech         self.engine.runAndWait() robotSay = RobotSay() RobotSay. say ("Hello") # will say~Hello (female voice)

Combine to become an automatic chat robot (it is very tough)

The code is as follows

 def talkWithRobot(msg):     url = ' http://api.qingyunke.com/api.php?key=free&appid=0&msg= {}'.format(urllib.parse.quote(msg))     html = requests.get(url)     return html.json()["content"] robotSay = RobotSay() speak = Speak() readTalk = ReadWav() while True: Speak. my_record() # Recording Text=readTalk.predict() ['result '] [0] # Call Baidu AI interface to convert recording into text information Print ("I said:", text) # Output text information Response_dialogue=talkWithRobot (text) # Call the Qingyun guest robot to answer the text message and return Print ("Qingyun guest said:", response_dialogue) # Output answer text information RobotSay. say (response_dialogue) # Play the answer information

Operation results (found it hard)

 """ Speaking End of speech I said: Hello. Qingyun guest said, "Oh, everything is fine." Speaking End of speech I said: What's your name? Qingyun guest said: I'm Feifei, a little beauty~ Speaking End of speech I said: Wow, how beautiful you are. The green cloud guest said: You seem to have many problems! Speaking End of speech I said: Do I look good? The Qingyun guest said: You are a real beauty. That means you are a beauty only in the tunnel, because there is no light in the tunnel Speaking End of speech I said: You are really a little cute. The Qingyun guest said: Ah, how do you know... I am.. Speaking End of speech I said: I won't tell you. Qingyun guest said: "Don't talk down." """

follow-up

Now it is a simple combination of functions. Later, a GUI interface will be created to add more functions and share them with you!