Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'coroutine' object is not iterable #3649

Closed
arjunsr1 opened this issue May 29, 2019 · 16 comments
Closed

TypeError: 'coroutine' object is not iterable #3649

arjunsr1 opened this issue May 29, 2019 · 16 comments

Comments

@arjunsr1
Copy link

Rasa version:
rasa 1.0.2
rasa-core-sdk 0.14.0
rasa-sdk 1.0.0

Python version:
Python 3.7.3

Operating system (windows, osx, ...):
Mac osx Mojave

Issue:
When I run the below command:
python dialogue_management_model.py

The error that pops up is this frame below:

WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:

Traceback (most recent call last):
File "dialogue_management_model.py", line 38, in
train_dialogue()
File "dialogue_management_model.py", line 25, in train_dialogue
agent.train(data)
File "/usr/local/lib/python3.7/site-packages/rasa/core/agent.py", line 668, in train
self.policy_ensemble.train(training_trackers, self.domain, **kwargs)
File "/usr/local/lib/python3.7/site-packages/rasa/core/policies/ensemble.py", line 89, in train
policy.train(training_trackers, domain, **kwargs)
File "/usr/local/lib/python3.7/site-packages/rasa/core/policies/memoization.py", line 152, in train
for t in training_trackers
TypeError: 'coroutine' object is not iterable
sys:1: RuntimeWarning: coroutine 'Agent.load_data' was never awaited

I'm unsure of how this is happening, and I saw on another post to add "await" before when i train the agent, but that gave a syntax error. Please help!!

Content of configuration file (config.yml):

{
    "pipeline":"spacy_sklearn",
    "path":"./models/nlu",
    "data":"./data/data.json"
}

Content of domain file (domain.yml):

slots:
  location:
    type: text


intents:
  - greet
  - goodbye
  - inform


entities:
  - location

templates:
  utter_greet:
    - 'Hello! How can I help you today?'
    - 'Hi there!'
  utter_goodbye:
    - 'Goodbye! I hope I was of assistance today :)'
    - 'Bye bye!'
    - 'See you later!'
  utter_ask_location:
    - 'Could you specify the location?'
    - 'In what location?'
    - 'Where, exactly?'


actions:
  - utter_greet
  - utter_goodbye
  - utter_ask_location
  - action_weather

Content of dialogue_management_model.py:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import rasa.core
from rasa.core.agent import Agent
from rasa.core.policies.keras_policy import KerasPolicy
from rasa.core.policies.memoization import MemoizationPolicy
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.core.run import serve_application
from rasa.core import config

logger = logging.getLogger(__name__)

def train_dialogue(domain_file = 'weather_domain.yml',
					model_path = './models/dialogue',
					training_data_file = './data/stories.md'):
					
	agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50)])
	data = agent.load_data(training_data_file)	
	

	agent.train(data)
	
	agent.persist(model_path)
	return agent
	
def run_weather_bot(serve_forever=True):
	interpreter = RasaNLUInterpreter('./models/nlu/default/weathernlu')
	agent = Agent.load('./models/dialogue', interpreter=interpreter)
	rasa.core.run.serve_application(agent ,channel='cmdline')
		
	return agent
	
if __name__ == '__main__':
	train_dialogue()
	run_weather_bot()
@akelad
Copy link
Contributor

akelad commented Jun 3, 2019

Thanks for raising this issue, @tabergma will get back to you about it soon.

@iuria21
Copy link

iuria21 commented Jun 3, 2019

same issue here

@tabergma
Copy link
Contributor

tabergma commented Jun 4, 2019

We use asyncio internally. agent.load_data is a coroutine that needs to be run and waited for. Please try out the following code:

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import logging
import asyncio
import rasa.core
from rasa.core.agent import Agent
from rasa.core.policies.keras_policy import KerasPolicy
from rasa.core.policies.memoization import MemoizationPolicy
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.core.run import serve_application
from rasa.core import config

logger = logging.getLogger(__name__)


