Example #1
0
 def __init__(self,
              domains: List[Domain],
              greet_on_first_turn: bool = False):
     Service.__init__(self, domain="")
     self.domains = domains
     self.current_domain = None
     self.greet_on_first_turn = greet_on_first_turn
Example #2
0
    def __init__(self,
                 domain="",
                 camera_id: int = 0,
                 openface_port: int = 6004,
                 delay: int = 2,
                 identifier=None):
        """
        Args:
            camera_id: index of the camera you want to use (if you only have one camera: 0)
        """
        Service.__init__(self, domain="", identifier=identifier)
        self.camera_id = camera_id
        self.openface_port = openface_port
        self.openface_running = False
        self.threshold = delay  # provide number of seconds as parameter, one second = 15 frames

        ctx = Context.instance()
        self.openface_endpoint = ctx.socket(zmq.PAIR)
        self.openface_endpoint.bind(f"tcp://127.0.0.1:{self.openface_port}")

        startExtraction = f"{os.path.join(get_root_dir(), 'tools/OpenFace/build/bin/FaceLandmarkVidZMQ')} -device {self.camera_id} -port 6004"  # todo config open face port
        self.p_openface = subprocess.Popen(
            startExtraction.split(), stdout=subprocess.PIPE)  # start OpenFace
        self.extracting = False
        self.extractor_thread = None
 def __init__(self):
     Service.__init__(self)
     self.speech_in_dir = os.path.dirname(os.path.abspath(__file__)) + '/'
     self.trained_model_path = os.path.join(
         'resources', 'models',
         'backchannel') + '/pytorch_acoustic_backchanneller.pt'
     self.load_model()
Example #4
0
 def __init__(self,
              domain: Union[str, Domain] = "",
              conversation_log_dir: str = None,
              enable_plotting: bool = False,
              threshold: int = 8000,
              voice_privacy: bool = False,
              identifier: str = None) -> None:
     """
     A service that can record a microphone upon a key pressing event 
     and publish the result as an array. The end of the utterance is 
     detected automatically, also the voice can be masked to alleviate 
     privacy issues.
     
     Args:
         domain (Domain): I don't know why this is here. Service needs it, but it means nothing in this context.
         conversation_log_dir (string): If this parameter is given, log files of the conversation will be created in this directory
         enable_plotting (boolean): If this is set to True, the recorder is no longer real time able and thus the recordings don't work properly. This is just to be used to tune the threshold for the end of utterance detection, not during deployment.
         threshold (int): The threshold below which the assumption of the end of utterance detection is silence
         voice_privacy (boolean): Whether or not to enable the masking of the users voice
         identifier (string): I don't know why this is here. Service needs it.
     """
     Service.__init__(self, domain=domain, identifier=identifier)
     self.conversation_log_dir = conversation_log_dir
     self.recording_indicator = False
     self.audio_interface = pyaudio.PyAudio()
     self.push_to_talk_listener = keyboard.Listener(
         on_press=self.start_recording)
     self.threshold = threshold
     self.enable_plotting = enable_plotting
     self.voice_privacy = voice_privacy
Example #5
0
    def __init__(self,
                 domain: Domain = "",
                 identifier=None,
                 conversation_log_dir: str = None,
                 use_cuda=False):
        """
        Transforms spoken input from the user to text for further processing.

        Args:
            domain (Domain): Needed for Service, but has no meaning here
            identifier (string): Needed for Service
            conversation_log_dir (string): If this is provided, logfiles will be placed by this Service into the specified directory.
            use_cuda (boolean): Whether or not to run the computations on a GPU
        """
        Service.__init__(self, domain=domain, identifier=identifier)
        self.conversation_log_dir = conversation_log_dir

        # load model
        model_dir = os.path.join(get_root_dir(), "resources", "models",
                                 "speech", "multi_en_20190916")
        self.model, conf = load_trained_model(
            os.path.join(model_dir, "model.bin"))
        self.vocab = conf.char_list

        # setup beam search
        self.bs = BeamSearch(scorers=self.model.scorers(),
                             weights={
                                 "decoder": 1.0,
                                 "ctc": 0.0
                             },
                             sos=self.model.sos,
                             eos=self.model.eos,
                             beam_size=4,
                             vocab_size=len(self.vocab),
                             pre_beam_score_key="decoder")

        self.bs.__class__ = BatchBeamSearch

        # choose hardware to run on
        if use_cuda:
            self.device = "cuda"
        else:
            self.device = "cpu"

        self.model.to(self.device)
        self.bs.to(self.device)

        # change from training mode to eval mode
        self.model.eval()
        self.bs.eval()

        # scale and offset for feature normalization
        # follows https://github.com/kaldi-asr/kaldi/blob/33255ed224500f55c8387f1e4fa40e08b73ff48a/src/transform/cmvn.cc#L92-L111
        norm = torch.load(os.path.join(model_dir, "cmvn.bin"))
        count = norm[0][-1]
        mean = norm[0][:-1] / count
        var = (norm[1][:-1] / count) - mean * mean
        self.scale = 1.0 / torch.sqrt(var)
        self.offset = -(mean * self.scale)
