forked from Ai-Austin/GPT4ALL-Voice-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (78 loc) · 3.22 KB
/
main.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
from os import system
import speech_recognition as sr
from gpt4all import GPT4All
import sys
import whisper
import warnings
import time
import os
wake_word = 'jarvis'
model_complete_filepath='/Users/YOUR_USERNAME_HERE/Library/Application Support/nomic.ai/GPT4All/ggml-model-gpt4all-falcon-q4_0.gguf'
model_path_directory, model_filename_complete = os.path.split(model_complete_filepath)
model_filename, model_extension = os.path.splitext(model_filename_complete)
device_for_running_LLM=input("Which device would you like to use for running the LLM?\nPlease type your selection without the quotes and press ENTER.\nSelections available: \"gpu\", \"cpu\", \"intel\", and \"amd\"\n\nYou've selected: ")
model = GPT4All(model_filename, model_path=model_path_directory, allow_download=False, device=device_for_running_LLM)
r = sr.Recognizer()
tiny_model_path = os.path.expanduser('~/.cache/whisper/tiny.pt')
base_model_path = os.path.expanduser('~/.cache/whisper/base.pt')
tiny_model = whisper.load_model(tiny_model_path)
base_model = whisper.load_model(base_model_path)
listening_for_wake_word = True
source = sr.Microphone()
warnings.filterwarnings("ignore", category=UserWarning, module='whisper.transcribe', lineno=114)
if sys.platform != 'darwin':
import pyttsx3
engine = pyttsx3.init()
def speak(text):
if sys.platform == 'darwin':
ALLOWED_CHARS = set('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,?!-_$:+-/ ')
clean_text = ''.join(c for c in text if c in ALLOWED_CHARS)
system(f"say '{clean_text}'")
else:
engine.say(text)
engine.runAndWait()
def listen_for_wake_word(audio):
global listening_for_wake_word
with open('wake_detect.wav', 'wb') as f:
f.write(audio.get_wav_data())
result = tiny_model.transcribe('wake_detect.wav')
text_input = result['text']
if wake_word in text_input.lower().strip():
print('Wake word detected. Please speak your prompt to GPT4All.')
speak('Listening')
listening_for_wake_word = False
def prompt_gpt(audio):
global listening_for_wake_word
try:
with open('prompt.wav', 'wb') as f:
f.write(audio.get_wav_data())
result = base_model.transcribe('prompt.wav')
prompt_text = result['text']
if len(prompt_text.strip()) == 0:
print('Empty prompt. Please speak again.')
speak('Empty prompt. Please speak again.')
listening_for_wake_word = True
else:
print('User: ' + prompt_text)
output = model.generate(prompt_text, max_tokens=200)
print('GPT4All: ', output)
speak(output)
print('\nSay', wake_word, 'to wake me up. \n')
listening_for_wake_word = True
except Exception as e:
print('Prompt error: ', e)
def callback(recognizer, audio):
global listening_for_wake_word
if listening_for_wake_word:
listen_for_wake_word(audio)
else:
prompt_gpt(audio)
def start_listening():
with source as s:
r.adjust_for_ambient_noise(s, duration=2)
print('\nSay', wake_word, 'to wake me up. \n')
r.listen_in_background(source, callback)
while True:
time.sleep(1)
if __name__ == '__main__':
start_listening()