summaryrefslogtreecommitdiff
path: root/jasper.py
blob: f3a2ac6a0b840249a73c23a662554b0ebd0e43f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python2

import os
import sys
import urllib2
import traceback
import shutil
import yaml

# Set $JASPER_HOME
jasper_home = os.getenv("JASPER_HOME")
if not jasper_home or not os.path.exists(jasper_home):
    if os.path.exists("/home/pi"):
        jasper_home = "/home/pi"
        os.environ["JASPER_HOME"] = jasper_home
    else:
        print("Error: $JASPER_HOME is not set.")
        sys.exit(0)

# Change CWD to $JASPER_HOME/jasper/client
os.chdir(os.path.join(os.getenv("JASPER_HOME"), "jasper" , "client"))

# Set $LD_LIBRARY_PATH
os.environ["LD_LIBRARY_PATH"] = "/usr/local/lib"

# Set $PATH
path = os.getenv("PATH")
if path:
    path = os.pathsep.join([path, "/usr/local/lib/"])
else:
    path = "/usr/local/lib/"
os.environ["PATH"] = path

from client import vocabcompiler
from client import speaker as speak
speaker = speak.newSpeaker()

def testConnection():
    try:
        urllib2.urlopen("http://www.google.com").getcode()
        print "CONNECTED TO INTERNET"

    except urllib2.URLError:
        print "COULD NOT CONNECT TO NETWORK"
        speaker.say(
            "Warning: I was unable to connect to a network. Parts of the system may not work correctly, depending on your setup.")

def fail(message):
    traceback.print_exc()
    speaker.say(message)
    #sys.exit(1)

def configure():
    try:
        print "COMPILING DICTIONARY"
        vocabcompiler.compile(
            "sentences.txt", "dictionary.dic", "languagemodel.lm")
        print "STARTING CLIENT PROGRAM"

    except OSError:
        print "BOOT FAILURE: OSERROR"
        fail(
            "There was a problem starting Jasper. You may be missing the language model and associated files. Please read the documentation to configure your Raspberry Pi.")

    except IOError:
        print "BOOT FAILURE: IOERROR"
        fail(
            "There was a problem starting Jasper. You may have set permissions incorrectly on some part of the filesystem. Please read the documentation to configure your Raspberry Pi.")

    except:
        print "BOOT FAILURE"
        fail(
            "There was a problem starting Jasper. Please read the documentation to configure your Raspberry Pi.")

old_client = os.path.abspath(os.path.join(os.pardir, "old_client"))
if os.path.exists(old_client):
    shutil.rmtree(old_client)

from client import stt
from client.conversation import Conversation

def isLocal():
    return len(sys.argv) > 1 and sys.argv[1] == "--local"

if isLocal():
    from client.local_mic import Mic
else:
    from client.mic import Mic

if __name__ == "__main__":

    print "==========================================================="
    print " JASPER The Talking Computer                               "
    print " Copyright 2013 Shubhro Saha & Charlie Marsh               "
    print "==========================================================="

    speaker.say("Hello.... I am Jasper... Please wait one moment.")
    testConnection()
    configure()

    profile = yaml.safe_load(open("profile.yml", "r"))

    try:
        api_key = profile['keys']['GOOGLE_SPEECH']
    except KeyError:
        api_key = None

    try:
        stt_engine_type = profile['stt_engine']
    except KeyError:
        print "stt_engine not specified in profile, defaulting to PocketSphinx"
        stt_engine_type = "sphinx"

    mic = Mic(speaker, stt.PocketSphinxSTT(),
              stt.newSTTEngine(stt_engine_type, api_key=api_key))

    addendum = ""
    if 'first_name' in profile:
        addendum = ", %s" % profile["first_name"]
    mic.say("How can I be of service%s?" % addendum)

    conversation = Conversation("JASPER", mic, profile)

    conversation.handleForever()