How to call nlu from jupyter?

I’m trying to train & run an NLU model from jupyter but can’t get the run part working. Training works fine:

import nest_asyncio
nest_asyncio.apply()

from rasa.train import train_nlu
model_path = train_nlu("config.yml", "data/nlu.yml", "models/")

I now want to run the trained nlu model on some sample utterances and get the interpreted intents and named entities. I tried this:

from rasa.nlu.model import Interpreter
interpreter = Interpreter.load("models/")

However it gives the following error:

FileNotFoundError: [Errno 2] No such file or directory: 'models/metadata.json'

I can’t find a metadata.json file on the file system, although there is one inside the gzippe’d model archive. Do I need to unzip the archive?

I had a look at the jupyter page and a dig through the reference docs but couldn’t find anything. Any help appreciated.

Thanks.

OK I found a way, documenting here in the hope it’s useful. Full code to train model, create an interpreter and run against some sample utterances as follows:

import nest_asyncio
nest_asyncio.apply()

from rasa.train import train_nlu
model_path = train_nlu("config.yml", "data/nlu.yml", "models/")

from rasa import model
from rasa.nlu.model import Interpreter

unpacked_model = model.get_model("models/")
_core, nlu_model = model.get_model_subdirectories(unpacked_model)

interpreter = Interpreter.load(nlu_model)

import pandas as pd
from IPython.display import display, HTML

def test(utterances):
    """Utility function to run interpreter on a list of utterances and print the results"""
    results = []
    for utterance in utterances:
        result = interpreter.parse(utterance)
        intent = result["intent"]["name"]
        confidence = result["intent"]["confidence"]
        results.append([utterance, intent, confidence])

    #Use a dataframe to print things out nicely
    df = pd.DataFrame(results, columns=["Utterance", "Intent", "Confidence"])
    display(HTML(df.to_html()))

utterances = [
    "I want a pizza",
    "I'm hungry, order me a pizza",
    "please order pizza",
    "call the pizza shop",
    "I'm hungry, get me some food"
]

test(utterances)
Utterance Intent Confidence
0 I want a pizza order_pizza 0.586469
1 I’m hungry, order me a pizza order_pizza 0.686637
2 please order pizza order_pizza 0.977444
3 call the pizza shop order_pizza 0.943890
4 I’m hungry, get me some food nlu_fallback 0.702700
1 Like