Example #1
0
 def listen(self):
     client = Wit(self.wit_token)
     if self.voice:
         with speech_recognition.Microphone() as source:
             self.logger.debug("Słucham!")
             audio = self.rec.listen(source)
             self.logger.debug("Myślę...")
             with open(path.join('local', 'mic-results.wav'), 'wb') as f:
                 f.write(audio.get_wav_data())
             with open(path.join('local', 'mic-results.wav'), 'rb') as f:
                 try:
                     rtr = client.post_speech(f)
                 except exceptions.BadRequestError:
                     return None, None, None
     else:
         self.logger.debug("Czytam!")
         message = input('>>')
         self.logger.debug("Myślę...")
         rtr = client.get_message(message)
     # print('---')
     # print(rtr)
     # print('---')
     text = rtr['_text']
     intent, entities = self.get_wit_intent(rtr, text)
     # print(text, '->', intent)
     # print('---')
     # print(entities)
     # print('---')
     return text, intent, entities
 def PostTextString(self, text):
   # Form a text query for Wit
   w = Wit(self.witToken)
   try:
     return WitAiQueryResponse(w.get_message(text))
   except:
     raise
Example #3
0
def witAI(text, city, state, country, zipCode):
    response = Wit(WIT_AI_SERVER_TOKEN)
    command = dict(response.get_message(text))
    if command['outcomes'][0]['confidence'] >= 0.6:
        if command['outcomes'][0]['intent'] == "translate":
            language = command['outcomes'][0]['entities']['toLanguage'][0][
                'value'].lower()
            translateContent = command['outcomes'][0]['entities']['trContent'][
                0]['value'].lower()
            return translate(translateContent, language)
        elif command['outcomes'][0]['intent'] == "email":
            emailContent = command['outcomes'][0]['entities']['content'][0][
                'value']
            contact = command['outcomes'][0]['entities']['emailContact'][0][
                'value'].title()
            try:
                emailLanguage = command['outcomes'][0]['entities'][
                    'toLanguage'][0]['value'].lower()
                emailContent = translate(emailContent, emailLanguage)
            except:
                pass
            sendemail(from_addr=SENDER_ADDRESS,
                      to_addr_list=[RECEIVER_ADDRESS],
                      cc_addr_list=[],
                      subject='Email from Abhi Gupta via Watson',
                      message=emailContent,
                      login='******',
                      password='******')
            return "Your message has been sent to %s, courtesy of Watson." % contact

    else:
        return wolframAlpha(text)
Example #4
0
class RulesEngine():
    def __init__(self):
        client = MongoClient(configuration['database']['server'], configuration['database']['port'])
        self.database = client.lisa

        self.wit = Wit(configuration['wit_server_token'])

    def Rules(self, jsonData, lisaprotocol):
        rulescollection = self.database.rules
        intentscollection = self.database.intents
        if "outcome" in jsonData.keys():
            jsonInput = {}
            jsonInput['outcome'] = jsonData['outcome']
        else:
            jsonInput = self.wit.get_message(unicode(jsonData['body']))
        jsonInput['from'], jsonInput['type'], jsonInput['zone'] = jsonData['from'], jsonData['type'], jsonData['zone']

        if configuration['debug']['debug_before_before_rule']:
            log.msg(unicode(_("Before 'before' rule: %(jsonInput)s" % {'jsonInput': str(jsonInput)})))
        for rule in rulescollection.find({"enabled": True, "before": {"$ne":None}}).sort([("order", 1)]):
            exec(rule['before'])
        if configuration['debug']['debug_after_before_rule']:
            log.msg(unicode(_("After 'before' rule: %(jsonInput)s" % {'jsonInput': str(jsonInput)})))
        if configuration['debug']['debug_wit']:
            log.msg("WIT: " + str(jsonInput['outcome']))

        oIntent = intentscollection.find_one({"name": jsonInput['outcome']['intent']})
        if oIntent and jsonInput['outcome']['confidence'] >= configuration['wit_confidence']:
            instance = namedAny(str(oIntent["module"]))()
            methodToCall = getattr(instance, oIntent['function'])
            jsonOutput = methodToCall(jsonInput)
        else:
            jsonOutput = {}
            jsonOutput['plugin'] = "None"
            jsonOutput['method'] = "None"
            jsonOutput['body'] = _("I have not the right plugin installed to answer you correctly")
        jsonOutput['from'] = jsonData['from']
        if configuration['debug']['debug_before_after_rule']:
            log.msg(unicode(_("Before 'after' rule: %(jsonOutput)s" % {'jsonOutput': str(jsonOutput)})))
        for rule in rulescollection.find({"enabled": True, "after": {"$ne":None}}).sort([("order", 1)]):
            exec(rule['after'])
            #todo it doesn't check if the condition of the rule after has matched to end the rules
            if rule['end']:
                break
        if configuration['debug']['debug_after_after_rule']:
            log.msg(unicode(_("After 'after' rule: %(jsonOutput)s" % {'jsonOutput': str(jsonOutput)})))
