forked from adarshsharma-18/OPENAI-Javis-VoiceAssisstant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
108 lines (91 loc) · 3.19 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 speech_recognition as sr
import webbrowser
import openai
import os
import datetime
import random
import pyttsx3
from config import apikey
chatStr = ""
def chat(query):
global chatStr
print(chatStr)
openai.api_key = apikey
chatStr += f"Adarsh: {query}\n Jarvis: "
response = openai.Completion.create(
model="text-davinci-003",
prompt=chatStr,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
chatStr += f"{response['choices'][0]['text']}\n"
return response["choices"][0]["text"]
def ai(prompt):
openai.api_key = apikey
text = f"OpenAI response for Prompt: {prompt} \n *************************\n\n"
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.7,
max_tokens=256,
top_p=1,
frequency_penalty=0,
presence_penalty=0
)
text += response["choices"][0]["text"]
if not os.path.exists("Openai"):
os.mkdir("Openai")
with open(f"Openai/{''.join(prompt.split('intelligence')[1:]).strip()}.txt", "w") as f:
f.write(text)
def takeCommand():
r = sr.Recognizer()
with sr.Microphone() as source:
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio, language="en-in")
print(f"User said: {query}")
return query
except Exception as e:
return "Some Error Occurred. Sorry from Jarvis"
if __name__ == '__main__':
print('Welcome to Jarvis A.I')
engine = pyttsx3.init()
engine.setProperty('rate', 150) # Speed of speech
engine.setProperty('volume', 1) # Volume (0.0 to 1.0)
engine.say("Welcome to Jarvis A.I")
engine.runAndWait()
while True:
print("Listening...")
query = takeCommand()
sites = [["youtube", "https://www.youtube.com"], ["wikipedia", "https://www.wikipedia.com"], ["google", "https://www.google.com"]]
for site in sites:
if f"Open {site[0]}".lower() in query.lower():
engine.say(f"Opening {site[0]} sir...")
webbrowser.open(site[1])
if "open music" in query:
musicPath = "/Users/harry/Downloads/downfall-21371.mp3"
os.system(f"open {musicPath}")
elif "the time" in query:
hour = datetime.datetime.now().strftime("%H")
min = datetime.datetime.now().strftime("%M")
engine.say(f"Sir, the time is {hour} bajke {min} minutes")
engine.runAndWait()
elif "open facetime".lower() in query.lower():
os.system(f"open /System/Applications/FaceTime.app")
elif "open pass".lower() in query.lower():
os.system(f"open /Applications/Passky.app")
elif "Using artificial intelligence".lower() in query.lower():
ai(prompt=query)
elif "Jarvis Quit".lower() in query.lower():
exit()
elif "reset chat".lower() in query.lower():
chatStr = ""
else:
print("Chatting...")
response = chat(query)
engine.say(response)
engine.runAndWait()