rasa run actions error
See original GitHub issueRasa version: 1.5.1
Rasa SDK version (if used & relevant): Latest
Rasa X version (if used & relevant): N/A
Python version: 3.7.5
Operating system (windows, osx, …): Windows
Issue: When I run “rasa run actions” in my terminal, it throws up an error. I have been doing some editing in the “actions.py” file to see if I could add speech recognition to it but it can’t seem to import speech recognition including other packages. Any help would be appreciated because I’ve been stuck on this problem for some time now and can’t seem to figure out what the problem is. I’m starting to think you can’t add new packages to the “actions.py” file and just have to leave it at its default setting.
Error (including full traceback):
Command or request that led to error: rasa run actions
Content of configuration file (config.yml) (if relevant):
language: "en"
pipeline:
- name: "WhitespaceTokenizer"
- name: "RegexFeaturizer"
- name: "CRFEntityExtractor"
- name: "EntitySynonymMapper"
- name: "CountVectorsFeaturizer"
- name: "EmbeddingIntentClassifier"
policies:
- name: MemoizationPolicy
- name: MappingPolicy
- name: EmbeddingPolicy
- name: FormPolicy
Content of domain file (domain.yml) (if relevant):
intents:
- greet_statement
- greet_question
- introduction_statement
- response_1
- goodbye
- affirm
- deny
- inform
- search_provider
entities:
- facility_type
- facility_id
- location
slots:
facility_type:
type: unfeaturized
facility_address:
type: unfeaturized
facility_id:
type: unfeaturized
location:
type: unfeaturized
forms:
- facility_form
actions:
- utter_greet_statement
- utter_greet_question
- utter_response_1
- utter_goodbye
- utter_introduction_statement
- utter_ask_location
- find_facility_types
- find_healthcare_address
- utter_address
templates:
utter_ask_location:
- text: Please provide your city name.
- text: What is your current city?
- text: Please provide your city name or zip code.
- text: Please enter your zip code or city name to find local providers.
utter_address:
- text: The address is {facility_address}.
utter_greet_statement:
- text: Hello how are you?
utter_greet_question:
- text: I am doing good sir. What can I do for you?
- text: I'm having a excellent day sir. How can I help?
- text: My day is going just fine. What can I help you with?
utter_introduction_statement:
- text: Nice to meet you. What is your name?
utter_response_1:
- text: That is wonderful to hear sir. What can I do for you?
- text: Great to hear sir. How can I help?
utter_goodbye:
- text: Goodbye
- text: Bye
Content of actions file (actions.py) (if relevant):
from rasa_sdk import Tracker
from rasa_sdk.executor import CollectingDispatcher
from typing import Dict, Text, Any, List
import requests
from rasa_sdk import Action
from rasa_sdk.events import SlotSet, FollowupAction
from rasa_sdk.forms import FormAction
import os
import speech_recognition as sr
import pyttsx3
import sys
engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[len(voices) - 1].id)
rate = engine.getProperty('rate')
engine.setProperty('rate', rate-23) # Slows down the speaking speed of the engine voice.
def speak(audio):
print("Alice: "+audio)
engine.say(audio)
engine.runAndWait()
class PlayMusic(Action):
def name(self) -> Text:
return "play_music"
def playMusic(self):
d = 'C:/Users/user/Desktop'
app = 'Spotify'
os.system('start ' +d+ '/%s' %app.replace(' ', r'\ '))
return []
# We use the medicare.gov database to find information about 3 different
# healthcare facility types, given a city name, zip code or facility ID
# the identifiers for each facility type is given by the medicare database
# xubh-q36u is for hospitals
# b27b-2uc7 is for nursing homes
# 9wzi-peqs is for home health agencies
ENDPOINTS = {
"base": "https://data.medicare.gov/resource/{}.json",
"xubh-q36u": {
"city_query": "?city={}",
"zip_code_query": "?zip_code={}",
"id_query": "?provider_id={}"
}
}
FACILITY_TYPES = {
"hospital":
{
"name": "hospital",
"resource": "xubh-q36u"
}
}
def command():
cmd = sr.Recognizer()
with sr.Microphone() as source:
cmd.adjust_for_ambient_noise(source) # Adjusts the level to recieve voice even in case of noise in surroundings
print('Listening...')
audio = cmd.listen(source)
try:
query = cmd.recognize_google(audio,language='en-in')
print('User: '+query+'\n')
except sr.UnknownValueError:
speak('Sorry but I cannot understand you. Do you mind typing it out instead?')
query = str(input('Command: '))
return query
def _create_path(base: Text, resource: Text,
query: Text, values: Text) -> Text:
"""Creates a path to find provider using the endpoints."""
if isinstance(values, list):
return (base + query).format(
resource, ', '.join('"{0}"'.format(w) for w in values))
else:
return (base + query).format(resource, values)
def _find_facilities(location: Text, resource: Text) -> List[Dict]:
"""Returns json of facilities matching the search criteria."""
if str.isdigit(location):
full_path = _create_path(ENDPOINTS["base"], resource,
ENDPOINTS[resource]["zip_code_query"],
location)
else:
full_path = _create_path(ENDPOINTS["base"], resource,
ENDPOINTS[resource]["city_query"],
location.upper())
#print("Full path:")
#print(full_path)
results = requests.get(full_path).json()
return results
def _resolve_name(facility_types, resource) ->Text:
for key, value in facility_types.items():
if value.get("resource") == resource:
return value.get("name")
return ""
class FindFacilityTypes(Action):
"""This action class allows to display buttons for each facility type
for the user to chose from to fill the facility_type entity slot."""
def name(self) -> Text:
"""Unique identifier of the action"""
return "find_facility_types"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List:
buttons = []
for t in FACILITY_TYPES:
facility_type = FACILITY_TYPES[t]
payload = "/inform{\"facility_type\": \"" + facility_type.get(
"resource") + "\"}"
buttons.append(
{"title": "{}".format(facility_type.get("name").title()),
"payload": payload})
# TODO: update rasa core version for configurable `button_type`
dispatcher.utter_button_template("utter_greet", buttons, tracker)
return []
class FindHealthCareAddress(Action):
"""This action class retrieves the address of the user's
healthcare facility choice to display it to the user."""
def name(self) -> Text:
"""Unique identifier of the action"""
return "find_healthcare_address"
def run(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict]:
facility_type = tracker.get_slot("facility_type")
healthcare_id = tracker.get_slot("facility_id")
full_path = _create_path(ENDPOINTS["base"], facility_type,
ENDPOINTS[facility_type]["id_query"],
healthcare_id)
results = requests.get(full_path).json()
if results:
selected = results[0]
if facility_type == FACILITY_TYPES["hospital"]["resource"]:
address = "{}, {}, {} {}".format(selected["address"].title(),
selected["city"].title(),
selected["state"].upper(),
selected["zip_code"].title())
elif facility_type == FACILITY_TYPES["nursing_home"]["resource"]:
address = "{}, {}, {} {}".format(selected["provider_address"].title(),
selected["provider_city"].title(),
selected["provider_state"].upper(),
selected["provider_zip_code"].title())
else:
address = "{}, {}, {} {}".format(selected["address"].title(),
selected["city"].title(),
selected["state"].upper(),
selected["zip"].title())
return [SlotSet("facility_address", address)]
else:
print("No address found. Most likely this action was executed "
"before the user choose a healthcare facility from the "
"provided list. "
"If this is a common problem in your dialogue flow,"
"using a form instead for this action might be appropriate.")
return [SlotSet("facility_address", "not found")]
class FacilityForm(FormAction):
"""Custom form action to fill all slots required to find specific type
of healthcare facilities in a certain city or zip code."""
def name(self) -> Text:
"""Unique identifier of the form"""
return "facility_form"
@staticmethod
def required_slots(tracker: Tracker) -> List[Text]:
"""A list of required slots that the form has to fill"""
return ["facility_type", "location"]
def slot_mappings(self) -> Dict[Text, Any]:
return {"facility_type": self.from_entity(entity="facility_type",
intent=["inform",
"search_provider"]),
"location": self.from_entity(entity="location",
intent=["inform",
"search_provider"])}
def submit(self,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]
) -> List[Dict]:
"""Once required slots are filled, print buttons for found facilities"""
location = tracker.get_slot('location')
facility_type = tracker.get_slot('facility_type')
results = _find_facilities(location, facility_type)
button_name = _resolve_name(FACILITY_TYPES, facility_type)
if len(results) == 0:
dispatcher.utter_message(
"Sorry, we could not find a {} in {}.".format(button_name,
location.title()))
return []
buttons = []
# limit number of results to 3 for clear presentation purposes
for r in results[:3]:
if facility_type == FACILITY_TYPES["hospital"]["resource"]:
facility_id = r.get("provider_id")
name = r["hospital_name"]
elif facility_type == FACILITY_TYPES["nursing_home"]["resource"]:
facility_id = r["federal_provider_number"]
name = r["provider_name"]
else:
facility_id = r["provider_number"]
name = r["provider_name"]
payload = "/inform{\"facility_id\":\"" + facility_id + "\"}"
buttons.append(
{"title": "{}".format(name.title()), "payload": payload})
if len(buttons) == 1:
message = "Here is a {} near you:".format(button_name)
else:
if button_name == "home health agency":
button_name = "home health agencie"
message = "Here are {} {}s near you:".format(len(buttons),
button_name)
# TODO: update rasa core version for configurable `button_type`
dispatcher.utter_button_message(message, buttons)
return []
if __name__ == '__main__':
while True:
query = command()
query = query.lower()
if 'play music' in query or 'play a song' in query or 'play Spotify' in query or 'hey can you play music for me?' 'hey can you play music for me?' in query or 'can you open up spotify?' in query:
speak("Playing your music.")
PlayMusic()
sys.exit()
So what I am trying to do is just add a new class. I am trying to see if I can add an action to rasa to open up a music app for me like Spotify but as you can see, doesn’t seem to be going my way. I have never tried it before so that’s why it is being such a struggle for me. I thought everything was going alright until I tried running the actions server and see what would happen from there but that is currently what my error is. If you see any errors within any of my code, I would be more than happy to listen to your input because I am still trying to get the hang of this and improve it in any way I can. Thank you in advance for any help and input! I’ll respond as quickly as possible.
Issue Analytics
- State:
- Created 3 years ago
- Comments:9 (2 by maintainers)
After hours of uninstalling and reinstalling everything… it finally works. It does not throw up an error if I run my code. Thank you for all your help @TyDunn! It was very much appreciated.
I listened to what you said and I uninstalled my virtual environment and installed a brand new one. That seemed to do the trick because it no longer gives me any errors! Thanks again for all your help and I’ll let you know if I have any future problems.
captionthis
@captionthis When you uninstalled and reinstalled, did you start with a brand new virtual environment? If not, I would try that. I would also try it with a later version of Rasa Open Source or maybe even a different environment manager