-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (79 loc) · 3.06 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
104
105
106
107
108
import os
import json
import time
from difflib import get_close_matches
import pyttsx3
'''
<<->> hic sunt dracones <<->>
'''
# Settings
knowledgebase_folder = "Knowledgebase"
knowledgebase_file = "Iris.json"
tts = False
typewritter_effect = True
bot_prompt = "Iris: "
user_prompt = ">> "
default_notfound_error = "I don't know how to respond. Please teach me or type [SKIP]."
default_iris_thank = "Thank you! I learnt a new thing."
engine = pyttsx3.init()
engine.setProperty('rate', 150)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[2].id)
# Knowledge base directory
current_directory = os.getcwd()
file_path = os.path.abspath(os.path.join(current_directory, knowledgebase_folder, knowledgebase_file))
# Load knowledge base
def load_knowledge_base(file_path: str) -> dict:
with open(file_path, "r", encoding="utf-8") as file:
data: dict = json.load(file)
return data
# Save new response to knowledge base
def save_knowledge_base(file_path: str, data: dict):
with open(file_path, "w", encoding="utf-8") as file:
json.dump(data, file, indent=2, ensure_ascii=False)
# Find prompt that is 60% similar to input
def find_best_match(user_prompt: str, prompts: list[str]) -> str | None:
matches: list = get_close_matches(user_prompt, prompts, n=1, cutoff=0.6)
return matches[0] if matches else None
# Get the response for the prompt if found
def get_response_for_prompt(prompt: str, knowledge_base: dict) -> str | None:
for p in knowledge_base["prompts"]:
if p["prompt"] == prompt:
return p["response"]
def typewrite(prefix, text, delay=0.05):
if typewritter_effect:
print(prefix, end='', flush=True) # Print the prefix normally
for char in text:
print(char, end='', flush=True) # Print each character of the text with the typewriter effect
time.sleep(delay)
print() # Add a newline after printing the text
if tts:
engine.say(text) # Speak the entire text
engine.runAndWait() # Wait for speech to finish
else:
pass
else:
print(prefix, text)
# Main Function
def main():
knowledge_base: dict = load_knowledge_base(file_path)
while True:
user_input: str = input(user_prompt)
if user_input.lower() == "q":
break
if user_input.lower() == "cls":
os.system("cls")
best_match: str | None = find_best_match(user_input, [p["prompt"] for p in knowledge_base["prompts"]])
if best_match:
response: str = get_response_for_prompt(best_match, knowledge_base)
typewrite(bot_prompt, response)
else:
print(bot_prompt, default_notfound_error)
new_response: str = input(user_prompt)
if new_response.lower() != "skip":
knowledge_base["prompts"].append({"prompt": user_input, "response": new_response})
save_knowledge_base(file_path, knowledge_base)
typewrite(bot_prompt, default_iris_thank)
# Run Script
if __name__ == "__main__":
main()