async def train_dialogue(domain_file='weather_domain.yml',
                   model_path='./models/dialogue',
                   training_data_file='./data/stories.md'):
    agent = Agent(domain_file, policies=[MemoizationPolicy(),
                                         KerasPolicy(max_history=3, epochs=200,
                                                     batch_size=50)])

    loop = asyncio.get_event_loop()
    data = loop.run_until_complete(agent.load_data(training_data_file))

    agent.train(data)

    agent.persist(model_path)
    return agent


def run_weather_bot(serve_forever=True):
    interpreter = RasaNLUInterpreter('./models/nlu/default/weathernlu')
    agent = Agent.load('./models/dialogue', interpreter=interpreter)
    rasa.core.run.serve_application(agent, channel='cmdline')

    return agent


if __name__ == '__main__':
    train_dialogue()
    run_weather_bot()

As you are using python3.7 you should be able to replace

    loop = asyncio.get_event_loop()
    data = loop.run_until_complete(agent.load_data(training_data_file))

by

    asyncio.run(agent.load_data(training_data_file))

@naoko
Copy link
Contributor

naoko commented Jun 4, 2019

@tabergma, thank you! that solved the problem!

@tabergma tabergma closed this as completed Jun 5, 2019
@neerajb1
Copy link

Hi @tabergma i had tried same but again getting same error. Please let me know what change required

async def train_dialogue(domain_file = 'domain.yml',
model_path = 'models/dialogue',
training_data_file = 'data/stories.md'):

agent = Agent(domain_file, policies = [MemoizationPolicy(),
        KerasPolicy(max_history=3, epochs=200, batch_size=50)])
loop = asyncio.get_event_loop()
data = loop.run_until_complete( agent.load_data(training_data_file ))
agent.train(data)

agent.persist(model_path)
return agent

Error:dialogue_model.py:53: RuntimeWarning: coroutine 'train_dialogue' was never awaited
train_dialogue()

@tabergma
Copy link
Contributor

@neerajb1 As soon as you put the keyword async to the function definition, it becomes a coroutine. If you not await the coroutine, the error you mentioned will show up.

In your case you can simply remove the keyword async from train_dialogue as it is not needed. Please have a look at https://docs.python.org/3/library/asyncio-task.html#coroutine for more information.

Try the following:

def train_dialogue(
  domain_file = 'domain.yml',
  model_path = 'models/dialogue',
  training_data_file = 'data/stories.md'
):

    agent = Agent(domain_file, policies = [MemoizationPolicy(), KerasPolicy(max_history=3, epochs=200, batch_size=50)])

    loop = asyncio.get_event_loop()
    data = loop.run_until_complete( agent.load_data(training_data_file ))

    agent.train(data)
    agent.persist(model_path)
    return agent

@ruchiawasthi63
Copy link

@tabergma
TypeError: 'coroutine' object is not callable
image

@tabergma
Copy link
Contributor

Can you please share your code, otherwise it is hard to figure out what is going wrong. Thanks.

@ruchiawasthi63
Copy link

@tabergma here is the code
domain_file` = 'C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf-pipeline-
master/My_bot/domain_bot.txt'
policy_config = 'C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf-pipeline-
master/My_bot/policies_bot.txt'
training_data_file = 'C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf-pipeline-
master/My_bot/Data/stories_bot.txt'

 fallback = FallbackPolicy(fallback_action_name="utter_unclear",
                      core_threshold=0.7,
                      nlu_threshold=0.7)
fallback2 = TwoStageFallbackPolicy()

agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy(), fallback, fallback2])
loop = asyncio.get_event_loop()
training_data = agent.load_data(training_data_file)
data = loop.run_until_complete(training_data())
agent.train(data)

model_path = agent.persist('C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf-pipeline- 
master/My_bot/model/dialogue')`

@tabergma
Copy link
Contributor

tabergma commented Jul 4, 2019

Can you try

 fallback = FallbackPolicy(fallback_action_name="utter_unclear",
                      core_threshold=0.7,
                      nlu_threshold=0.7)
