예제 #1
0
    def __init__(self):
        self.config = ChatBotConfigParser().parser

        zomatoConfig = {"user_key": self.config.get('Zomato', 'user_key')}
        self.zomato = Zomato(zomatoConfig)
        # With ref from : https://en.wikipedia.org/wiki/Classification_of_Indian_cities #
        self.selectedCityList = [
            'Ahmedabad', 'Bangalore', 'Bengaluru', 'Chennai', 'Delhi',
            'Delhi NCR', 'Hyderabad', 'Kolkata', 'Mumbai', 'Pune', 'Agra',
            'Ajmer', 'Aligarh', 'Allahabad', 'Amravati', 'Amritsar', 'Asansol',
            'Aurangabad', 'Bareilly', 'Belgaum', 'Bhavnagar', 'Bhiwandi',
            'Bhopal', 'Bhubaneswar', 'Bikaner', 'Bokaro Steel City',
            'Chandigarh', 'Coimbatore', 'Cuttack', 'Dehradun', 'Dhanbad',
            'Durg-Bhilai Nagar', 'Durgapur', 'Erode', 'Faridabad', 'Firozabad',
            'Ghaziabad', 'Gorakhpur', 'Gulbarga', 'Guntur', 'Gurgaon',
            'Guwahati', 'Gwalior', 'Hubli-Dharwad', 'Indore', 'Jabalpur',
            'Jaipur', 'Jalandhar', 'Jammu', 'Jamnagar', 'Jamshedpur', 'Jhansi',
            'Jodhpur', 'Kannur', 'Kanpur', 'Kakinada', 'Kochi', 'Kottayam',
            'Kolhapur', 'Kollam', 'Kota', 'Kozhikode', 'Kurnool', 'Lucknow',
            'Ludhiana', 'Madurai', 'Malappuram', 'Mathura', 'Goa', 'Mangalore',
            'Meerut', 'Moradabad', 'Mysore', 'Nagpur', 'Nanded', 'Nashik',
            'Nellore', 'Noida', 'Palakkad', 'Patna', 'Pondicherry', 'Raipur',
            'Rajkot', 'Rajahmundry', 'Ranchi', 'Rourkela', 'Salem', 'Sangli',
            'Siliguri', 'Solapur', 'Srinagar', 'Sultanpur', 'Surat',
            'Thiruvananthapuram', 'Thrissur', 'Tiruchirappalli', 'Tirunelveli',
            'Tiruppur', 'Ujjain', 'Vijayapura', 'Vadodara', 'Varanasi',
            'Vasai-Virar City', 'Vijayawada', 'Visakhapatnam', 'Warangal'
        ]
예제 #2
0
 def __init__(self):
     try:
         self.config = ChatBotConfigParser().parser
     except Exception as e:
         logger.error("Unable to build RasaCoreTrain Obj, exception : %s" %
                      (str(e)))
         raise (e)
예제 #3
0
 def __init__(self):
     try:
         self.builder = ComponentBuilder(use_cache=True)
         self.config = ChatBotConfigParser().parser
     except Exception as e:
         logger.error("Unable to build NluModel Obj, exception : %s" %
                      (str(e)))
         raise (e)
예제 #4
0
class RasaTrainOnline(object):
    def __init__(self):
        try:
            self.config = ChatBotConfigParser().parser
        except Exception as e:
            logger.error(
                "Unable to build RasaTrainOnline Obj, exception : %s" %
                (str(e)))
            raise (e)

    def runRasaTrainOnline(self):
        try:
            input_channel = ConsoleInputChannel()
            interpreter = RasaNLUInterpreter(
                os.path.realpath(self.config.get('nluModel',
                                                 'model_location')))
            domain_file = os.path.realpath(
                self.config.get('inputData', 'coreyml'))
            training_data_file = os.path.realpath(
                self.config.get('inputData', 'stories'))
            logger.info(
                "nluModel = %s, domain_file = %s, train_data_file = %s" %
                (str(
                    os.path.realpath(
                        self.config.get('nluModel', 'model_location'))),
                 str(domain_file), str(training_data_file)))
            agent = Agent(domain_file,
                          policies=[MemoizationPolicy(),
                                    KerasPolicy()],
                          interpreter=interpreter)

            agent.train_online(training_data_file,
                               input_channel=input_channel,
                               max_history=2,
                               batch_size=50,
                               epochs=200,
                               max_training_samples=300)

            return agent, "Rasa Train Online completed successfully"
        except Exception as e:
            logger.error("Unable to run Rasa Train Online, exception : %s" %
                         (str(e)))
            raise (e)