Example #6
0
    def __init__(self, domain: Domain = ""):
        """
        Given a sound, this service extracts features and passes them on to the decoder for ASR

        Args:
            domain (Domain): Needed for Service, no meaning here
        """
        Service.__init__(self, domain=domain)
Example #7
0
def change_ip(password):
    logger = logging.getLogger('change_ip')
    try:
        logger.debug(f"wait {Service.wait_new_ip} seconds for new ip")
        time.sleep(Service.wait_new_ip)
        Service.renew_connection(password)
        logger.debug("get new ip")
    except Exception as exp:
        raise exp
Example #8
0
    def post(self):
        # curl -d name="Conjured Mana Cake" -d sell_in=3 -d quality=6
        # http://127.0.0.1:5000/items -X POST
        args = self.parseRequest()

        # 201 response: request has succeeded and
        # a new resource has been created as a result.
        Service.postItem(args)
        return '', 201
Example #9
0
    def __init__(self):
        """ Emotion recognition module.

        On initialization all necessary models are loaded.
        """
        Service.__init__(self)
        self.emotion_dir = os.path.dirname(os.path.abspath(__file__))
        self.model_path = os.path.abspath(
            os.path.join(
                self.emotion_dir, "..", "..", "resources", "models", "emotion"
            )
        )

        def load_args(emo_representation):
            arg_dict = pickle.load(
                open(os.path.join(
                    self.model_path, f'{emo_representation}_args.pkl'),
                     'rb')
            )
            return arg_dict

        def load_model(emo_representation, arg_dict):
            ARGS = arg_dict['args']
            model = cnn(
                kernel_size=(ARGS.height, arg_dict['D_in']),
                D_out=arg_dict['D_out'],
                args=ARGS
            )
            model.load_state_dict(
                torch.load(
                    os.path.join(self.model_path,
                                 f'{emo_representation}_model_params.pt'),
                    map_location=torch.device('cpu')
                )
            )
            model.eval()
            return model

        self.emo_representations = ['category', 'arousal', 'valence']
        self.models = {}
        self.args = {}
        for emo_representation in self.emo_representations:
            self.args[emo_representation] = load_args(emo_representation)
            self.models[emo_representation] = load_model(
                emo_representation,
                self.args[emo_representation]
            )
        self.arousal_mapping = {0: 'low', 1: 'medium', 2: 'high'}
        self.valence_mapping = {0: 'negative', 1: 'neutral', 2: 'positive'}
        self.category_mapping = {
            0: EmotionType.Angry,
            1: EmotionType.Happy,
            2: EmotionType.Neutral,
            3: EmotionType.Sad
        }
Example #10
0
 def __init__(self,
              domain: JSONLookupDomain = None,
              logger: DiasysLogger = None,
              random: bool = True,
              static_emotion: EmotionType = EmotionType.Neutral,
              static_engagement: EngagementType = EngagementType.High):
     Service.__init__(self, domain=domain)
     self.domain = domain
     self.logger = logger
     self.random = random
     self.engagement = static_engagement
     self.emotion = static_emotion
