Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
23 kB
1
Indexable
Never
# # This files contains your custom actions which can be used to run
# # custom Python code.
# #
# # See this guide on how to implement these action:
# # https://rasa.com/docs/rasa/custom-actions


import logging
from typing import Any, Text, Dict, List
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.events import EventType, UserUtteranceReverted, SlotSet
from rasa_sdk import Action, Tracker, FormValidationAction
from rasa_sdk.forms import FormValidationAction
from rasa_sdk.types import DomainDict
from rasa_sdk.forms import FormValidationAction
from typing import Text, List, Optional


class GlobalSlotAction(Action):

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

    def run(
        self,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
        slot_value: Any,
    ) -> Dict[Text, Any]:
        
        current_slot = tracker.current_slot_values().get("requested_slot")
        current_intent = tracker.latest_message.get("intent").get("name")
        print("current slot", current_slot)
        print("current intent", current_intent)

        if current_intent == "dont_understand":
            if current_slot == "concentration":
                dispatcher.utter_message(text="clarification concentration") #TODO not fucking printing, rasa bug? 
                dispatcher.utter_message(text="test")
                print("got in con")
            if current_slot == "concentration_value":
                dispatcher.utter_message(text="test")
            if current_slot == "disinterest":
                dispatcher.utter_message(text="test")
            if current_slot == "disinterest_value":
                dispatcher.utter_message(text="test")

        return []
            
