Is there any way to switch between two models

@nik202 I have two models on two different conda environment like

  1. In Arabic
  2. In English

What i want is to switch to particular model when the user enters the message.Like if the user enters in Arabic then it will trigger Arabic model and If users talks in English then it talks in English, Or there is another way ,like translation . What I dont want is like manually add multiple languages in a single model .

I think having both models in two different environments is a problem. I don’t think it would be possible to switch that way. Why not use the same environment?

Yes, you can create a Custom Pipeline Component that uses a translation API to detect the language and translate it into English before preprocessing. Then, in Custom Actions, you can translate back to this language, or utter different responses based on it.

@ChrisRahme Sorry for late reply,

You are saying that First create a bot in Arabic in same environment and then in English then we have two different models in (Models directory).And then use Custom Pipeline Component.

No need to have two models. Just a Custom Pipeline Component that detects the language and translates it to English.

@ChrisRahme Ok means Custom Pipeline Component helps in detecting the language and translate it to English and look for the response in English ,what if I want the response back but in translated format. Let me explain with a example.

Suppose you are the user and I am the developer of the bot, You type Hello in (Arabic or Some other language) but I have not added any intent related to Arabic but I want is that using Custom Pipeline Component and detects the language like Arabic and then translates it to English and look for the responses that I have created in my domain.yml file and translates it back to Arabic to get you the response in Arabic.

Since the Custom Component detected the language, you can use that result in Custom Actions to display the response according to that.

if language == 'ar':
    dispatcher.utter_message('مرحبا كيف يمكنني المساعدة؟')
else:
    dispatcher.utter_message('Hello, how can I help?')

@ChrisRahme This means for every intent I have to define the custom action .Don’t you think this will be lengthy process.I mean suppose I have 15-20 intents with 100 each expressions then for every intent I have to define custom action accordingly.

Yup. Look at one of the custom actions in my project:

class ActionUtterGreet(Action):
    def name(self):
        return 'action_utter_greet'
    
    def run(self, dispatcher, tracker, domain):
        followup_action = 'action_utter_service_types'

        text = get_text_from_lang(
            tracker,
            ['Hi, I’m XXX automated virtual assistant.',
            'Bonjour, je suis l\'assistant virtuel automatisé de XXX.',
            'مرحبًا ، أنا مساعد افتراضي تلقائي لنظام XXX.',
            'Ողջույն, ես XXX ավտոմատացված վիրտուալ օգնական եմ.'])

        if tracker.get_slot('language') is None:
            followup_action = 'action_utter_ask_language'
        
        print('\nBOT:', text)
        dispatcher.utter_message(text = text)

        return [FollowupAction(followup_action)]

get_text_from_lang() is a function that accesses the language from tracker, which is stored in a slot, and outputs the corresponding message in the list.


You can leave all your intents’ examples in English only. That’s what the custom component will avoid - multilingual intents.

But as a response, you will always have to use a custom action, unless you change the source code of Rasa - you will have to create a new topic for that particular case and be very clear in the title.

Back to custom actions, in my example above I explicitly typed the utterances. But you can also use a translation API like in the custom component. My code will become:

class ActionUtterGreet(Action):
    def name(self):
        return 'action_utter_greet'
    
    def run(self, dispatcher, tracker, domain):
        followup_action = 'action_utter_service_types'

        text = translate(tracker, 'Hi, I’m XXX automated virtual assistant.')

        if tracker.get_slot('language') is None:
            followup_action = 'action_utter_ask_language'
        
        print('\nBOT:', text)
        dispatcher.utter_message(text = text)

        return [FollowupAction(followup_action)]

translate() is a function that accesses the language from tracker, which is stored in a slot, and outputs the message translated to that language.

The benefit of this approach is that both the custom component and the custom action will be able to understand any language and speak any language. Of course, you could choose to understand all but only reply in Arabic if the user spoke in Arabic and English if the user spoke anything else for example.

@ChrisRahme Ok I will try this approch if this works will let you know and mark this as a solution.Thank you for providing me the details :slightly_smiling_face:.

1 Like