Example #11
0
    def httpOpen(self, filename, mode):
        self.manager.log.debug('httpOpen: ' + filename + ' ' + hex(mode))

        if not filename:
            self.manager.log.error('DLNA:HTTPOPEN:filename is None')
            return None

        if mode is not UpnpOpenFileMode.UPNP_READ:
            self.manager.log.error(
                'DLNA:HTTPOPEN:mode is not UpnpOpenFileMode.upnp_read')
            return None

        # We need to remove the web handle part of the filename. So for example the
        # filename that was passed in could be '/web/cds.xml'. We need to make that
        # be just 'cds.xml'. If the file that was requested is '/web/movies/awesome.mp4'
        # this should be made into 'movies/awesome.mp4'.
        filename = filename.lstrip(self.manager.webRoot)

        _file = None

        if filename == CDSService.CDS_LOCATION:
            _file = Service.getFileMemory(
                self.manager.webRoot + CDSService.CDS_LOCATION,
                CDSService.CDS_DESCRIPTION)
        elif filename == CMSService.CMS_LOCATION:
            _file = Service.getFileMemory(
                self.manager.webRoot + CMSService.CMS_LOCATION,
                CMSService.CMS_DESCRIPTION)
        elif filename == MSRService.MSR_LOCATION:
            _file = Service.getFileMemory(
                self.manager.webRoot + MSRService.MSR_LOCATION,
                MSRService.MSR_DESCRIPTION)
        else:
            upnpId = int(filename[filename.rfind('/') + 1:filename.rfind('.')])
            serverManager = OneServerManager()
            entry = serverManager.rootEntry.getChild(upnpId)
            if entry == None:
                self.manager.log.error('DLNA:HTTPOPEN:Entry is None')
                return None

            if entry.fullPath == None:
                self.manager.log.error('DLNA:HTTPOPEN:Entry.fullPath is None')
                return None

            fh = entry.generateFileHandle()
            _file = WebFile(entry.fullPath, 0, fh, entry)

        # Find a GUID for a handle and add it to the map.
        handle = uuid.uuid4().int >> 64
        DLNAService.fileHandles[handle] = _file

        return handle
Example #12
0
    def __init__(self,
                 domain: JSONLookupDomain = None,
                 logger: DiasysLogger = DiasysLogger()):
        """
        Initializes the policy

        Arguments:
            domain (JSONLookupDomain): the domain that the affective policy should operate in

        """
        self.first_turn = True
        Service.__init__(self, domain=domain)
        self.logger = logger
Example #13
0
    def __init__(self, domain: Domain, sub_topic_domains={}, template_file: str = None,
                 logger: DiasysLogger = DiasysLogger(), template_file_german: str = None,
                 emotions: List[str] = [], debug_logger = None):
        """Constructor mainly extracts methods and rules from the template file"""
        Service.__init__(self, domain=domain, sub_topic_domains=sub_topic_domains, debug_logger=debug_logger)

        self.domain = domain
        self.template_filename = template_file
        self.templates = {}
        self.logger = logger
        self.emotions = emotions

        self._initialise_templates()
Example #14
0
 def __init__(self, domain: Domain = ""):
     Service.__init__(self, domain=domain)
     self.module_dir = os.path.dirname(os.path.abspath(__file__))
     # # CLAHE (Contrast Limited Adaptive Histogram Equalization)
     self.CLAHE = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
     # for detecting faces (returns coordinates of rectangle(s) of face area(s))
     self.DETECTOR = dlib.get_frontal_face_detector()
     # facial landmark predictor
     predictor_file = os.path.abspath(
         os.path.join(self.module_dir, '..', '..', '..', 'resources',
                      'models', 'video',
                      'shape_predictor_68_face_landmarks.dat'))
     self.PREDICTOR = dlib.shape_predictor(predictor_file)
Example #15
0
 def __init__(self,
              domain: Domain = "",
              conversation_log_dir: str = None,
              identifier: str = None):
     """
     Service that plays the system utterance as sound
     
     Args:
         domain (Domain): Needed for Service, but has no meaning here
         conversation_log_dir (string): If this is provided it will create log files in the specified directory.
         identifier (string): Needed for Service.
     """
     Service.__init__(self, domain=domain, identifier=identifier)
     self.conversation_log_dir = conversation_log_dir
     self.interaction_count = 0
