Load Agent in Notebook is not working

Trying to get TeachingDataScience/rasa_nlu_exercise_TBD.ipynb at master · yogeshhk/TeachingDataScience · GitHub running with recent Rasa 1.0 (not rasa x)

In notebook rasa agent had to be loaded with “await”. But then, at the usage time, it errors out

Your bot is ready to talk! Type your messages here or send 'stop'
Hi
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-4-8da8be804628> in <module>
----> 1 load_assistant()

<ipython-input-3-67306454dd0d> in load_assistant()
     25             break
     26         responses = agent.handle_text(a)
---> 27         for response in responses:
     28             print(response["text"])

TypeError: 'coroutine' object is not iterable

The usage code is i mport IPython from IPython.display import clear_output from rasa_core.agent import Agent from rasa_core.interpreter import NaturalLanguageInterpreter # from rasa_core.utils import EndpointConfig from rasa.utils.endpoints import EndpointConfig

import time

def load_assistant():
    messages = ["Hi! you can chat in this window. Type 'stop' to end the conversation."]
    model_dir = 'models/nlu/current' # or model_directory defined earlier
    interpreter = NaturalLanguageInterpreter.create(model_dir)
    endpoint = EndpointConfig('http://localhost:5055/webhook')
    agent = Agent.load('models/current/dialogue', interpreter=interpreter, action_endpoint = endpoint)

    print("Your bot is ready to talk! Type your messages here or send 'stop'")
    while True:
        a = input()
        if a == 'stop':
            break
        responses = agent.handle_text(a)
        for response in responses:
            print(response["text"])
load_assistant()   

How to fix?

hi @yogeshkulkarni, try running the below code:

basically I am using asyncio module to handle the text which is why you got the above error

asyncio.run is available in Python >= 3.7, for earlier versions, there is another arrangement.

Apart from this, need to

  • pip install nest_asyncio

  • write following code in the first cell

    import nest_asyncio

    nest_asyncio.apply() print(“Event loop ready.”)

(Ref: Jupyter Notebooks)

Hi, I am facing some other issues here. If I try to load agent model like you did, I get the error
ValueError: You are trying to load a MODEL from ‘/home/anika/Desktop/RasaHost-master/src/RasaHost/sample/models/core-20200319-114712/core’, which is not possible. The model path should be a ‘tar.gz’ file or a directory containing the various model files in the sub-directories ‘core’ and ‘nlu’.

If you want to load training data instead of a model, use agent.load_data(...) instead.

And If a load the model as a tar.gz file, the I get the following error:
AttributeError: ‘RasaNLUInterpreter’ object has no attribute 'type’

I am not sure whether my problem is at training nlu and core properly or somewhere else. :frowning: I am stuck at this for weeks. any help is appreciable. Thanks! :smiley:

1 Like

hey @AnikaTabassum, can you share the screenshot of your code, it will help me to replicate the issue?

Also let me know the version of rasa you are using?

Here is my code… And I’m using:
rasa-nlu Version: 0.15.1 rasa-core Version: 0.14.5 rasa Version: 1.6.1

Ans this is my error traceback:

hey @AnikaTabassum, I have updated the code, test it out and let me know if this helps you:

from rasa.core.interpreter import NaturalLanguageInterpreter, RasaNLUInterpreter
from rasa.core.agent import Agent
from typing import Any, Dict, Text, Optional
import asyncio
import nest_asyncio
nest_asyncio.apply()
print("Event loop ready.")

def _display_bot_response(response: Dict):
    from IPython.display import Image, display  # pytype: disable=import-error

    for response_type, value in response.items():
        if response_type == "text":
            print(value)

def handle_text(message):
    agent= Agent.load(r"C:\Users\Jitesh\Documents\test\models")
    
    loop = asyncio.get_event_loop()

    responses = loop.run_until_complete(agent.handle_text(message))
    for response in responses:
        _display_bot_response(response)
        
handle_text("hi")   

Output:

1 Like

Can you please tell me what is in your “C:\Users\Jitesh\Documents\test\models” directory?

hey @AnikaTabassum, I have unzipped the 20200319-001312.tar.gz(trained model zip file):

hey @AnikaTabassum, you can even run it without unzipping the model file, just pass the model file name as shown below:

agent= Agent.load(r"C:\Users\Jitesh\Documents\test\models\20200319-001312.tar.gz")

Hey Thanks a lot! I am not getting any error anymore… and the chat is working properly.Thanks a bunch :innocent::innocent::innocent:

1 Like

@AnikaTabassum Glad it worked out for you, Thanks. :blush:

1 Like

Hi what version of Rasa does this work with? I am having trouble with from rasa.core.agent import Agent using Rasa 2.1.0

It is working with 2.2.5

@rehmanraza it is that’s great news, I’ve download a virtual machine and I’m using Ubuntu 20.04 along with Facebook. Thank you though