class AppidQueue(object): queue = Queue.Queue() profile = cmInstance.getConfig('voice_engines') if 'accounts' in profile.iflytek: accounts = profile.iflytek.accounts for account in accounts: queue.put_nowait(str(account.appid)) # while len(accounts) > 1: # account = random.choice(accounts) # queue.put_nowait(str(account.appid)) # accounts.remove(account) @staticmethod def put(appid): _logger.debug("put %s back into appid pool" % appid) AppidQueue.queue.put(appid) @staticmethod def get(): while True: if AppidQueue.queue.not_empty: appid = AppidQueue.queue.get() _logger.debug("get %s from appid pool" % appid) return appid else: time.sleep(0.1)
def query(self, texts, session=None): """ Passes user input to the appropriate module, testing it against each candidate module's isValid function. Arguments: text -- user input, typically speech, to be parsed by a module session -- to separate different user conversation session """ answer = None control = None if cmInstance.hasConfig('ai_engine'): ai_engine_slug = cmInstance.getConfig('ai_engine') try: ai_engine_class = ai.get_engine_by_slug(ai_engine_slug) answer, control = ai_engine_class.get_instance().chat( texts, session) except ValueError: pass if control: self._logger.info("Get Conversation control %s " % json.dumps(control).decode('unicode-escape')) control = MappingDict(control) platform = control.platform.lower() controller_class = hand.get_controller_by_slug(str(platform)) controlanswer = controller_class.get_instance().handleControl( session, answer, control) if controlanswer: return controlanswer #Get the answer from AI engine if answer: return {"answer": answer} else: # go through all available module to handle the original text for module in self.modules: if module.isValid(texts): self._logger.debug( "'%s' is a valid phrase for module " + "'%s'", texts, module.__name__) try: self.handling = True answer = module.handle(texts) self.handling = False except Exception: self._logger.error('Failed to execute module', exc_info=True) answer = _( "I'm sorry. I had some trouble with that operation. Please try again later." ) else: self._logger.debug( "Handling of phrase '%s' by " + "module '%s' completed", texts, module.__name__) finally: return {"answer": answer} self._logger.debug( "No module was able to handle any of these " + "phrases: %r", texts)
def get_config(cls): config = {} profile = cmInstance.getConfig('ai_engines') if 'tuling' in profile: if 'APIkey' in profile.tuling: config['APIkey'] = profile.tuling.APIkey if 'secret' in profile.tuling: config['secret'] = profile.tuling.secret return config
def get_config(cls): conf = {'fst_model': os.path.join(configuration.CONFIG_PATH, 'g014b2b', 'g014b2b.fst')} profile = cmInstance.getConfig('voice_engines') if 'pocketsphinx' in profile: if 'fst_model' in profile['pocketsphinx']: conf['fst_model'] = \ profile['pocketsphinx']['fst_model'] if 'nbest' in profile['pocketsphinx']: conf['nbest'] = int(profile['pocketsphinx']['nbest']) return conf
def get_config(cls): # FIXME: Replace this as soon as we have a config module config = {} profile = cmInstance.getConfig('voice_engines') if 'baidu_yuyin' in profile: if 'api_key' in profile.baidu_yuyin: config['api_key'] = profile.baidu_yuyin.api_key if 'secret_key' in profile.baidu_yuyin: config['secret_key'] = profile.baidu_yuyin.secret_key return config
def init_iflytck_sdk(appid): profile = cmInstance.getConfig('voice_engines') ostype = "x64" if 'ostype' in profile.iflytek: ostype = profile.iflytek.ostype sofile = configuration.config("iflytek_msc", appid, "libs", ostype, "libmsc.so") _logger.info("Loading iflytec msc libaray file: %s" % sofile) sdk = cdll.LoadLibrary(sofile) #sdk:speech synthesis return sdk
def get_config(cls): config = {} profile = cmInstance.getConfig('voice_engines') if 'aliyun' in profile: if 'ak_id' in profile.aliyun: config['ak_id'] = profile.aliyun.ak_id if 'ak_secret' in profile.aliyun: config['ak_secret'] = profile.aliyun.ak_secret if 'voice_name' in profile.aliyun: config['voice_name'] = profile.aliyun.voice_name return config
def get_config(cls): # FIXME: Replace this as soon as we have a config module config = {} # HMM dir # Try to get hmm_dir from config profile = cmInstance.getConfig('voice_engines') try: config['hmm_dir'] = profile['pocketsphinx']['hmm_dir'] except KeyError: pass return config
def get_config(cls): config = {} profile = cmInstance.getConfig('voice_engines') if 'iflytek' in profile: # if 'accounts' in profile.iflytek: # account = random.choice(profile.iflytek.accounts) # config['appid'] = account.appid if 'voice_name' in profile.iflytek: config['voice_name'] = profile.iflytek.voice_name else: config['voice_name'] = "yanping" return config
def get_config(cls): config = {} profile = cmInstance.getConfig('voice_engines') if 'iflytek' in profile: if 'ostype' in profile.iflytek: config['ostype'] = profile.iflytek.ostype else: config['ostype'] = "x64" if 'accounts' in profile.iflytek: account = random.choice(profile.iflytek.accounts) config['appid'] = account.appid return config
def translation(text, mode): richway = cmInstance.getConfig('ai_engines', 'richway') if 'translation_url' in richway: translation_url = richway.translation_url else: return text body = {"text": text, "type": mode} data = utils.restpost(translation_url, body) result = data["result"] html_parser = HTMLParser.HTMLParser() translatedTxt = html_parser.unescape(result) return translatedTxt
def get_config(cls): config = {} profile = cmInstance.getConfig('ai_engines') if 'richway' in profile: if 'service_url' in profile.richway: config['service_url'] = profile.richway.service_url if 'robotId' in profile.richway: config['robotId'] = profile.richway.robotId if 'sessionId' in profile.richway: config['sessionId'] = profile.richway.sessionId if 'unknown_reply' in profile.richway: config['unknown_reply'] = profile.richway.unknown_reply return config
def handleForever(self): """ Delegates user input to the handling function when activated. """ self._logger.info("Starting to handle conversation with keyword '%s'.", self.persona) utils.subscribe(const.TOPIC_PASSIVE_LISTEN, self.wakeup) #树莓派不使用被动听 if not utils.isPi(): thread = threading.Thread(target=self.listenKeyword) thread.setDaemon(True) thread.start() while True: self.event.wait() answer, audio_cache = self.listenAndThink(self.threshold) self.mic.Pixels.speak() self._logger.info("Answer: %s" % answer) max_length = cmInstance.getConfig('max_length') unicodetext = unicode(str(answer), "utf-8") if len(unicodetext) > max_length: mouth.say('文字内容太多,是否需要全部读出,请在滴一声后进行确认') input = self.mic.activeListen() if input and any(word in input for word in ["确认", "可以", "好的", "是", "OK"]): self.mic.Pixels.speak() if audio_cache: self._logger.info( "Cached audio file %s exist, play it without synthesize." % audio_cache) mouth.play(audio_cache) else: mouth.say(answer) else: if audio_cache: self._logger.info( "Cached audio file %s exist, play it without synthesize." % audio_cache) mouth.play(audio_cache) else: mouth.say(answer) self.mic.Pixels.off() self._logger.info("wake up event clear") self.event.clear()
def handle(text): """ Reports the current time based on the user's timezone. Arguments: text -- user-input, typically transcribed speech mic -- used to interact with the user (for both input and output) profile -- contains information related to the user (e.g., phone number) """ tz = utils.getTimezone(cmInstance.getConfig("timezone")) now = datetime.datetime.now(tz=tz) service = DateService() response = service.convertTime(now) return _("Now it is %s") % response
def get_config(cls): # FIXME: Replace this as soon as we have a config module config = {} # Try to get snowboy config from config profile = cmInstance.getConfig('voice_engines') if 'snowboy' in profile: if 'model' in profile['snowboy']: config['model'] = \ profile['snowboy']['model'] else: config['model'] = os.path.join(configuration.LIB_PATH, 'snowboy/hey_watson.umdl') if 'sensitivity' in profile['snowboy']: config['sensitivity'] = \ profile['snowboy']['sensitivity'] else: config['sensitivity'] = "0.5" if 'robot_name' in profile: config['hotword'] = profile['robot_name'] else: config['hotword'] = 'HEYWATSON' return config
def is_available(cls): profile = cmInstance.getConfig('ai_engines') if 'richway' in profile: return True else: return False
def get_default_engine_slug(): return 'tuling-ai' if not cmInstance.hasConfig( "default_ai_engine") else cmInstance.getConfig("default_ai_engine")
""" The Mic class handles all interactions with the Respeaker 2 microphone array Repuirements: sudo pip install spidev """ import sys from robot import configuration from robot.configuration import cmInstance from robot.ear.BaseMic import BaseMic sys.path.append(configuration.tools("respeaker")) if cmInstance.getConfig("active_mic") == "respeaker-2mic": class ReSpeaker2ArrayMic(BaseMic): SLUG = "respeaker-2mic" class Pixels: from pixels import Pixels pixels = Pixels() thinking = False @classmethod def wakeup(cls): cls.pixels.wakeup() @classmethod def think(cls): cls.pixels.think()