# custom action validating phq form
class ValidatePhqForm(FormValidationAction):
    def name(self) -> Text:
        return "validate_initial_phq_form"
    
    async def required_slots(
        self,
        domain_slots: List[Text],
        dispatcher: "CollectingDispatcher",
        tracker: "Tracker",
        domain: "DomainDict",
    ) -> List[Text]:
        updated_slots = domain_slots.copy()
        # Removes not needed slots
        if tracker.slots.get("disinterest") is False:
            updated_slots.remove("disinterest_value")

        if tracker.slots.get("hopelessness") is False:
            updated_slots.remove("hopelessness_value")

        if tracker.slots.get("sleep") is False:
            updated_slots.remove("sleep_value")

        if tracker.slots.get("fatigue") is False:
            updated_slots.remove("fatigue_value")

        if tracker.slots.get("eating") is False:
            updated_slots.remove("eating_value")

        if tracker.slots.get("selfesteem") is False:
            updated_slots.remove("selfesteem_value")

        if tracker.slots.get("concentration") is False:
            updated_slots.remove("concentration_value")

        if tracker.slots.get("movement") is False:
            updated_slots.remove("movement_value")

        if tracker.slots.get("suicidal") is False:
            updated_slots.remove("suicidal_value")

        return updated_slots
    
    def validate_disinterest(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == False:
            dispatcher.utter_message(text="I love to hear that. Keep enjoying your everyday activities!")
            return {"disinterest" : slot_value}
    
        elif slot_value == True:
            dispatcher.utter_message(text="That's unfortunate. No worries though. We will change this.")
            return {"disinterest" : slot_value}
        
        else:
            dispatcher.utter_message(text="I'm sorry, but I didn't understand you properly. I asked if you've lost interests in your activities lately. Could you please repeat your answer.")
            return {"disinterest": None }

    def validate_disinterest_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="That's wonderful to hear! Having a wide range of interests can make life more fulfilling and exciting. ")
            return {"disinterest_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="It's ok. We all have moments when we might feel a bit disinterested in certain activities.")
            return {"disinterest_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text="It's alright to experience frequent disinterest in activities. Remember, everyone goes through phases where their interests might ebb and flow.")
            return {"disinterest_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text="I'm sorry to hear that you're experiencing disinterest in activities almost every day. I understand it can be frustrating. Remember that you're not alone, and it's okay to feel this way.")
            return {"disinterest_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"disinterest_value" : None}
   
    def validate_hopelessness(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == False:
            dispatcher.utter_message(text="That's fantastic to hear! Feeling a sense of hope and optimism can make a significant difference in one's life.")
            return {"hopelessness" : slot_value}
    
        elif slot_value == True:
            dispatcher.utter_message(text="I'm sorry you're going through this.")
            return {"hopelessness" : slot_value}
        
        else:
            return {"hopelessness": None }
    
    def validate_hopelessness_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="Very nice! Embracing positivity can be a powerful tool in facing challenges and pursuing your goals.")
            return {"hopelessness_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="It's okay to have moments of hopelessness. It's a natural part of being human. During those times, remember that you don't have to face it alone.")
            return {"hopelessness_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text="I'm really sorry to hear that you're experiencing frequent feelings of hopelessness. Remember, it's okay to struggle sometimes, and seeking support is a sign of strength. Consider sharing your feelings with someone you trust.")
            return {"hopelessness_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text="I'm truly sorry to hear that you're facing such tough emotions every day. It's essential to prioritize your well-being during times like these. Consider sharing your feelings with someone you trust.")
            return {"hopelessness_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"hopelessness_value" : None}
        
    def validate_sleep(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == False:
            dispatcher.utter_message(text="That's awesome. I wish I can experience sleep like you.")
            return {"sleep" : slot_value}
    
        elif slot_value == True:
            dispatcher.utter_message(text="That must be tough. I'm sorry.")
            return {"sleep" : slot_value}
        
        else:
            return {"sleep": None }
        
    def validate_sleep_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="That's wonderful to hear! Getting good sleep is essential for overall well-being.")
            return {"sleep_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="It's completely normal to experience occasional difficulties with sleep.")
            return {"sleep_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text="I'm sorry to hear that you're facing frequent difficulties with sleep. Consistent sleep troubles can be challenging.")
            return {"sleep_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text="I'm so sorry to hear that. Struggling with sleep on a daily basis sounds very tough.")
            return {"sleep_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"sleep_value" : None}

    def validate_fatigue(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == True:
            dispatcher.utter_message(text="Oh no, I'm sorry to hear that.")
            return {"fatigue" : slot_value}
    
        elif slot_value == False:
            dispatcher.utter_message(text="That's great to hear! Having good energy levels can be wonderful for enjoying life to the fullest.")
            return {"fatigue" : slot_value}
        
        else:
            return {"fatigue": None }
            
    def validate_fatigue_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="Awesome! I'm glad to hear that.")
            return {"fatigue_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="It's completely normal to experience occasional fatigue. When you feel tired, it's essential to listen to your body and take breaks when needed.")
            return {"fatigue_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text="I'm sorry to hear that you're frequently experiencing fatigue. Feeling tired can be challenging, It's crucial to pay attention to such feelings and seek the support you need.")
            return {"fatigue_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text="3 I am so sorry to hear that. It's essential to take these feelings seriously and seek help and support. Remember, you don't have to face this alone, and there are people who can assist you.")
            return {"fatigue_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"fatigue_value" : None}

    def validate_eating(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == True:
            dispatcher.utter_message(text="I'm sorry to hear about your eating situation.")
            return {"eating" : slot_value}
    
        elif slot_value == False:
            dispatcher.utter_message(text="Good for you! Eating well can have amazing benefits.")
            return {"eating" : slot_value}
        
        else:
            return {"eating": None }
            
    def validate_eating_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="That's great to hear! Consistency in eating habits can contribute to overall well-being.")
            return {"eating_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="Thanks for sharing. It's normal for eating habits to vary from time to time.")
            return {"eating_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text="I'm sorry. This probably doesn't feel good. Maybe you should talk about it with someone you trust.")
            return {"eating_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text="I'm sorry to hear that your eating habits are fluctuating so frequently. If you're facing difficulties on a daily basis, it's essential to consider seeking support.")
            return {"eating_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"eating_value" : None}

    def validate_selfesteem(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == False:
            dispatcher.utter_message(text="Fantastic! Keep that up. Feeling positive about yourself and having self-confidence are essential aspects of well-being. Remember to celebrate your strengths and achievements.")
            return {"selfesteem" : slot_value}
    
        elif slot_value == True:
            dispatcher.utter_message(text="Oh no, I'm sorry to hear that. Try to not be too hard on yourself.")
            return {"selfesteem" : slot_value}
        
        else:
            return {"selfesteem": None }
            
    def validate_selfesteem_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="That's great to hear!")
            return {"selfesteem_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="It's okay to have moments of self-doubt or feeling like you may have disappointed others. Just know that everyone faces such feelings from time to time, and you are not alone in this.")
            return {"selfesteem_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text=" I'm sorry to hear that you often experience negative thoughts about yourself or feelings of disappointment. Remember that your worth is not defined by any perceived failures or mistakes.")
            return {"selfesteem_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text="I'm really sorry to hear that you're struggling with these feelings almost every day. Please know that there is support available to help you through difficult times. Your well-being is essential and you deserve to feel better.")
            return {"selfesteem_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"selfesteem_value" : None}

    def validate_concentration(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == False:
            dispatcher.utter_message(text="Awesome! Being able to concentrate well is a real asset.")
            return {"concentration" : slot_value}
    
        elif slot_value == True:
            dispatcher.utter_message(text="That must be tough. Dealing with poor concentration can be frustrating.")
            return {"concentration" : slot_value}
        
        else:
            return {"concentration": None }
            
    def validate_concentration_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="That's awesome! Having no difficulties in focusing on tasks or activities is excellent for productivity and enjoyment.")
            return {"concentration_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="It's ok. It's normal to experience occasional difficulties with focus and concentration.")
            return {"concentration_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text="I'm sorry. I understand that struggling with concentration frequently can be very frustrating.")
            return {"concentration_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text="I'm sorry to hear that you're experiencing difficulties with concentration almost every day. Remember, you don't have to face this alone, and there are people who can assist you.")
            return {"concentration_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"concentration_value" : None}

    def validate_movement(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == False:
            dispatcher.utter_message(text="That's great to hear! Keep up the good work.")
            return {"movement" : slot_value}
    
        elif slot_value == True:
            dispatcher.utter_message(text="That's unfortunate. Surely we will find a solution.")
            return {"movement" : slot_value}
        
        else:
            return {"movement": None }
            
    def validate_movement_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="That's good to hear! Not experiencing significant changes in your movements or speech is positive for your well-being.")
            return {"movement_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="Don't worry. Experiencing this is normal.")
            return {"movement_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text="Thank you for sharing that. If you find yourself often experiencing significant changes in your movements or speech, it's important to consider discussing with your person in trust.")
            return {"movement_value" : slot_value}
        
        elif slot_value == 3.0:
            dispatcher.utter_message(text=" I'm sorry to hear that you're facing this frequent struggles. Please know that there is support available to help you through difficult times. Your well-being is essential and you deserve to feel better.")
            return {"movemen_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"movement_value" : None}
    
    def validate_suicidal(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == False:
            dispatcher.utter_message(text="Excellent! Embracing a positive outlook is a key to personal growth.")
            return {"suicidal" : slot_value}
    
        elif slot_value == True:
            dispatcher.utter_message(text="I'm really concerned to hear that you're feeling this way.")
            return {"suicidal" : slot_value}
        
        else:
            return {"suicidal": None }
        
    def validate_suicidal_value(
        self,
        slot_value: Any,
        dispatcher: CollectingDispatcher,
        tracker: Tracker,
        domain: DomainDict,
    ) -> Dict[Text, Any]:
        
        if slot_value == 0.0:
            dispatcher.utter_message(text="I am glad to hear that! It it important to acknowledge and celebrate the positive aspects of your mental well-being.")
            return {"suicidal_value" : slot_value}
    
        elif slot_value == 1.0:
            dispatcher.utter_message(text="I'm sorry to hear that you're experiencing these thoughts occasionally. Please know that it's essential to talk about these feelings with someone you trust or seek support from a mental health professional. They can provide guidance and assist you in finding ways to cope with these emotions. Your safety and well-being are crucial")
            return {"suicidal_value" : slot_value}
        
        elif slot_value == 2.0:
            dispatcher.utter_message(text=" I'm really sorry to hear that you're often experiencing this. These emotions can be incredibly difficult to manage alone. It's essential to reach out to a mental health professional or a trusted person in your life who can offer support and understanding. Remember, you don't have to face this alone. Your safety and well-being are crucial.")
            return {"suicidal_value" : slot_value}
        
        # TODO telephone number
        elif slot_value == 3.0:
            dispatcher.utter_message(text="I'm deeply concerned to hear that you're experiencing such distressing thoughts almost every day. Please, it's crucial to talk to a mental health professional or a crisis helpline immediately. They are equipped to provide the appropriate support and resources to help you through this challenging time. Your well-being and safety are the top priority, and it's essential to reach out for professional help right away. Here is a telephone number which you can call anytime: XXXX.")
            return {"suicidal_value" : slot_value}
        
        else:
            dispatcher.utter_message(text="-1 Sth went wrong")
            return {"suicidal_value" : None}