-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (76 loc) · 2.88 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
print("Loading packages")
import speech_recognition as sr
from gtts import gTTS
# import pyaudio
# from pydub import AudioSegment
# import wave
import playsound
import random
import json
import torch
from train.model import NeuralNet
from utils.preprocessing import bag_of_words
from utils.tokenization import tokenize
print("Loading modules")
import modules.modules as modules
modules.find_modules()
print("Loading model")
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
with open('data/intents.json', 'r') as f:
intents = json.load(f)
FILE = 'models/data.pth'
data = torch.load(FILE)
input_size = data['input_size']
hidden_size = data['hidden_size']
output_size = data['output_size']
all_words = data['all_words']
tags = data['tags']
model_state = data['model_state']
model = NeuralNet(input_size, hidden_size, output_size).to(device)
model.load_state_dict(model_state)
model.eval()
print("Loaded.")
listener = sr.Recognizer()
CHUNK = 1024 # For audio
bot_name = 'Phantom'
print(f'Hi I\'m {bot_name}. Let\'s chat!')
while True:
sentence = input('You: ')
if sentence == "quit":
break
sentence = tokenize(sentence)
x = bag_of_words(sentence, all_words)
x = x.reshape(1, x.shape[0])
x = torch.from_numpy(x)
output = model(x)
_, predicted = torch.max(output, dim=1)
tag = tags[predicted.item()]
probs = torch.softmax(output, dim=1)
prob = probs[0][predicted.item()]
# print(f"probability: {prob.item()}")
if prob.item() > 0.85:
for intent in intents['intents']:
if tag == intent['tag']:
response = random.choice(intent["responses"])
if "{" in response and "}" in response:
response = modules.load_module(response.strip("{").strip("}"), sentence)
print(f'{bot_name}: {response}')
tts = gTTS(response)
tts.save("out/out.mp3")
# playsound.playsound("out.mp3")
# audio = AudioSegment.from_mp3("D:\Python Documents\VoiceAssistant\out\out.mp3")
# audio.export("out/out.wav", format="wav")
# time.sleep(1)
# with wave.open("out/out.wav", 'rb') as wf:
# p = pyaudio.PyAudio()
# stream = p.open(format=p.get_format_from_width(wf.getsampwidth()),
# channels=wf.getnchannels(),
# rate=wf.getframerate(),
# output=True)
# while len(d := wf.readframes(CHUNK)):
# stream.write(d)
# stream.stop_stream()
# stream.close()
# p.terminate()
else:
print(f'{bot_name}: I do not understand...')