Exemplo n.º 1
0
def get_question(message):
    if (not is_ascii(message.text)):
        bot.send_message(
            message.chat.id,
            'Your question contains non-ASCII symbols, try again.')
        return

    qa.add_question(message.chat.id, message.text)

    pair = qa.get_pair(message.chat.id)
    qa.set_answering(message.chat.id)

    global counter
    counter += 1

    try:
        annotated = annotate(('interact-{}'.format(counter), pair[0], pair[1]),
                             meta['wv_cased'])
        model_in = to_id(annotated, w2id, tag2id, ent2id)
        model_in = next(
            iter(
                BatchGen([model_in],
                         batch_size=1,
                         gpu=args.cuda,
                         evaluation=True)))
        prediction = model.predict(model_in)[0]

        bot.send_message(message.chat.id, "Answer:")
        bot.send_message(message.chat.id, prediction)
        qa.set_null(message.chat.id)
    except Exception as e:
        traceback.print_exc()
        bot.send_message(message.chat.id,
                         "Something went wrong, try again with /ask command.")
        qa.set_null(message.chat.id)
with open('SQuAD/meta.msgpack', 'rb') as f:
    meta = msgpack.load(f, encoding='utf8')
embedding = torch.Tensor(meta['embedding'])
opt['pretrained_words'] = True
opt['vocab_size'] = embedding.size(0)
opt['embedding_dim'] = embedding.size(1)
opt['pos_size'] = len(meta['vocab_tag'])
opt['ner_size'] = len(meta['vocab_ent'])
opt['cuda'] = args.cuda
BatchGen.pos_size = opt['pos_size']
BatchGen.ner_size = opt['ner_size']
model = DocReaderModel(opt, embedding, state_dict)
if args.cuda:
    model.cuda()
w2id = {w: i for i, w in enumerate(meta['vocab'])}
tag2id = {w: i for i, w in enumerate(meta['vocab_tag'])}
ent2id = {w: i for i, w in enumerate(meta['vocab_ent'])}
init()

evidence = 'Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend "Venite Ad Me Omnes". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.'
question = 'When the Virgin Mary reputedly appeared to Saint Bernadette Soubirous ?'

start_time = time.time()
annotated = annotate(('interact-{}'.format(id_), evidence, question), meta['wv_cased'])
model_in = to_id(annotated, w2id, tag2id, ent2id)
model_in = next(iter(BatchGen([model_in], batch_size=1, gpu=args.cuda, evaluation=True)))
prediction = model.predict(model_in)[0]
end_time = time.time()
print('Answer: {}'.format(prediction))
print('Time: {:.4f}s'.format(end_time - start_time))