예제 #5
0
 def __init__(self):
     try:
         self.config = ChatBotConfigParser().parser
         self.nlu_interpretor = RasaNLUInterpreter(
             self.config.get('nluModel', 'model_location'))
         self.agent = Agent.load(
             self.config.get('coreModel', 'model_location'))
         self.input_channel = SlackInput(
             self.config.get('slack',
                             'app_verification'),  # app verification token
             self.config.get('slack',
                             'bot_verification'),  # bot verification token
             self.config.get(
                 'slack', 'slack_verification'),  # slack verification token
             True)
     except Exception as e:
         logger.error(
             "Unable to create object for RasaSlackApp, exception : %s" %
             (str(e)))
         raise (e)
예제 #6
0
class RasaSlackApp(object):
    def __init__(self):
        try:
            self.config = ChatBotConfigParser().parser
            self.nlu_interpretor = RasaNLUInterpreter(
                self.config.get('nluModel', 'model_location'))
            self.agent = Agent.load(
                self.config.get('coreModel', 'model_location'))
            self.input_channel = SlackInput(
                self.config.get('slack',
                                'app_verification'),  # app verification token
                self.config.get('slack',
                                'bot_verification'),  # bot verification token
                self.config.get(
                    'slack', 'slack_verification'),  # slack verification token
                True)
        except Exception as e:
            logger.error(
                "Unable to create object for RasaSlackApp, exception : %s" %
                (str(e)))
            raise (e)

    def run_app(self):
        try:
            self.agent.handle_channel(
                HttpInputChannel(5004, "/", self.input_channel))
        except Exception as e:
            logger.error("Unable to start slack app, exception : %s" %
                         (str(e)))
            raise (e)


#nlu_interpreter = RasaNLUInterpreter('./models/ourgroup/nlu/model_20181027T202656/default/restaurantnlu')
#agent = Agent.load('./models/ourgroup/dialogue/model_20181027T202736', interpreter = nlu_interpreter)

#input_channel = SlackInput('xoxp-460834353523-461984426807-465152376016-227cf68b2f965e85773c3d8c3de2b90c', #app verification token
#							'xoxb-460834353523-467290740166-gSiOoS05TflKL19xb9dix609', # bot verification token
#							'048rGeNtBMmp2ipFkA7BAgiU', # slack verification token
#							True)

#agent.handle_channel(HttpInputChannel(5004, '/', input_channel))
예제 #7
0
class RunChatBotLocal(object):
	def __init__(self):
		try:
			self.config = ChatBotConfigParser().parser
		except Exception as e:
			logger.error("Unable to build RunChatBotLocal Obj, exception : %s" %(str(e)))
			raise(e)
	
	def run_chat_bot(self):
		try:
			nlu_model = os.path.realpath(self.config.get('nluModel','model_location'))
			core_model = os.path.realpath(self.config.get('coreModel','model_location'))
			interpreter = RasaNLUInterpreter(nlu_model)
			agent = Agent.load(core_model, interpreter = interpreter)
			
			if ast.literal_eval(self.config.get('runChatBotLocal','serve_forever')):
				agent.handle_channel(ConsoleInputChannel())
				
			return agent,"Rasa Chatbot local execution completed"
		except Exception as e:
			logger.error("Unable to execute chatbot, exception : %s" %(str(e)))
			raise(e)
