-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
226 lines (176 loc) · 5.98 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
import pyttsx3, datetime, pywhatkit, pyjokes, pyautogui, pygame, os, random, requests, openai
from newsapi import NewsApiClient
from nltk.sentiment import SentimentIntensityAnalyzer
import speech_recognition as sr
import webbrowser as wb
from config import apikey
def ai(prompt):
text = ""
openai.api_key = apikey
response = openai.completions.create(
model="gpt-3.5-turbo-instruct",
prompt=prompt,
temperature=1,
max_tokens=100,
top_p=1,
frequency_penalty=0,
presence_penalty=0,
)
# return response.choices[0].text
text += response.choices[0].text
if not os.path.exists("Openai"):
os.mkdir("Openai")
with open(f"Openai/{prompt}.txt", "w") as f:
f.write(text)
return text
def say(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def greet():
hr = datetime.datetime.now().hour
if hr >= 6 and hr < 12:
say("Good Morning Sir!")
elif hr >= 12 and hr < 17:
say("Good Afternoon Sir!")
elif hr >= 17 and hr < 24:
say("Good Evening Sir!")
else:
say("Good Night Sir!")
def take_ss():
timestamp = datetime.datetime.now().strftime("%Y-%m-$d_%H-%M-%S")
ss = pyautogui.screenshot()
ss.save("Screenshot_" + timestamp + ".png")
say("Taken a screenshot")
pygame.mixer.init()
def get_music_files(directory):
music_files = [
f for f in os.listdir(directory) if f.endswith((".mp3", ".wav", ".ogg"))
]
return music_files
def play_random_music():
music_folder = os.path.join(os.environ["HOMEPATH"], "Music")
if os.path.exists(music_folder):
music_files = get_music_files(music_folder)
if music_files:
chosen_music = os.path.join(music_folder, random.choice(music_files))
pygame.mixer.music.load(chosen_music)
say("Playing: " + chosen_music)
pygame.mixer.music.play()
else:
say("No files found!")
else:
say("No correct folder found!")
music_playing = False
def toggle_music():
global music_playing
if music_playing:
pygame.mixer.music.stop()
say("Music stopped!")
else:
play_random_music()
music_playing = not music_playing
def get_news(api_key):
newsapi = NewsApiClient(api_key=api_key)
headlines = newsapi.get_top_headlines(language="en", country="us")
if headlines["status"] == "ok":
articles = headlines["articles"]
return articles
else:
return None
def read_news():
api_key = (
"31d075d55aa94617b63a765673f68268" # Replace with your actual News API key
)
articles = get_news(api_key)
if articles:
say("Here are the latest news headlines:")
for i, article in enumerate(articles, start=1):
title = article["title"]
say(f"News {i}: {title}")
# Check for the stop command after each news item
query = take_cmd().lower()
if "stop" in query or "offline" in query:
say("Stopping the news reading.")
return
else:
say("Sorry, I couldn't fetch the latest news at the moment.")
def get_sentiment(text):
sid = SentimentIntensityAnalyzer()
# Get a compound sentiment score between -1 (negative) and 1 (positive)
sentiment_score = sid.polarity_scores(text)["compound"]
if sentiment_score >= 0.05:
return "positive"
elif sentiment_score <= -0.05:
return "negative"
else:
return "neutral"
def take_cmd():
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"Ayush said: {query}")
return query
except Exception as e:
return "Some Error"
if __name__ == "__main__":
say("Hello sir! I am Jarvis.")
greet()
while True:
query = take_cmd().lower()
if "time" in query:
hr = datetime.datetime.now().strftime("%H")
min = datetime.datetime.now().strftime("%M")
say(f"Sir the current time is {hr} and {min} minutes.")
elif "remember" in query:
say("What should i remember?")
data = take_cmd()
say("You told me to remember that " + data)
remember = open("data.txt", "a")
remember.write(data + "\n")
remember.close()
elif "forgot" in query:
remember = open("data.txt", "r")
say(
"In case you forgot sir here's what you told me to remember: "
+ remember.read()
)
elif "youtube" in query:
say("Search for what?")
search = take_cmd()
pywhatkit.playonyt(search)
elif "search" in query:
say("What should I search for?")
search = take_cmd()
wb.open("https://www.google.com/search?q=" + search)
elif "instagram" in query:
wb.open("www.instagram.com")
elif "camera" in query:
camera_url = "microsoft.windows.camera:"
wb.open(camera_url)
elif "joke" in query:
joke = pyjokes.get_joke()
print(joke)
say(joke)
elif "screenshot" in query:
take_ss()
elif "music" in query:
toggle_music()
elif "read news" in query:
read_news()
elif "how" in query:
mood = get_sentiment(query)
if mood == "positive":
say("I'm doing well, thank you!")
elif mood == "negative":
say("I'm sorry to hear that. Is there anything I can do to help?")
else:
say("I'm doing fine, thanks for asking!")
elif "bye" in query:
say("Thank you! Turning off...")
quit()
elif "using artificial intelligence" in query:
say(ai(prompt=query))