Example #5
0
class RulesEngine():
    def __init__(self):
        client = MongoClient(configuration['database']['server'],
                             configuration['database']['port'])
        self.database = client.lisa

        self.wit = Wit(configuration['wit_server_token'])

    def Rules(self, jsonData, lisaprotocol):
        rulescollection = self.database.rules
        intentscollection = self.database.intents
        if "outcome" in jsonData.keys():
            jsonInput = {}
            jsonInput['outcome'] = jsonData['outcome']
        else:
            jsonInput = self.wit.get_message(unicode(jsonData['body']))
        jsonInput['from'], jsonInput['type'], jsonInput['zone'] = jsonData[
            'from'], jsonData['type'], jsonData['zone']

        if configuration['debug']['debug_before_before_rule']:
            log.msg(
                unicode(
                    _("Before 'before' rule: %(jsonInput)s" %
                      {'jsonInput': str(jsonInput)})))
        for rule in rulescollection.find({
                "enabled": True,
                "before": {
                    "$ne": None
                }
        }).sort([("order", 1)]):
            exec(rule['before'])
        if configuration['debug']['debug_after_before_rule']:
            log.msg(
                unicode(
                    _("After 'before' rule: %(jsonInput)s" %
                      {'jsonInput': str(jsonInput)})))
        if configuration['debug']['debug_wit']:
            log.msg("WIT: " + str(jsonInput['outcome']))

        oIntent = intentscollection.find_one(
            {"name": jsonInput['outcome']['intent']})
        if oIntent and jsonInput['outcome']['confidence'] >= configuration[
                'wit_confidence']:
            instance = namedAny(str(oIntent["module"]))()
            methodToCall = getattr(instance, oIntent['function'])
            jsonOutput = methodToCall(jsonInput)
        else:
            jsonOutput = {}
            jsonOutput['plugin'] = "None"
            jsonOutput['method'] = "None"
            jsonOutput['body'] = _(
                "I have not the right plugin installed to answer you correctly"
            )
        jsonOutput['from'] = jsonData['from']
        if configuration['debug']['debug_before_after_rule']:
            log.msg(
                unicode(
                    _("Before 'after' rule: %(jsonOutput)s" %
                      {'jsonOutput': str(jsonOutput)})))
        for rule in rulescollection.find({
                "enabled": True,
                "after": {
                    "$ne": None
                }
        }).sort([("order", 1)]):
            exec(rule['after'])
            #todo it doesn't check if the condition of the rule after has matched to end the rules
            if rule['end']:
                break
        if configuration['debug']['debug_after_after_rule']:
            log.msg(
                unicode(
                    _("After 'after' rule: %(jsonOutput)s" %
                      {'jsonOutput': str(jsonOutput)})))
