-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvoice_assistant_02.py
More file actions
134 lines (117 loc) · 6.08 KB
/
voice_assistant_02.py
File metadata and controls
134 lines (117 loc) · 6.08 KB
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
import speech_recognition as sr
import pyttsx3
import datetime
import webbrowser
import sys
import wikipedia
from translate import Translator
from currency_converter import CurrencyConverter
def speak(text):
engine = pyttsx3.init()
engine.say(text)
engine.runAndWait()
def listen():
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='en-in')
print(f"Recognized query: {query}") # Add this line to print the recognized text
except Exception as e:
print("Say that again please...")
return "None"
return query
# def text_chat():
# query = input("You: ").lower()
# return query
def greet_user(name):
hour = datetime.datetime.now().hour
if hour >= 0 and hour < 12:
speak(f"Good morning, {name}!")
elif hour >= 12 and hour < 18:
speak(f"Good afternoon, {name}!")
else:
speak(f"Good evening, {name}!")
# Add a summary of the day here (e.g., date, time, weather if you integrate it)
today = datetime.date.today()
now = datetime.datetime.now()
current_time = now.strftime("%H:%M:%S")
speak(f"Today's date is {today} and the current time is {current_time}")
if __name__ == "__main__":
user_name = "Adarsh" # User's name
greet_user(user_name)
while True:
# You can choose between voice input (listen()) or text input (text_chat())
query = listen().lower()
# query = text_chat() # Using text input for demonstration
if 'hello' in query:
speak(f"Hello to you too, {user_name}!")
elif 'time' in query or "what's the time" in query:
now = datetime.datetime.now()
current_time = now.strftime("%H:%M:%S")
speak(f"The current time is {current_time}")
elif 'date' in query or "what's today's date" in query:
today = datetime.date.today()
speak(f"Today's date is {today}")
elif 'search google for' in query:
search_query = query.replace('search google for', '').strip()
speak(f"Searching Google for {search_query}")
url = f"https://www.google.com/search?q={search_query}"
webbrowser.open(url)
elif 'search wikipedia for' in query:
search_query = query.replace('search wikipedia for', '').strip()
speak(f"Searching Wikipedia for {search_query}")
try:
summary = wikipedia.summary(search_query, sentences=2)
speak("According to Wikipedia,")
speak(summary)
except wikipedia.exceptions.PageError:
speak("Sorry, I could not find anything on Wikipedia for that query.")
except wikipedia.exceptions.DisambiguationError as e:
speak("There are multiple results for that query. Please be more specific.")
print(e.options)
elif 'translate' in query and 'to' in query:
try:
parts = query.split('translate')[1].strip().split('to')
text_to_translate = parts[0].strip()
target_language_code = parts[1].strip() # The 'translate' library uses language codes (e.g., 'es' for Spanish)
translator = Translator(to_lang=target_language_code)
translation = translator.translate(text_to_translate)
speak(f"The translation of {text_to_translate} to {target_language_code} is: {translation}")
except Exception as e:
speak("Sorry, I could not perform the translation. Please make sure you are specifying a valid language code.")
print(e)
elif 'convert' in query and 'to' in query:
try:
parts = query.split('convert')[1].strip().split('to')
amount_and_from_currency = parts[0].strip().split()
amount = float(amount_and_from_currency[0])
from_currency = amount_and_from_currency[1].upper()
to_currency = parts[1].strip().upper()
c = CurrencyConverter()
converted_amount = c.convert(amount, from_currency, to_currency)
speak(f"{amount} {from_currency} is equal to {converted_amount:.2f} {to_currency}")
except Exception as e:
speak("Sorry, I could not perform the currency conversion. Please make sure you specify the amount and currencies correctly.")
print(e)
elif 'what can you do' in query or 'list your commands' in query:
speak("I can perform the following tasks: Greet you, tell you the time and date, search Google and Wikipedia, translate text, convert currencies, open YouTube and Chrome, tell you your name, and exit.")
elif 'introduce yourself' in query or 'who are you' in query:
speak("Hello, I am adarsh's voice assistant, created to help you with various tasks.")
elif 'introduce me ' in query:
print("Reached 'introduce adarsh pandey' block") # Debug print
speak("Meet Adarsh Pandey, 23 years old, final-year student at G.H. Raisoni College of Engineering, living in Nagpur. He’s the guy who can talk tech for hours but will still find time to hit the swimming pool. When he’s not coding smart systems, you might find him enjoying a swim or thinking up his next cool project idea.")
elif 'open youtube' in query:
speak("Opening YouTube")
webbrowser.open("https://www.youtube.com")
elif 'launch chrome' in query or 'open chrome' in query:
speak("Launching Chrome")
webbrowser.open("https://www.google.com") # Opens Google in the default browser
elif 'what is my name' in query or 'who am i' in query:
speak(f"Your name is {user_name}")
elif 'exit' in query:
speak("Goodbye! have a nice day")
break