예제 #8
0
class RasaCoreTrain(object):
    def __init__(self):
        try:
            self.config = ChatBotConfigParser().parser
        except Exception as e:
            logger.error("Unable to build RasaCoreTrain Obj, exception : %s" %
                         (str(e)))
            raise (e)

    def trainRasaCore(self):
        try:
            training_data_file = "./" + self.config.get('inputData', 'stories')
            domain_yml = "./" + self.config.get('inputData', 'coreyml')

            logger.info(
                "Building RASA Core model with stories : %s, domain_yml : %s" %
                (training_data_file, domain_yml))
            model_name = "model_" + datetime.now().strftime("%Y%m%dT%H%M%S")
            model_location = "./models/ourgroup/dialogue/" + model_name

            featurizer = MaxHistoryTrackerFeaturizer(
                BinarySingleStateFeaturizer(), max_history=5)
            agent = Agent(domain_yml,
                          policies=[
                              MemoizationPolicy(max_history=4),
                              KerasPolicy(featurizer)
                          ])

            agent.train(
                training_data_file,
                augmentation_factor=50,
                #max_history = 4,
                epochs=500,
                batch_size=30,
                validation_split=0.2)
            agent.persist(model_location)

            model_location = os.path.realpath(model_location)
            logger.info("RASA Core model_location : %s" %
                        (str(model_location)))

            self.config.set('coreModel',
                            'model_location',
                            value=model_location)
            with open("./etc/config.ini", "w+") as f:
                self.config.write(f)
            return ("RASA core model training completed, see details above")
        except Exception as e:
            logger.error("unable to train rasa core model, exception : %s" %
                         (str(e)))
            raise (e)
예제 #9
0
class NluModel(object):
    def __init__(self):
        try:
            self.builder = ComponentBuilder(use_cache=True)
            self.config = ChatBotConfigParser().parser
        except Exception as e:
            logger.error("Unable to build NluModel Obj, exception : %s" %
                         (str(e)))
            raise (e)

    def train_nlu(self):
        try:
            nlu_data_json = "./" + self.config.get('inputData', 'nluData')
            nlu_spacy_json = "./" + self.config.get('inputData', 'spacyConfig')
            logger.info(
                "Building NLU model with data_json : %s, spacy_json : %s" %
                (nlu_data_json, nlu_spacy_json))
            training_data = load_data(nlu_data_json)
            trainer = Trainer(config.load(nlu_spacy_json), self.builder)
            trainer.train(training_data)
            model_name = "model_" + datetime.now().strftime("%Y%m%dT%H%M%S")
            model_location = "./models/ourgroup/nlu/" + model_name
            model_directory = trainer.persist(model_location,
                                              fixed_model_name='restaurantnlu')
            model_directory = os.path.realpath(model_directory)
            logger.info("NLU model_directory returned : %s" %
                        (str(model_directory)))
            #return ("NLU model training completed, see details above")
            self.config.set('nluModel',
                            'model_location',
                            value=model_directory)
            with open("./etc/config.ini", "w+") as f:
                self.config.write(f)
            return ("NLU model training completed, see details above")
        except Exception as e:
            logger.error("Unable to build NLU model, exception : %s" %
                         (str(e)))
            raise (e)
