-
Notifications
You must be signed in to change notification settings - Fork 0
/
chatbot.py
176 lines (129 loc) · 5.28 KB
/
chatbot.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
import pyttsx3
import datetime
import speech_recognition as sr
import wikipedia
import webbrowser
import os
import pywhatkit
from pyfiglet import Figlet
import creds
import openai
import speedtest
openai.api_key = creds.openai_api_key
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
engine.setProperty('voice' , voices[0].id)
webbrowser.register('chrome' , None , webbrowser.BackgroundBrowser("C:\Program Files\Google\Chrome\Application\chrome.exe"))
figlet = Figlet()
fonts = figlet.getFonts()
figlet.setFont(font='big')
def speak(audio):
engine.say(audio)
engine.runAndWait()
def greet():
hour = int(datetime.datetime.now().hour)
if morning(hour):
speak('Good Morning')
elif afternoon(hour):
speak("Good Afternoon")
else:
speak("Good Evening")
speak("I am Echo , Your Personal Intelligent Assistant")
speak("How may I help you")
def takeCommand():
r = sr.Recognizer()
with sr.Microphone(device_index=1) as source:
print("Listening...")
r.pause_threshold = 1
audio = r.listen(source)
try:
print("Recognizing...")
query = r.recognize_google(audio , language='en-in' , show_all=True )
query = query['alternative']
query = query[0]
query = query['transcript']
print(f"User said : {query}\n")
except Exception as e:
#print(e)
print("Say that again Please")
return "None"
return query
def valid_search(search):
if search:
return True
else:
return False
def morning(hour):
if 0 <= hour < 12:
return True
else:
return False
def afternoon(hour):
if 12 <= hour < 18:
return True
else:
return False
if __name__ == "__main__":
print(figlet.renderText("Echo AI"))
greet()
while True:
query = takeCommand().lower()
if 'wikipedia' in query:
speak('Searching Wikipedia...')
query = query.replace("wikipedia" , "")
search = wikipedia.search(query , results = 1 , suggestion = False)
results = wikipedia.summary(search , sentences = 2 , auto_suggest = False , redirect = True)
speak("According to Wikipedia , ")
print(f"According to Wikipedia : {results}")
speak(results)
elif 'open' in query:
if 'google' in query:
webbrowser.get('chrome').open("google.com")
elif 'stack overflow' in query:
webbrowser.get('chrome').open("stackoverflow.com")
elif 'code' in query or 'vs code' in query:
codePath = "C:\\Users\\Com\\AppData\\Local\\Programs\\Microsoft VS Code\\Code.exe"
os.startfile(codePath)
elif 'sublime' in query or 'sublime text' in query:
codePath ="C:\\Program Files\\Sublime Text 3\\sublime_text.exe"
os.startfile(codePath)
else:
query = query.replace("open" , "")
speak("Do you want me to open it in Web Browser")
new_query = takeCommand()
if "yes" in new_query:
webbrowser.get('chrome').open("google.com/search?q=" + query)
else:
speak("I cant perform that task as of now , I am Sorry")
elif 'the time' in query:
strTime = datetime.datetime.now().strftime("%H:%M:%S")
print(strTime)
speak(f" Sir, the time is {strTime}")
elif 'youtube' in query:
query = query.replace("youtube" , "")
#speak("what should i search for")
#search=takeCommand()
if valid_search(query):
pywhatkit.playonyt(query)
else:
speak("Invalid Search")
elif 'chat gpt' in query or 'chat' in query:
speak("What should i look up")
prompt = takeCommand()
if prompt == 'exit':
break
else:
response=openai.Completion.create(engine='text-davinci-003' , prompt=prompt , max_tokens=200)
result = response['choices'][0]["text"]
print(f"Result: {result}")
elif 'internet' in query or 'speed' in query:
st = speedtest.Speedtest()
d1 = st.download()
d1 = d1*0.000000125
u1 = st.upload()
u1 = u1*0.000000125
print(f"Sir , your download speed is {d1:.2f} mega bytes per second and your upload speed is {u1:.2f} mega bytes per second ")
speak(f"Sir , your download speed is {d1:.2f} mega bytes per second and your upload speed is {u1:.2f} mega bytes per second ")
elif 'exit' in query or 'quit' in query:
speak(" Thank You ")
break