1+ import speech_recognition as sr
2+ import pyttsx3
3+ import webbrowser
4+ import datetime
5+ import wikipedia
6+
7+ # initialize
8+ engine = pyttsx3 .init ()
9+ recognizer = sr .Recognizer ()
10+
11+ def speak (text ):
12+ engine .say (text )
13+ engine .runAndWait ()
14+
15+ def listen ():
16+ with sr .Microphone () as source :
17+ print ("Listening..." )
18+ audio = recognizer .listen (source )
19+ try :
20+ command = recognizer .recognize_google (audio )
21+ print ("You said:" , command )
22+ return command .lower ()
23+ except :
24+ speak ("Sorry, I didn't catch that" )
25+ return ""
26+
27+ def process_command (command ):
28+ if "hello" in command :
29+ speak ("Hello Ronit! How can I help you?" )
30+
31+ elif "time" in command :
32+ time = datetime .datetime .now ().strftime ("%H:%M" )
33+ speak (f"The time is { time } " )
34+
35+ elif "open youtube" in command :
36+ webbrowser .open ("https://www.youtube.com" )
37+ speak ("Opening YouTube" )
38+
39+ elif "open my github profile" in command :
40+ webbrowser .open ("https://www.github.com/Ronit049" )
41+ speak ("Opening Your github profile Ronit" )
42+
43+ elif "open my Twitter profile" in command :
44+ webbrowser .open ("https://x.com/its_rsr04" )
45+ speak ("Opening your twitter profile" )
46+
47+ elif "search" in command :
48+ speak ("What should I search?" )
49+ query = listen ()
50+ webbrowser .open (f"https://www.google.com/search?q={ query } " )
51+
52+ elif "wikipedia" in command :
53+ speak ("Searching Wikipedia..." )
54+ query = command .replace ("wikipedia" , "" )
55+ result = wikipedia .summary (query , sentences = 2 )
56+ speak (result )
57+
58+ elif "exit" in command :
59+ speak ("Goodbye!" )
60+ exit ()
61+
62+ # main loop
63+ speak ("Voice assistant started" )
64+
65+ while True :
66+ command = listen ()
67+ process_command (command )
0 commit comments