-
Notifications
You must be signed in to change notification settings - Fork 104
/
01._AI_Chat_is_now_a_Voice_Assistant.py
237 lines (193 loc) · 9.21 KB
/
01._AI_Chat_is_now_a_Voice_Assistant.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
import speech_recognition as sr
import pyttsx3
import webbrowser
import datetime
import pywhatkit
import requests
# Initialize the text-to-speech engine
search = pyttsx3.init("sapi5")
print("Sk. Salahuddin - Morning 01 Batch")
# Set the voice to a female voice
voices = search.getProperty("voices")
search.setProperty("voice", voices[1].id)
search.setProperty("rate", 180)
def takeCommand():
record = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
audio = record.listen(source)
try:
user_input = record.recognize_google(audio, language='en-in')
print(f"user said:{user_input}\n")
except UnboundLocalError:
speak("I didn't hear you, please say that again")
return user_input.lower()
def speak(text):
search.say(text)
search.runAndWait()
def esshan():
hour = datetime.datetime.now().hour
if hour >= 0 and hour < 12:
speak("Good Morning!")
elif hour >= 12 and hour < 18:
speak("Good Afternoon")
elif hour >= 18 and hour < 19:
speak("Good evening")
else:
speak("Good night!")
# Opening Welcome or you can set greetings in here:
speak("Welcome, Sheikh Salahuddin")
def get_weather(city):
# API URL with your API key
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid=API_KEY&units=metric'
# Send an HTTP GET request to the API and get the response
response = requests.get(url)
# Check if the API call was successful
if response.status_code == 200:
# Parse the JSON response to get the weather information
weather = response.json()
# Extract the relevant weather information from the JSON response
temperature = weather['main']['temp']
humidity = weather['main']['humidity']
description = weather['weather'][0]['description']
# Return the weather report for the city
return f"The temperature in {city} is {temperature} Kelvin. The humidity is {humidity}% and the weather is {description}."
else:
# Return an error message if the API call was not successful
return "Unable to get the weather report. Please try again later."
get_weather("cityname")
while True:
speak("How may I help you?")
# Get from input by user
user_input = takeCommand().lower()
# If you want to shut down it use this three keywords : good bye / ok bye / turn off
if "exit" in user_input or "good bye" in user_input or "ok bye" in user_input or "turn off" in user_input:
speak('Take care, and see you later')
esshan()
speak('Bye!')
break
# AI talks it everytime when user gives input
speak("Please wait")
# Check if the user wants the weather report
if 'weather' in user_input:
# Ask for the city name
search.say("Sure, which city?")
search.runAndWait()
print("City name: ")
# Get the city name from the user
city = takeCommand().lower()
# Get the weather report for the city
weather_report = get_weather(city)
# Speak the weather report
speak(weather_report)
# Open YouTube if the user wants to go to YouTube
if "youtube" in user_input:
search.say("OK, What do you want to search from YouTube?")
search.runAndWait()
search_youtube = takeCommand().lower()
# Open YouTube if the user wants to go to YouTube
if "open youtube" in search_youtube:
search.say("Opning YouTube, Please wait for a while!")
search.runAndWait()
webbrowser.open("https://www.youtube.com/")
# Open YouTube search if the user wants search on YouTube
elif "search youtube" in search_youtube:
search.say("What do you want to search from YouTube?")
search.runAndWait()
search_youtube_items = takeCommand().lower()
search.say("Opening your search items from YouTube, just wait for a while")
search.runAndWait()
webbrowser.open(f"https://www.youtube.com/results?search_query={search_youtube_items}")
# Play on YouTube if the user wants to go to play anything from YouTube
elif "play" in search_youtube:
search.say("What do you want to Playing on YouTube?")
search.runAndWait()
song = takeCommand().lower()
search.say("You want to listen from YouTube " + song)
search.runAndWait()
pywhatkit.playonyt(song)
# From the user is asked to know your local time and date together
elif 'time' in user_input:
now = datetime.datetime.now()
date = now.strftime('%Y-%m-%d')
clock = now.strftime('%I:%M %p')
speak(f"Today's date is {date} and the current time is {clock}")
#--------------------Single Date and Single Time area Start-----------------------
# কমেন্ট করা আছে কমেন্ট টি মুছে ব্যবহার করুন - যদি শুধু তারিখ (Date) অথবা সময় (Time) ব্যবহার করতে চান
# From the user is asked to know your only local time
# শুধু সময় (Time)
# elif 'time' in user_input:
# clock = datetime.datetime.now().strftime('%I:%M %p')
# speak(f"Your local time is {clock}")
# From the user is asked to know your only local date
# শুধু তারিখ (Date)
# elif 'date' in user_input:
# date = datetime.datetime.now().strftime('%Y-%m-%d')
# speak(f"Your local time is {date}")
#---------------------Single Date and Single Time area END------------------------
# From the user is asked if he wants to open Facebook
elif "facebook" in user_input:
search.say("What do you want to search from Facebook?")
search.runAndWait()
search_from_facebook = takeCommand().lower()
# Open Facebook if the user wants to go to Facebook
if "open facebook" in search_from_facebook:
search.say("Opning Facebook, Please wait for a while!")
search.runAndWait()
webbrowser.open("https://www.facebook.com/")
# Open Facebook profile if the user wants to go to Facebook profile
elif "facebook profile" in search_from_facebook:
search.say("Opening Facebook profile")
search.runAndWait()
webbrowser.open(f"https://www.facebook.com/profile.php?=facebook%20{search_from_facebook}")
# Open Facebook settings if the user wants to go to Facebook settings
elif "facebook settings" in search_from_facebook:
search.say("Opning Facebook settings")
search.runAndWait()
webbrowser.open(f"https://www.facebook.com/settings/?tab=account{search_from_facebook}")
# Open Facebook Reel if the user wants to go to Facebook Reel
elif "facebook reel" in search_from_facebook:
search.say("Opning Facebook Reel")
search.runAndWait()
webbrowser.open(f"https://www.facebook.com/reel/?s=ifu{search_from_facebook}")
# Open Facebook Messenger if the user wants to go to Facebook Messenger
elif "facebook messenger" in search_from_facebook:
search.say("Opning Facebook Messenger")
search.runAndWait()
webbrowser.open(f"https://www.facebook.com/messages/t/{search_from_facebook}")
# Open Facebook Video if the user wants to go to Facebook Video
elif "facebook video" in search_from_facebook:
search.say("Opning Facebook Video")
search.runAndWait()
webbrowser.open(f"https://www.facebook.com/video/")
# Open Facebook Notifications if the user wants to go to Facebook Notifications
elif "facebook notification" in search_from_facebook:
search.say("Opning Facebook Video")
search.runAndWait()
webbrowser.open(f"https://www.facebook.com/notifications{search_from_facebook}")
# Search Google Map if the user wants to search for something
elif "map" in user_input:
search.say("What do you want to search from Google Map?")
search.runAndWait()
search_from_map = takeCommand().lower()
# Search for Google map
if "google map" in search_from_map:
search.say("Opning Google Map, Please wait for a while!")
search.runAndWait()
webbrowser.open(f"https://www.google.com/maps/")
# Search for Google map for to know city location
elif "city" in search_from_map:
search.say("Which city: ")
search.runAndWait()
city_name = takeCommand().lower()
search.say(f"Opening your {city_name} city, just wait for a while")
search.runAndWait()
webbrowser.open(f"https://www.google.com/maps/place/{city_name}")
# Search Google if the user wants to search for something
elif "google" in user_input:
search.say("What do you want to search from Google?")
search.runAndWait()
search_items = takeCommand().lower()
search.say("Opening your search items, just wait for a while")
search.runAndWait()
webbrowser.open(f"https://www.google.com/search?q={search_items}")