-
Notifications
You must be signed in to change notification settings - Fork 0
/
Conversational AI.py
74 lines (55 loc) · 2.12 KB
/
Conversational AI.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
# सबसे पहले हम ये सारे Packeges install करेंगे by "pip install <Package Name>"
# Please Make your Wolframalpha Account and take your API and paste below at line 46
import speech_recognition as sr
import pyttsx3
import wolframalpha
import wikipedia
import webbrowser
engine = pyttsx3.init('sapi5')
voices = engine.getProperty('voices')
# for v in voices:
# print(v)
engine.setProperty('voice', 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Speech\Voices\Tokens\TTS_MS_EN-US_DAVID_11.0')
def speak(audio):
engine.say(audio)
engine.runAndWait()
speak('Hey my name is sam, I am python bot')
def command():
r = sr.Recognizer()
with sr.Microphone() as source:
print("Listening...")
r.pause_threshold = 0.6
audio = r.listen(source)
try:
ask = r.recognize_google(audio, language='en-us')
print(f"You said : {ask}")
except Exception:
print("Say that again...")
return ""
return ask
if __name__ == "__main__":
while True:
query = command().lower()
print(query)
# Error को handel करने के लिए ।
try:
if 'Sam' in query:
query = query.replace('Sam', '')
client = wolframalpha.Client("L8R452-VW7TYWTQV4") # Paste Your API Key Here....!!!
res = client.query(query)
ans = next(res.results).text
print(ans)
speak(ans)
except Exception:
try:
query = query.replace('Sam', '')
results = wikipedia.summary(query, sentences=2)
print(results)
speak(results)
except Exception:
try:
query = query.replace('Sam', '')
webbrowser.open('https://google.com/?#q=' + query)
except:
print("It is weird but I got nothing try re-running the program")
speak("It is weird but I got nothing try re-running the program")