Hello, So, I’m working on integrating Watson Tone Analyser into my Rasa Bot 1- I added this class in actions.py
class ActionMood(Action):
def name(self) -> Text:
return "action_mood"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
#print(actions.mood.mood(intentToAnalyze))
if len(actions.mood.mood(intentToAnalyze)) > 0:
first_mood = actions.mood.mood(intentToAnalyze)[0]
second_mood = ''
SlotSet("mood_first", first_mood)
if len(actions.mood.mood(intentToAnalyze)) == 2:
second_mood = actions.mood.mood(intentToAnalyze)[1]
SlotSet("mood_first", second_mood)
if first_mood == 'fear' or second_mood=='fear':
dispatcher.utter_message(text="Don't be afraid. Everything will be all right")
elif first_mood == 'sadness' or second_mood=='sadness':
dispatcher.utter_message(text="Cheer up. I'm sure there is a solution.")
elif first_mood == 'tentative' or second_mood == 'tentative':
dispatcher.utter_message(text="I am sure you can do better. Don't be shy.")
problem_slot = tracker.get_slot("problem")
if problem_slot == 'homework':
dispatcher.utter_message(text="Maybe you need a tutor. Our faculty provides such service. You can check on the faculty website")
elif problem_slot == 'language':
dispatcher.utter_message(text="The faculty organizes Polish language classes every term. Check the info on the faculty website")
elif problem_slot == 'lost':
dispatcher.utter_message(text="Perhaps you should talk to faculty's psychologist.")
elif problem_slot == 'teacher':
dispatcher.utter_message(text="Oh, this is serious. You should notify the deans office.")
else:
dispatcher.utter_message(text="I think you need real human for this problem")
2- I added this file.py in Rasa bot directory
from rasa.nlu.components import Component
from rasa.nlu import utils
from rasa.nlu.model import Metadata
from ibm_watson import ToneAnalyzerV3
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import os
class ToneAnalyzer(Component):
"""A pre-trained sentiment component"""
name = "emotion"
provides = ["entities"]
requires = []
defaults = {}
language_list = ["en"]
authenticator = IAMAuthenticator('GH.................................')
def __init__(self, component_config=None):
super(ToneAnalyzer, self).__init__(component_config)
def train(self, training_data, cfg, **kwargs):
"""Not needed, because the the model is pretrained"""
pass
def convert_to_rasa(self, value, confidence):
"""Convert model output into the Rasa NLU compatible output format."""
entity = {"value": value,
"confidence": confidence,
"entity": "emotion",
"extractor": "emotion_extractor"}
return entity
def process(self, message, **kwargs):
"""Retrieve the text message, pass it to the classifier
and append the prediction results to the message class."""
tone_analyzer = ToneAnalyzerV3(
version='2021-08-31',
authenticator=authenticator
)
tone_analyzer.set_service_url('https://api.eu-gb...........')
intentToAnalyze = text
tone_analysis = tone_analyzer.tone(
{'text': message.text},
content_type='application/json'
).get_result()
mood = [tone['tone_id'] for tone in tone_analysis['document_tone']['tones']]
score = [tone['score'] for tone in tone_analysis['document_tone']['tones']]
entity = self.convert_to_rasa(mood, score)
message.set("entities", [entity], add_to_output=True)
def persist(self, model_dir):
"""Pass because a pre-trained model is already persisted"""
pass
3- I updated credentiel file
finally I have this error
in run
if len(actions.mood.mood(intentToAnalyze)) > 0:
NameError: name 'actions' is not defined