Skip to content

Commit 780d0db

Browse files
committed
This my first voice assistance made from python
0 parents  commit 780d0db

File tree

7 files changed

+122
-0
lines changed

7 files changed

+122
-0
lines changed

.idea/.gitignore

+3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/inspectionProfiles/profiles_settings.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/misc.xml

+4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/modules.xml

+8
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/python voice assistance.iml

+10
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/vcs.xml

+6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

main.py

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import pyttsx3
2+
import speech_recognition as sr
3+
import pywhatkit
4+
import datetime
5+
import wikipedia
6+
import pyjokes
7+
8+
# Initialize the pyttsx3 engine
9+
engine = pyttsx3.init()
10+
11+
def speak(text):
12+
"""
13+
Function to convert text to speech
14+
"""
15+
engine.say(text)
16+
engine.runAndWait()
17+
18+
def listen():
19+
"""
20+
Function to listen for user speech input
21+
"""
22+
recognizer = sr.Recognizer()
23+
24+
with sr.Microphone() as source:
25+
print("Listening...")
26+
recognizer.pause_threshold = 1
27+
audio = recognizer.listen(source)
28+
29+
try:
30+
print("Recognizing...")
31+
query = recognizer.recognize_google(audio, language="en-US")
32+
print(f"User: {query}")
33+
return query
34+
except Exception as e:
35+
print("Sorry, I didn't catch that. Could you please repeat?")
36+
return None
37+
38+
# Example usage
39+
speak("Hello! I am your voice assistant. How can I assist you today?")
40+
41+
while True:
42+
query = listen()
43+
if query:
44+
# Convert the query to lowercase for easier comparison
45+
query = query.lower()
46+
47+
if "play" in query:
48+
song = query.replace("play", "")
49+
speak(f"Playing {song}")
50+
pywhatkit.playonyt(song)
51+
elif "search" in query:
52+
term = query.replace("search", "")
53+
speak(f"Searching for {term}")
54+
pywhatkit.search(term)
55+
elif "send message" in query:
56+
speak("To whom would you like to send a message?")
57+
recipient = listen()
58+
speak("What message would you like to send?")
59+
message = listen()
60+
pywhatkit.sendwhatmsg(recipient, message, 0, 0) # Sending WhatsApp message
61+
speak("Message sent successfully!")
62+
elif "date" in query:
63+
date = datetime.datetime.now().strftime("%A, %B %d, %Y")
64+
speak(f"The current date is {date}")
65+
elif "time" in query:
66+
time = datetime.datetime.now().strftime("%I:%M %p")
67+
speak(f"The current time is {time}")
68+
elif "wikipedia" in query:
69+
search_query = query.replace("wikipedia", "")
70+
speak("Searching Wikipedia...")
71+
try:
72+
summary = wikipedia.summary(search_query, sentences=2)
73+
speak(f"According to Wikipedia, {summary}")
74+
except wikipedia.exceptions.DisambiguationError as e:
75+
speak("Multiple results found. Please be more specific.")
76+
except wikipedia.exceptions.PageError as e:
77+
speak("Sorry, no information found for that query.")
78+
elif "joke" in query:
79+
joke = pyjokes.get_joke()
80+
speak(joke)
81+
elif "bye" in query:
82+
speak("Goodbye!")
83+
break
84+
else:
85+
speak("Sorry, I didn't understand that. Can you please repeat?")

0 commit comments

Comments
 (0)