-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
68 lines (33 loc) · 1.26 KB
/
app.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
import requests,pyttsx3,json,speech_recognition as sr
class Joke:
def __init__(self,set_up,punch_line) -> None:
self.set_up=set_up
self.punch_line=punch_line
def __str__(self) -> str:
return f"{self.set_up},{self.punch_line}"
r=sr.Recognizer()
url:str="https://official-joke-api.appspot.com/random_joke"
def joke_reader():
response=requests.get(url)
data=json.loads(response.text)
joke=Joke(data["setup"],data["punchline"])
pyttsx3.speak(joke)
def error_reader():
pyttsx3.speak("Sorry, couldn't understand you")
def listner_daemon():
try:
with sr.Microphone() as source2:
r.adjust_for_ambient_noise(source2, duration=1)
audio2 = r.listen(source2)
MyText = r.recognize_google(audio2)
MyText = MyText.lower()
if(MyText=="tell me a joke"):
joke_reader()
else:
error_reader()
except sr.RequestError as e:
print("Could not request results; {0}".format(e))
except sr.UnknownValueError:
print("unknown error occurred")
print("Say: \"Tell me a joke\"")
listner_daemon()