-
Notifications
You must be signed in to change notification settings - Fork 4
/
ai_launch_no_PVC.py
627 lines (507 loc) · 20.1 KB
/
ai_launch_no_PVC.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
# -*- coding: utf-8 -*-
import asyncio
import re
from urllib3 import encode_multipart_formdata
import vosk
import json
import os
import queue
import random
import struct
import subprocess
import time
from ctypes import POINTER, cast
import re
import pvporcupine
import simpleaudio as sa
import vosk
import yaml
from comtypes import CLSCTX_ALL
from fuzzywuzzy import fuzz
from pvrecorder import PvRecorder
from pycaw.pycaw import (
AudioUtilities,
IAudioEndpointVolume
)
from rich import print
from working_tts import working_tts
from pydub import playback
import speech_recognition as sr
from EdgeGPT import Chatbot #ConversationStyle
import datetime
from num2words import num2words
import configparser
import click
import sqlite3
from multiprocessing import Process, Pipe
import threading
#! внизу мои модули
from multiprocess_bing import working_chat
from multiprocess_wake_word_recognition_no_PVC import main123
from working_getter_from_db import working_getter_from_db
from google_rec_no_PVC import google_rec
from working_numbers_to_words import numbers_to_wards
from bard_chat_multiprocess import bard_msg
from modules.gpt4free.gpt_3_ask import gpt_3_ask
#! fixed
import sys
sys.path.insert(0, f'{os.getcwd()}\modules\gpt4free')
# play(f'{CDIR}\\sound\\ok{random.choice([1, 2, 3, 4])}.wav')
async def play(phrase, wait_done=True):
global recorder
#recorder.stop()
try:recorder.stop()
except: pass
filename = f"{CDIR}\\sound\\"
if phrase == "greet": # for py 3.8
filename += f"greet{random.choice([1, 2, 3])}.wav"
elif phrase == "ok":
filename += f"ok{random.choice([1, 2, 3])}.wav"
elif phrase == "not_found":
filename += 'not_found.wav'
elif phrase == "thanks":
filename += 'thanks.wav'
elif phrase == "run":
if 5 <= hour < 12:
# Ваш код для утренней проверки здесь
filename += f"run{random.choice([1, 2, 3])}.wav"
elif 12 <= hour < 19:
# Ваш код для дневной проверки здесь
filename += f"run{random.choice([1, 2, 4])}.wav"
else:
# Ваш код для вечерней/ночной проверки здесь
filename += f"run{random.choice([1, 2, 5])}.wav"
#?filename += f"run{random.choice([1, 2, 3])}.wav"
elif phrase == "stupid":
filename += 'stupid.wav'
elif phrase == "reload":
filename += f'reload{random.choice([1, 2, 3])}.wav'
elif phrase == "off":
filename += 'off.wav'
elif phrase == 'internet':
filename += f'internet{random.choice([1, 2, 3])}.wav'
elif phrase == 'off_internet':
filename += f'off_internet{random.choice([1, 2, 3])}.wav'
elif phrase == 'reload':
filename += f'reload{random.choice([1, 2, 3])}.wav'
wave_obj = sa.WaveObject.from_wave_file(filename)
play_obj = wave_obj.play()
if wait_done:
play_obj.wait_done()
# time.sleep((len(wave_obj.audio_data) / wave_obj.sample_rate) + 0.5)
# print("END")
# time.sleep(0.5)
#recorder.start()
async def custum_command(voice):
if 'скажи' in voice:return False
data = f"{working_getter_from_db(text=voice)}"
if data != 'None':
await play('ok')
click.launch(data)
return True
else: return False
async def execute_cmd(cmd: str, voice: str):
#recorder.stop()
if cmd == 'sound_off':
await play("ok", True)
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume.SetMute(1, None)
elif cmd == 'sound_on':
devices = AudioUtilities.GetSpeakers()
interface = devices.Activate(IAudioEndpointVolume._iid_, CLSCTX_ALL, None)
volume = cast(interface, POINTER(IAudioEndpointVolume))
volume.SetMute(0, None)
await play("ok")
elif cmd == 'thanks':
await play("thanks")
elif cmd == 'stupid':
await play("stupid")
elif cmd == 'offf':
global ltc
await play("off", True)
#recorder.stop()
print('заморожен на 30 секунд')
time.sleep(30)
#recorder.start()
async def listen_for_cancel():
global canceled
#recorder.stop()
recognizer = sr.Recognizer()
microphone = sr.Microphone()
with microphone as source:
recognizer.adjust_for_ambient_noise(source)
print('Скажите "отмена" для отмены поиска...')
while not canceled:
#recorder.stop()
with microphone as source:
audio = recognizer.listen(source)
await asyncio.sleep(4)
try:
text = recognizer.recognize_google(audio, language='ru-RU')
#print(f"Recognized text: {text}")
if "отмена" in text.lower():
canceled = True
print("Поиск отменен.")
await play('internet_off')
return
except sr.UnknownValueError:
pass
except sr.RequestError as e:
print(f"Request error: {e}")
async def vosk_listen_for_cancel():
global canceled
#recorder.stop()
import vosk
import pyaudio
import json
MICROPHONE_INDEX = -1 # Индекс микрофона, -1 чтобы выбрать по умолчанию
BUFFER_SIZE = 2048 # Количество байт в буфере записи
SAMPLE_RATE = 16000 # Частота дискретизации
# Загружаем модель и создаем распознаватель
model = vosk.Model("model_small")
rec = vosk.KaldiRecognizer(model, SAMPLE_RATE)
# Создаем объект pyaudio для записи аудио
p = pyaudio.PyAudio()
# Открываем поток для чтения аудио из микрофона
stream = p.open(format=pyaudio.paInt16, channels=1, rate=SAMPLE_RATE, input=True,
frames_per_buffer=BUFFER_SIZE, input_device_index=MICROPHONE_INDEX)
# Цикл записи и распознавания аудио
while canceled == False:
# Считываем аудиоданные из потока
data = stream.read(BUFFER_SIZE)
if len(data) == 0:
break
# Передаем данные в распознаватель
if rec.AcceptWaveform(data):
result = json.loads(rec.Result())["text"]
print(result)
print('можно сказать ОТМЕНА')
await asyncio.sleep(4)
if 'отмен' in result:
canceled = True
print("Cancel keyword detected. Cancelling all tasks.")
bot = Chatbot(cookie_path='cookies.json')
await bot.close()
await play('off_internet')
# Останавливаем поток и освобождаем ресурсы
stream.stop_stream()
stream.close()
p.terminate()
return
def split_string(s):
return [s[i:i+1000] for i in range(0, len(s), 1000)]
#!! bard
async def bard_answer(text:str,conn):
text = f'{text}, {config.get("add_to_prompt","add_to_prompt")}'
global recorder
await play('internet')
conn.send(text)
canceled = False
while not canceled:
while True:
if conn.poll():
response = conn.recv()
print('\nОтвет:',response)
working_tts(response)
canceled = True
return True
else:
await asyncio.sleep(5)
#recorder.stop()
async def gpt_answer(text: str,conn,bug=None):
global dd
global d
try:
global recorder, ltc
recorder.stop()
except:pass
global canceled
global list_of_text
text = f'{text}, {config.get("add_to_prompt","add_to_prompt")}'
if not bug: await play('internet')
parent_conn, child_conn = Pipe()
p1 = Process(target=main123, args=(child_conn,'отмена'))
p1.start()
def check_for_cancel():
#recorder.stop()
global canceled
if parent_conn.poll():
canceled = parent_conn.recv()
return True
return canceled
while not canceled:
print('Jarvis зашел в интернет')
# Select only the bot response from the response dictionary
#отправляем запрос в working
canceled = check_for_cancel()
if canceled == True:break
conn.send(text)
while True:
canceled = check_for_cancel()
if canceled == True:break
if conn.poll():
canceled = check_for_cancel()
if canceled == True:break
response = conn.recv()
response = f'{response[0]}'
print('response- ',response)
#?
# for message in response["item"]["messages"]:
# if message["author"] == "bot":
# bot_response = message["text"]
#?
# Remove [^#^] citations in response
bot_response = response
bot_response = re.sub('\[\^\d+\^\]', '', bot_response)
bot_response = bot_response.replace('привет, это Bing',' вот что я нашел в интернете ')
bot_response = bot_response.replace('Здравствуйте, это Bing','вот что я нашел ')
bot_response = bot_response.replace('Здравствуйте',' ')
bot_response = bot_response.replace('Привет',' ')
bot_response = bot_response.replace('Привет,',' ')
bot_response = bot_response.replace('это Bing',' ')
pogoda = ['погода','градус', "погоду","градусов","градус","погод"]
for word in pogoda:
if word in text:
bot_response = bot_response.replace('+','плюс ')
bot_response = bot_response.replace('-','минус ')
bot_response = bot_response.replace('°',' ')
#!!
if len(bot_response) < 10:
await gpt_answer(text=text,conn=conn,bug=True)
p1.terminate()
return
#!!
len_of_texts=len(list_of_text)
dd = dd+1
d.update({dd: [text,bot_response]})
canceled = check_for_cancel()
if canceled == True:break
try:
Speech_it = d[len_of_texts][1]
if Speech_it:
result = split_string(d[len_of_texts][1])
for i in result:
working_tts(i)
canceled = True
p1.terminate()
else:
print('что то пошло не так...')
canceled = True
p1.terminate()
except:pass
await asyncio.sleep(5)
continue
async def recognize_cmd(cmd: str):
"""
берет значения(не ключи) из yaml и
фильтрует через fuzz.raio.
фильтрует их на совпадение с запросом,
если хоть что то совпадает больше чем на 0%
то ключи этого значения
вставляет в кастомный
словарь, с командой с
наимбольшим процентом и с значением
процента совпадения
например на запрос -
"открыть браузер" будет:
{'cmd': 'open_browser', 'percent': 100}
"""
rc = {'cmd': '', 'percent': 0}
for c, v in VA_CMD_LIST.items():
for x in v:
vrt = fuzz.ratio(cmd, x)
if vrt > rc['percent']:
rc['cmd'] = c
rc['percent'] = vrt
return rc
async def filter_cmd(raw_voice: str):
"""
удаляет слова из запроса:
'джарвис', 'скажи', 'покажи', 'ответь', 'произнеси', 'расскажи', 'сколько', 'слушай'
"""
cmd = raw_voice
for x in VA_ALIAS:
cmd = cmd.replace(x, "").strip()
for x in VA_TBR:
cmd = cmd.replace(x, "").strip()
return cmd
async def va_respond(voice: str,conn):
global message_log
global first_request
global dd
global choose_ai_model
print(f"Распознано: {voice}")
voice = str(voice)
if await custum_command(voice):
#recorder.start()
return True
cmd = await recognize_cmd(await filter_cmd(voice))
print(cmd)
#если тишина
if len(cmd['cmd'].strip()) <= 0:
#recorder.start()
return False
elif cmd['percent'] < 70 or cmd['cmd'] not in VA_CMD_LIST.keys():
if fuzz.ratio(voice.join(voice.split()[:1]).strip(), "скажи") > 75:
if first_request:
message_log.append({"role": "user", "content": voice})
first_request = False
global canceled
global list_of_text
# Создаем и запускаем поток для функции gpt_answer
canceled = False
# создаем счетчик для алгоритма - корректного озвучивания
list_of_text.append(voice)
# получаем ответ от ai_model
if choose_ai_model == 'bing':
await gpt_answer(voice,conn)
else:
await bard_answer(voice,conn)
await play('reload')
return False
else:
await play("not_found")
return False
else:
if not await execute_cmd(cmd['cmd'], voice):
return False
else:
return True
async def main(conn):
global ltc
global canceled
global list_of_text
global CDIR,VA_ALIAS,VA_CMD, recognizer,VA_NAME
global VA_VER,VA_TBR , VA_CMD_LIST,icrophone_index, model, samplerate, device, kaldi_rec, q, CHROME_PATH, message_log, first_request, dd
global hour, porcupine
CDIR = os.getcwd()
VA_CMD_LIST = yaml.safe_load(
open('commands.yaml', 'rt', encoding='utf8'),)
# Create a recognizer object and wake word variables
recognizer = sr.Recognizer()
# Конфигурация
VA_NAME = 'Jarvis'
VA_VER = "3.0"
VA_ALIAS = ('джарвис',)
VA_TBR = ('скажи', 'покажи', 'ответь', 'произнеси', 'расскажи', 'сколько', 'слушай')
# PORCUPINE
# Токен Picovoice
global config
config = configparser.ConfigParser()
config.read('config.ini',encoding='utf-8')
# bard / bing
global choose_ai_model
choose_ai_model = config.get('ai','model') # bard / bing
CHROME_PATH = r'C:\Program Files (x86)\Google\Chrome\Application'
# VOSK
MICROPHONE_INDEX = int(config.get('MIC','microphone_index'))
model = vosk.Model("model_small")
samplerate = 16000
device = MICROPHONE_INDEX
kaldi_rec = vosk.KaldiRecognizer(model, samplerate)
q = queue.Queue()
recorder = PvRecorder(device_index=MICROPHONE_INDEX, frame_length=512)
# ChatGPT vars
message_log = [
{"role": "system", "content": "Ты голосовой ассистент из железного человека."}
]
#body
now = datetime.datetime.now()
hour = now.hour
global canceled
canceled = False
global list_of_text
list_of_text = []
global d
global dd
d = {}
dd = 0
#! #recorder.start()
time.sleep(0.5)
ltc = time.time() - 1000
first_request = True
print('Произнесите команду Джарвис')
await play('run')
counter = 0
def set_to_false():
global wake_worder,counter
while True:
wake_worder = False
time.sleep(30)
counter = 0
t = threading.Thread(target=set_to_false)
t.start()
wake_worder = False
while True:
try:
try:recorder.stop()
except:pass
# pcm = #recorder.read()
# keyword_index = porcupine.process(pcm)
# wake_worder = False
if wake_worder == True:
pass
else:wake_worder = await google_rec(['джарвис','жарвис','джирвис','жирвис', 'жир вес','Jeans'])#! experemental
if wake_worder == True:
#recorder.stop()
if counter == 0:
await play('greet',True)
print("Здравствуйте.")
counter = 1
#recorder.start() # prevent self recording
ltc = time.time()
#! while True делает бесконечный цикл и он не спит
#! while time.time() - ltc <= 10: дефолт
while time.time() - ltc <= 30:
recorder.start()
pcm = recorder.read()
sp = struct.pack("h" * len(pcm), *pcm)
if kaldi_rec.AcceptWaveform(sp):
recorder.stop()
if await va_respond(json.loads(kaldi_rec.Result())["text"],conn):
ltc = time.time()
break
except Exception as err:
print(f"Unexpected {err=}, {type(err)=}")
raise
def bing_starter(conn):
asyncio.run(working_chat(conn))
def main_starter(conn):
asyncio.run(main(conn))
def bard_starter(conn):
asyncio.run(bard_msg(conn))
if __name__ == "__main__":
# чтение config.ini
global config
config = configparser.ConfigParser()
config.read('config.ini')
# bard / bing
global choose_ai_model
choose_ai_model = config.get('ai','model') # bard / bing
# если пользователь выбрал bing, то запускается bing_starter, иначе запускается bard_starter
if choose_ai_model == 'bing':
parent_conn, child_conn = Pipe()
p1 = Process(target=main_starter, args=(parent_conn,))
p2 = Process(target=bing_starter, args=(child_conn,))
p1.start()
p2.start()
p1.join()
p2.join()
elif choose_ai_model == 'gpt3':
parent_conn, child_conn = Pipe()
p1 = Process(target=main_starter, args=(parent_conn,))
p2 = Process(target=gpt_3_ask,args=(child_conn,))
p1.start()
p2.start()
p1.join()
p2.join()
else:
parent_conn,child_conn = Pipe()
p1 = Process(target=main_starter, args=(parent_conn,))
p2 = Process(target=bard_starter, args=(child_conn,))
p2.start()
p1.start()
p2.join()
p1.join()