Skip to content

Instantly share code, notes, and snippets.

@jr-k
Forked from Psychokiller1888/offlineFallback.py
Created January 27, 2019 18:12
Show Gist options
  • Save jr-k/aada67199a71d163bc38dc81e4e1ecb2 to your computer and use it in GitHub Desktop.
Save jr-k/aada67199a71d163bc38dc81e4e1ecb2 to your computer and use it in GitHub Desktop.
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