Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A way to fallback to local services for Snips when your internet goes down
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import subprocess
import time
RUNNING = False
ONLINE = True
def checkOnlineState():
global ONLINE
try:
req = requests.get('http://clients3.google.com/generate_204')
if req.status_code != 204:
raise Exception
if not ONLINE:
subprocess.call(['/home/pi/offlineFallback/shell/switchOnlineState.sh', "1"])
print('Internet is back, switching back to Amazon Polly voice and Google ASR')
return True
except OSError:
pass
if ONLINE:
subprocess.call(['/home/pi/offlineFallback/shell/switchOnlineState.sh', "0"])
print('No more internet connection, falling back to PicoTTS and Snips ASR')
return False
def main():
global RUNNING, ONLINE
subprocess.call(['/home/pi/offlineFallback/shell/switchOnlineState.sh', "1"])
try:
while RUNNING:
ONLINE = checkOnlineState()
time.sleep(60)
except KeyboardInterrupt:
pass
exit(0)
if __name__ == '__main__':
RUNNING = True
main()
#!/usr/bin/env bash
state="$1"
if [[ "$1" -eq "1" ]]; then
sudo sed -i -e 's/provider = "picotts"/provider = "customtts"/' /etc/snips.toml
sudo systemctl stop snips-asr
sudo systemctl start snips-asr-google
else
sudo sed -i -e 's/provider = "customtts"/provider = "picotts"/' /etc/snips.toml
sudo systemctl stop snips-asr-google
sudo systemctl start snips-asr
fi
sudo systemctl restart snips-*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.