Skip to content

Instantly share code, notes, and snippets.

@Hackin7
Created July 16, 2018 14:53
Show Gist options
  • Save Hackin7/992dbb641bc6fbca1c36981d15074e11 to your computer and use it in GitHub Desktop.
Save Hackin7/992dbb641bc6fbca1c36981d15074e11 to your computer and use it in GitHub Desktop.
Improved Google Assistant Demo for AIY project kit
#!/usr/bin/env python3
# Copyright 2017 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# This is the Google Assistant for the AIY projects kit demo code
# It talks to alert the user when it is ready
# However, if there is no Internet connection, it will go into an offline mode
# The offline mode makes the device play a random hardcoded quote whenever you press the button
# Put in AIY-projects-python/src/ as main.py and set systemd service to run on boot
"""A demo of the Google Assistant GRPC recognizer."""
import logging
import aiy.assistant.grpc
import aiy.audio
import aiy.voicehat
logging.basicConfig(
level=logging.INFO,
format="[%(asctime)s] %(levelname)s:%(name)s:%(message)s"
)
import os
from time import sleep
#import festival
#festival.initialize(1, 210000)
#festival.say("Hello World")
def offline_message(message):
#os.system("amixer set Master 50%")
#os.system("say \""+message+"\"")
#festival.say(message)
#sleep(0.085*len(message))
#os.system("amixer set Master 100%")
os.system("espeak -a 40 -s 120 \""+message+"\"")
'''
#https://www.instructables.com/id/DIY-Drum-Machine-with-Raspberry-Pi-Hover/
import pygame
pygame.mixer.init()
kick = pygame.mixer.Sound('samples/kick.wav')
def drumming(sound):
button = aiy.voicehat.get_button()
while True:
button.wait_for_press()
sound.play()
'''
import random
def inspirational_quotes(messages):
button = aiy.voicehat.get_button()
while True:
button.wait_for_press()
index = random.randint(0, len(messages)-1)
offline_message(messages[index])
messages = []
messages.append("I respect ambition, but not ruthless ambition")
messages.append("Strive not to be a success, but rather to be of value")
messages.append("Shit")
messages.append("You miss 100% of the shots you don’t take")
messages.append("The cake is a lie")
messages.append("Your life is a lie")
messages.append("You miss 100% of the shots you don’t take")
messages.append("Every strike brings me closer to the next home run")
messages.append("Life is 10% what happens to me and 90% of how I react to it.")
messages.append("When life gives you lemons, you make lemonade")
messages.append("The most difficult thing is the decision to act, the rest is merely tenacity")
messages.append("Definiteness of purpose is the starting point of all achievement")
messages.append("We become what we think about")
messages.append("Education costs money. But then so does ignorance")
messages.append("It's only O levels")
messages.append("The only way to do great work is to love what you do")
messages.append("It’s not the years in your life that count. It’s the life in your years")
messages.append("When everything seems to be going against you, remember that the airplane takes off against the wind, not with it")
messages.append("You suck")
messages.append("Life is what we make it, always has been, always will be")
messages.append("It's your place in the world; it's your life. Go on and do all you can with it, and make it the life you want to live.")
messages.append("Dreaming, after all, is a form of planning")
messages.append("You should be studying")
messages.append("Dream big and dare to fail")
messages.append("Remember that not getting what you want is sometimes a wonderful stroke of luck")
messages.append("Our lives begin to end the day we become silent about things that matter")
messages.append("Do what you can, where you are, with what you have")
messages.append("Build your own dreams, or someone else will hire you to build theirs")
messages.append("I would rather die of passion than of boredom")
messages.append("I didn’t fail the test. I just found 100 ways to do it wrong")
messages.append("Limitations live only in our minds. But if we use our imaginations, our possibilities become limitless.")
messages.append("What’s money? A man is a success if he gets up in the morning and goes to bed at night and in between does what he wants to do. ")
messages.append("LOL")
messages.append("Challenges are what make life interesting and overcoming them is what makes life meaningful")
messages.append("Too many of us are not living our dreams because we are living our fears")
messages.append("Try not to fail Chemistry")
messages.append("You can’t fall if you don’t climb. But there’s no joy in living your whole life on the ground")
messages.append("Budget yup")
messages.append("If the wind will not serve, take to the oars")
messages.append("When I let go of what I am, I become what I might be")
messages.append("Make memes, not war")
messages.append("If you hold someone long enough under water, They stop being a cunt")
messages.append("I won't apologize for who I am")
messages.append("I don't have a short temper. I just have a quick reaction to bullshit")
messages.append("I drink your milkshake!")
messages.append("The earth is flat")
messages.append("I require an Internet connection, thank you")
# From https://www.forbes.com/sites/kevinkruse/2013/05/28/inspirational-quotes/#1d41aaad6c7a
def main():
status_ui = aiy.voicehat.get_status_ui()
status_ui.status('starting')
assistant = aiy.assistant.grpc.get_assistant()
button = aiy.voicehat.get_button()
with aiy.audio.get_recorder():
while True:
status_ui.status('ready')
print('Press the button and speak')
button.wait_for_press()
status_ui.status('listening')
print('Listening...')
text, audio = assistant.recognize()
if text:
if text == 'goodbye':
status_ui.status('stopping')
print('Bye!')
offline_message("Quote machine activating now")
break
if text == 'drumming':
drumming(kick)
if text == 'power off':
offline_message("Bye")
os.system("sudo poweroff")
break
print('You said "', text, '"')
if audio:
aiy.audio.play_audio(audio, assistant.get_volume())
if __name__ == '__main__':
offline_message("Google Assistant getting ready")
try: main()
except:pass
inspirational_quotes(messages)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment