Search code examples
pythonpython-3.xtensorflowjupyter-notebookcell

How do I output this Python, jyputer, deepplavlov code correctly on a notebook cell?


I have a functional setup with Tensorflow and Jupyter. I have configured Tensorflow==1.14 to run on gpu.

Now to the questions: I'm using an open source conversational AI framework called DeepPavlov. Its all up and running (in the configuration side) but I don't have much experience with calling python from a notebook (or at all). I could run this code on console but that's not the goal for me. My problem:

I have a normal python code:

from deeppavlov import build_model, configs

model = build_model(configs.squad.squad, download=True)
model(['DeepPavlov is library for NLP and dialog systems.'], ['What is DeepPavlov?'])

this is my output:

2020-04-20 07:08:23.478 INFO in 'deeppavlov.download'['download'] at line 117: Skipped http://files.deeppavlov.ai/deeppavlov_data/squad_model_1.4_cpu_compatible.tar.gz download because of matching hashes
2020-04-20 07:08:37.884 INFO in 'deeppavlov.download'['download'] at line 117: Skipped http://files.deeppavlov.ai/embeddings/wiki-news-300d-1M.vec download because of matching hashes
2020-04-20 07:08:38.343 INFO in 'deeppavlov.download'['download'] at line 117: Skipped http://files.deeppavlov.ai/embeddings/wiki-news-300d-1M-char.vec download because of matching hashes
2020-04-20 07:08:38.364 INFO in 'deeppavlov.models.preprocessors.squad_preprocessor'['squad_preprocessor'] at line 310: SquadVocabEmbedder: loading saved tokens vocab from C:\Users\Administrator\.deeppavlov\models\squad_model\emb\vocab_embedder.pckl
2020-04-20 07:08:39.158 INFO in 'deeppavlov.models.preprocessors.squad_preprocessor'['squad_preprocessor'] at line 310: SquadVocabEmbedder: loading saved chars vocab from C:\Users\Administrator\.deeppavlov\models\squad_model\emb\char_vocab_embedder.pckl
2020-04-20 07:08:40.599 INFO in 'deeppavlov.core.layers.tf_layers'['tf_layers'] at line 615: 
Warning! tf.contrib.cudnn_rnn.CudnnCompatibleGRUCell is used. It is okay for inference mode, but if you train your model with this cell it could NOT be used with tf.contrib.cudnn_rnn.CudnnGRUCell later. 
2020-04-20 07:08:41.185 INFO in 'deeppavlov.core.layers.tf_layers'['tf_layers'] at line 615: 
Warning! tf.contrib.cudnn_rnn.CudnnCompatibleGRUCell is used. It is okay for inference mode, but if you train your model with this cell it could NOT be used with tf.contrib.cudnn_rnn.CudnnGRUCell later. 
2020-04-20 07:08:41.520 INFO in 'deeppavlov.core.layers.tf_layers'['tf_layers'] at line 615: 
Warning! tf.contrib.cudnn_rnn.CudnnCompatibleGRUCell is used. It is okay for inference mode, but if you train your model with this cell it could NOT be used with tf.contrib.cudnn_rnn.CudnnGRUCell later. 
2020-04-20 07:08:41.748 INFO in 'deeppavlov.core.layers.tf_layers'['tf_layers'] at line 615: 
Warning! tf.contrib.cudnn_rnn.CudnnCompatibleGRUCell is used. It is okay for inference mode, but if you train your model with this cell it could NOT be used with tf.contrib.cudnn_rnn.CudnnGRUCell later. 
2020-04-20 07:09:23.205 INFO in 'deeppavlov.core.models.tf_model'['tf_model'] at line 51: [loading model from C:\Users\Administrator\.deeppavlov\models\squad_model\model]
INFO:tensorflow:Restoring parameters from C:\Users\Administrator\.deeppavlov\models\squad_model\model
[['library for NLP and dialog systems'], [14], [8040850.5]]

it runs normally but instead of giving me any option to interact like a prompt or something it just stops.

My goal is to get a prompt where I can input and get a output (text etc). I know I'm probably doing some very basic mistake of python notebook/cell usage, if you need more info please ask. thanks.


Solution

  • DeepPavlov comes with a bunch of predefined components powered by TensorFlow and Keras for solving NLP-related problems.

    The one you are using is the BERT for Question Answering. Context question answering is the task of finding an answer to a question over a given context (e.g, a paragraph from Wikipedia), where the answer to each question is a segment of the context.

    The model returns the below on calling model(contexts_list, questions_list)

    answers_list, answers_starts_list, logits_list
    

    Example 1 :

    from deeppavlov import build_model, configs
    model = build_model(configs.squad.squad_bert)
    model(['DeepPavlov is library for NLP and dialog systems.'], ['What is DeepPavlov?'])
    

    Output-

    INFO:tensorflow:Restoring parameters from C:\Users\RF00538236\.deeppavlov\models\squad_bert\model
    [['library for NLP and dialog systems'], [14], [158.80197143554688]]
    

    The answer returns the answer list, answer start list(answer is present at 14th character) and logits list.

    Example 2 : Have copied the context about Mahatma Gandhiji from here.

    model(['Born and raised in a Hindu family in coastal Gujarat, western India, Gandhi was trained in law at the Inner Temple, London, and called to the bar at age 22 in June 1891. After two uncertain years in India, where he was unable to start a successful law practice, he moved to South Africa in 1893 to represent an Indian merchant in a lawsuit. He went on to stay for 21 years. It was in South Africa that Gandhi raised a family, and first employed nonviolent resistance in a campaign for civil rights.'],['What was Gandhi trained at?'])
    

    Output -

    [['law'], [91], [106616.5390625]]
    

    Alternatively, you can build deeppavlov Interacting with the model. You can interact with the model by running it from the command line with interact parameter and the name of the model's configuration file (-d indicates to download all required files). Else you can use build_model from the Python Jupyter code as in the below example.

    Install the dependecies -

    !python -m deeppavlov install tfidf_logreg_en_faq
    

    Example 1 -

    from deeppavlov import configs
    from deeppavlov.core.common.file import read_json
    from deeppavlov.core.commands.infer import build_model
    
    faq = build_model(configs.faq.tfidf_logreg_en_faq, download = True)
    a = faq(["I need help"])
    a
    

    Output -

    [['If you have any further inquiries, you can address them to the International Students Office, which is located in the Auditorium Building, Room 315. The phone number is (7-495) 408-7043.'],
     [[0.0005971888349561158,
       0.0004990413077070781,
       0.0003260898111600398,
       0.0004955716039888539,
       0.9920733828654503,
       0.0004564850775432216,
       0.0012178910790545932,
       0.0006631341572001673,
       0.0006362137679856412,
       0.0010445215260383672,
       0.0011939766282353169,
       0.0004438081627736979,
       0.00035269517790671484]]]
    

    Example 2 -

    a = faq(["i need help on medical offices"])
    a
    

    Output -

    [['All Russian universities have medical offices for first aid and general medical care.'],
     [[0.00030839735143966185,
       0.0005853193249863691,
       0.0004579660993813656,
       0.0004773336684218436,
       0.4791163218068051,
       0.00028413386610718364,
       0.0009917714916957442,
       0.00047599362403134946,
       0.000766086074333974,
       0.0007509714241461073,
       0.5120912464072629,
       0.0032745806081316926,
       0.0004198782532568704]]]
    

    Hope this answers your question. Happy Learning.