-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdavid_virtual_assistant.py
More file actions
210 lines (156 loc) · 7.71 KB
/
david_virtual_assistant.py
File metadata and controls
210 lines (156 loc) · 7.71 KB
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
import speech_recognition as sr
import webbrowser
import win32com.client
import os
# from playsound import playsound
import datetime
from config import gemini_api_key
import google.generativeai as genai
conversation = []
# user_history = []
def chat(prompt):
global conversation
# print(f"Role : User \nContent : {prompt}")
genai.configure(api_key=gemini_api_key)
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
# "response_mime_type": "text/plain",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config,
system_instruction=f"you are david, a virtual assistant, built by 'Darshan Choudhary'. you are provided with conversation history {conversation} and your work is to go through the history and analyze it. if your input prompt is available in history then answer the input from conversation history and if the question is new to you then answer how you normally answer. answers should be to the point and short. act friendly with user, give polite replies and act similar to humans",
)
chat_session = model.start_chat(history = [])
response = chat_session.send_message(prompt)
return response.text
def gemini_ai(prompt):
genai.configure(api_key=gemini_api_key)
generation_config = {
"temperature": 1,
"top_p": 0.95,
"top_k": 64,
"max_output_tokens": 8192,
# "response_mime_type": "text/plain",
}
model = genai.GenerativeModel(
model_name="gemini-1.5-flash",
generation_config=generation_config,
system_instruction="Your name is David, an AI assistant. Act as a friendly, smart, supportive, guiding assistant to user. also remember the inputs of user. Keep your answer short, to the point. ",
)
chat = model.start_chat(history = [])
response = chat.send_message(prompt)
return (response.text)
def conversation_appender(prompt, response = ""):
conversation.append({"role" : "user" , "content" : prompt})
conversation.append({"role" : "david","content" : response})
def classifier(command):
prompt = f"classify the following statement in category of 'Command' or 'question' or 'normal' and return just that one word : {command}"
result = gemini_ai(prompt)
# print(result)
return result.lower()
def new_classifier(command):
prompt = f"go through {basic_category} and based on it classify the following statement in category of 'other' or 'normal' and return just that one word : {command}"
result = gemini_ai(prompt)
# print(result)
return result.lower()
def cleaner(code_response):
'''removes python word and other symbols from code response'''
code_response1 = code_response.replace("python","")
final_code_response = code_response1.replace("```","")
return final_code_response
def summarizer(history):
'''summarizes whole history'''
summary = gemini_ai(f"summarize the following text : {history}")
return summary
def say(text):
speaker = win32com.client.Dispatch("SAPI.SpVoice")
speaker.Speak(text)
def takeCommand():
r = sr.Recognizer()
# r.adjust_for_ambient_noise = 1
with sr.Microphone() as source:
r.pause_threshold = 0.8
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language="en-us")
return query
except Exception as e:
pass
# print(e)
# say("Some error occurred by David A I")
basic_category = {
"other" : ["open text file", "open specific app/file" ,"create a new text file with name", "find specific folder", "give path of specific file" , "play specific song, etc.."] ,
"normal" : ["remember something" , "recall it","tell me a joke"]}
if __name__ == "__main__":
print("Hello I am David A I")
say("Hello I am David A I")
sites = [["Youtube","https://www.youtube.com"],["wikipedia","https://www.wikipedia.org"],["google","https://google.com"]]
while True:
print("listening..")
try:
command = takeCommand().lower()
print(f"User : {command}")
if "quit" in command :
print("OK Thank You Sir , Have a nice day")
say("Ok Thank You Sir , Hve a nice day")
exit()
if command=="david":
continue
category = classifier(command)
if "command".lower() in category:
print("Working on it Sir.")
say("Working on it Sir.")
new_category = new_classifier(command)
# print(new_category)
if "clear conversation history".lower() in command:
conversation = []
elif "conversation history".lower() in command :
# print("here")
print(conversation)
elif "other".lower() in new_category:
# print("here")
new_prompt = f"just return the code which will fulfill the command , just return the code of following command and nothing else , mostly use os module for it,but if not suitable then you can also use webbrowser module to open sites and consider that os module is already imported: {command} consider that the device is of Windows OS"
code_response = gemini_ai(new_prompt)
final_code = cleaner(code_response)
conversation_appender(command,final_code)
# print(final_code)
exec(final_code)
else:
# print("in command")
chat_response = chat(command)
conversation_appender(command,chat_response)
print(f"Role : David\nContent : {chat_response}")
say(chat_response)
elif "question".lower() in category:
if "time" in command :
hour = int(datetime.datetime.now().strftime("%H"))
if hour>12:
hour = hour - 12
mins = int(datetime.datetime.now().strftime("%H"))
print(f"Current time is {hour} hours {mins} minutes")
say(f"Current time is {hour} hours {mins} minutes")
else:
# print("in question")
chat_response = chat(command)
conversation_appender(command,chat_response)
print(f"Role : David\nContent : {chat_response}")
say(chat_response)
elif "artificial".lower() in command :
prompt = command.split("intelligence",1)[1]
response = gemini_ai(prompt)
conversation_appender(command,chat_response)
print(response)
else:
# print("in else")
chat_response = chat(command)
conversation_appender(command,chat_response)
print(f"Role : David\nContent : {chat_response}")
say(chat_response)
except Exception as e:
# print(e)
pass