Skip to content

Commit

Permalink
Logging Added
Browse files Browse the repository at this point in the history
Signed-off-by: Rishi <[email protected]>
  • Loading branch information
rshrc committed Oct 27, 2023
1 parent 5d50687 commit efe6bd6
Showing 1 changed file with 51 additions and 95 deletions.
146 changes: 51 additions & 95 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import subprocess
import random
import signal
from logging.handlers import RotatingFileHandler

from serial_number import get_serial_number

import gc
Expand All @@ -18,7 +20,6 @@
import speech_recognition as sr
from dotenv import load_dotenv
from fuzzywuzzy import fuzz
from icecream.icecream import IceCreamDebugger
from nltk.tag import pos_tag
from nltk.tokenize import word_tokenize
from config import get_configuration
Expand All @@ -33,6 +34,13 @@
wake_word_cues, wake_word_dict, chat_mode_activated_dict, chat_mode_activated_cues, stop_chat_dict, stop_chat_cues)
from measure import timing
from voice import TextToSpeechPlayer, output_voice, play_audio
import logging

logging.basicConfig(level=logging.INFO, filename='falcon_mini.log', filemode='a', format='%(asctime)s - %(levelname)s - %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
handler = RotatingFileHandler('falcon_mini.log', maxBytes=5*1024*1024, backupCount=3)
logger = logging.getLogger()

logger.addHandler(handler)

def increase_volume():
subprocess.call(['sudo', '/home/rishi/falcon_mini/scripts/setup/set_volume.sh'])
Expand All @@ -41,7 +49,6 @@ def increase_volume():

BPRAP = "voice_cues"

ic = IceCreamDebugger()

os.environ['ALSA_WARNINGS'] = '0'

Expand All @@ -63,13 +70,8 @@ def increase_volume():
CHAT_MODE = False
ASK_FOR_WAKE_WORD = True


# Configure Device etc
configuration = get_configuration(BASE_URL, test_device_uuid)

print(f"Line 71 : {configuration}")

# sys.exit()
tts = TextToSpeechPlayer(configuration.voice.url)

MEMORY_CONTEXT = 0
Expand All @@ -81,19 +83,12 @@ def increase_volume():

MEMORY_CONTEXT = configuration.memory.context_limit

print(f"Memory Context {MEMORY_CONTEXT}")
logger.info(f"Memory Context {MEMORY_CONTEXT}")
else:
print("Unexpected type of configuration data.")

# sys.exit()


# Number of conversations that are kept track of, depend on get_configuration
logger.info("Unexpected type of configuration data.")

# Example Usage:
display_controller = DisplayController()


@timing
def check_similar_song(user_input: str, directory_path: str):
print("Using V1 Search Algorithm")
Expand Down Expand Up @@ -124,24 +119,11 @@ def check_similar_song(user_input: str, directory_path: str):

@timing
def update_conversation(input, output):

# store in DB
store_data(input, output)

# Single Operation Conversation
# conversation.extend([
# {'role': 'user', 'content': input},
# {'role': 'assistant', 'content': output}
# ])
# print("Conversation Updated")


@timing
async def process_input(recognized_text):
# doc = nlp(recognized_text)
awaiting_response_cue = random.choice(awaiting_response_cues)
# play(f"voice_cues/awaiting_response_cues/{awaiting_response_dict[awaiting_response_cue]}")
# tts.text_to_speech(awaiting_response_cue, "awaiting_response_cue.mp3")
display_controller.render_text_threaded_v2(awaiting_response_cue)
tts.load_and_play(
f"{os.getcwd()}/assets/voice_cues/awaiting_response_cues/{awaiting_response_dict[awaiting_response_cue]}.mp3", use_thread=True)
Expand Down Expand Up @@ -172,8 +154,8 @@ async def process_input(recognized_text):
play_intent = False
stop_intent = False

print(f"Play Intent -> {play_intent}")
print(f"Stop Intent -> {stop_intent}")
logger.info(f"Play Intent -> {play_intent}")
logger.info(f"Stop Intent -> {stop_intent}")

if play_intent:
similar_song_found, song_path = check_similar_song(
Expand All @@ -200,26 +182,22 @@ async def process_input(recognized_text):
'child_id': IDENTIFIER,
}

print(data)

# not gonna take conversation, to reduce token utilization

if False and len(conversation) > 0:
data['conversation'] = json.dumps(conversation[:MEMORY_CONTEXT])

try:

print(f"POST DATA {data}")

response = r.post(API_URL, json=data, headers=headers)
response = r.post(API_URL, json=data, headers=headers)

print(response.status_code)
logger.info(f"AI TALK API RESPONSE {response.status_code}")

speech = response.json()['response']
update_conversation(recognized_text, speech)
speech = response.json()['response']
update_conversation(recognized_text, speech)

# print(f"Response : {conversation}")
display_controller.render_text_threaded_v2(speech)
# await output_voice(speech)
tts.play(speech, headers)
display_controller.render_text_threaded_v2(speech)
tts.play(speech, headers)
except Exception as e:
logger.warning(f"API Call Fail with {data} & {e}")


def voice_filler():
Expand All @@ -228,44 +206,41 @@ def voice_filler():

@timing
async def interact():
logger.info("Interact Mode Accessed")

global CHAT_MODE
global ASK_FOR_WAKE_WORD

with sr.Microphone() as source:
logger.info("Microphone On")
try:
recognizer.adjust_for_ambient_noise(source, duration=1)
logger.info("Ambience Adjusted")
recognizer.energy_threshold = 7000
print("Energy threshold:", recognizer.energy_threshold)
logger.info(f"Energy Threshold Set to {recognizer.energy_threshold}")

listen = True

if listen:
print("Listening for Wake Word")
logger.info("Listening for Wake Word")

if ASK_FOR_WAKE_WORD:
wake_word_cue = random.choice(wake_word_cues)
# tts.text_to_speech(wake_word_cue, "wake_word_cue.mp3")
display_controller.render_text_threaded_v2(wake_word_cue)
tts.load_and_play(
f"{os.getcwd()}/assets/voice_cues/wake_word_cues/{wake_word_dict[wake_word_cue]}.mp3")
# tts.play(f"voice_cues/wake_word_cues/{wake_word_dict[wake_word_cue]}")
logger.info("User Informed About State")

ASK_FOR_WAKE_WORD = False
start_time = time.time()
logger.info("Started Listening")
audio = recognizer.listen(source)
# audio_received_cue = random.choice(audio_received_cues)
# tts.text_to_speech(audio_received_cue, "audio_received_cue.mp3")
# display_controller.render_text_threaded_v2(audio_received_cue)
# tts.load_and_play(
# f"{os.getcwd()}/assets/voice_cues/audio_received_cues/{audio_received_dict[audio_received_cue]}.mp3", use_thread=True)
# play(f"voice_cues/audio_received_cues/{audio_received_dict[audio_received_cue]}")

print("There was some audio input!")

logger.info(f"There was some audio input! Time Delta {time.time()-start_time:.2f}")

try:
recognized_text = recognizer.recognize_google(
audio).lower()
print(f"Recognized Text : {recognized_text}")
logger.info(f"Recognized Text : {recognized_text}")

words = recognized_text.split()

Expand All @@ -274,52 +249,51 @@ async def interact():

lets_chat_match = fuzz.partial_ratio(recognized_text, "let's chat")

# If "let's chat" isn't recognized, check for wake word in the first two words
logger.info(f"Match {recognized_text} and Lets Chat - {lets_chat_match}")

if lets_chat_match <= 70 and len(words) >= 2:
logger.info(f"Lets Chat Not Matched: Words Length {len(words)}")
first_two_words = " ".join(words[:2])
wake_word_match = fuzz.partial_ratio(first_two_words, WAKE_WORD)

# If wake word is found and there are four or more words,
# check words 3 and 4 for "let's chat"
if wake_word_match > 70 and len(words) >= 4:
next_two_words = " ".join(words[2:4])
lets_chat_match = fuzz.partial_ratio(next_two_words, "let's chat")

if wake_word_match > 70 or lets_chat_match > 70:
logger.info(f"Wake Word Match {wake_word_match} and Lets Chat Match {lets_chat_match}")
if lets_chat_match > 70:
chat_mode_cue = random.choice(chat_mode_activated_cues)
display_controller.render_text_threaded_v2(chat_mode_cue)
tts.load_and_play(f"{os.getcwd()}/assets/voice_cues/chat_mode_cues/{chat_mode_activated_dict[chat_mode_cue]}.mp3")

while True:
print("Preparing for Audio I/O")

print("Listening for second command!")

logger.info("Listening for second command!")
start_time = time.time()
audio = recognizer.listen(source, timeout=3)

print("Resuming Conversation")
logger.info(f"Resuming Conversation - {time.time() - start_time:2f}")
try:

recognized_text = recognizer.recognize_google(
audio)

logger.info(f"Recognized Text : {recognized_text}")

if fuzz.partial_ratio(recognized_text, "stop chat") > 70:
print("We reached fuzz")
logger.info("Stop Chat Command Received")
stop_chat_cue = random.choice(stop_chat_cues)
display_controller.render_text_threaded_v2(stop_chat_cue)
tts.load_and_play(f"{os.getcwd()}/assets/voice_cues/stop_chat_cues/{stop_chat_dict[stop_chat_cue]}.mp3")
ASK_FOR_WAKE_WORD = True
break

print(
f"Recognized Instruction : {recognized_text}")

await process_input(recognized_text)
gc.collect()
except Exception as e:
gc.collect()
print(f"Recognition Failed : {e}")
logger.warning(f"Recognition Failed : {e}")


else:
Expand All @@ -329,21 +303,18 @@ async def interact():

else:
pass
print(f"Keep Sleeping")
logger.warning(f"False Trigger! Keep Sleeping!")
except Exception as e:
# display_controller.render_text_threaded_v2(
# "Something happened?")
print(f"Some Error Occoured : {e}")

logger.warning(f"Some Error Occoured : {e}")
except Exception as e:
display_controller.render_text_threaded_v2("I heard some noise")
print(f"Something Crashed {e}")
logger.warning(f"False Wake Up! Keep Sleeping! {e}")


@timing
async def main():
# while True:
# ic("I am running indeed")
# time.sleep(1)
logger.info("Falcon Mini Booted")
parser = argparse.ArgumentParser(
description="Process input and optionally generate output audio.")
parser.add_argument("-O", "--output", dest="output_text",
Expand All @@ -355,23 +326,8 @@ async def main():
print(f"Output {args.output_text}")
await output_voice(args.output_text)
else:
# load data.yaml config file
while True:
await interact()


# def cleanup():
# display_controller.render_text_threaded_v2("Not Running...")
# print("Cleaning up before exiting...")


# def exit_handler():
# cleanup()


# atexit.register(exit_handler)
# signal.signal(signal.SIGINT, exit_handler)
# signal.signal(signal.SIGNINT, exit_handler)

if __name__ == "__main__":
asyncio.run(main())

0 comments on commit efe6bd6

Please sign in to comment.