def webhook():
    try:
        ml_prediction
    except NameError:
        ml_prediction = Prediction(MODEL_DIR)
        ml_prediction.load_model()
    answer = ''
    if request.method == 'POST':
        try:
            data = json.loads(request.data)
            bot_id = data['recipient']['id']
            bot_name = data['recipient']['name']
            recipient = data['from']
            service = data['serviceUrl']
            sender = data['conversation']['id']
            text = data['text']

            bot.send_message(bot_id, bot_name, recipient, service, sender,
                             ml_prediction.response(text, sender))
        except Exception as e:
            print(e)
    if request.method == 'GET':
        question = request.args.get('q')
        answer = ml_prediction.response(question if question else 'Hi')
    return 'Code: 200. {}'.format(answer)
def train():
    if request.method == 'POST':
        try:
            intents_file = upload_file(request)
        except Exception as e:
            return "Error: {}".format(e)
    if request.method == 'GET':
        input_file = request.args.get('file')
        intents_file = input_file if input_file else "intents.json"

    ml = Train(intents_file, MODEL_DIR)
    try:
        ml.training()
        ml_prediction = Prediction(MODEL_DIR)
        ml_prediction.load_model()
        return "Train is completed"
    except Exception as e:
        return "Traing is failed {}".format(str(e))
Example #3
0
def webhook():
    try:
        ml_prediction
    except NameError:
        ml_prediction = Prediction(MODEL_DIR)
        ml_prediction.load_model()
    answer = ''
    if request.method == 'POST':
        try:
            data = json.loads(request.data)
            bot_id = data['recipient']['id']
            bot_name = data['recipient']['name']
            recipient = data['from']
            service = data['serviceUrl']
            sender = data['conversation']['id']
            text = data['text']

            answer = ml_prediction.response(text, sender)
            if answer:
                if answer.endswith('.png'):
                    url = request.url_root + app.static_url_path + '/' + answer
                    bot.send_media(bot_id, bot_name, recipient, service, sender, 'image/png', url)
                else:
                    bot.send_message(bot_id, bot_name, recipient, service, sender, answer)
            else:
                bot.send_message(bot_id, bot_name, recipient, service, sender, 'Sorry, I do not understand.')
                url = request.url_root + app.static_url_path + '/monkey.gif'
                bot.send_media(bot_id, bot_name, recipient, service, sender, 'image/gif', url)
        except Exception as e:
            print(e)
    if request.method == 'GET':
        question = request.args.get('q')
        answer = ml_prediction.response(question if question else 'Hi')
        if answer:
            if answer.endswith('.png'): return app.send_static_file(answer)
            return answer
        return app.send_static_file('monkey.gif')
Example #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)