Skip to content

Commit 71d42e1

Browse files
adeen-sprateekiiest
authored andcommitted
Initial chatbot I/O proof of concept (prateekiiest#115)
* Initial chatbot I/O proof of concept * Fix PEP-8 issues in chatbot * Fix another PEP-8 issue * Remove backup file and unnecessary pyaudio dependency
1 parent 65d2a3d commit 71d42e1

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Code-Sleep-Python/chatbot/code.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import speech_recognition as sr
2+
import os
3+
import subprocess
4+
from gtts import gTTS
5+
6+
r = sr.Recognizer()
7+
8+
with sr.Microphone() as source:
9+
r.adjust_for_ambient_noise(source)
10+
print("Say something!")
11+
audio = r.listen(source)
12+
print("Got the speech. Now processing...")
13+
14+
# recognize speech using Wit.ai
15+
# export your wit.ai key to your environment before running this program
16+
WIT_AI_KEY = os.getenv("WITAIKEY")
17+
if not WIT_AI_KEY:
18+
print("API key not available")
19+
exit()
20+
21+
in_speech = ""
22+
try:
23+
in_speech = r.recognize_wit(audio, key=WIT_AI_KEY)
24+
print("You said " + in_speech)
25+
except sr.UnknownValueError:
26+
print("Wit.ai could not understand audio")
27+
except sr.RequestError as e:
28+
print("Could not request results from Wit.ai service; {0}".format(e))
29+
30+
# in_speech contains the string of the input audio.
31+
# It can be processed and actions can be performed based on it.
32+
# TODO implement actions based on keywords
33+
# Suggestions: Utilise wit.ai for proper NLP
34+
35+
if (in_speech):
36+
tts = gTTS(text=in_speech, lang="en")
37+
tts.save("sample.mp3")
38+
subprocess.Popen(["mpg123", "-q", "sample.mp3"]).wait()
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
gTTS==2.0.0
2+
gTTS-token==1.1.1
3+
SpeechRecognition==3.8.1
4+

0 commit comments

Comments
 (0)