今天小編就帶大家來共同學習python 2.7實現智能對話的詳細操作方法,希望可以幫助到有需要的小伙伴哦。
python 2.7實現智能對話的詳細操作方法
1.下載python運行環(huán)境,這里是使用的2.7,可網絡搜索python2.7下載安裝
2.python 代碼
from aip import AipSpeech
import urllib
import json
import requests
import mp3play
import pyaudio
import wave
import time
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
key = ''
api = 'http://www.tuling123.com/openapi/api?key=' + key + '&info='
url = "http://www.tuling123.com/openapi/api"
user_id = 'tosobright'
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getHtml2(data):
body = {'key': key, 'info': data.encode('utf8'), 'userid': user_id}
r = requests.post(url, data=body)
return r.text
while 1:
print("------Start-------")
p = pyaudio.PyAudio()
RECORD_SECONDS = 5
stream = p.open(format=8,channels=1,rate=16000,input=True,frames_per_buffer=1024)
print("* recording")
frames = []
for i in range(0, int(16000 / 1024 * RECORD_SECONDS)):
data = stream.read(1024)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open("record.pcm", 'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(8))
wf.setframerate(16000)
wf.writeframes(b''.join(frames ))
wf.close()
print('* Get Data From Baidu')
res = aipSpeech.asr(get_file_content('record.pcm'), 'pcm', 16000, {
'lan': 'zh',
})
print res
print res.get('result')[0].encode('cp936')
print('* Get Data From Tuling')
#request = api + res.get('result')[0].encode('cp936')
#print request
#response = getHtml(request)
response = getHtml2(res.get('result')[0])
dic_json = json.loads(response)
print dic_json['text']
print('* Get mp3 From Baidu')
result = aipSpeech.synthesis(dic_json['text'], 'zh', 1, {
'vol': 5,
if not isinstance(result, dict):
with open('au.mp3', 'wb') as f:
f.write(result)
print('* Play mp3')
mp3 = mp3play.load('au.mp3')
mp3.play()
time.sleep(min(30, mp3.seconds()))
mp3.close()
3.注冊百度賬號,并進入百度AI開放平臺,下載sdk,并獲取到你個人的
4.注冊靈圖賬號,并獲取靈圖key,將key值填入代碼
5.安裝python的依賴庫pip install baidu-aip
pip install requests
pip install mp3play
pip install pyaudio
6.將第2步的代碼保存成ai.py文件,并使用python IDE打開,運行即可.
以上就是python 2.7實現智能對話的詳細操作方法,希望學習后對你有所幫助。
體育競技
310.89MB
47.8MB
88.6MB
游戲輔助 | 4.5GB
2024-04-24
動作格斗 | 20GB
2024-04-23
角色扮演 | 3.2GB
2024-04-22
角色扮演 | 500MB
休閑益智 | 30.17MB
2024-04-18
生活服務 | 144.35MB
學習教育 | 24.26MB
2023-11-08
系統(tǒng)工具 | 6.78MB
體育競技 | 88.6MB
體育競技 | 47.8MB
體育競技 | 310.89MB
聯系:81537344@qq.com
CopyRight?2023 yiwangvip.com All Right Reserved 京ICP備12030808號-4
python 2.7實現智能對話的詳細操作方法
今天小編就帶大家來共同學習python 2.7實現智能對話的詳細操作方法,希望可以幫助到有需要的小伙伴哦。
python 2.7實現智能對話的詳細操作方法
1.下載python運行環(huán)境,這里是使用的2.7,可網絡搜索python2.7下載安裝
2.python 代碼
from aip import AipSpeech
import urllib
import json
import requests
import mp3play
import pyaudio
import wave
import time
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
aipSpeech = AipSpeech(APP_ID, API_KEY, SECRET_KEY)
key = ''
api = 'http://www.tuling123.com/openapi/api?key=' + key + '&info='
url = "http://www.tuling123.com/openapi/api"
user_id = 'tosobright'
def get_file_content(filePath):
with open(filePath, 'rb') as fp:
return fp.read()
def getHtml(url):
page = urllib.urlopen(url)
html = page.read()
return html
def getHtml2(data):
body = {'key': key, 'info': data.encode('utf8'), 'userid': user_id}
r = requests.post(url, data=body)
return r.text
while 1:
print("------Start-------")
p = pyaudio.PyAudio()
RECORD_SECONDS = 5
stream = p.open(format=8,channels=1,rate=16000,input=True,frames_per_buffer=1024)
print("* recording")
frames = []
for i in range(0, int(16000 / 1024 * RECORD_SECONDS)):
data = stream.read(1024)
frames.append(data)
print("* done recording")
stream.stop_stream()
stream.close()
p.terminate()
wf = wave.open("record.pcm", 'wb')
wf.setnchannels(1)
wf.setsampwidth(p.get_sample_size(8))
wf.setframerate(16000)
wf.writeframes(b''.join(frames ))
wf.close()
print('* Get Data From Baidu')
res = aipSpeech.asr(get_file_content('record.pcm'), 'pcm', 16000, {
'lan': 'zh',
})
print res
print res.get('result')[0].encode('cp936')
print('* Get Data From Tuling')
#request = api + res.get('result')[0].encode('cp936')
#print request
#response = getHtml(request)
response = getHtml2(res.get('result')[0])
dic_json = json.loads(response)
print dic_json['text']
print('* Get mp3 From Baidu')
result = aipSpeech.synthesis(dic_json['text'], 'zh', 1, {
'vol': 5,
})
if not isinstance(result, dict):
with open('au.mp3', 'wb') as f:
f.write(result)
print('* Play mp3')
mp3 = mp3play.load('au.mp3')
mp3.play()
time.sleep(min(30, mp3.seconds()))
mp3.close()
3.注冊百度賬號,并進入百度AI開放平臺,下載sdk,并獲取到你個人的
APP_ID = ''
API_KEY = ''
SECRET_KEY = ''
4.注冊靈圖賬號,并獲取靈圖key,將key值填入代碼
5.安裝python的依賴庫pip install baidu-aip
pip install requests
pip install mp3play
pip install pyaudio
6.將第2步的代碼保存成ai.py文件,并使用python IDE打開,運行即可.
以上就是python 2.7實現智能對話的詳細操作方法,希望學習后對你有所幫助。
體育競技
310.89MB
體育競技
47.8MB
體育競技
88.6MB
游戲輔助 | 4.5GB
2024-04-24
動作格斗 | 20GB
2024-04-23
角色扮演 | 3.2GB
2024-04-22
角色扮演 | 500MB
2024-04-22
休閑益智 | 30.17MB
2024-04-18
生活服務 | 144.35MB
2024-04-18
生活服務 | 144.35MB
2024-04-18
生活服務 | 144.35MB
2024-04-18
學習教育 | 24.26MB
2023-11-08
系統(tǒng)工具 | 6.78MB
2023-11-08
體育競技 | 88.6MB
體育競技 | 47.8MB
體育競技 | 310.89MB