青云客的回答确实不如图灵123的精确。
但图灵机器人现在实名认证不给审核,收19.9免审核,这19.9却是按时间收费,5天就到期,到期依然每天10条限制。
测试用不起图灵,青云客起码可以正常使用。除了聊天有点答非所问,其他功能还是都可以有的。
流程 依然不变
录音交给百度处理成 文字,文字交给青云客获得文字回答,然后再交给百度转换成语音输出。
import os import time import wave import json import pyaudio import requests import speech_recognition as sr from aip import AipSpeech # 百度 APP_ID = ' ' API_KEY = ' ' SECRET_KEY = ' ' client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) # 用 SpeechRecognition 录音 def rec(rate=16000): r = sr.Recognizer() with sr.Microphone(sample_rate=rate) as source: print("[话痨版小飞] 正在等你说话...") audio = r.listen(source) with open("recording.wav", "wb") as f: f.write(audio.get_wav_data()) # 百度语音AI 语音转文字 def listen(): with open('recording.wav', 'rb') as f: audio_data = f.read() result = client.asr(audio_data, 'wav', 16000, { 'dev_pid': 1536, }) # print(result) if "result" in result.keys(): result_text = result["result"][0] else: result_text = "我沉默了" print("你说: " + result_text) return result_text # 青云客机器人问答 def robot(text): url = "http://api.qingyunke.com/api.php?key=free&appid=0&msg=" + text html = requests.get(url) result = html.json()["content"] print("小飞说:", result) return result # 百度语音AI 文字转语音 def speak(text=""): result = client.synthesis(text, 'zh', 1, { 'spd': 5, 'vol': 5, 'per': 4, }) if not isinstance(result, dict): with open('audio.mp3', 'wb') as f: f.write(result) # 转换MP3 到wav播放 因为百度返回的只能是mp3格式 多了一步转换 def play(): os.system('sox audio.mp3 audio.wav') wf = wave.open('audio.wav', 'rb') p = pyaudio.PyAudio() def callback(in_data, frame_count, time_info, status): data = wf.readframes(frame_count) return (data, pyaudio.paContinue) stream = p.open(format=p.get_format_from_width(wf.getsampwidth()), channels=wf.getnchannels(), rate=wf.getframerate(), output=True, stream_callback=callback) stream.start_stream() while stream.is_active(): time.sleep(0.1) stream.stop_stream() stream.close() wf.close() p.terminate() while True: rec() request = listen() response = robot(request) speak(response) play()