Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -10,13 +10,13 @@ def reset_conversation():
|
|
10 |
Resets Conversation
|
11 |
'''
|
12 |
st.session_state.messages = []
|
|
|
|
|
|
|
13 |
return None
|
14 |
|
15 |
-
# Define model
|
16 |
-
|
17 |
-
"Biblical Religious Advisor": "NousResearch/Nous-Hermes-2-Yi-34B",
|
18 |
-
"Mental health AI": "NousResearch/Nous-Hermes-2-Yi-34B"
|
19 |
-
}
|
20 |
|
21 |
# Define pre-instructions for each model
|
22 |
model_pre_instructions = {
|
@@ -25,7 +25,7 @@ model_pre_instructions = {
|
|
25 |
}
|
26 |
|
27 |
# Function to interact with the selected model via the Together API
|
28 |
-
def interact_with_together_api(messages, model_link):
|
29 |
all_messages = []
|
30 |
|
31 |
# Add pre-instructions to the message history if it's the first interaction with this model
|
@@ -35,12 +35,18 @@ def interact_with_together_api(messages, model_link):
|
|
35 |
all_messages.append({"role": "system", "content": f"Switched to model: {selected_model}"})
|
36 |
|
37 |
# Append user and assistant messages
|
38 |
-
for
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
url = "https://api.together.xyz/v1/chat/completions"
|
46 |
payload = {
|
@@ -60,17 +66,22 @@ def interact_with_together_api(messages, model_link):
|
|
60 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
61 |
}
|
62 |
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
70 |
return assistant_response
|
71 |
|
|
|
|
|
|
|
|
|
72 |
# Create sidebar with model selection dropdown and reset button
|
73 |
-
selected_model = st.sidebar.selectbox("Select Model", list(
|
74 |
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
75 |
|
76 |
# Add cautionary message about testing phase at the bottom of the sidebar
|
@@ -80,11 +91,18 @@ st.sidebar.markdown("**Note**: This model is still in the beta phase. Responses
|
|
80 |
st.sidebar.image("https://assets.isu.pub/document-structure/221118065013-a6029cf3d563afaf9b946bb9497d45d4/v1/2841525b232adaef7bd0efe1da81a4c5.jpeg", width=200)
|
81 |
st.sidebar.write("A product proudly developed by Kisii University")
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# Initialize chat history
|
84 |
if "messages" not in st.session_state:
|
85 |
st.session_state.messages = []
|
86 |
st.session_state.message_count = 0
|
87 |
st.session_state.ask_intervention = False
|
|
|
88 |
|
89 |
# Display chat messages from history on app rerun
|
90 |
for message in st.session_state.messages:
|
@@ -112,17 +130,25 @@ if prompt := st.chat_input(f"Hi, I'm {selected_model}, ask me a question"):
|
|
112 |
for keyword in intervention_keywords:
|
113 |
if keyword in prompt.lower():
|
114 |
# Intervention logic here
|
115 |
-
st.markdown("<span style='color:red;'>I have a feeling you may need to talk to a therapist. If you agree with me please contact +254793609747; Name: Davis. If you
|
116 |
break # Exit loop once intervention is triggered
|
117 |
|
118 |
# Interact with the selected model
|
119 |
-
|
|
|
|
|
|
|
|
|
|
|
120 |
|
121 |
# Display assistant response in chat message container
|
122 |
-
with st.
|
123 |
-
st.markdown("AI is typing...")
|
124 |
-
st.empty()
|
125 |
st.markdown(assistant_response)
|
126 |
|
127 |
# Add assistant response to chat history
|
128 |
st.session_state.messages.append(("assistant", assistant_response))
|
|
|
|
|
|
|
|
|
|
|
|
10 |
Resets Conversation
|
11 |
'''
|
12 |
st.session_state.messages = []
|
13 |
+
st.session_state.message_count = 0
|
14 |
+
st.session_state.ask_intervention = False
|
15 |
+
st.session_state.diagnosis = None
|
16 |
return None
|
17 |
|
18 |
+
# Define model link for the Addiction Recovery and Mental Health models
|
19 |
+
model_link = "NousResearch/Nous-Hermes-2-Yi-34B"
|
|
|
|
|
|
|
20 |
|
21 |
# Define pre-instructions for each model
|
22 |
model_pre_instructions = {
|
|
|
25 |
}
|
26 |
|
27 |
# Function to interact with the selected model via the Together API
|
28 |
+
def interact_with_together_api(messages, model_link, diagnostic=False):
|
29 |
all_messages = []
|
30 |
|
31 |
# Add pre-instructions to the message history if it's the first interaction with this model
|
|
|
35 |
all_messages.append({"role": "system", "content": f"Switched to model: {selected_model}"})
|
36 |
|
37 |
# Append user and assistant messages
|
38 |
+
for message in messages:
|
39 |
+
if message[0] == "user":
|
40 |
+
all_messages.append({"role": "user", "content": message[1]})
|
41 |
+
else:
|
42 |
+
all_messages.append({"role": "assistant", "content": message[1]})
|
43 |
+
|
44 |
+
if diagnostic:
|
45 |
+
diagnostic_prompt = "Analyze the following conversation and predict the mental issue the user might be suffering from:"
|
46 |
+
all_messages.append({"role": "user", "content": diagnostic_prompt})
|
47 |
+
for message in messages:
|
48 |
+
if message[0] == "user":
|
49 |
+
all_messages.append({"role": "user", "content": message[1]})
|
50 |
|
51 |
url = "https://api.together.xyz/v1/chat/completions"
|
52 |
payload = {
|
|
|
66 |
"Authorization": f"Bearer {TOGETHER_API_KEY}",
|
67 |
}
|
68 |
|
69 |
+
try:
|
70 |
+
response = requests.post(url, json=payload, headers=headers)
|
71 |
+
response.raise_for_status() # Ensure HTTP request was successful
|
72 |
+
response_data = response.json()
|
73 |
+
assistant_response = response_data["choices"][0]["message"]["content"]
|
74 |
+
except requests.exceptions.RequestException as e:
|
75 |
+
assistant_response = "Sorry, I couldn't connect to the server. Please try again later."
|
76 |
+
|
77 |
return assistant_response
|
78 |
|
79 |
+
# Function to diagnose mental health issue
|
80 |
+
def diagnose_mental_health(messages):
|
81 |
+
return interact_with_together_api(messages, model_link, diagnostic=True)
|
82 |
+
|
83 |
# Create sidebar with model selection dropdown and reset button
|
84 |
+
selected_model = st.sidebar.selectbox("Select Model", list(model_pre_instructions.keys()))
|
85 |
st.sidebar.button('Reset Chat', on_click=reset_conversation)
|
86 |
|
87 |
# Add cautionary message about testing phase at the bottom of the sidebar
|
|
|
91 |
st.sidebar.image("https://assets.isu.pub/document-structure/221118065013-a6029cf3d563afaf9b946bb9497d45d4/v1/2841525b232adaef7bd0efe1da81a4c5.jpeg", width=200)
|
92 |
st.sidebar.write("A product proudly developed by Kisii University")
|
93 |
|
94 |
+
# Display diagnosis in the sidebar
|
95 |
+
if st.session_state.message_count >= 4:
|
96 |
+
if "diagnosis" not in st.session_state or st.session_state.diagnosis is None:
|
97 |
+
st.session_state.diagnosis = diagnose_mental_health(st.session_state.messages)
|
98 |
+
st.sidebar.markdown(f"### Diagnosis:\n**{st.session_state.diagnosis}**")
|
99 |
+
|
100 |
# Initialize chat history
|
101 |
if "messages" not in st.session_state:
|
102 |
st.session_state.messages = []
|
103 |
st.session_state.message_count = 0
|
104 |
st.session_state.ask_intervention = False
|
105 |
+
st.session_state.diagnosis = None
|
106 |
|
107 |
# Display chat messages from history on app rerun
|
108 |
for message in st.session_state.messages:
|
|
|
130 |
for keyword in intervention_keywords:
|
131 |
if keyword in prompt.lower():
|
132 |
# Intervention logic here
|
133 |
+
st.markdown("<span style='color:red;'>I have a feeling you may need to talk to a therapist. If you agree with me please contact +254793609747; Name: Davis. If you don't, then keep talking to me as we figure this out.</span>", unsafe_allow_html=True)
|
134 |
break # Exit loop once intervention is triggered
|
135 |
|
136 |
# Interact with the selected model
|
137 |
+
placeholder = st.empty()
|
138 |
+
with placeholder:
|
139 |
+
st.markdown("AI is typing...")
|
140 |
+
|
141 |
+
assistant_response = interact_with_together_api(st.session_state.messages, model_link)
|
142 |
+
placeholder.empty()
|
143 |
|
144 |
# Display assistant response in chat message container
|
145 |
+
with st.chat_message("assistant"):
|
|
|
|
|
146 |
st.markdown(assistant_response)
|
147 |
|
148 |
# Add assistant response to chat history
|
149 |
st.session_state.messages.append(("assistant", assistant_response))
|
150 |
+
|
151 |
+
# Update diagnosis if necessary
|
152 |
+
if st.session_state.message_count >= 4:
|
153 |
+
st.session_state.diagnosis = diagnose_mental_health(st.session_state.messages)
|
154 |
+
st.sidebar.markdown(f"### Diagnosis:\n**{st.session_state.diagnosis}**")
|