Example #6
0
class ClientFactory(Factory):
    # Singleton instance
    __instance = None

    #-----------------------------------------------------------------------------
    def __init__(self):
        # Check Singleton
        if ClientFactory.__instance is not None:
            raise Exception("Singleton can't be created twice !")

        # Variables init
        self.clients = {}
        self.zones = {}
        self._lock = threading.RLock()
        self.wit = None

    #-----------------------------------------------------------------------------
    def startFactory(self):
        # Init global contexts
        NeoContext.init(factory=self)

        # Init Wit
        self.wit = Wit(configuration['wit_token'])

    #-----------------------------------------------------------------------------
    def buildProtocol(self, addr):
        # Create protocol
        return LisaProtocol()

    #-----------------------------------------------------------------------------
    def stopFactory(self):
        # Clean
        if ClientFactory.__instance is not None:
            ClientFactory.__instance.clients = {}
            ClientFactory.__instance.zones = {}
            ClientFactory.__instance.wit = None
            ClientFactory.__instance = None

        # Clear global contexts
        NeoContext.deinit()

    #-----------------------------------------------------------------------------
    @classmethod
    def initClient(cls, client_name, zone_name):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()
        self = cls.__instance

        # Lock access
        self._lock.acquire()

        # Get zone
        zone_uid = cls.getOrCreateZone(zone_name)

        # Search if we already had a connection with this client
        client = None
        client_uid = None
        for c in self.clients:
            if self.clients[c]['name'] == client_name and self.clients[c][
                    'zone'] == zone_name:
                return self.clients[c]

        # If not found
        if client is None:
            # Add client
            client_uid = str(uuid.uuid1())
            self.clients[client_uid] = {
                'uid': client_uid,
                'protocols': {},
                'name': client_name,
                'zone': zone_name,
                'zone_uid': zone_uid
            }
            client = self.clients[client_uid]

            # Each client has its own context
            client['context'] = NeoContext(client_uid=client_uid)

            # Add client to zone
            found_flag = False
            for c in self.zones[zone_uid]['client_uids']:
                if c == client['uid']:
                    found_flag = True
                    break
            if found_flag == False:
                self.zones[zone_uid]['client_uids'].append(client['uid'])

        # Release access
        self._lock.release()

        return client

    #-----------------------------------------------------------------------------
    @classmethod
    def parseChat(cls, jsonData, client_uid):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()
        self = cls.__instance

        # If input has already a decoded intent
        if jsonData.has_key("outcome") == True:
            jsonInput = {}
            jsonInput['outcome'] = jsonData['outcome']
        elif len(jsonData['body']) > 0:
            # Ask Wit for intent decoding
            jsonInput = self.wit.get_message(unicode(jsonData['body']))
        else:
            # No input => no output
            return

        # Initialize output from input
        jsonInput['from'], jsonInput['type'], jsonInput['zone'] = jsonData[
            'from'], jsonData['type'], jsonData['zone']

        # Show wit result
        if configuration['debug']['debug_wit']:
            log.msg("WIT: " + str(jsonInput['outcome']))

        # Execute intent
        client = cls.getClient(client_uid)
        intent = PluginManager.getIntent(
            intent_name=jsonInput['outcome']['intent'])
        if intent is not None:
            # Call plugin
            client['context'].parse(jsonInput=jsonInput,
                                    plugin_name=intent.plugin_name,
                                    method_name=intent.method_name)
        else:
            # Parse without intent
            client['context'].parse(jsonInput=jsonInput)

    #-----------------------------------------------------------------------------
    @classmethod
    def getClient(cls, client_uid):
        # Get singleton
        if cls.__instance is None:
            return None

        # Return client
        return cls.__instance.clients[client_uid]

    #-----------------------------------------------------------------------------
    @classmethod
    def getOrCreateZone(cls, zone_name):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        # All zones
        if zone_name == "all":
            return "all"

        # Lock access
        cls.__instance._lock.acquire()

        # Search zone
        zone = None
        zone_uid = None
        for z in cls.__instance.zones:
            if cls.__instance.zones[z]['name'] == zone_name:
                zone = cls.__instance.zones[z]
                zone_uid = z
                break

        # If not found
        if zone is None:
            # Create zone
            zone_uid = str(uuid.uuid1())
            cls.__instance.zones[zone_uid] = {
                'name': zone_name,
                'client_uids': []
            }
            zone = cls.__instance.zones[zone_uid]

        # Release access
        cls.__instance._lock.release()

        return zone_uid

    #-----------------------------------------------------------------------------
    @classmethod
    def sendToClients(cls, jsonData, client_uids=[], zone_uids=[]):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        # Parse clients
        for c in cls.__instance.clients:
            # If client is in destination
            if "all" in zone_uids or cls.__instance.clients[c][
                    'zone_uid'] in zone_uids or "all" in client_uids or c in client_uids:
                # Parse client protocols
                for p in cls.__instance.clients[c]['protocols']:
                    # Send to client through protocol
                    cls.__instance.clients[c]['protocols'][p][
                        'object'].sendToClient(jsonData)

    #-----------------------------------------------------------------------------
    @classmethod
    def LisaReload(cls):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        log.msg("Reloading engine")
        cls.__instance.build_activeplugins()

    #-----------------------------------------------------------------------------
    @classmethod
    def SchedReload(cls):
        global taskman
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        log.msg("Reloading task scheduler")
        cls.__instance.taskman = taskman
        return cls.__instance.taskman.reload()

    #-----------------------------------------------------------------------------
    @classmethod
    def get(cls):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()
        return cls.__instance