Example #16
0
	def httpOpen(self, filename, mode):
		self.manager.log.debug('httpOpen: ' + filename + ' ' + hex(mode))

		if not filename:
			self.manager.log.error('DLNA:HTTPOPEN:filename is None')
			return None

		if mode is not UpnpOpenFileMode.UPNP_READ:
			self.manager.log.error('DLNA:HTTPOPEN:mode is not UpnpOpenFileMode.upnp_read')
			return None

		# We need to remove the web handle part of the filename. So for example the
		# filename that was passed in could be '/web/cds.xml'. We need to make that
		# be just 'cds.xml'. If the file that was requested is '/web/movies/awesome.mp4'
		# this should be made into 'movies/awesome.mp4'.
		filename = filename.lstrip(self.manager.webRoot)

		_file = None

		if filename == CDSService.CDS_LOCATION:
			_file = Service.getFileMemory(self.manager.webRoot + CDSService.CDS_LOCATION,
							  CDSService.CDS_DESCRIPTION)
		elif filename == CMSService.CMS_LOCATION:
			_file = Service.getFileMemory(self.manager.webRoot + CMSService.CMS_LOCATION,
							  CMSService.CMS_DESCRIPTION)
		elif filename == MSRService.MSR_LOCATION:
			_file = Service.getFileMemory(self.manager.webRoot + MSRService.MSR_LOCATION,
							  MSRService.MSR_DESCRIPTION)
		else:
			upnpId = int( filename[filename.rfind('/') + 1:filename.rfind('.')])
			serverManager = OneServerManager()
			entry = serverManager.rootEntry.getChild(upnpId)
			if entry == None:
				self.manager.log.error('DLNA:HTTPOPEN:Entry is None')
				return None

			if entry.fullPath == None:
				self.manager.log.error('DLNA:HTTPOPEN:Entry.fullPath is None')
				return None

			fh = entry.generateFileHandle()
			_file = WebFile(entry.fullPath, 0, fh, entry)

		# Find a GUID for a handle and add it to the map.
		handle = uuid.uuid4().int >> 64
		DLNAService.fileHandles[handle] = _file

		return handle
Example #17
0
 def __init__(self, domain=None, camera_id: int = 0, capture_interval: int = 10e5, identifier: str = None):
     """
     Args:
         camera_id (int): device id (if only 1 camera device is connected, id is 0, if two are connected choose between 0 and 1, ...)
         capture_interval (int): try to capture a frame every x microseconds - is a lower bound, no hard time guarantees (e.g. 5e5 -> every >= 0.5 seconds)
     """
     Service.__init__(self, domain, identifier=identifier)
     
     self.cap = cv2.VideoCapture(camera_id)  # get handle to camera device
     if not self.cap.isOpened():
         self.cap.open()                     # open
     
     self.terminating = Event()
     self.terminating.clear()
     self.capture_thread = Thread(target=self.capture) # create thread object for capturing
     self.capture_interval = capture_interval
Example #18
0
def add_numbers():

    a = request.args.get('a', 0, type=str)
    summarizer = Summarizer()
    # a = u"http://www.asriran.com/fa/news/332468/%D9%BE%DB%8C%D8%A7%D9%85-%D8%B3%DB%8C%D8%AF-%D9%85%D8%AD%D9%85%D8%AF-%D8%AE%D8%A7%D8%AA%D9%85%DB%8C-%D8%A8%D9%87-%DA%A9%D9%86%DA%AF%D8%B1%D9%87-%D9%8A%DA%A9-%D8%AD%D8%B2%D8%A8"
    page = Fetch(a)
    if page.service == 'iransamane':
        service = IranSamane(page.page)
    elif page.service == 'news-studio':
        service = NewsStudio(page.page)
    else:
        service = Service(page.page)

    print "fetching content"
    content = service.fetch_content()
    # print content
    print "end fetching"
    summarized = summarizer.summarize(content)
    print "end summarize"
    res = {
        'summarized': summarized,
        'title': service.fetch_title(),
        'tags': service.fetch_tags(),
        'image': service.fetch_image(),
    }

    # For log
    log = (datetime.datetime.now(), res['title'], content, pickle.dumps(summarized)) 
    query_db('INSERT INTO log (submitted_time, title, source, summarized_text) VALUES (?,?,?,?)', log)
    # 
    return jsonify(result=res)
Example #19
0
def admin_media(request):
    """ Middleware for Jinja template

    Args:
        request (Django request)

    Returns:
        DICT: Variable for jinja template
    """
    try:
        cluster = Cluster.objects.get()
        node = cluster.get_current_node()
        assert node
        node_name = node.name

    except (ObjectDoesNotExist, AssertionError):
        node_name = ""

    menu = (System().menu, Service().menu, Authentication().menu,
            Portal().menu, Darwin().menu, Apps().menu, Workflows().menu)
    results = {
        'MENUS': menu,
        'VERSION': settings.VERSION,
        'CURRENT_NODE': node_name,
        'DEV_MODE': settings.DEV_MODE,
        'TITLE': get_hostname(),
        'COLLAPSE': request.session.get('collapse')
    }

    return results