예제 #10
0
class ZomatoInfo:
    def __init__(self):
        self.config = ChatBotConfigParser().parser

        zomatoConfig = {"user_key": self.config.get('Zomato', 'user_key')}
        self.zomato = Zomato(zomatoConfig)
        # With ref from : https://en.wikipedia.org/wiki/Classification_of_Indian_cities #
        self.selectedCityList = [
            'Ahmedabad', 'Bangalore', 'Bengaluru', 'Chennai', 'Delhi',
            'Delhi NCR', 'Hyderabad', 'Kolkata', 'Mumbai', 'Pune', 'Agra',
            'Ajmer', 'Aligarh', 'Allahabad', 'Amravati', 'Amritsar', 'Asansol',
            'Aurangabad', 'Bareilly', 'Belgaum', 'Bhavnagar', 'Bhiwandi',
            'Bhopal', 'Bhubaneswar', 'Bikaner', 'Bokaro Steel City',
            'Chandigarh', 'Coimbatore', 'Cuttack', 'Dehradun', 'Dhanbad',
            'Durg-Bhilai Nagar', 'Durgapur', 'Erode', 'Faridabad', 'Firozabad',
            'Ghaziabad', 'Gorakhpur', 'Gulbarga', 'Guntur', 'Gurgaon',
            'Guwahati', 'Gwalior', 'Hubli-Dharwad', 'Indore', 'Jabalpur',
            'Jaipur', 'Jalandhar', 'Jammu', 'Jamnagar', 'Jamshedpur', 'Jhansi',
            'Jodhpur', 'Kannur', 'Kanpur', 'Kakinada', 'Kochi', 'Kottayam',
            'Kolhapur', 'Kollam', 'Kota', 'Kozhikode', 'Kurnool', 'Lucknow',
            'Ludhiana', 'Madurai', 'Malappuram', 'Mathura', 'Goa', 'Mangalore',
            'Meerut', 'Moradabad', 'Mysore', 'Nagpur', 'Nanded', 'Nashik',
            'Nellore', 'Noida', 'Palakkad', 'Patna', 'Pondicherry', 'Raipur',
            'Rajkot', 'Rajahmundry', 'Ranchi', 'Rourkela', 'Salem', 'Sangli',
            'Siliguri', 'Solapur', 'Srinagar', 'Sultanpur', 'Surat',
            'Thiruvananthapuram', 'Thrissur', 'Tiruchirappalli', 'Tirunelveli',
            'Tiruppur', 'Ujjain', 'Vijayapura', 'Vadodara', 'Varanasi',
            'Vasai-Virar City', 'Vijayawada', 'Visakhapatnam', 'Warangal'
        ]

    def isLocationAvailable(self, location, debugMode=False):
        try:
            loc = json.loads(self.zomato.get_location(location, limit=1))
            if len(loc['location_suggestions']) == 0:
                logger.debug("city_name = %s, Not found" % (str(location)))
                return {"location_found": 'notfound'}

            city_name = loc["location_suggestions"][0]["city_name"].strip()
            if debugMode:
                logger.debug("city_name = %s" % (str(city_name)))
            if city_name.lower() not in [
                    city.lower() for city in self.selectedCityList
            ]:
                logger.info("City : %s not in area where Foodie operates" %
                            (city_name))
                return {"location_found": 'tier3'}
            else:
                return {"location_found": 'found'}
        except Exception as e:
            logger.error(
                "Exception is searching through zomato, exception : %s" %
                (str(e)))
            logger.error("%s" % (str("\n".join([
                line.rstrip('\n') for line in traceback.format_exception(
                    e.__class__, e, e.__traceback__)
            ]))))
            return {"location_found": "notfound"}

    def getDetails(self,
                   location,
                   cuisine,
                   lowerRateLimit=0,
                   upperRateLimit=300,
                   debugMode=True):

        try:
            # get the location details -> latitude and longitude #
            loc = json.loads(self.zomato.get_location(location, limit=1))
            city_lat = loc["location_suggestions"][0]["latitude"]
            city_long = loc["location_suggestions"][0]["longitude"]
            city_id = loc["location_suggestions"][0]["city_id"]
            city_name = loc["location_suggestions"][0]["city_name"].strip()
            #print(city_name)
            #print("Bangalore" in self.selectedCityList)
            if debugMode:
                logger.debug(
                    "city_name = %s, city_lat = %s, city_long = %s, city_id = %s"
                    % (str(city_name), str(city_lat), str(city_long),
                       str(city_id)))
            if city_name.lower() not in [
                    city.lower() for city in self.selectedCityList
            ]:
                logger.info("City : %s not in area where Foodie operates" %
                            (city_name))

                return {
                    "results_found": [],
                    "cuisine_found": "",
                    "price_found": ""
                }
            # get cuisine #
            selected_cuisine_info = dict()
            available_cuisines = self.zomato.get_cuisines(city_id)
            if cuisine not in [
                    cu.lower() for cu in available_cuisines.values()
            ]:
                logger.info(
                    "In City : %s, selected cuisine : %s not available" %
                    (city_name, cuisine))
                #dispatcher.utter_message("-----\n"+"In City : %s, selected cuisine : %s not available" %(city_name,cuisine))
                return {
                    "results_found": [],
                    "cuisine_found": 'notfound',
                    "price_found": ""
                }

            else:
                for key, value in available_cuisines.items():
                    if value.lower() == cuisine.lower():
                        #print(value)
                        selected_cuisine_info[value.lower()] = key
            # Get restaurant details #
            list1 = [0, 20, 40, 60, 80]
            zomato_results = []
            if upperRateLimit == 300:
                order = 'asc'
                sort = 'cost'
            else:
                order = ""
                sort = ""
            for i in list1:
                #results = self.zomato.restaurant_search("", city_lat, city_long, str(selected_cuisine_info.get(cuisine)), limit=i)
                #print(str(selected_cuisine_info.get(cuisine)))

                results = self.zomato.restaurant_search_sort(
                    "",
                    city_lat,
                    city_long,
                    str(selected_cuisine_info.get(cuisine.lower())),
                    start=i,
                    limit=20,
                    sort_by=sort,
                    order=order)

                temp_result = json.loads(results)
                zomato_results.extend(temp_result['restaurants'])
                #print("Number of Zomato results :",len(zomato_results))
            #zomato_result = json.loads(self.zomato.restaurant_search("", city_lat, city_long, str(selected_cuisine_info.get(cuisine)), limit=100))
            result_list = list()
            user_rating_list = list()

            for rest in zomato_results:
                #print("In rest")
                #rest_data = rest["restaurant"]
                rest_data = rest['restaurant']
                if rest_data[
                        "average_cost_for_two"] > lowerRateLimit and rest_data[
                            "average_cost_for_two"] <= upperRateLimit:
                    result_data = {
                        "name": rest_data["name"],
                        "location": rest_data["location"]["address"],
                        "user_rating":
                        rest_data["user_rating"]["aggregate_rating"],
                        "budget": rest_data["average_cost_for_two"]
                    }
                    user_rating_list.append(
                        rest_data["user_rating"]["aggregate_rating"])
                    result_list.append(result_data)
            if debugMode:
                #logger.debug("Unsorted result list:\n%s" %(str(result_list)))
                logger.debug("Length of unsorted list = %d" %
                             (len(result_list)))
            if len(result_list) > 0:
                # Sort by user_rating
                price_found = "found"
                result_list = list(
                    np.array(result_list)[np.argsort(
                        np.array(user_rating_list))])
                # Since above was ascending order, lets
                result_list.reverse()
                result_list = result_list[0:10]
            else:
                price_found = "notfound"

            if debugMode:
                #logger.debug("Final result list:\n%s" %(str(result_list)))
                logger.debug("Length of final result list = %d" %
                             (len(result_list)))
            return {
                "results_found": result_list,
                "cuisine_found": "found",
                'price_found': price_found
            }

        except Exception as e:
            logger.error(
                "Exception is searching through zomato, exception : %s" %
                (str(e)))
            logger.error("%s" % (str("\n".join([
                line.rstrip('\n') for line in traceback.format_exception(
                    e.__class__, e, e.__traceback__)
            ]))))
            return {"results_found": [], "cuisine_found": "", price_found: ""}