質問編集履歴
1
連続した会話を実現したい
test
CHANGED
File without changes
|
test
CHANGED
@@ -2,31 +2,63 @@
|
|
2
2
|
|
3
3
|
前提
|
4
4
|
|
5
|
-
docomo api
|
5
|
+
docomo apiからRepl-AIに変更し、1度返事を得ることができた。
|
6
|
-
|
7
|
-
|
6
|
+
|
7
|
+
|
8
8
|
|
9
9
|
実現したいこと
|
10
10
|
|
11
|
+
連続した会話をしたい。
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
※初心者で的を射ない質問で大変申し訳ありません。
|
16
|
+
|
11
|
-
|
17
|
+
どうしても明日中に会話をさせるAIを完成させたく質問させていただきました。
|
12
18
|
|
13
19
|
|
14
20
|
|
15
21
|
#発生している問題・エラーメッセージ
|
16
22
|
|
23
|
+
1.repl-AIからの返答を音声で話してくれない
|
24
|
+
|
25
|
+
speakの戻り値に問題があるのでしょうか?
|
26
|
+
|
27
|
+
2.テキストでの返答はあるが、連続した会話にならない
|
28
|
+
|
29
|
+
ここがよくわかりません
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
Traceback (most recent call last):
|
34
|
+
|
35
|
+
File "/home/pi/ai/ai/ai-dialoguereplret.py", line 175, in <module>
|
36
|
+
|
37
|
+
speak(reply)
|
38
|
+
|
39
|
+
File "/home/pi/ai/ai/ai-dialoguereplret.py", line 112, in speak
|
40
|
+
|
41
|
+
print(commands.getoutput(cmdline))
|
42
|
+
|
43
|
+
File "/usr/lib/python2.7/commands.py", line 50, in getoutput
|
44
|
+
|
45
|
+
return getstatusoutput(cmd)[1]
|
46
|
+
|
47
|
+
File "/usr/lib/python2.7/commands.py", line 59, in getstatusoutput
|
48
|
+
|
17
|
-
|
49
|
+
pipe = os.popen('{ ' + cmd + '; } 2>&1', 'r')
|
50
|
+
|
51
|
+
UnicodeEncodeError: 'ascii' codec can't encode characters in position 7-15: ordinal not in range(128)
|
18
52
|
|
19
53
|
|
20
54
|
|
21
55
|
#使用言語
|
22
56
|
|
23
|
-
|
57
|
+
python
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
58
|
+
|
28
|
-
|
59
|
+
|
60
|
+
|
29
|
-
|
61
|
+
```
|
30
62
|
|
31
63
|
#!/usr/bin/env python
|
32
64
|
|
@@ -56,6 +88,16 @@
|
|
56
88
|
|
57
89
|
|
58
90
|
|
91
|
+
REPLAI_API_KEY = "tIbYtdhqLp4GFArA93Np791lpiBM1f7M6pUAns61";
|
92
|
+
|
93
|
+
REPLAI_API_BOTID = "sample";
|
94
|
+
|
95
|
+
REPLAI_API_URLBASE = "https://api.repl-ai.jp/v1/";
|
96
|
+
|
97
|
+
REPLAI_API_TOPICID = "docomoapi"
|
98
|
+
|
99
|
+
|
100
|
+
|
59
101
|
# 録音
|
60
102
|
|
61
103
|
def listen(seconds):
|
@@ -132,76 +174,140 @@
|
|
132
174
|
|
133
175
|
|
134
176
|
|
135
|
-
#
|
177
|
+
# 対話
|
178
|
+
|
136
|
-
|
179
|
+
def request_replai_api(url, params):
|
180
|
+
|
181
|
+
print(url)
|
182
|
+
|
183
|
+
headers = {}
|
184
|
+
|
185
|
+
headers['content-type'] = 'application/json'
|
186
|
+
|
187
|
+
headers['x-api-key'] = REPLAI_API_KEY
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
r = requests.post(url, headers=headers, json=params)
|
192
|
+
|
193
|
+
return r.json()
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
def registration():
|
198
|
+
|
199
|
+
url = requests.compat.urljoin(REPLAI_API_URLBASE, 'registration')
|
200
|
+
|
201
|
+
params = {}
|
202
|
+
|
203
|
+
params['botId'] = REPLAI_API_BOTID
|
204
|
+
|
205
|
+
return request_replai_api(url, params)['appUserId']
|
206
|
+
|
207
|
+
|
208
|
+
|
209
|
+
# 初回の発話(initialize=True)時は tipic_id を設定する
|
210
|
+
|
211
|
+
def dialogue(app_id, message, initialize=False, topic_id=None):
|
212
|
+
|
213
|
+
url = requests.compat.urljoin(REPLAI_API_URLBASE, 'dialogue')
|
214
|
+
|
215
|
+
params = {}
|
216
|
+
|
217
|
+
params['appUserId'] = app_id
|
218
|
+
|
219
|
+
params['botId'] = REPLAI_API_BOTID
|
220
|
+
|
221
|
+
params['voiceText'] = message
|
222
|
+
|
223
|
+
if initialize and topic_id:
|
224
|
+
|
225
|
+
params['initTalkingFlag'] = True
|
226
|
+
|
227
|
+
params['initTopicId'] = topic_id
|
228
|
+
|
229
|
+
else:
|
230
|
+
|
231
|
+
params['initTalkingFlag'] = False
|
232
|
+
|
233
|
+
return request_replai_api(url, params)['systemText']['utterance']
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
# 動作確認
|
240
|
+
|
241
|
+
app_id = registration()
|
242
|
+
|
243
|
+
first_time = True
|
244
|
+
|
245
|
+
while True:
|
246
|
+
|
247
|
+
if first_time:
|
248
|
+
|
249
|
+
ret = dialogue(app_id, 'こんにちは', True, REPLAI_API_TOPICID)
|
250
|
+
|
251
|
+
print(ret)
|
252
|
+
|
253
|
+
first_time = False
|
254
|
+
|
255
|
+
else:
|
256
|
+
|
257
|
+
ret = dialogue(app_id, 'さようなら')
|
258
|
+
|
259
|
+
print(ret)
|
260
|
+
|
261
|
+
break
|
262
|
+
|
263
|
+
time.sleep(10)
|
264
|
+
|
265
|
+
|
266
|
+
|
267
|
+
# 読み上げ
|
268
|
+
|
137
|
-
def
|
269
|
+
def speak(message):
|
138
|
-
|
270
|
+
|
139
|
-
print('
|
271
|
+
print('speaking...' + message)
|
140
|
-
|
272
|
+
|
141
|
-
|
273
|
+
JDIC_DIR='/var/lib/mecab/dic/open-jtalk/naist-jdic/'
|
142
|
-
|
274
|
+
|
143
|
-
|
275
|
+
VOICE_DATA='/home/pi/ai/ai/mei/mei_happy.htsvoice'
|
144
|
-
|
145
|
-
payload = {'utt': message}
|
146
276
|
|
147
277
|
|
148
278
|
|
279
|
+
cmdline = 'echo ' + message + ' | open_jtalk -x ' + JDIC_DIR + \
|
280
|
+
|
281
|
+
' -m ' + VOICE_DATA + ' -ow ' + VOICE_OUT_PATH + \
|
282
|
+
|
283
|
+
' -s 17000 -p 100 -a 0.03'
|
284
|
+
|
285
|
+
print(commands.getoutput(cmdline))
|
286
|
+
|
287
|
+
os.system('play ' + VOICE_OUT_PATH)
|
288
|
+
|
289
|
+
|
290
|
+
|
149
|
-
|
291
|
+
def current_milli_time():
|
150
|
-
|
292
|
+
|
151
|
-
|
293
|
+
return int(round(time.time() * 1000))
|
152
|
-
|
153
|
-
|
294
|
+
|
154
|
-
|
295
|
+
|
296
|
+
|
155
|
-
|
297
|
+
if __name__ == '__main__':
|
156
|
-
|
298
|
+
|
157
|
-
r
|
299
|
+
#first time record will be failed.
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
300
|
+
|
162
|
-
|
163
|
-
|
301
|
+
listen(1)
|
164
|
-
|
302
|
+
|
165
|
-
|
303
|
+
speak('こんにちは、今日の調子はどうですか?')
|
166
|
-
|
304
|
+
|
167
|
-
|
305
|
+
no_word = 0
|
168
|
-
|
306
|
+
|
169
|
-
|
307
|
+
wifi_err = 0
|
170
308
|
|
171
309
|
|
172
310
|
|
173
|
-
cmdline = 'echo ' + message + ' | open_jtalk -x ' + JDIC_DIR + \
|
174
|
-
|
175
|
-
' -m ' + VOICE_DATA + ' -ow ' + VOICE_OUT_PATH + \
|
176
|
-
|
177
|
-
' -s 17000 -p 100 -a 0.03'
|
178
|
-
|
179
|
-
print(commands.getoutput(cmdline))
|
180
|
-
|
181
|
-
os.system('play ' + VOICE_OUT_PATH)
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
def current_milli_time():
|
186
|
-
|
187
|
-
return int(round(time.time() * 1000))
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
if __name__ == '__main__':
|
192
|
-
|
193
|
-
#first time record will be failed.
|
194
|
-
|
195
|
-
listen(1)
|
196
|
-
|
197
|
-
speak('こんにちは、今日の調子はどうですか?。')
|
198
|
-
|
199
|
-
no_word = 0
|
200
|
-
|
201
|
-
wifi_err = 0
|
202
|
-
|
203
|
-
|
204
|
-
|
205
311
|
try:
|
206
312
|
|
207
313
|
while True:
|
@@ -282,7 +388,7 @@
|
|
282
388
|
|
283
389
|
t0 = current_milli_time()
|
284
390
|
|
285
|
-
reply = dialogue(message)
|
391
|
+
reply = dialogue(app_id, message, initialize=False, topic_id=None)
|
286
392
|
|
287
393
|
print 'replied:' + str(current_milli_time() - t0) + 'ms'
|
288
394
|
|
@@ -312,100 +418,6 @@
|
|
312
418
|
|
313
419
|
pass
|
314
420
|
|
421
|
+
|
422
|
+
|
315
|
-
|
423
|
+
```
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
#今回変更したRepl Aiを用いたコードの部分
|
320
|
-
|
321
|
-
# @@対話
|
322
|
-
|
323
|
-
#ID習得
|
324
|
-
|
325
|
-
import requests
|
326
|
-
|
327
|
-
|
328
|
-
|
329
|
-
REPLAI_API_URL = "https://api.repl-ai.jp/v1/registration";
|
330
|
-
|
331
|
-
REPLAI_API_KEY = "xtPmepC69E5e7zw1ezSQX8WAFa421HcF3H0RR0E3";
|
332
|
-
|
333
|
-
REPLAI_API_BOTID = "raspberrypi";
|
334
|
-
|
335
|
-
#リクエストボディ
|
336
|
-
|
337
|
-
payload = {}
|
338
|
-
|
339
|
-
payload['botId'] = REPLAI_API_BOTID
|
340
|
-
|
341
|
-
#リクエストヘッダ
|
342
|
-
|
343
|
-
headers = {}
|
344
|
-
|
345
|
-
headers['content-type'] = 'application/json'
|
346
|
-
|
347
|
-
headers['x-api-key'] = REPLAI_API_KEY
|
348
|
-
|
349
|
-
|
350
|
-
|
351
|
-
r = requests.post(REPLAI_API_URL, headers=headers, json=payload)
|
352
|
-
|
353
|
-
if r.status_code == 200:
|
354
|
-
|
355
|
-
print(r.json())
|
356
|
-
|
357
|
-
#習得したID
|
358
|
-
|
359
|
-
appUserId = "813a9cdb-e2d6-43af-a9b0-a4ed2f8bfdf4"
|
360
|
-
|
361
|
-
#Repl-AIとの会話
|
362
|
-
|
363
|
-
def dialogue(message):
|
364
|
-
|
365
|
-
print('dialoguing...' + message)
|
366
|
-
|
367
|
-
url = 'REPLAI_API_URL'\
|
368
|
-
|
369
|
-
'REPLAI_API_KEY={}'.format(REPLAI_API_KEY)
|
370
|
-
|
371
|
-
|
372
|
-
|
373
|
-
#リクエストボディ
|
374
|
-
|
375
|
-
payload = {}
|
376
|
-
|
377
|
-
payload['utt'] = appUserIdmessage
|
378
|
-
|
379
|
-
payload['appUserId'] = appUserId
|
380
|
-
|
381
|
-
payload['botId'] = REPLAI_API_BOTID
|
382
|
-
|
383
|
-
payload['voiceText'] = "init"
|
384
|
-
|
385
|
-
payload['initTalkingFlag'] = "true"
|
386
|
-
|
387
|
-
|
388
|
-
|
389
|
-
#リクエストヘッダ
|
390
|
-
|
391
|
-
headers = {}
|
392
|
-
|
393
|
-
headers['content-type'] = 'application/json'
|
394
|
-
|
395
|
-
headers['x-api-key'] = REPLAI_API_KEY
|
396
|
-
|
397
|
-
|
398
|
-
|
399
|
-
try:
|
400
|
-
|
401
|
-
r = requests.post(url, data=json.dumps(payload))
|
402
|
-
|
403
|
-
#except:
|
404
|
-
|
405
|
-
#eturn '#ERROR'
|
406
|
-
|
407
|
-
return r.json()['utt'].encode('utf-8')
|
408
|
-
|
409
|
-
if r.status_code == 200:
|
410
|
-
|
411
|
-
print(r.json())
|