obj_p.setJsonPath(
    os.path.join(execution_path, "gestures", "json", "model_class.json"))
obj_p.setModelPath(
    os.path.join(execution_path, "gestures", "models1",
                 "model_ex-006_acc-0.998940.h5"))
obj_p.setModelTypeAsResNet()
obj_p.loadPrediction()

while True:
    ret, frame = cap.read()
    if ret == False:
        break
    show = frame.copy()
    frame_rgb = frame[..., ::-1]
    predictions, probabilities = obj_p.predict(frame_rgb,
                                               result_count=1,
                                               input_type='array')

    show = cv2.putText(show, predictions[0] + ":" + probabilities[0], (50, 50),
                       font, 1.2, (255, 255, 255), 2)

    cv2.namedWindow('capture1')
    cv2.imshow('capture1', show)

    #out.write(show)#保存帧

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cv2.destroyWindow('capture1')
cap.release()
예제 #2
0
def prediction(dataset):
    prediction_model = Prediction(dataset)
    prediction_model.predict()
    prediction_model.write_result_to_file()
예제 #3
0
class Chatbot:
    """
    The chatbot per se! Yay <3
    """
    def __init__(self, token):
        logging.basicConfig(
            format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
            level=logging.INFO)
        self.logger = logging.getLogger("log")
        self.bot = Bot(token)
        self.updater = Updater(token)
        self.dispatcher = self.updater.dispatcher

        start_handler = CommandHandler('start', self.start)
        self.dispatcher.add_handler(start_handler)
        info_handler = CommandHandler('info', self.info)
        self.dispatcher.add_handler(info_handler)
        feedbac_handler = CommandHandler('feedback', self.feedback)
        self.dispatcher.add_handler(feedbac_handler)
        insertion_handler = CommandHandler('insert', self.insert)
        self.dispatcher.add_handler(insertion_handler)
        self.dispatcher.add_handler(CallbackQueryHandler(self.button_clicked))
        text_message_handler = MessageHandler(Filters.text, self.text_message)
        self.dispatcher.add_handler(text_message_handler)
        document_message_handler = MessageHandler(Filters.document,
                                                  self.document_message)
        self.dispatcher.add_handler(document_message_handler)
        self.dispatcher.add_handler(document_message_handler)
        self.dispatcher.add_error_handler(self.error)

        self.ner = Ner_Babel()
        self.prediction = Prediction()
        self.kb_interface = KB_interface()
        self.text_processing = text_processing()
        #O dicionario contem dicionarios que representam que tem o mesmo nome dos
        # metodos que requisitam precionar de butão pelo usuario, esses dict possuem as
        # variaveis uteis para lidar com essa ação.
        # O callback_query do botao será então a identidicação desse estado e botão precionado
        self.global_variables = {
            "last_question": "",
            "last_answer": "",
            "main_entity": "",
            "related_entity": "",
            "ambiguos_entities": [],
            "sugests_topics": {
                "buttons": []
            },
            "ask_for_answer_evaluation": {
                "buttons": ["P", "N"]
            },
            "sugests_question": {
                "buttons": []
            }
        }
        # self.insetion_states = {
        #                         "active"          : 0,
        #                         "ask_entity_name" : {
        #                                               "active" : 0,
        #                                               "not_valid_message" : "Name not valid"
        #                                               },
        #                         "ask_for_synonyms" : {
        #                                               "active" : 0,
        #                                               "not_valid_message" : "Synonyms not valid"
        #                                              },
        #                         "ask_for_context" : {
        #                                               "active" : 0,
        #                                               "not_valid_message" : "Context not valid"
        #                                              },
        #                         "ask_for_relations" : {
        #                                               "active" : 0,
        #                                               "not_valid_message" : "relations not valid"
        #                                              }
        #                         }

    def verify_bot(self):
        return (self.bot.get_me().username, self.bot.get_me().id)

    def start(self, bot, update):
        """
        Start command to start bot on Telegram.
        @bot = information about the bot
        @update = the user info.
        """
        bot.send_chat_action(chat_id=update.message.chat_id,
                             action=telegram.ChatAction.TYPING)
        sleep(3.5)
        name = update.message['chat']['first_name']
        start_text = (
            f"Hi {name}, I'm a chatbot in development. Feel free to type '/feedback'"
            +
            " after any question to let us know that do you think of answer given"
        )
        bot.send_message(chat_id=update.message.chat_id, text=start_text)

    def info(self, bot, update):
        bot.send_chat_action(chat_id=update.message.chat_id,
                             action=telegram.ChatAction.TYPING)
        bot.send_message(
            chat_id=update.message.chat_id,
            text="Made for William's TCC",
        )

    def feedback(self, bot, update):
        bot.send_chat_action(chat_id=update.message.chat_id,
                             action=telegram.ChatAction.TYPING)
        #Now we ask the user if the answer was helpfull
        no_question_bool = self.global_variables["last_question"] != ""
        nome = self.global_variables["last_question"]
        if (no_question_bool):
            self.ask_for_answer_evaluation(bot, update)

    def text_message(self, bot, update):
        #_____________________ colocar tudo a baixo em outro metodo
        entities = self.ner.get_entities(update.effective_message.text)
        bot.send_chat_action(chat_id=update.message.chat_id,
                             action=telegram.ChatAction.TYPING)
        if (entities is None):
            bot.send_message(chat_id=update.message.chat_id,
                             text="No entity reconized")

        #geting entities names(if they are called by nick names)ps: this helps finding the answer
        kb_entities = []
        for entity in entities:
            kb_entities.append(
                self.kb_interface.get_entity_id_and_name(entity))

        #trying on the FIRST[0] entity_id[0] of the list to select the context
        print(f"AQUI: {kb_entities}")
        if kb_entities == [0]:
            bot.send_message(
                chat_id=update.message.chat_id,
                text="Make sure to ask about things in the knowledge base")

        else:
            entities_ids, entities_names = zip(*kb_entities)
            self.global_variables[
                "last_question"] = update.effective_message.text
            print(f"{len(kb_entities)}")
            if len(kb_entities) == 1:

                context = self.kb_interface.get_context_from_entityid(
                    kb_entities[0][0])
                question = self.text_processing.rewrite_question(
                    entities, entities_names, update.effective_message.text)
                response = self.prediction.predict(question, context)
                self.global_variables["last_answer"] = response
                #TODO _____________________ colocar tudo a acima em outro metodo

                bot.send_chat_action(chat_id=update.message.chat_id,
                                     action=telegram.ChatAction.TYPING)
                # bot.send_message(chat_id=update.message.chat_id, text="<b>Question rewrite:</b> {}".format(question), parse_mode=telegram.ParseMode.HTML)
                # bot.send_message(chat_id=update.message.chat_id, text="<b>Answer:</b> {}".format(response), parse_mode=telegram.ParseMode.HTML)
                bot.send_message(chat_id=update.message.chat_id,
                                 text="{}".format(response),
                                 parse_mode=telegram.ParseMode.HTML)

                #List of entities related to the main entity of the question(the first one 0)
                entities_tuple_list = self.kb_interface.get_related_entities(
                    entities_ids[0])
                self.global_variables["main_entity"] = entities_names[0]
                #Now let us sugest related topics to explore
                self.sugest_topics(update, entities_tuple_list)

            else:
                print("PIPI")
                print(kb_entities)
                bot.send_message(
                    chat_id=update.message.chat_id,
                    text="Select one of the following entities to focus on",
                    parse_mode=telegram.ParseMode.HTML)
                self.show_ambiguos_entities(update, entities_names)

    def sugest_topics(self, update, entities_tuple_list):
        """
        Make the buttons for the entity sugestions
        """
        keyboard = []
        i = 0
        while (i < len(entities_tuple_list)):
            my_list = []
            left_entity_name = entities_tuple_list[i][1]
            query_data_left = "{}sugests_topics".format(str(i))
            self.global_variables["sugests_topics"]["buttons"].append(
                left_entity_name)
            left_button = InlineKeyboardButton(left_entity_name,
                                               callback_data=query_data_left)
            my_list.append(left_button)
            i += 1
            if (i < len(entities_tuple_list)):
                right_entity_name = entities_tuple_list[i][1]
                query_data_right = "{}sugests_topics".format(str(i))
                self.global_variables["sugests_topics"]["buttons"].append(
                    right_entity_name)
                right_button = InlineKeyboardButton(
                    right_entity_name, callback_data=query_data_right)
                my_list.append(right_button)
                i += 1
            keyboard.append(my_list)
        my_reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text('Explore more topics:',
                                  reply_markup=my_reply_markup,
                                  parse_mode=telegram.ParseMode.HTML)

    def show_ambiguos_entities(self, update, entities_tuple_list):
        """
        Make the buttons for the entity sugestions
        """
        keyboard = []
        i = 0
        while (i < len(entities_tuple_list)):
            my_list = []
            left_entity_name = entities_tuple_list[i]
            query_data_left = "{}ambiguos_entities".format(str(i))
            left_button = InlineKeyboardButton(left_entity_name,
                                               callback_data=query_data_left)
            my_list.append(left_button)
            i += 1
            if (i < len(entities_tuple_list)):
                right_entity_name = entities_tuple_list[i]
                query_data_right = "{}ambiguos_entities".format(str(i))
                right_button = InlineKeyboardButton(
                    right_entity_name, callback_data=query_data_right)
                my_list.append(right_button)
                i += 1
            self.global_variables["ambiguos_entities"].append(left_entity_name)
            self.global_variables["ambiguos_entities"].append(
                right_entity_name)
            keyboard.append(my_list)
        my_reply_markup = InlineKeyboardMarkup(keyboard)
        update.message.reply_text('Selecte one:',
                                  reply_markup=my_reply_markup,
                                  parse_mode=telegram.ParseMode.HTML)

    def ask_for_answer_evaluation(self, bot, update):
        query_data = "0ask_for_answer_evaluation"
        positive_button = InlineKeyboardButton(emojize(":thumbs_up:"),
                                               callback_data=query_data)

        query_data = "1ask_for_answer_evaluation"
        negative_button = InlineKeyboardButton(emojize(":thumbs_down:"),
                                               callback_data=query_data)

        query_data = "2ask_for_answer_evaluation"
        not_now_button = InlineKeyboardButton("Not now",
                                              callback_data=query_data)

        binary_markup = InlineKeyboardMarkup(
            [[positive_button, negative_button, not_now_button]])
        update.message.reply_text('This answer was useful?',
                                  reply_markup=binary_markup,
                                  parse_mode=telegram.ParseMode.HTML)

    def sugest_questions(self, bot, query):
        question_entity = self.global_variables["main_entity"]
        related_entity = self.global_variables["related_entity"]
        q1 = "What is {}?".format(related_entity)
        q2 = "Is {} a kind of {}?".format(related_entity, question_entity)
        questios_union_string = "<b>1</b>: {}\n<b>2</b>: {}".format(q1, q2)
        possible_question_list = [q1, q2]
        keyboard = []
        i = 0
        while (i < len(possible_question_list)):
            my_list = []

            #Mounting the first button
            query_data = "{}sugests_question".format(str(i))
            self.global_variables["sugests_question"]["buttons"].append(
                possible_question_list[i])
            left_button = InlineKeyboardButton(
                str(i + 1),
                callback_data=query_data,
                parse_mode=telegram.ParseMode.HTML)
            my_list.append(left_button)
            i = i + 1
            #Mounting the second button
            if (i < len(possible_question_list)):
                query_data = "{i}{sugests_question}"
                self.global_variables["sugests_question"]["buttons"].append(
                    possible_question_list[i])
                rigth_button = InlineKeyboardButton(str(i + 1),
                                                    callback_data=query_data)

                #Puting the row of buttons together
                my_list.append(rigth_button)
                i = i + 1
            keyboard.append(my_list)
        my_reply_markup = InlineKeyboardMarkup(keyboard)

        text = "<b>{}</b>\nChoose a question:\n{}".format(
            related_entity, questios_union_string)
        bot.edit_message_text(text=text,
                              chat_id=query.message.chat_id,
                              message_id=query.message.message_id,
                              reply_markup=my_reply_markup,
                              parse_mode=telegram.ParseMode.HTML)

    def button_clicked(self, bot, update):
        query = update.callback_query
        query_str = query.data
        button_index = int(query_str[0])
        state = query_str[1:]
        message = ""
        if (state == "ask_for_answer_evaluation"):
            if (button_index == 0 or button_index == 1):
                #button Index: 0 is positive 1 is negative
                self.kb_interface.insert_feedback(
                    self.global_variables["last_question"],
                    self.global_variables["last_answer"],
                    self.global_variables["main_entity"], button_index)
                message = "Thank you for the feedback"
            else:
                message = "OK"

            bot.edit_message_text(text=message,
                                  chat_id=query.message.chat_id,
                                  message_id=query.message.message_id)

        elif (state == "sugests_topics"):
            #name of the related entity as shown in the button clicked
            related_entity_name = self.global_variables["sugests_topics"][
                "buttons"][button_index]
            self.global_variables["related_entity"] = related_entity_name
            self.sugest_questions(bot, query)

        elif (state == "sugests_question"):
            related_entity_name = self.global_variables["related_entity"]
            related_id = self.kb_interface.get_entity_id(related_entity_name)
            context = self.kb_interface.get_context_from_entityid(related_id)
            question = self.global_variables["sugests_question"]["buttons"][
                button_index]
            response = self.prediction.predict(question, context)
            text = "Question: {}\nAnswer:{}".format(question, response)
            bot.edit_message_text(text=text,
                                  chat_id=query.message.chat_id,
                                  message_id=query.message.message_id)

        elif (state == "ambiguos_entities"):
            selectedEntity = self.global_variables["ambiguos_entities"][
                button_index]
            self.global_variables["main_entity"] = selectedEntity
            selected_id = self.kb_interface.get_entity_id(selectedEntity)
            context = self.kb_interface.get_context_from_entityid(selected_id)
            question = self.global_variables["last_question"]
            response = self.prediction.predict(question, context)
            text = "<b>Focus</b> Entity: {}\n<b>Answer</b>:{}".format(
                self.global_variables["main_entity"], response)
            bot.edit_message_text(text=text,
                                  chat_id=query.message.chat_id,
                                  message_id=query.message.message_id)

    def document_message(self, bot, update):
        file_id = update.message.document.file_id
        newFile = bot.get_file(file_id)
        FILE_PATH = "bot/new_entity.csv"
        newFile.download(FILE_PATH)
        self.read_insertion_file(bot, update, FILE_PATH)

    def insert(self, bot, update):
        # print("Inserion mode activated")
        # print("Inserion mode activated")
        #SEND 2 FILES, template e lista de entidades contidas
        message = "Look at the file entidades.txt to see what entities already exist. Then Download the CSV file and put the information you want to insert and send it back"
        bot.send_message(chat_id=update.message.chat_id, text=message)
        self.build_entity_file()
        bot.send_document(chat_id=update.message.chat_id,
                          document=open('bot/entidades.txt', 'rb'))
        bot.send_document(chat_id=update.message.chat_id,
                          document=open('bot/insertion_file.csv', 'rb'))

    def build_entity_file(self):
        entity_list = self.kb_interface.get_all_entities()
        relations_list = self.kb_interface.get_all_relations_types()
        FILE_PATH = "bot/entidades.txt"
        with open(FILE_PATH, "w") as data_file:
            data_file.write("Entities:\n")
            for entity in entity_list:
                data_file.write(f"{entity}\n")
            data_file.write("\nRelations:\n")
            for relation in relations_list:
                data_file.write(f"{relation}\n")

    def read_insertion_file(self, bot, update, file_path):
        # bot.send_message(chat_id=update.message.chat_id, text="Error reading file, please check if it conforms to the template")
        bot.send_message(chat_id=update.message.chat_id,
                         text="Success.Entity inserted into knowledge base")

        entity_dict = {
            'name': '',
            'nicknames': [],
            'relations': [],
            'context': ''
        }
        with open(file_path, 'r', newline='') as csvfile:
            line_count = 1
            csv_reader = csv.reader(csvfile, delimiter=',')
            for row in csv_reader:
                if (line_count == 2):
                    entity_dict['name'] = row[0]
                    entity_dict['nicknames'].append(row[1])
                    entity_dict['relations'].append((row[2], row[3]))
                    entity_dict['context'] = row[4]
                elif (line_count > 2):
                    if (row[1] != ''):
                        entity_dict['nicknames'].append(row[1])
                    if (row[2] != '' and row[3] != ''):
                        entity_dict['relations'].append((row[2], row[3]))
                line_count += 1
        # pprint.pprint(entity_dict)
        self.kb_interface.insert_data_from_dict(entity_dict)

    def error(self, bot, update, error):
        self.logger.warning('Update "%s" caused error "%s"', update, error)

    def run(self):
        # Start the Bot
        print('Bot configured. Receiving messages now.')
        self.updater.start_polling()

        # Run the bot until you press Ctrl-C or the process receives SIGINT,
        # SIGTERM or SIGABRT. This should be used most of the time, since
        # start_polling() is non-blocking and will stop the bot gracefully.
        self.updater.idle()
