File size: 6,024 Bytes
a521442
 
 
 
 
577f02c
a521442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
577f02c
a521442
 
 
 
577f02c
a521442
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import streamlit as st
from llama_guard import moderate_chat, get_category_name
import time
from chat_agent import convo, main
from chat_agent import choose_model1, delete_all_variables
from recommendation_agent import recommend2, choose_model2, is_depressed, start_recommend
from functools import cached_property
from streamlit_js_eval import streamlit_js_eval


# ST : https://docs.streamlit.io/knowledge-base/tutorials/build-conversational-apps

# Set the page to wide mode
st.set_page_config(layout="wide")

# Set the title
st.title('BrighterDays Mentor')

# Adjust sidebar width to take half the screen
col1, col2 = st.columns([2, 3])

model = st.sidebar.selectbox(label="Choose the LLM model", options=["mistral-7b-base-model", "mental-health-mistral-7b-finetuned-model"])
print("\n\nSelected LLM model from Dropdown",model)
choose_model1(model)
choose_model2(model)
main()
start_recommend()
# Function to update recommendations in col1
def update_recommendations(sum):
    # with col1:
    #     st.header("Recommendation")
    #     recommend = recommend2(sum)
    #     st.write(recommend)  # Update the content with new_content
    with st.sidebar:
        st.divider()
        st.write("Potential Mental Health Condition:")
        st.write(is_depressed(sum))
        st.header("Mental Health Advice:")
        with st.spinner('Thinking...'):
            #time.sleep(5)
            recommend = recommend2(sum)  # Assuming recommend2 doesn't require input
        st.write(recommend)
        
        # Add refresh button (simulated)
        # if st.button("Refresh Chat"):
        #     del st.session_state
        #     delete_all_variables(True)
        #     startup()
        #     st.rerun()

@cached_property
def get_recommendations():
    return "These are some updated recommendations."


def response_generator(response):
    '''
    responds the text with a type writter effect
    '''
    response_buffer = response.strip()
    for word in response_buffer.split():
        yield word + " "
        time.sleep(0.03)


def startup():
    with st.chat_message("assistant"):
        time.sleep(0.2)
        st.markdown("Hi, I am your Mental Health Counselar. How can I help you today?")  

    # Initialize chat history
    if "messages" not in st.session_state:
        st.session_state.messages = []

    for message in st.session_state.messages:
        with st.chat_message(message["role"]):
            st.markdown(message["content"])


    # Check if 'llama_guard_enabled' is already in session state, otherwise initialize it
    if 'llama_guard_enabled' not in st.session_state:
        st.session_state['llama_guard_enabled'] = True  # Default value to True

    # Modify the checkbox call to include a unique key parameter
    llama_guard_enabled = st.sidebar.checkbox("Enable LlamaGuard",
                                            value=st.session_state['llama_guard_enabled'],
                                                key="llama_guard_toggle")


    # Update the session state based on the checkbox interaction
    st.session_state['llama_guard_enabled'] = llama_guard_enabled

    #with st.chat_message("assistant"):
        #st.write("Please tell me about your mental health condition and we can explore together. Potential mental health advice that could help you will be in the sidebar as we talk")

    # Accept user input
    #if user_prompt := st.chat_input("Hello, How are you doing today"):
    if user_prompt := st.chat_input(""):
        st.session_state.messages.append({"role": "user", "content": user_prompt})
        with st.chat_message("user"):
            st.markdown(user_prompt)

        with st.chat_message("assistant"):
            print('llama guard enabled',st.session_state['llama_guard_enabled'])
            is_safe = True
            unsafe_category_name = ""
            #added on March 29th
            response = ""
            if st.session_state['llama_guard_enabled']:
                #guard_status = moderate_chat(user_prompt)
                guard_status, error = moderate_chat(user_prompt)
                if error:
                    st.error(f"Failed to retrieve data from Llama Gaurd: {error}")
                else:
                    if 'unsafe' in guard_status[0]['generated_text']:
                        is_safe = False
                        #added on March 24th
                        unsafe_category_name = get_category_name(guard_status[0]['generated_text'])
                        print(f'Guard status {guard_status}, Category name {unsafe_category_name}')
            if is_safe==False:
                #added on March 24th
                response = f"I see you are asking something about {unsafe_category_name} Due to eithical and safety reasons, I can't provide the help you need. Please reach out to someone who can, like a family member, friend, or therapist. In urgent situations, contact emergency services or a crisis hotline. Remember, asking for help is brave, and you're not alone."
                st.write_stream(response_generator(response))
                response,summary = convo("")
                st.write_stream(response_generator(response))
                #update_recommendations(summary)
            else:
                response,summary = convo(user_prompt)
                # print(conversation.memory.buffer)
                time.sleep(0.2)
                st.write_stream(response_generator(response))
                print("This is the response from app.py",response)
                update_recommendations(summary)
            
            st.session_state.messages.append({"role": "assistant", "content": response}) 

# if st.button("Refresh Chat"):
#     st.session_state={'messages': []}
#     print("\n\n refressed session state:::::::::::::::;",st.session_state)
#     startup()
#     st.rerun()
    # delete_all_variables(True)
    # startup()


if st.button("Reset Chat"):
    delete_all_variables()
    streamlit_js_eval(js_expressions="parent.window.location.reload()")
        
startup()