Example #7
0
class ClientFactory(Factory):
    # Singleton instance
    __instance = None

    #-----------------------------------------------------------------------------
    def __init__(self):
        # Check Singleton
        if ClientFactory.__instance is not None:
            raise Exception("Singleton can't be created twice !")

        # Variables init
        self.clients = {}
        self.zones = {}
        self._lock = threading.RLock()
        self.wit = None

    #-----------------------------------------------------------------------------
    def startFactory(self):
        # Init global contexts
        NeoContext.init(factory = self)

        # Init Wit
        self.wit = Wit(configuration['wit_token'])

    #-----------------------------------------------------------------------------
    def buildProtocol(self, addr):
        # Create protocol
        return LisaProtocol()

    #-----------------------------------------------------------------------------
    def stopFactory(self):
        # Clean
        if ClientFactory.__instance is not None:
            ClientFactory.__instance.clients = {}
            ClientFactory.__instance.zones = {}
            ClientFactory.__instance.wit = None
            ClientFactory.__instance = None

        # Clear global contexts
        NeoContext.deinit()

    #-----------------------------------------------------------------------------
    @classmethod
    def initClient(cls, client_name, zone_name):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()
        self = cls.__instance

        # Lock access
        self._lock.acquire()

        # Get zone
        zone_uid = cls.getOrCreateZone(zone_name)

        # Search if we already had a connection with this client
        client = None
        client_uid = None
        for c in self.clients:
            if self.clients[c]['name'] == client_name and self.clients[c]['zone'] == zone_name:
                return self.clients[c]

        # If not found
        if client is None:
            # Add client
            client_uid = str(uuid.uuid1())
            self.clients[client_uid] = {'uid': client_uid, 'protocols': {}, 'name': client_name, 'zone': zone_name, 'zone_uid': zone_uid}
            client = self.clients[client_uid]

            # Each client has its own context
            client['context'] = NeoContext(client_uid = client_uid)

            # Add client to zone
            found_flag = False
            for c in self.zones[zone_uid]['client_uids']:
                if c == client['uid']:
                    found_flag = True
                    break
            if found_flag == False:
                self.zones[zone_uid]['client_uids'].append(client['uid'])

        # Release access
        self._lock.release()

        return client

    #-----------------------------------------------------------------------------
    @classmethod
    def parseChat(cls, jsonData, client_uid):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()
        self = cls.__instance

        # If input has already a decoded intent
        if jsonData.has_key("outcome") == True:
            jsonInput = {}
            jsonInput['outcome'] = jsonData['outcome']
        elif len(jsonData['body']) > 0:
            # Ask Wit for intent decoding
            jsonInput = self.wit.get_message(unicode(jsonData['body']))
        else:
            # No input => no output
            return

        # Initialize output from input
        jsonInput['from'], jsonInput['type'], jsonInput['zone'] = jsonData['from'], jsonData['type'], jsonData['zone']

        # Show wit result
        if configuration['debug']['debug_wit']:
            log.msg("WIT: " + str(jsonInput['outcome']))

        # Execute intent
        client = cls.getClient(client_uid)
        intent = PluginManager.getIntent(intent_name = jsonInput['outcome']['intent'])
        if intent is not None:
            # Call plugin
            client['context'].parse(jsonInput = jsonInput, plugin_name = intent.plugin_name, method_name = intent.method_name)
        else:
            # Parse without intent
            client['context'].parse(jsonInput = jsonInput)

    #-----------------------------------------------------------------------------
    @classmethod
    def getClient(cls, client_uid):
        # Get singleton
        if cls.__instance is None:
            return None

        # Return client
        return cls.__instance.clients[client_uid]

    #-----------------------------------------------------------------------------
    @classmethod
    def getOrCreateZone(cls, zone_name):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        # All zones
        if zone_name == "all":
            return "all"

        # Lock access
        cls.__instance._lock.acquire()

        # Search zone
        zone = None
        zone_uid = None
        for z in cls.__instance.zones:
            if cls.__instance.zones[z]['name'] == zone_name:
                zone = cls.__instance.zones[z]
                zone_uid = z
                break

        # If not found
        if zone is None:
            # Create zone
            zone_uid = str(uuid.uuid1())
            cls.__instance.zones[zone_uid] = {'name': zone_name, 'client_uids': []}
            zone = cls.__instance.zones[zone_uid]

        # Release access
        cls.__instance._lock.release()

        return zone_uid

    #-----------------------------------------------------------------------------
    @classmethod
    def sendToClients(cls, jsonData, client_uids = [], zone_uids = []):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        # Parse clients
        for c in cls.__instance.clients:
            # If client is in destination
            if "all" in zone_uids or cls.__instance.clients[c]['zone_uid'] in zone_uids or "all" in client_uids or c in client_uids:
                # Parse client protocols
                for p in cls.__instance.clients[c]['protocols']:
                    # Send to client through protocol
                    cls.__instance.clients[c]['protocols'][p]['object'].sendToClient(jsonData)

    #-----------------------------------------------------------------------------
    @classmethod
    def LisaReload(cls):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        log.msg("Reloading engine")
        cls.__instance.build_activeplugins()

    #-----------------------------------------------------------------------------
    @classmethod
    def SchedReload(cls):
        global taskman
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()

        log.msg("Reloading task scheduler")
        cls.__instance.taskman = taskman
        return cls.__instance.taskman.reload()

    #-----------------------------------------------------------------------------
    @classmethod
    def get(cls):
        # Create singleton
        if cls.__instance is None:
            cls.__instance = ClientFactory()
        return cls.__instance
