Hi, rasa community, I followed this post for realizing sentiment analysis. I want to use a pre-trained sentiment analysis model. But instead of using NLTK Vader, I used Transformers pipeline. When testing NLU, it works fine, but when testing the entire chatbot, it will throw a asyncio error.
After typing “hi”:
Bot loaded. Type a message and press enter (use '/stop' to exit):
Your input -> hi
Some layers from the model checkpoint at distilbert-base-uncased-finetuned-sst-2-english were not used when initializing TFDistilBertForSequenceClassification: ['dropout_19']
- This IS expected if you are initializing TFDistilBertForSequenceClassification from the checkpoint of a model trained on another task or with another architecture (e.g. initializing a BertForSequenceClassification model from a BertForPreTraining model).
- This IS NOT expected if you are initializing TFDistilBertForSequenceClassification from the checkpoint of a model that you expect to be exactly identical (initializing a BertForSequenceClassification model from a BertForSequenceClassification model).
After a while, throw error:
2022-04-08 10:53:09 ERROR asyncio - Task exception was never retrieved
future: <Task finished name='Task-2' coro=<configure_app.<locals>.run_cmdline_io() done, defined at /Users/baoyue/miniconda3/envs/rasa3/lib/python3.8/site-packages/rasa/core/run.py:131> exception=TimeoutError()>
Here’s my code:
class SentimentAnalyzer(Component):
"""A pre-trained sentiment component"""
name = "sentiment"
provides = ["entities"]
requires = []
defaults = {}
language_list = ["en"]
def __init__(self, component_config=None):
super(SentimentAnalyzer, 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": "sentiment",
"extractor": "sentiment_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."""
classifier = pipeline("sentiment-analysis", model="distilbert-base-uncased-finetuned-sst-2-english")
res = classifier(str(message.get("text")))
key, value = res[0]['label'], res[0]['score']
entity = self.convert_to_rasa(key, value)
message.set("entities", [entity], add_to_output=True)
def persist(self, file_name, model_dir):
"""Pass because a pre-trained model is already persisted"""
pass
Hope to hear from you soon. Thanks!