Skip to content

Commit d91626a

Browse files
Create 01._AI_Chat_is_now_a_Voice_Assistant.py
1 parent 5c872ed commit d91626a

File tree

1 file changed

+237
-0
lines changed

1 file changed

+237
-0
lines changed
+237
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
import speech_recognition as sr
2+
import pyttsx3
3+
import webbrowser
4+
import datetime
5+
import pywhatkit
6+
import requests
7+
8+
# Initialize the text-to-speech engine
9+
search = pyttsx3.init("sapi5")
10+
11+
print("Sk. Salahuddin - Morning 01 Batch")
12+
13+
# Set the voice to a female voice
14+
voices = search.getProperty("voices")
15+
search.setProperty("voice", voices[1].id)
16+
search.setProperty("rate", 180)
17+
18+
def takeCommand():
19+
record = sr.Recognizer()
20+
with sr.Microphone() as source:
21+
print("Listening...")
22+
audio = record.listen(source)
23+
24+
try:
25+
user_input = record.recognize_google(audio, language='en-in')
26+
print(f"user said:{user_input}\n")
27+
28+
except UnboundLocalError:
29+
speak("I didn't hear you, please say that again")
30+
return user_input.lower()
31+
32+
def speak(text):
33+
search.say(text)
34+
search.runAndWait()
35+
36+
def esshan():
37+
hour = datetime.datetime.now().hour
38+
if hour >= 0 and hour < 12:
39+
speak("Good Morning!")
40+
elif hour >= 12 and hour < 18:
41+
speak("Good Afternoon")
42+
elif hour >= 18 and hour < 19:
43+
speak("Good evening")
44+
else:
45+
speak("Good night!")
46+
47+
# Opening Welcome or you can set greetings in here:
48+
speak("Welcome, Sheikh Salahuddin")
49+
50+
def get_weather(city):
51+
# API URL with your API key
52+
url = f'https://api.openweathermap.org/data/2.5/weather?q={city}&appid=API_KEY&units=metric'
53+
54+
# Send an HTTP GET request to the API and get the response
55+
response = requests.get(url)
56+
57+
# Check if the API call was successful
58+
if response.status_code == 200:
59+
# Parse the JSON response to get the weather information
60+
weather = response.json()
61+
62+
# Extract the relevant weather information from the JSON response
63+
temperature = weather['main']['temp']
64+
humidity = weather['main']['humidity']
65+
description = weather['weather'][0]['description']
66+
67+
# Return the weather report for the city
68+
return f"The temperature in {city} is {temperature} Kelvin. The humidity is {humidity}% and the weather is {description}."
69+
else:
70+
# Return an error message if the API call was not successful
71+
return "Unable to get the weather report. Please try again later."
72+
get_weather("cityname")
73+
74+
while True:
75+
speak("How may I help you?")
76+
77+
# Get from input by user
78+
user_input = takeCommand().lower()
79+
80+
# If you want to shut down it use this three keywords : good bye / ok bye / turn off
81+
if "exit" in user_input or "good bye" in user_input or "ok bye" in user_input or "turn off" in user_input:
82+
speak('Take care, and see you later')
83+
esshan()
84+
speak('Bye!')
85+
break
86+
87+
# AI talks it everytime when user gives input
88+
speak("Please wait")
89+
90+
# Check if the user wants the weather report
91+
if 'weather' in user_input:
92+
# Ask for the city name
93+
search.say("Sure, which city?")
94+
search.runAndWait()
95+
print("City name: ")
96+
97+
# Get the city name from the user
98+
city = takeCommand().lower()
99+
100+
# Get the weather report for the city
101+
weather_report = get_weather(city)
102+
103+
# Speak the weather report
104+
speak(weather_report)
105+
106+
# Open YouTube if the user wants to go to YouTube
107+
if "youtube" in user_input:
108+
search.say("OK, What do you want to search from YouTube?")
109+
search.runAndWait()
110+
search_youtube = takeCommand().lower()
111+
112+
# Open YouTube if the user wants to go to YouTube
113+
if "open youtube" in search_youtube:
114+
search.say("Opning YouTube, Please wait for a while!")
115+
search.runAndWait()
116+
webbrowser.open("https://www.youtube.com/")
117+
118+
# Open YouTube search if the user wants search on YouTube
119+
elif "search youtube" in search_youtube:
120+
search.say("What do you want to search from YouTube?")
121+
search.runAndWait()
122+
search_youtube_items = takeCommand().lower()
123+
search.say("Opening your search items from YouTube, just wait for a while")
124+
search.runAndWait()
125+
webbrowser.open(f"https://www.youtube.com/results?search_query={search_youtube_items}")
126+
127+
# Play on YouTube if the user wants to go to play anything from YouTube
128+
elif "play" in search_youtube:
129+
search.say("What do you want to Playing on YouTube?")
130+
search.runAndWait()
131+
song = takeCommand().lower()
132+
search.say("You want to listen from YouTube " + song)
133+
search.runAndWait()
134+
pywhatkit.playonyt(song)
135+
136+
# From the user is asked to know your local time and date together
137+
elif 'time' in user_input:
138+
now = datetime.datetime.now()
139+
date = now.strftime('%Y-%m-%d')
140+
clock = now.strftime('%I:%M %p')
141+
speak(f"Today's date is {date} and the current time is {clock}")
142+
143+
#--------------------Single Date and Single Time area Start-----------------------
144+
145+
# কমেন্ট করা আছে কমেন্ট টি মুছে ব্যবহার করুন - যদি শুধু তারিখ (Date) অথবা সময় (Time) ব্যবহার করতে চান
146+
147+
# From the user is asked to know your only local time
148+
# শুধু সময় (Time)
149+
# elif 'time' in user_input:
150+
# clock = datetime.datetime.now().strftime('%I:%M %p')
151+
# speak(f"Your local time is {clock}")
152+
153+
# From the user is asked to know your only local date
154+
# শুধু তারিখ (Date)
155+
# elif 'date' in user_input:
156+
# date = datetime.datetime.now().strftime('%Y-%m-%d')
157+
# speak(f"Your local time is {date}")
158+
159+
#---------------------Single Date and Single Time area END------------------------
160+
161+
# From the user is asked if he wants to open Facebook
162+
elif "facebook" in user_input:
163+
search.say("What do you want to search from Facebook?")
164+
search.runAndWait()
165+
search_from_facebook = takeCommand().lower()
166+
167+
# Open Facebook if the user wants to go to Facebook
168+
if "open facebook" in search_from_facebook:
169+
search.say("Opning Facebook, Please wait for a while!")
170+
search.runAndWait()
171+
webbrowser.open("https://www.facebook.com/")
172+
173+
# Open Facebook profile if the user wants to go to Facebook profile
174+
elif "facebook profile" in search_from_facebook:
175+
search.say("Opening Facebook profile")
176+
search.runAndWait()
177+
webbrowser.open(f"https://www.facebook.com/profile.php?=facebook%20{search_from_facebook}")
178+
179+
# Open Facebook settings if the user wants to go to Facebook settings
180+
elif "facebook settings" in search_from_facebook:
181+
search.say("Opning Facebook settings")
182+
search.runAndWait()
183+
webbrowser.open(f"https://www.facebook.com/settings/?tab=account{search_from_facebook}")
184+
185+
# Open Facebook Reel if the user wants to go to Facebook Reel
186+
elif "facebook reel" in search_from_facebook:
187+
search.say("Opning Facebook Reel")
188+
search.runAndWait()
189+
webbrowser.open(f"https://www.facebook.com/reel/?s=ifu{search_from_facebook}")
190+
191+
# Open Facebook Messenger if the user wants to go to Facebook Messenger
192+
elif "facebook messenger" in search_from_facebook:
193+
search.say("Opning Facebook Messenger")
194+
search.runAndWait()
195+
webbrowser.open(f"https://www.facebook.com/messages/t/{search_from_facebook}")
196+
197+
# Open Facebook Video if the user wants to go to Facebook Video
198+
elif "facebook video" in search_from_facebook:
199+
search.say("Opning Facebook Video")
200+
search.runAndWait()
201+
webbrowser.open(f"https://www.facebook.com/video/")
202+
203+
# Open Facebook Notifications if the user wants to go to Facebook Notifications
204+
elif "facebook notification" in search_from_facebook:
205+
search.say("Opning Facebook Video")
206+
search.runAndWait()
207+
webbrowser.open(f"https://www.facebook.com/notifications{search_from_facebook}")
208+
209+
# Search Google Map if the user wants to search for something
210+
elif "map" in user_input:
211+
search.say("What do you want to search from Google Map?")
212+
search.runAndWait()
213+
search_from_map = takeCommand().lower()
214+
215+
# Search for Google map
216+
if "google map" in search_from_map:
217+
search.say("Opning Google Map, Please wait for a while!")
218+
search.runAndWait()
219+
webbrowser.open(f"https://www.google.com/maps/")
220+
221+
# Search for Google map for to know city location
222+
elif "city" in search_from_map:
223+
search.say("Which city: ")
224+
search.runAndWait()
225+
city_name = takeCommand().lower()
226+
search.say(f"Opening your {city_name} city, just wait for a while")
227+
search.runAndWait()
228+
webbrowser.open(f"https://www.google.com/maps/place/{city_name}")
229+
230+
# Search Google if the user wants to search for something
231+
elif "google" in user_input:
232+
search.say("What do you want to search from Google?")
233+
search.runAndWait()
234+
search_items = takeCommand().lower()
235+
search.say("Opening your search items, just wait for a while")
236+
search.runAndWait()
237+
webbrowser.open(f"https://www.google.com/search?q={search_items}")

0 commit comments

Comments
 (0)