Example #8
0
class Recorder(threading.Thread):
    """
    Continuous recording class.
    """

    #-----------------------------------------------------------------------------
    def __init__(self, factory):
        # Init thread class
        threading.Thread.__init__(self)
        self._stopevent = threading.Event()
        self.running_state = False
        self.rec_sink = None

        self.factory = factory
        self.configuration = ConfigManager.getConfiguration()
        self.wit = Wit(self.configuration['wit_token'])
        self.wit_context = None
        self.record = {'activated' : False, 'start' : 0, 'started' : False, 'end' : 0, 'ended' : False, 'buffers' : deque({})}
        self.continuous_mode = False
        self.temp_file = "/tmp/asr_sound"

        # Start thread
        threading.Thread.start(self)

    #-----------------------------------------------------------------------------
    def setRunningState(self, state, rec_sink = None):
        self.running_state = state
        # Get app sink
        if rec_sink is not None and self.rec_sink is not rec_sink:
            self.rec_sink = rec_sink
            self.rec_sink.connect('new-buffer', self._captureAudioBuffer)

    #-----------------------------------------------------------------------------
    def stop(self):
        # Raise stop event
        self._stopevent.set()

    #-----------------------------------------------------------------------------
    def activate(self):
        """
        Called to activate current utter as a record
        """
        # Activate record
        if self.record['started'] == True:
            self.record['activated'] = True

    #-----------------------------------------------------------------------------
    def setContinuousMode(self, enabled, wit_context = None):
        """
        Called to activate continous record mode
        """
        # Activate record
        self.continuous_mode = enabled
        self.wit_context = wit_context

    #-----------------------------------------------------------------------------
    def run(self):
        """
        Recorder main loop
        """
        # Encoded format
        CONTENT_TYPE = 'audio/mpeg3'

        # Thread loop
        while not self._stopevent.isSet():
            # Test if record is ended
            if self.record['started'] == True and self.record['ended'] == False and self.record['end'] <= time():
                # If current record was not activated before end
                if self.record['activated'] == False and self.continuous_mode == False:
                    self.record['start'] = 0
                    self.record['started'] = False
                    self.record['end'] = 0
                    self.record['ended'] = False
                    self.record['activated'] = False

                    continue

                # Current record is activated and already ended
                self.record['ended'] = True

            # If current record is not activated
            if self.running_state == False or self.record['started'] == False or (self.record['activated'] == False and self.continuous_mode == False):
                sleep(.1)
                continue

            # Send activated record to Wit
            wit_e = None
            result = ""
            try:
                if self.configuration['asr'] == "ispeech":
                    for b in self._readAudioBuffer(file_mode = True):
                        pass
                    params= {}
                    params["action"] = "recognize"
                    params["apikey"] = "developerdemokeydeveloperdemokey"
                    params["freeform"] = "3"
                    params["locale"] = "fr-FR"
                    params["output"] = "json"
                    params["content-type"] = "speex"
                    params["speexmode"] = "2"
                    params["audio"] = base64.b64encode(open(self.temp_file, 'rt').read()).replace(b'\n',b'')
                    result = requests.get("http://api.ispeech.org/api/rest?" + urlencode(params))
                    result = self.wit.get_message(query = result.json()['text'], context = self.wit_context)

                elif self.configuration['asr'] == "google":
                    for b in self._readAudioBuffer(file_mode = True):
                        pass
                    url = 'https://www.google.com/speech-api/v2/recognize?output=json&lang=fr-fr&key=AIzaSyCQv4U1mTaw_r_j1bWb1peeaTihzV0q-EQ'
                    audio = open(self.temp_file, "rb").read()
                    header = {"Content-Type": "audio/x-flac; rate=16000"}
                    post = urlopen(Request(url, audio, header))
                    result = loads(post.read().split("\n")[1])['result'][0]['alternative'][0]['transcript']
                    result = self.wit.get_message(query = result, context = self.wit_context)

                # Defautl Wit
                else:
                    result = self.wit.post_speech(data = self._readAudioBuffer(), content_type = CONTENT_TYPE, context = self.wit_context)

                result['msg_body'] = result['msg_body'].encode("utf-8")
            except Exception as e:
                wit_e = e

            # If record was stopped during Wit access
            if self._stopevent.isSet():
                break

            # Question mode
            if len(result) > 0 and self.continuous_mode == True and result.has_key('msg_body') == True and len(result['msg_body']) > 0:
                # Send answer
                self.factory.sendChatToServer(message = result['msg_body'], outcome = result['outcome'])
            # If Wit did not succeeded
            elif len(result) == 0 or result.has_key('outcome') == False or result['outcome'].has_key('confidence') == False or result['outcome']['confidence'] < self.configuration['wit_confidence']:
                if wit_e is not None:
                    log.err("Wit exception : {0}".format(str(e)))
                elif len(result) == 0:
                    log.err("No response from Wit")
                elif result.has_key('outcome') == False or result['outcome'].has_key('confidence') == False:
                    log.err("Wit response syntax error")
                    log.err("result : {0}".format(result))
                elif result['outcome']['confidence'] < self.configuration['wit_confidence']:
                    log.err("Wit confidence {confidence} too low : {result}".format(confidence = result['outcome']['confidence'], result = result['msg_body']))
                else:
                    log.err("Error response from Wit : {0}".format(result['msg_body']))

            # Send recognized intent to the server
            else:
                log.msg("Wit result : {0}".format(result['msg_body']))
                self.factory.sendChatToServer(message = result['msg_body'], outcome = result['outcome'])

            # Finish current record
            self.record['start'] = 0
            self.record['started'] = False
            self.record['end'] = 0
            self.record['ended'] = False
            self.record['activated'] = False
            self.record['buffers'].clear()

    #-----------------------------------------------------------------------------
    def vaderStart(self):
        """
        Vader start detection
        """
        # If record is running
        if self.record['ended'] == False:
            # New start
            if self.record['started'] == False:
                self.record['started'] = True
                self.record['start'] = time()

            # Reset max recording time
            self.record['end'] = self.record['start'] + MAX_RECORD_DURATION_s

    #-----------------------------------------------------------------------------
    def vaderStop(self):
        """
        Vader stop detection
        """
        # If record is running
        if self.record['ended'] == False and self.record['end'] > time() + MAX_SILENCE_s:
            # End recording when no new activity during next silence
            self.record['end'] = time() + MAX_SILENCE_s

    #-----------------------------------------------------------------------------
    def _captureAudioBuffer(self, app):
        """
        Gstreamer pipeline callback : Audio buffer capture
        """
        # Get buffer
        buf = self.rec_sink.emit('pull-buffer')

        # If record is running
        if self.record['started'] == True and self.record['ended'] == False:
            cur_time = time()

            # Add buffer to queue
            self.record['buffers'].append({'time' : cur_time, 'data' : buf})

            # Delete too old buffers when utter is not activated
            if self.record['activated'] == False and self.continuous_mode == False:
                while self.record['buffers'][0]['time'] + MAX_TIME_BEFORE_KWS_s < cur_time:
                    self.record['buffers'].popleft()

    #-----------------------------------------------------------------------------
    def _readAudioBuffer(self, file_mode = False):
        """
        Read buffers from capture queue
        """
        # Check current record
        if self.record['activated'] == False and self.continuous_mode == False:
            return

        f = None
        if file_mode == True:
            f = open(self.temp_file, "wb")

        # While recording is running
        log.msg("Wit send start")
        while not self._stopevent.isSet():
            # Test if record is ended
            if self.record['ended'] == False and self.record['end'] <= time():
                self.record['ended'] = True

            # If there is no captured buffer
            if len(self.record['buffers']) == 0:
                # No more buffer when record is ended, it's over
                if self.record['ended'] == True:
                    break

                # Record is live, wait another buffer
                sleep(.05)
                continue

            # Read buffer
            buf = None
            while len(self.record['buffers']) > 0 and (buf is None or len(buf) + len(self.record['buffers'][0]['data']) < 1200):
                data = self.record['buffers'].popleft()
                if buf is None:
                    buf = data['data']
                else:
                    buf = buf.merge(data['data'])
            if file_mode == True:
                f.write(buf)
            yield buf

        if file_mode == True:
            f.close()
        log.msg("Wit send end")