Example #20
0
    def __init__(self, domain: JSONLookupDomain, logger: DiasysLogger = DiasysLogger(),
                 max_turns: int = 25):
        """
        Initializes the policy

        Arguments:
            domain {domain.jsonlookupdomain.JSONLookupDomain} -- Domain

        """
        self.first_turn = True
        Service.__init__(self, domain=domain)
        self.current_suggestions = []  # list of current suggestions
        self.s_index = 0  # the index in current suggestions for the current system reccomendation
        self.domain_key = domain.get_primary_key()
        self.logger = logger
        self.max_turns = max_turns
Example #21
0
 def process_services(self, services_json):
     services = []
     for service in services_json.get("services", []):
         auth = service.get("auth")
         auth_service = None
         if auth:
             auth_service = self.get_auth_service(services_json, auth)
         services.append(Service(service, auth=auth_service))
     return services
Example #22
0
    def __init__(self, domain: LookupDomain, \
        logger: DiasysLogger = DiasysLogger(), device: str = 'cpu'):
        """Creates neural networks for semantic parsing and other required utils

        Args:
            domain: the QA domain
            logger: the logger
            device: PyTorch device name
        """
        Service.__init__(self, domain=domain, debug_logger=logger)
        self.device = torch.device(device)
        self.nn_relation = self._load_relation_model()
        self.nn_entity = self._load_entity_model()
        self.nn_direction = self._load_direction_model()

        self.tags = self._load_tag_set()

        self.max_seq_len = 40
        self.embedding_creator = BertEmbedding(max_seq_length=self.max_seq_len)
Example #23
0
    def __init__(self, domain: JSONLookupDomain, logger: DiasysLogger = DiasysLogger(),
                 language: Language = None):
        """
        Loads
            - domain key
            - informable slots
            - requestable slots
            - domain-independent regular expressions
            - domain-specific regualer espressions

        It sets the previous system act to None

        Args:
            domain {domain.jsonlookupdomain.JSONLookupDomain} -- Domain
        """
        Service.__init__(self, domain=domain)
        self.logger = logger

        self.language = language if language else Language.ENGLISH

        # Getting domain information
        self.domain_name = domain.get_domain_name()
        self.domain_key = domain.get_primary_key()

        # Getting lists of informable and requestable slots
        self.USER_INFORMABLE = domain.get_informable_slots()
        self.USER_REQUESTABLE = domain.get_requestable_slots()

        # Getting the relative path where regexes are stored
        self.base_folder = os.path.join(get_root_dir(), 'resources', 'nlu_regexes')

        # Setting previous system act to None to signal the first turn
        # self.prev_sys_act = None
        self.sys_act_info = {
            'last_act': None, 'lastInformedPrimKeyVal': None, 'lastRequestSlot': None}

        self.language = Language.ENGLISH
        self._initialize()
Example #24
0
    def __init__(self,
                 domain: Domain,
                 template_file: str = None,
                 sub_topic_domains: Dict[str, str] = {},
                 logger: DiasysLogger = DiasysLogger(),
                 template_file_german: str = None,
                 language: Language = None):
        """Constructor mainly extracts methods and rules from the template file"""
        Service.__init__(self,
                         domain=domain,
                         sub_topic_domains=sub_topic_domains)

        self.language = language if language else Language.ENGLISH
        self.template_english = template_file
        # TODO: at some point if we expand languages, maybe make kwargs? --LV
        self.template_german = template_file_german
        self.domain = domain
        self.template_filename = None
        self.templates = None
        self.logger = logger

        self.language = Language.ENGLISH
        self._initialise_language(self.language)
Example #25
0
 def delete(self):
     args = self.parseRequest()
     Service.deleteItem(args)
     return '', 204
Example #26
0
 def get(self, quality):
     return Service.get_item_by_quality(quality)
Example #27
0
 def __init__(self, config=None):
     Service.__init__(self, config)
     self._url = config["url"]
     self._headers = config["headers"]
     self._current_article = 0
 def get(self):
     return Service.updateQuality(tienda)
Example #29
0
 def __init__(self, domain: LookupDomain, logger: DiasysLogger = DiasysLogger()):
     # only call super class' constructor
     Service.__init__(self, domain=domain, debug_logger=logger)
Example #30
0
def test_init(aff_nlg, domain):
	aff_nlg.__init__(domain)
	Service.__init__(aff_nlg, domain='superhero')
	
	assert aff_nlg.domain == 'superhero'
Example #31
0
 def __init__(self, config, service_resolver):
     Service.__init__(self, config=config, service_resolver=service_resolver)
Example #32
0
 def get(self, itemName):
     return Service.getItem(itemName), 200