예제 #4
0
class MedicalQADataset(Dataset):
    """自定义医疗QA数据集"""
    def __init__(self, dataset_ids):
        self.dataset_ids = dataset_ids

    def __getitem__(self, item):

        return self.dataset_ids[item]

    def __len__(self):

        return len(self.dataset_ids)


if __name__ == "__main__":
    # 测试predict是否OK
    from prediction import Prediction

    model = Prediction()
    model.load_model()

    result = model.predict(
        department='',
        title='孕妇经常胃痛会影响到胎儿吗',
        ask='"我怀上五个多月了,自从怀上以来就经常胃痛(两个胸之间往下一点儿是胃吧?)有时痛十几分钟,有时痛'
        '半个钟,每次都痛得好厉害,不过痛过这一阵之后就不痛了,我怀上初期饮食不规律,经常吃不下东西,会不'
        '会是这样引发的呀?我好忧心对胎儿有影响,该怎么办呢?可有食疗的方法可以纾解一下痛呢?"')
    print(result)

    exit(0)
예제 #5
0
                np.array(text + [padding] * (max_len - len(text))))
    return batch_text


def get_batches(texts, labels, batch_size, text_padding):
    for batch_i in range(0, len(labels) // batch_size):
        start_i = batch_i * batch_size
        texts_batch = texts[start_i:start_i + batch_size]
        labels_batch = labels[start_i:start_i + batch_size]

        pad_texts_batch = batch_padding(texts_batch, text_padding)
        yield pad_texts_batch, labels_batch


def get_val_batch(texts, labels, batch_size, text_padding):
    texts_batch = texts[:batch_size]
    labels_batch = labels[:batch_size]
    pad_texts_batch = batch_padding(texts_batch, text_padding)
    return pad_texts_batch, labels_batch


if __name__ == "__main__":
    from prediction import Prediction

    model = Prediction()
    model.load_model()

    result = model.predict(title='甲状腺功能减退能治好吗?', text='无')
    print(result)

    exit(0)
    dominant_frequencies = np.array(
        common.convert_robj(dominant_frequencies).iloc[:, 0])
    dominant_frequencies = dominant_frequencies * 1000
    features = []
    features.append(r_features['mean'][0])
    features.append(r_features['sd'][0])
    features.append(r_features['median'][0])
    features.append(r_features['Q25'][0])
    features.append(r_features['Q75'][0])
    features.append(r_features['IQR'][0])
    features.append(r_features['skewness'][0])
    features.append(r_features['kurtosis'][0])
    features.append(r_features['sh'][0])
    features.append(r_features['sfm'][0])
    features.append(r_features['mode'][0])
    features.append(r_features['cent'][0])
    features.append(np.mean(fundamental_frequencies))
    features.append(np.min(fundamental_frequencies))
    features.append(np.max(fundamental_frequencies))
    features.append(np.mean(dominant_frequencies))
    features.append(np.min(dominant_frequencies))
    features.append(np.max(dominant_frequencies))
    features.append(
        np.max(dominant_frequencies) - np.min(dominant_frequencies))
    features.append(0)
    return np.array(features)


prediction = Prediction()
prediction.predict(vectorize_audio())
예제 #7
0
		json_data = json.loads(json_data)
		print('Predict:',json_data)

		# controller will convert json data to dataframe		
		input_df = pd.json_normalize(json_data)

		# save json_data and input_df for debugging purpose, save using unique name
		json_data_unique_filename = config.PATH_TO_DATASET+utils.get_unique_filename('json_data.json')
		input_df_unique_filename = config.PATH_TO_DATASET+utils.get_unique_filename('input_df.csv')		
		
		with open(json_data_unique_filename, 'w') as outfile:
			json.dump(json_data, outfile)

		input_df.to_csv(input_df_unique_filename, index=False)

		
		prediction = Prediction(user_id, input_df_unique_filename, random_state)
		predictions, labels = prediction.predict()

		print('predictions:',predictions)
		print('prediction labels:',labels)	
		print('ground truth:',json_data[config.TARGET_COLUMN])

		

	else:
		print('file is not exist')


	msg = __name__+'.'+utils.get_function_caller()+' -> exit'
	log.print(msg)
예제 #8
0
파일: app.py 프로젝트: sayantansatpati/ml
def main(data_to_predict_json):
    print('### Delivery Time Prediction App ###')
    pred = Prediction(data_to_predict_json)
    pred.predict()