-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
240 lines (190 loc) · 7.34 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import requests
from functions.online_ops import *
import pyttsx3
import speech_recognition as sr
from decouple import config
from datetime import datetime
from functions.os_ops import *
from random import choice
from utils import opening_text
from pprint import pprint
import openai
# ENV
USERNAME = config('USER')
BOTNAME = config('BOTNAME')
OPEINAI_API_KEY = config('OPENAI_API_KEY')
OPENAI_ORG = config('OPENAI_ORG')
engine = pyttsx3.init('nsss')
# Set Rate
engine.setProperty('rate', 190)
# Set Volume
engine.setProperty('volume', 1.0)
# Set Voice (Female)
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[10].id)
# Text to Speech Conversion
def speak(text):
"""Used to speak whatever text is passed to it"""
engine.say(text)
engine.runAndWait()
# Greet the user
def greet_user():
"""Greets the user according to the time"""
hour = datetime.now().hour
if (hour >= 6) and (hour < 12):
speak(f"Good Morning {USERNAME}")
elif (hour >= 12) and (hour < 16):
speak(f"Good afternoon {USERNAME}")
elif (hour >= 16) and (hour < 19):
speak(f"Good Evening {USERNAME}")
speak(f"I am {BOTNAME}. How may I assist you?")
# Takes Input from User
def take_user_input():
"""Takes user input, recognizes it using Speech Recognition module and converts it into text"""
r = sr.Recognizer()
with sr.Microphone() as source:
print('Listening....')
r.pause_threshold = 1
audio = r.listen(source)
try:
print('Recognizing...')
query = r.recognize_google(audio, language='fr-fr')
if not 'exit' in query or 'stop' in query:
speak(choice(opening_text))
else:
hour = datetime.now().hour
if hour >= 21 and hour < 6:
speak("Good night sir, take care!")
else:
speak('Have a good day sir!')
exit()
except Exception:
speak('Sorry, I could not understand. Could you please say that again?')
query = 'None'
return query
# Chat GPT
openai.organization = OPENAI_ORG
openai.api_key = OPEINAI_API_KEY
start_sequence = "\nAI:"
restart_sequence = "\nHuman: "
def gpt_ouput(prompt):
response = openai.Completion.create(
model="text-davinci-003",
prompt=prompt,
temperature=0.9,
max_tokens=150,
top_p=1,
frequency_penalty=0,
presence_penalty=0.6,
stop=[" Human:", " AI:"]
)
data = response.choices[0].text
print(data)
speak(data)
# Main function
if __name__ == '__main__':
greet_user()
while True:
query = take_user_input().lower()
if 'open visual studio code' in query:
open_vscode()
elif 'open finder' in query:
open_finder()
elif 'open discord' in query:
open_discord()
elif 'open command' in query or 'open terminal' in query:
open_cmd()
elif 'open camera' in query:
open_camera()
elif 'open calculator' in query:
open_calculator()
elif 'open safari' in query:
open_safari()
elif 'open brave' in query:
open_brave()
elif 'open chrome' in query:
open_chrome()
elif 'open contact' in query:
open_contacts()
elif 'open calendar' in query:
open_calendar()
elif 'open notes' in query:
open_notes()
elif 'open messages' in query:
open_messages()
elif 'open plans' in query:
open_plans()
elif 'open photos' in query:
open_photos()
elif 'ip address' in query:
ip_address = find_my_ip()
speak(f'Your IP Address is {ip_address}.\n For your convenience, I am printing it on the screen sir.')
print(f'Your IP Address is {ip_address}')
elif 'wikipedia' in query:
speak('What do you want to search on Wikipedia, sir?')
search_query = take_user_input().lower()
results = search_on_wikipedia(search_query)
speak(f"According to Wikipedia, {results}")
speak("For your convenience, I am printing it on the screen sir.")
print(results)
elif 'youtube' in query:
speak('What do you want to play on Youtube, sir?')
video = take_user_input().lower()
play_on_youtube(video)
elif 'google' in query:
speak('What do you want to search on Google, sir?')
query = take_user_input().lower()
search_on_google(query)
elif "send whatsapp message" in query:
speak(
'On what number should I send the message sir? Please enter in the console: ')
number = input("Enter the number: ")
speak("What is the message sir?")
message = take_user_input().lower()
send_whatsapp_message(number, message)
speak("I've sent the message sir.")
elif "send an email" in query:
speak("On what email address do I send sir? Please enter in the console: ")
receiver_address = input("Enter email address: ")
speak("What should be the subject sir?")
subject = take_user_input().capitalize()
speak("What is the message sir?")
message = take_user_input().capitalize()
if send_email(receiver_address, subject, message):
speak("I've sent the email sir.")
else:
speak("Something went wrong while I was sending the mail. Please check the error logs sir.")
elif 'joke' in query:
speak(f"Hope you like this one sir")
joke = get_random_joke()
speak(joke)
speak("For your convenience, I am printing it on the screen sir.")
pprint(joke)
elif "advice" in query:
speak(f"Here's an advice for you, sir")
advice = get_random_advice()
speak(advice)
speak("For your convenience, I am printing it on the screen sir.")
pprint(advice)
elif "trending movies" in query:
speak(f"Some of the trending movies are: {get_trending_movies()}")
speak("For your convenience, I am printing it on the screen sir.")
print(*get_trending_movies(), sep='\n')
elif 'news' in query:
speak(f"I'm reading out the latest news headlines, sir")
speak(get_latest_news())
speak("For your convenience, I am printing it on the screen sir.")
print(*get_latest_news(), sep='\n')
elif 'weather' in query:
ip_address = find_my_ip()
city = requests.get(f"https://ipapi.co/{ip_address}/city/").text
speak(f"Getting weather report for your city {city}")
weather, temperature, feels_like = get_weather_report(city)
speak(f"The current temperature is {temperature}, but it feels like {feels_like}")
speak(f"Also, the weather report talks about {weather}")
speak("For your convenience, I am printing it on the screen sir.")
print(f"Description: {weather}\nTemperature: {temperature}\nFeels like: {feels_like}")
elif 'stop' in query:
quit()
else:
gpt_ouput(query)