Example #9
0
def clarify_image_2(url):
    item_list = {
        'batteries': 0,
        'electronics': 0,
        'clothes': 0,
        'plastics': 0,
        'glass': 0,
        'makeup': 0,
        'aluminum cans': 0,
        'paper': 0,
        'hazardous waste': 0
    }

    # Pywit Initialization
    client = Wit(WIT_API_TOKEN)

    # Google API Initialization
    payload = {
        "requests": [{
            "features": [{
                "type": "LABEL_DETECTION"
            }],
            "image": {
                "source": {
                    "imageUri": url
                }
            }
        }]
    }
    r = requests.post(
        'https://vision.googleapis.com/v1/images:annotate?key=AIzaSyChIiHYrv5zRMN7lc7Xr_hYPbD6Gr_0VXk',
        json=payload,
        verify=False)
    response = json.loads(r.text)

    concepts = response["responses"][0]["labelAnnotations"]

    # print(str(concepts))
    for concept in concepts:
        resp = client.get_message(concept['description'])
        # print(concept['description'], concept['score'])

        # We only consider the value of clarification over 0.9
        if concept['score'] > 0.7 and len(
                resp['outcomes'][0]['entities']) != 0:
            # print(str(resp))
            for item in item_list:
                # We only consider the value of confidence over 0.75
                if resp['outcomes'][0]['entities']['item'][0][
                        'value'] == item and resp['outcomes'][0]['entities'][
                            'item'][0]['confidence'] > 0.75:
                    item_list[item] += 1

    # Return the highest frequency category
    result = ""
    max_count = 0
    max_value = 0
    for item in item_list.keys():
        if item_list[item] > max_count:
            result = item
            max_value = concept['score']
        # When the max_count is equal and the 'value' is bigger, we replace the result
        elif item_list[item] == max_count:
            for concept in concepts:
                if concept['description'] == item and concept[
                        'score'] > max_value:
                    result = item
                    max_value = concept['score']

    # Return type is string. If no matches, returns ""
    if result == "":
        return final_response_handler(listOfAnswers=[
            "We're having trouble identifying what you are trying to recycle."
        ])

    return final_response_handler(
        listOfAnswers=['\n'.join(categories_dict[result])])
Example #10
0
def witAI(text, city, state, country, zipCode):
	response = Wit(WIT_AI_SERVER_TOKEN)
	return dict(response.get_message(text))