I am trying to add a SentimentAnalyzer
custom component to GitHub - RasaHQ/financial-demo: A demo for a financial services bot. However, during training the component is stuck in a infinite loop. Here’s my code for custom component:
class SentimentAnalyzer(Component):
"""A pre-trained sentiment component"""
name = "sentiment"
defaults = {}
supported_language_list = ["en"]
@classmethod
def required_components(cls) -> List[Type[Component]]:
"""Specify which components need to be present in the pipeline."""
return []
def __init__(self, component_config: Optional[Dict[Text, Any]] = None) -> None:
super().__init__(component_config)
def train(
self,
training_data: TrainingData,
config: Optional[RasaNLUModelConfig] = None,
**kwargs: Any,
) -> None:
"""Train this component.
This is the components chance to train itself provided
with the training data. The component can rely on
any context attribute to be present, that gets created
by a call to :meth:`components.Component.pipeline_init`
of ANY component and
on any context attributes created by a call to
:meth:`components.Component.train`
of components previous to this one."""
pass
def convert_to_rasa(self, value, confidence):
"""Convert model output into the Rasa NLU compatible output format."""
entity = {"value": value,
"confidence": confidence,
"entity": "sentiment",
"extractor": "sentiment_extractor"}
return entity
def process(self, message: Message, **kwargs: Any) -> None:
"""Retrieve the text message, pass it to the classifier
and append the prediction results to the message class."""
sid = SentimentIntensityAnalyzer()
res = sid.polarity_scores(message.get(prop='text', default='test'))
print(res)
key, value = max(res.items(), key=lambda x: x[1])
entity = self.convert_to_rasa(key, value)
if 'entities' not in message.data:
message.set(prop="entities", info=[], add_to_output=True)
message.data["entities"] += [entity]
def persist(self, file_name: Text, model_dir: Text) -> Optional[Dict[Text, Any]]:
"""Persist this component to disk for future loading."""
pass
@classmethod
def load(
cls,
meta: Dict[Text, Any],
model_dir: Optional[Text] = None,
model_metadata: Optional["Metadata"] = None,
cached_component: Optional["Component"] = None,
**kwargs: Any,
) -> "Component":
"""Load this component from file."""
if cached_component:
return cached_component
else:
return cls(meta)
OS: Windows 10
Rasa version: 2.1.3
Rasa SDK version: 2.1.2
Python version: 3.8.6
Some log before the infinite loop:
$ rasa train
...
2020-12-16 23:28:00 INFO rasa.nlu.model - Starting to train component sentiment
2020-12-16 23:28:00 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:28:00 INFO rasa.nlu.model - Starting to train component WhitespaceTokenizer
2020-12-16 23:28:00 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:28:00 INFO rasa.nlu.model - Starting to train component RegexFeaturizer
2020-12-16 23:28:00 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:28:00 INFO rasa.nlu.model - Starting to train component LexicalSyntacticFeaturizer
2020-12-16 23:28:00 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:28:00 INFO rasa.nlu.model - Starting to train component CountVectorsFeaturizer
2020-12-16 23:28:01 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:28:01 INFO rasa.nlu.model - Starting to train component CountVectorsFeaturizer
2020-12-16 23:28:01 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:28:01 INFO rasa.nlu.model - Starting to train component DIETClassifier
Epochs: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 100/100 [01:20<00:00, 1.25it/s, t_loss=1.323, i_acc=1.000, e_f1=0.994]
2020-12-16 23:29:34 INFO rasa.utils.tensorflow.models - Finished training.
2020-12-16 23:29:34 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:29:34 INFO rasa.nlu.model - Starting to train component FallbackClassifier
2020-12-16 23:29:34 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:29:34 INFO rasa.nlu.model - Starting to train component DucklingEntityExtractor
2020-12-16 23:29:34 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:29:34 INFO rasa.nlu.model - Starting to train component SpacyNLP
2020-12-16 23:29:34 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:29:34 INFO rasa.nlu.model - Starting to train component SpacyEntityExtractor
2020-12-16 23:29:34 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:29:34 INFO rasa.nlu.model - Starting to train component EntitySynonymMapper
2020-12-16 23:29:34 INFO rasa.nlu.model - Finished training component.
2020-12-16 23:29:35 INFO rasa.nlu.model - Successfully saved model into 'C:\Users\USER\AppData\Local\Temp\tmpe3__222g\nlu'
NLU model training completed.
2020-12-16 23:29:59 INFO rasa.nlu.components - Added 'SpacyNLP' to component cache. Key 'SpacyNLP-en_core_web_md'.
Training Core model...
Processed story blocks: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 45/45 [00:00<00:00, 937.46it/s, # trackers=1]
Processed story blocks: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 45/45 [00:01<00:00, 42.17it/s, # trackers=34]
Processed story blocks: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 45/45 [00:01<00:00, 25.56it/s, # trackers=50]
Processed story blocks: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 45/45 [00:01<00:00, 26.59it/s, # trackers=50]
Processed rules: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 21/21 [00:00<00:00, 1500.26it/s, # trackers=1]
Processed trackers: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 34/34 [00:00<00:00, 139.31it/s, # actions=132]
Processed actions: 132it [00:00, 3424.15it/s, # examples=132]
Processed trackers: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 534/534 [00:13<00:00, 38.87it/s, # actions=4526]
{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
{'neg': 0.0, 'neu': 1.0, 'pos': 0.0, 'compound': 0.0}
...