# Implementation of a pretrained argumentative model

**URL:** <https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120>\
**Category:** Rasa Open Source\
**Created:** [February 21, 2020, 1:27pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120 "2020-02-21T13:27:34Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![TKueng](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tkueng/32/7262_2.png) [@TKueng](https://forum.rasa.com/u/TKueng)\
**Post date:** [February 21, 2020, 1:27pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/1 "2020-02-21T13:27:34Z")

</div>

Hello everyone 👋🖖

I am a Masterstudent at the University of St.Gallen and currently developing a Chatbot for argumentative learning. Throug your amazing masterclass I’ve managed to create the basic chatbot functions, intents and also got my bot up and running on my own server with rasax. What’s missing right now, is that I need to implement a pretrained model as a custom action. This model automatically recognises claims and premises of user and gives feedback for their learning journey. The code for model is the following:

[model.py](https://forum.rasa.com/uploads/short-url/nsPdTvyYZXyzhkmLUql4I37d8x.py) (652 Bytes)

The code for the student feedback is the following:

[app.py](https://forum.rasa.com/uploads/short-url/vaCG8fZ736qyAPpoYbW0E7fmLoN.py) (3.7 KB)

Right now I’m looking for help to figure out, what code is relevant for my chatbot, that I developed with rasa. That means which lines of code can I implement without any changes and which lines do I need to adapt or even write completely new.

Your help would be highly appreciated. Thank you and greetings from Switzerland.🙌

---

<div class="post-metadata">

**Author:** ![dakshvar22](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/dakshvar22/32/3993_2.png) [@dakshvar22](https://forum.rasa.com/u/dakshvar22)\
**Post date:** [March 2, 2020, 4:05pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/2 "2020-03-02T16:05:13Z")

</div>

Hi @TKueng, would request you to kindly follow this blog post - [The Rasa Masterclass Handbook: Episode 6](https://blog.rasa.com/the-rasa-masterclass-handbook-episode-6-2/) to understand how to implement custom actions. Feel free to ask specific questions post that. Thanks

---

<div class="post-metadata">

**Author:** ![TKueng](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tkueng/32/7262_2.png) [@TKueng](https://forum.rasa.com/u/TKueng)\
**Post date:** [March 3, 2020, 1:30pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/3 "2020-03-03T13:30:52Z")

</div>

Hello @dakshvar22

Thank your for your reply. My specific questions right now would be, how I can load the pretrained model into the action file. I tried to integrate to integrate the original function into the run function of the action file.

```
    def load_bert_model():
        """Load in the pre-trained model"""
        global model
        model_path = './models.py'
        model = Inferencer.load(model_path)

     def run(self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
    load_bert_model()

```

Do you have any thoughts on that, is this the right way? Cause right now I’m getting this error:

```
NameError: name 'load_bert_model' is not defined

```

Also I wanted to know with which argument can I load the text of the Users. Is this Text, like I used in the functions below?

```
 elements = predict_components(Text) 
 feedback = prepare_feedback(Text, elements)

```

Thank you really much for your help!

---

<div class="post-metadata">

**Author:** ![dakshvar22](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/dakshvar22/32/3993_2.png) [@dakshvar22](https://forum.rasa.com/u/dakshvar22)\
**Post date:** [March 9, 2020, 1:28pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/4 "2020-03-09T13:28:46Z")

</div>

@TKueng Can you share your action file as well?

---

<div class="post-metadata">

**Author:** ![TKueng](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tkueng/32/7262_2.png) [@TKueng](https://forum.rasa.com/u/TKueng)\
**Post date:** [March 11, 2020, 11:10am UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/5 "2020-03-11T11:10:43Z")

</div>

Hello @dakshvar22Of course! Current action file looks like this:

```
from typing import Any, Text, Dict, List

from farm.infer import Inferencer
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher

class ActionGiveFeedback(Action):

def __init__ (self):
    self.model = Inferencer.load("C:\\Users\\tobia\\OneDrive\\Desktop\\arguebot_test\\Model")

def name(self) -> Text:
    return "action_give_feedback"

def predict_components(text: str):
    text_to_analyze = [{'text': '{}'.format(text)}]
    result = self.model.inference_from_dicts(dicts=text_to_analyze)

    annotated_text = [[i['label'], i['start'], i['end']] for i in result[0]['predictions'] if i['probability'] > 0.75]

    count = 0
    count_claim = 0
    count_premise = 0
    elements = []
    for ann in annotated_text:
        if ann[0] != 'O':
            elements.append({
                'id': count,
                'label': ann[0].lower(),
                'start': ann[1],
                'end': ann[2]
            })
            if ann[0].lower() == 'claim':
                count_claim += 1
            else:
                count_premise += 1
        else:
            continue
        count += 1

    return elements, count_claim, count_premise

def prepare_feedback(text: str, elements: tuple):
    feedback_text = "Hier kommt das Feedback zu Deiner Argumentation, " \
                    "Claims werden *fett* und Premises _kursiv_ dargestellt:\n\n\n"
    before = 0
    for e in elements[0]:
        start = e['start']
        end = e['end']
        marker = '*' if e['label'] == 'claim' else '_'
        feedback_text += text[before:start]
        feedback_text += marker
        feedback_text += text[start:end]
        feedback_text += marker
        before = end
    if before == 0:
        feedback_text += text

    if elements[1] > elements[2] or elements[1] < 2:
        if elements[1] < 2:
            feedback_text += "\n\nIch würde dir empfehlen, deinen Text noch argumentativer zu gestalten. " \
                             "Versuche mindestens zwei Claims mit relevanten Premises zu stützen\n"
        else:
            feedback_text += "\n\nIch würde dir empfehlen, deinen Text noch argumentativer zu gestalten. " \
                             "Versuche Deine Claims besser mit relevanten Premises zu stützen\n"
    else:
        feedback_text += "\n\nIch empfinde Deine Argumentation als gelungen! " \
                         "Du hast mehrere Aussagen gemacht und diese mit relevanten Premises gestützt. Weiter so!\n"
    return feedback_text

def run(self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

    elements = predict_components(Text) 
    feedback = prepare_feedback(Text, elements)
    dispatcher.utter_message("{}".format(feedback))
    return []

```

The current error occurs when I try to load the BERT-Model:

File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\runpy.py”, line 193, in _run\_module\_as\_main “ **main** ”, mod\_spec) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\runpy.py”, line 85, in run\_code exec(code, run\_globals) File "C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa\_sdk\_main.py", line 33, in main() File "C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa\_sdk\_main_.py", line 29, in main main\_from\_args(cmdline\_args) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa\_sdk\__main_\_.py”, line 20, in main\_from\_args args.ssl\_password, File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa\_sdk\endpoint.py”, line 116, in run app = create\_app(action\_package\_name, cors\_origins=cors\_origins) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa\_sdk\endpoint.py”, line 68, in create\_app executor.register\_package(action\_package\_name) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa\_sdk\executor.py”, line 222, in register\_package self.register\_action(action) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa\_sdk\executor.py”, line 157, in register\_action action = action() File “C:\Users\tobia\OneDrive\Desktop\arguebot\_test\actions.py”, line 15, in **init** self.model = Inferencer.load(“C:\Users\tobia\OneDrive\Desktop\arguebot\_test\Model”) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\farm\infer.py”, line 133, in load model = AdaptiveModel.load(model\_name\_or\_path, device, strict=strict) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\farm\modeling\adaptive\_model.py”, line 125, in load language\_model = LanguageModel.load(load\_dir) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\farm\modeling\language\_model.py”, line 108, in load language\_model = cls.subclasses[config[“name”]].load(pretrained\_model\_name\_or\_path) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\farm\modeling\language\_model.py”, line 323, in load bert.model = BertModel.from\_pretrained(farm\_lm\_model, config=bert\_config, \*\*kwargs) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\modeling\_utils.py”, line 414, in from\_pretrained elif os.path.isfile(pretrained\_model\_name\_or\_path) or is\_remote\_url(pretrained\_model\_name\_or\_path): File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\transformers\file\_utils.py”, line 143, in is\_remote\_url parsed = urlparse(url\_or\_filename) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\urllib\parse.py”, line 367, in urlparse url, scheme, \_coerce\_result = \_coerce\_args(url, scheme) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\urllib\parse.py”, line 123, in \_coerce\_args return \_decode\_args(args) + (\_encode\_result,) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\urllib\parse.py”, line 107, in \_decode\_args return tuple(x.decode(encoding, errors) if x else ‘’ for x in args) File “C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\urllib\parse.py”, line 107, in return tuple(x.decode(encoding, errors) if x else ‘’ for x in args) AttributeError: ‘WindowsPath’ object has no attribute ‘decode’

Pretty stuck with this error for a while now…😓

---

<div class="post-metadata">

**Author:** ![dakshvar22](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/dakshvar22/32/3993_2.png) [@dakshvar22](https://forum.rasa.com/u/dakshvar22)\
**Post date:** [March 12, 2020, 4:21pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/6 "2020-03-12T16:21:54Z")

</div>

I see `farm.infer` is your self-written custom module. Isn’t the error being raised from there when you make the call to `Inferencer.load`?

```auto
157, in register_action action = action() File “C:\Users\tobia\OneDrive\Desktop\arguebot_test\actions.py”, line 15, in init self.model = Inferencer.load(“C:\Users\tobia\OneDrive\Desktop\arguebot_test\Model”)

```

You need to double check your custom code. I would suggest running `Inferencer.load` outside the rasa project once.

---

<div class="post-metadata">

**Author:** ![TKueng](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tkueng/32/7262_2.png) [@TKueng](https://forum.rasa.com/u/TKueng)\
**Post date:** [March 23, 2020, 10:25am UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/7 "2020-03-23T10:25:32Z")

</div>

Hello @dakshvar22

So you were right there was a problem with model. It could not load the pretrained model, because of different python, pytorch and farm versions. So what I did now, is that I retrained the model with adequate versions and I can actually load the model into my chatbot now. But when I try to trigger the custom action I get this error, which doesn’t make a lot of sense for me…

```
2020-03-23 10:50:23 INFO rasa_sdk.executor - Registered function for 'action_give_feedback'.
Exception occurred while handling uri: 'http://localhost:5055/webhook'
Traceback (most recent call last):
File "C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site- packages\sanic\app.py", line 942, in handle_request
response = await response
 File "C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\sitepackages\rasa_sdk\endpoint.py", line 86, in webhook
result = await executor.run(action_call)
File "C:\Users\tobia\AppData\Local\Programs\Python\Python36\lib\site-packages\rasa_sdk\executor.py", line 280, in run
   events = action(dispatcher, tracker, domain)
  File "C:\Users\tobia\OneDrive\Desktop\arguebot_test\actions.py", line 83, in run
elements = predict_components(Text) #Todo text muss noch geholt werden
   NameError: name 'predict_components' is not defined

```

But as you can see in my code above I defined the name _predict\_components_ function… Any thoughts on this issue??

---

<div class="post-metadata">

**Author:** ![dakshvar22](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/dakshvar22/32/3993_2.png) [@dakshvar22](https://forum.rasa.com/u/dakshvar22)\
**Post date:** [March 24, 2020, 6:28am UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/8 "2020-03-24T06:28:00Z")

</div>

`predict_components` is supposed to be a method of the class `ActionGiveFeedback` while I don’t see a `self` variable in its function signature. After doing that you need to invoke it as `self.predict_components(text)`

---

<div class="post-metadata">

**Author:** ![TKueng](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tkueng/32/7262_2.png) [@TKueng](https://forum.rasa.com/u/TKueng)\
**Post date:** [March 24, 2020, 2:34pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/9 "2020-03-24T14:34:55Z")

</div>

Thanks @dakshvar22Close to my breakthrough. Gonna quickly post my updatet code…

```
from typing import Any, Text, Dict, List

from farm.infer import Inferencer
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher 

class ActionGiveFeedback(Action):

    def __init__ (self):
       self.model = Inferencer.load("C:\\Users\\tobia\\OneDrive\\Desktop\\arguebot_test\\ModelTob")

    def name(self) -> Text:
        return "action_give_feedback"

    def predict_components(self, text :str):
        text_to_analyze = [{'text': '{}'.format(text)}]
        result = self.model.inference_from_dicts(dicts=text_to_analyze)
 
        annotated_text = [[i['label'], i['start'], i['end']] for i in result[0]['predictions'] if i['probability'] > 0.75]
        count = 0
        count_claim = 0
        count_premise = 0
        elements = []
        for ann in annotated_text:
            if ann[0] != 'O':
                elements.append({
                'id': count,
                'label': ann[0].lower(),
                'start': ann[1],
                'end': ann[2]
                })
                if ann[0].lower() == 'claim':
                     count_claim += 1
                else:
                     count_premise += 1
         else:
             continue
         count += 1
  return elements, count_claim, count_premise

def prepare_feedback(self, text: str, elements: tuple):
    feedback_text = "Hier kommt das Feedback zu Deiner Argumentation, " \
                    "Claims werden *fett* und Premises _kursiv_ dargestellt:\n\n\n"
   before = 0
    for e in elements[0]:
        start = e['start']
        end = e['end']
        marker = '*' if e['label'] == 'claim' else '_'
        feedback_text += text[before:start]
        feedback_text += marker
        feedback_text += text[start:end]
        feedback_text += marker
        before = end
    if before == 0:
        feedback_text += text

    if elements[1] > elements[2] or elements[1] < 2:
    
        if elements[1] < 2:
            feedback_text += "\n\nIch würde dir empfehlen, deinen Text noch argumentativer zu gestalten. " \
                             "Versuche mindestens zwei Claims mit relevanten Premises zu stützen\n"
        else:
            feedback_text += "\n\nIch würde dir empfehlen, deinen Text noch argumentativer zu gestalten. " \
                             "Versuche Deine Claims besser mit relevanten Premises zu stützen\n"
        else:
        feedback_text += "\n\nIch empfinde Deine Argumentation als gelungen! " \
                         "Du hast mehrere Aussagen gemacht und diese mit relevanten Premises gestützt. Weiter so!\n"

   return feedback_text

   def run(self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
   
       elements = self.predict_components(???)
       feedback = self.prepare_feedback(???, elements)
       dispatcher.utter_message(feedback)
       return [feedback]

```

1’000’000 Dollar question now for me is, if I want to have the input text of the user as argument(placeholder=???), what is the correct term for that? With Text or text the function is not working…

---

<div class="post-metadata">

**Author:** ![JulianGerhard](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/juliangerhard/32/3781_2.png) [@JulianGerhard](https://forum.rasa.com/u/JulianGerhard)\
**Post date:** [March 24, 2020, 2:41pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/10 "2020-03-24T14:41:17Z")

</div>

Hi @TKueng,

you can simply use:

`last_utterance = tracker.latest_message["text"]`

to get the desired text!

Kind regards  
Julian

---

<div class="post-metadata">

**Author:** ![TKueng](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tkueng/32/7262_2.png) [@TKueng](https://forum.rasa.com/u/TKueng)\
**Post date:** [March 26, 2020, 3:05pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/11 "2020-03-26T15:05:20Z")

</div>

Thanks @JulianGerhard

You’re really a true Rasa Superhero. The code is working now! But now I got a different problem…

When I trigger my custom feedback the following is happening:

 ![Timeout%20Error](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/4/4603bc3079f07a4105fcabd62a0abeb804e45224.png)

I get this Timeout Error in the command prompt where I called _rasa shell_ and get the chatbot answer that I wanted to have in the command prompt where I called _rasa actions_.

 ![Code%20in%20second%20Command%20Prompt](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/e/e6cd2b6a9cca633f4aca766c78239290ef971377.png)

I ignored this error and hoped that I would get the response on the rasa server, but I don’t get actually an answer there:

 ![Server](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/7/7ffc6f7add2139ced6f9d1989e920794037751db.png)

Any suggestions that I get answer of the chatbot in the rasa shell command prompt or on my Server?

Best regards und danke vielmals!

Tobias

---

<div class="post-metadata">

**Author:** ![TKueng](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.rasa.com/tkueng/32/7262_2.png) [@TKueng](https://forum.rasa.com/u/TKueng)\
**Post date:** [March 27, 2020, 1:04pm UTC](https://forum.rasa.com/t/implementation-of-a-pretrained-argumentative-model/25120/12 "2020-03-27T13:04:36Z")

</div>

@JulianGerhard @dakshvar22

Okay what I’ve found now this code line in the console.py file and set it to 25.

```
DEFAULT_STREAM_READING_TIMEOUT_IN_SECONDS = 25  

```

No I get my answer back in the right console.

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/rasa/original/2X/7/72fd164300600217eec65a1a42215a2be0c4a58e.png)

But how can I add this time for an answer on the Rasa X tool of my server? I think this is the last step before my chatbot would be fully functioning!

Best regards,

Tobias
