Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added log.py and implemented logging functionality #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/log.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import logging

def setup_custom_logger(name):
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter('%(asctime)s:%(levelname)s:%(name)s:%(message)s')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line length exceeds 79. Pep8 violation 😸

file_handler = logging.FileHandler('main.log')
file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
return logger
27 changes: 13 additions & 14 deletions app/main.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import logging
logging.basicConfig(filename='logger.log', level=logging.INFO,
format='%(asctime)s:%(levelname)s:%(name)s:%(message)s' )

import pyttsx3 as tts # For Text to Speech
import speech_recognition as stt # For Speech to Text
Expand All @@ -10,6 +7,8 @@
from app.config import tts_config
from app.config import stt_config

import app.log as app_logger
logger = app_logger.setup_custom_logger('root')

def start_app():

Expand All @@ -25,30 +24,30 @@ def start_app():
device_id = 0 # first device (microphone)

# Get Microphone Device ID
print('Available Audio Devices: ')
logger.info('Available Audio Devices: ')
mic_list = stt.Microphone.list_microphone_names()
for i, micName in enumerate(mic_list):
print(i, micName)

try:
device_id = int(input('Microphone ID: '))
except Exception as e:
logging.info('Invalid Driver ID, using 0')
logger.error('Invalid Driver ID, using 0')
finally:
device_id = 0 if device_id < 0 or device_id >= len(mic_list) else device_id

try:
with stt.Microphone(device_index=device_id, sample_rate=sample_rate, chunk_size=chunk_size) as source:
recognizer.adjust_for_ambient_noise(source) # removing noise
logging.info("Ready.")
logger.info("Ready.")

while True: # STT Loop
audio = recognizer.listen(source) # listen from mic

try:
text = recognizer.recognize_google(audio) # recognize with google
logging.info("You said:")
logging.info(text)
logger.info("You said:")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

concatenate You said with the text.

logger.info(text)
text = text.lower()

# Fetch Reply
Expand All @@ -66,17 +65,17 @@ def start_app():
reply.action(text) # Perform Action

output = reply.getReply(text) # Generate Reply
logging.info(output) #logging Reply
logger.info(output) #logger Reply
engine.say(output) # TTS
engine.runAndWait() # Wait for Speech To Complete

except stt.UnknownValueError as e:
logging.info("Google Speech Recognition could not understand audio")
logger.error("Google Speech Recognition could not understand audio")
except stt.RequestError as e:
logging.info("Could not request results from Google Speech Recognition service; {0}".format(e))
logger.error("Could not request results from Google Speech Recognition service; {0}".format(e))
except Exception as e:
logging.info(e)
logger.warn(e)

except Exception as e:
logging.info("Exception Occurred")
logging.info(e)
logger.warn("Exception Occurred")
logger.warn(e)