-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathtest_rest_server.py
executable file
·273 lines (245 loc) · 10.4 KB
/
test_rest_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# OS : GNU/Linux Ubuntu 16.04 or 18.04
# LANGUAGE : Python 3.5.2 or later
# AUTHOR : Klim V. O.
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
'''
Тестирование rest_server.py путём отправки на него всех поддерживаемых запросов.
'''
import time
import logging
import base64
import signal
import os
import sys
import curses
import subprocess
import http.client as http_client
import requests
from pydub import AudioSegment
def train():
print('[i] Обучение сети пока не поддерживается сервером.')
def predict(speech_recognition=False, speech_synthesis=False):
''' Работа с обученной моделью seq2seq.
1. speech_recognition - включение распознавания речи с микрофона с помощью PocketSphinx
2. speech_synthesis - включение озвучивания ответов с помощью RHVoice '''
# Что бы записать звук с микрофона в терминале: arecord output.wav (для остановки надо нажать Ctrl+C)
# Что бы проиграть .wav в терминале: aplay output.wav
#name_dataset = f_name_w2v_model_plays[f_name_w2v_model_plays.rfind('w2v_model_')+len('w2v_model_'):f_name_w2v_model_plays.rfind('.bin')]
#ttt = TextToText(f_name_w2v_model=f_name_w2v_model, f_name_model=f_name_model, f_name_model_weights=f_name_model_weights)
if speech_recognition:
print('[i] Загрузка языковой модели для распознавания речи...')
#stt = SpeechToText('from_microphone', name_dataset)
if speech_synthesis:
print('[i] Загрузка синтезатора речи...')
#tts = TextToSpeech('playback')
print()
question = ''
while(True):
if speech_recognition:
print('Слушаю... (для остановки нажмите Ctrl+C)')
command_line = "arecord voice.wav"
subprocess.call(command_line, shell=True)
os.write(sys.stdout.fileno(), curses.tigetstr('cuu1'))
print('Пользователь: ' + quest)
else:
question = input('Вы: ')
'''
answer, lost_words = ttt.predict(question, True)
print('\t=> %s' % answer)
if len(lost_words) > 0:
print('[w] Потерянные слова: ' + ', '.join(lost_words) + '\n')
else:
print()
if speech_synthesis:
tts.get(answer)
'''
def get_address_on_local_network():
''' Определение адреса машины в локальной сети с помощью утилиты 'ifconfig' из пакета net-tools.
1. возвращает строку с адресом или 127.0.0.1, если локальный адрес начинается не с 192.Х.Х.Х или 172.Х.Х.Х
Проверено в Ubuntu 16.04 и 18.04. '''
command_line = 'ifconfig'
proc = subprocess.Popen(command_line, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
out = out.decode()
if out.find('not found') != -1:
print("\n[E] 'ifconfig' не найден.")
sys.exit(0)
if out.find('inet 127.0.0.1') != -1:
template = 'inet '
elif out.find('inet addr:127.0.0.1') != -1:
template = 'inet addr:'
i = 0
host_192xxx = None
host_172xxx = None
while host_192xxx is None or host_172xxx is None:
out = out[out.find(template) + len(template):]
host = out[:out.find(' ')]
out = out[out.find(' '):]
if host.find('192.168') != -1:
host_192xxx = host
elif host.find('172.') != -1:
host_172xxx = host
i += 1
if i >= 10:
break
if host_192xxx:
return host_192xxx
elif host_172xxx:
return host_172xxx
else:
print('\n[E] Неподдерживаемый формат локального адреса, требуется корректировка исходного кода.\n')
return '127.0.0.1'
def main():
'''
http_client.HTTPConnection.debuglevel = 1
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger('requests.packages.urllib3')
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True
'''
auth = base64.b64encode('bot:test_bot'.encode())
headers = {'Authorization' : 'Basic ' + auth.decode()}
http = 'http://'
addr = get_address_on_local_network() + ':5000'
'''
curses.setupterm()
quest = ''
print('Записываю... (для остановки нажмите Enter)')
command_line = "temp/arecord voice.wav"
proc = subprocess.Popen(command_line, stdout=sys.stdout, shell=True, preexec_fn=os.setsid)
quest = input()
os.killpg(os.getpgid(proc.pid), signal.SIGINT)
os.write(sys.stdout.fileno(), curses.tigetstr('cuu1'))
time.sleep(0.01)
os.write(sys.stdout.fileno(), curses.tigetstr('cuu1'))
print(' ')
os.write(sys.stdout.fileno(), curses.tigetstr('cuu1'))
print('Распознано: ' + quest)
'''
print('GET-запрос на /chatbot/about')
result = requests.get(http + addr + '/chatbot/about', headers=headers, verify=False)
data = result.json()
data = data.get('text')
print('Результат:')
print(data + '\n')
print(result.headers)
print(result.text)
print('GET-запрос на /chatbot/questions')
result = requests.get(http + addr + '/chatbot/questions', headers=headers, verify=False)
data = result.json()
data = data.get('text')
print('Результат (первые 5 элементов):')
print(str(data[:5]) + '\n')
print(result.headers)
print(result.text[:1000] + '\n')
'''
print('POST-запрос на /chatbot/speech-to-text')
print('Чтение temp/speech.opus...')
data = None
with open('temp/speech.opus', 'rb') as audio:
data = audio.read()
data = base64.b64encode(data)
data = {'opus' : data.decode()}
result = requests.post(http + addr + '/chatbot/speech-to-text', headers=headers, json=data, verify=False)
data = result.json()
data = data.get('text')
print('Результат:')
print(data + '\n')
print(result.headers)
print(result.text)
'''
print('POST-запрос на /chatbot/speech-to-text')
print('Чтение temp/test1.wav...')
with open('temp/test1.wav', 'rb') as audio:
data = audio.read()
data = base64.b64encode(data)
data = {'wav' : data.decode()}
result = requests.post(http + addr + '/chatbot/speech-to-text', headers=headers, json=data, verify=False)
data = result.json()
data = data.get('text')
print('Результат:')
print(data + '\n')
print(result.headers)
print(result.text)
print('POST-запрос на /chatbot/text-to-speech')
print('Отправлено: как тебя зовут?')
data = {'text':'как тебя зовут?'}
result = requests.post(http + addr + '/chatbot/text-to-speech', headers=headers, json=data, verify=False)
data = result.json()
data = base64.b64decode(data.get('wav'))
print('Результат в temp/synthesized_speech.wav\n')
with open('temp/synthesized_speech.wav', 'wb') as audio:
audio.write(data)
print(result.headers)
print(result.text[:1000] + '\n')
'''
print('POST-запрос на /chatbot/speech-to-text')
print('Чтение temp/synthesized_speech.wav...')
with open('temp/synthesized_speech.wav', 'rb') as audio:
data = audio.read()
data = base64.b64encode(data)
data = {'wav' : data.decode()}
result = requests.post(http + addr + '/chatbot/speech-to-text', headers=headers, json=data, verify=False)
data = result.json()
data = data.get('text')
print('Результат:')
print(data + '\n')
print(result.headers)
print(result.text + '\n')
'''
print('POST-запрос на /chatbot/text-to-text')
data = {'text':'как тебя зовут?'}
result = requests.post(http + addr + '/chatbot/text-to-text', headers=headers, json=data, verify=False)
data = result.json()
data = data.get('text')
print('Результат:')
print(data + '\n')
print(result.headers)
print(result.text + '\n')
while True:
question = input('Введите вопрос боту: ')
#question = 'мне нужна помощь'
start_time = time.time()
data = {'text':question}
result = requests.post(http + addr + '/chatbot/text-to-text', headers=headers, json=data, verify=False)
data = result.json()
answer = data.get('text')
print('Ответ: ' + answer)
print('Время: ' + str(time.time() - start_time) + ' сек' + '\n')
#time.sleep(0.2)
'''
curses.setupterm()
print('[i] Выберите вариант удалённой работы с ботом:')
print('\t1. train - обучение модели seq2seq')
print('\t2. predict - работа с обученной моделью seq2seq')
print('\t3. predict -ss - включено озвучивание ответов с помощью RHVoice')
print('\t4. predict -sr - включено распознавание речи с помощью PocketSphinx')
print('\t5. predict -ss -sr - включено озвучивание ответов и распознавание речи')
while True:
choice = input('Введите цифру: ')
if choice == '1':
choice = input('Вы уверены?(д/н) ')
if choice == 'д':
train()
break
elif choice == '2':
predict()
break
elif choice == '3':
predict(False, True)
break
elif choice == '4':
predict(True, False)
break
elif choice == '5':
predict(True, True)
break
else:
os.write(sys.stdout.fileno(), curses.tigetstr('cuu1'))
'''
if __name__ == '__main__':
main()