fallback2 = TwoStageFallbackPolicy()

agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy(), fallback, fallback2])
loop = asyncio.get_event_loop()
data = loop.run_until_complete(agent.load_data(training_data_file))
agent.train(data)

model_path = agent.persist('C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf-pipeline- 
master/My_bot/model/dialogue')`

@ruchiawasthi63
Copy link

@tabergma
Here's another error
`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
in
5 agent = Agent(domain_file, policies=[MemoizationPolicy(), KerasPolicy(), fallback])
6 loop = asyncio.get_event_loop()
----> 7 data = loop.run_until_complete(agent.load_data(training_data_file))
8 agent.train(data)
9 interactive.run_interactive_learning(agent, training_data_file)

 C:\ProgramData\Anaconda3\lib\asyncio\base_events.py in run_until_complete(self, future)
    561 
    562         new_task = not futures.isfuture(future)
--> 563         future = tasks.ensure_future(future, loop=self)
    564         if new_task:
    565             # An exception is raised if the future didn't complete, so there

C:\ProgramData\Anaconda3\lib\asyncio\tasks.py in ensure_future(coro_or_future, loop)
    590         return ensure_future(_wrap_awaitable(coro_or_future), loop=loop)
    591     else:
--> 592         raise TypeError('An asyncio.Future, a coroutine or an awaitable is '
    593                         'required')
    594 

 TypeError: An asyncio.Future, a coroutine or an awaitable is required`

@ruchiawasthi63
Copy link

@tabergma
Although running this alone trains the model;
agent = Agent(domain_file, policies=[MemoizationPolicy(max_history = 5), KerasPolicy(epochs = 100, max_history = 5, batch_size = 50),fallback]) # loop = asyncio.get_event_loop() training_data = agent.load_data(training_data_file) # data = loop.run_until_complete(training_data()) agent.train(training_data) # model_path = agent.persist('C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf- pipeline-master/My_bot/model/dialogue_final_test')

But when i try to do interactive learning, it is not happening
agent = Agent(domain_file, policies=[MemoizationPolicy(max_history = 5), KerasPolicy(epochs = 100, max_history = 5, batch_size = 50),fallback]) # loop = asyncio.get_event_loop() training_data = agent.load_data(training_data_file) # data = loop.run_until_complete(training_data()) agent.train(training_data) # model_path = agent.persist('C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf- pipeline-master/My_bot/model/dialogue_final_test') interactive.run_interactive_learning(agent, training_data_file)

Error:
` Bot loaded. Visualisation at http://localhost:5005/visualization.html.
Type a message and press enter (press 'Ctr-c' to exit).

Processed Story Blocks:   0%|                                                                    | 0/7 [00:00<?, ?it/s]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks: 100%█████████████████████████████████████████████| 
7/7 [00:00<00:00, 319.08it/s, # 
    trackers=1]
<WSGIServer at 0x1ff01e0d630 address=0.0.0.0:5005>
Exception in thread Thread-144:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_core\training\interactive.py", line 1237, in 
record_messages
    _enter_user_message(sender_id, endpoint)
  File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_core\training\interactive.py", line 1087, in 
_enter_user_message
    lambda a: not a)
  File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_core\training\interactive.py", line 312, in 
_ask_or_abort
    answers = questions.ask()
  File "C:\ProgramData\Anaconda3\lib\site-packages\questionary\question.py", line 45, in ask
    return self.unsafe_ask(patch_stdout)
  File "C:\ProgramData\Anaconda3\lib\site-packages\questionary\question.py", line 59, in unsafe_ask
    return self.application.run()
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\application\application.py", line 
709, in run
    return run()
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\application\application.py", line 
682, in run
    run_until_complete(f, inputhook=inputhook)
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\eventloop\defaults.py", line 123, 
in run_until_complete
return get_event_loop().run_until_complete(future, inputhook=inputhook)
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\eventloop\win32.py", line 60, in 
run_until_complete
    raise Exception('Event loop is already running')
Exception: Event loop is already running`

@tabergma
Copy link
Contributor

@ashish1471 Please ask your question in our forum (https://forum.rasa.com/). Also use ``` around your code so that it is more readable. Thanks.

@ashish1471
Copy link

@tabergma
Although running this alone trains the model;
agent = Agent(domain_file, policies=[MemoizationPolicy(max_history = 5), KerasPolicy(epochs = 100, max_history = 5, batch_size = 50),fallback]) # loop = asyncio.get_event_loop() training_data = agent.load_data(training_data_file) # data = loop.run_until_complete(training_data()) agent.train(training_data) # model_path = agent.persist('C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf- pipeline-master/My_bot/model/dialogue_final_test')

But when i try to do interactive learning, it is not happening
agent = Agent(domain_file, policies=[MemoizationPolicy(max_history = 5), KerasPolicy(epochs = 100, max_history = 5, batch_size = 50),fallback]) # loop = asyncio.get_event_loop() training_data = agent.load_data(training_data_file) # data = loop.run_until_complete(training_data()) agent.train(training_data) # model_path = agent.persist('C:/Users/user/Downloads/tutorial-tf-pipeline-master/tutorial-tf- pipeline-master/My_bot/model/dialogue_final_test') interactive.run_interactive_learning(agent, training_data_file)

Error:
` Bot loaded. Visualisation at http://localhost:5005/visualization.html.
Type a message and press enter (press 'Ctr-c' to exit).

Processed Story Blocks:   0%|                                                                    | 0/7 [00:00<?, ?it/s]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks:   0%|                                                      | 0/7 [00:00<?, ?it/s, # trackers=1]


Processed Story Blocks: 100%█████████████████████████████████████████████| 
7/7 [00:00<00:00, 319.08it/s, # 
    trackers=1]
<WSGIServer at 0x1ff01e0d630 address=0.0.0.0:5005>
Exception in thread Thread-144:
Traceback (most recent call last):
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 917, in _bootstrap_inner
    self.run()
  File "C:\ProgramData\Anaconda3\lib\threading.py", line 865, in run
    self._target(*self._args, **self._kwargs)
  File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_core\training\interactive.py", line 1237, in 
record_messages
    _enter_user_message(sender_id, endpoint)
  File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_core\training\interactive.py", line 1087, in 
_enter_user_message
    lambda a: not a)
  File "C:\ProgramData\Anaconda3\lib\site-packages\rasa_core\training\interactive.py", line 312, in 
_ask_or_abort
    answers = questions.ask()
  File "C:\ProgramData\Anaconda3\lib\site-packages\questionary\question.py", line 45, in ask
    return self.unsafe_ask(patch_stdout)
  File "C:\ProgramData\Anaconda3\lib\site-packages\questionary\question.py", line 59, in unsafe_ask
    return self.application.run()
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\application\application.py", line 
709, in run
    return run()
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\application\application.py", line 
682, in run
    run_until_complete(f, inputhook=inputhook)
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\eventloop\defaults.py", line 123, 
in run_until_complete
return get_event_loop().run_until_complete(future, inputhook=inputhook)
  File "C:\ProgramData\Anaconda3\lib\site-packages\prompt_toolkit\eventloop\win32.py", line 60, in 
run_until_complete
    raise Exception('Event loop is already running')
Exception: Event loop is already running`

same problem. Did you able to solve it?

@ahmedbr
Copy link

ahmedbr commented Nov 21, 2019

@tabergma thank you very much, you saved my life :)

@gvijay1788
Copy link

I am getting same issue. THe below code is working with spark 3.0 and below but throwing error with spark version 3.0

temp_df = client.get("TEST_Data_0_2021-11-29_DC", tolerance={'type': 'range', 'to': 'latest'} ).toPandas()
temp_df

Below is error message -

TypeError: 'coroutine' object is not iterable
During handling of the above exception, another exception occurred:

Below code alone is working-
temp_df = client.get("TEST_Data_0_2021-11-29_DC", tolerance={'type': 'range', 'to': 'latest'} ).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants