The implementation of Python to make a simple intelligent chat robot
General idea
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
Environment to be prepared
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
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()
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
{'corpus_no ':' 708788408348433929 ',' err_msg ':' success. ',' err_no ': 0,' result ': ['What's your name?'], 'sn': '255158586831650276613'}
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!")
Yo~All right, all right
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)
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
""" 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." """