ogegadavis254 commited on
Commit
d362116
1 Parent(s): ae1aad0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -44
app.py CHANGED
@@ -1,3 +1,4 @@
 
1
  import streamlit as st
2
  import requests
3
  import os
@@ -13,36 +14,6 @@ def reset_conversation():
13
  st.session_state.messages = []
14
  return None
15
 
16
- def analyze_diagnosis(messages, model_link):
17
- # Extract user messages
18
- user_messages = [message[1] for message in messages if message[0] == "user"]
19
-
20
- # Define mental conditions and associated keywords
21
- mental_conditions = {
22
- "Depression": ["depression", "sad", "hopeless", "lonely", "empty", "worthless", "miserable"],
23
- "Anxiety": ["anxiety", "nervous", "worried", "fearful", "panicked", "stressed", "tense"],
24
- "Panic disorder": ["panic attack", "panic", "scared", "terrified", "frightened", "hyperventilate", "heart racing"],
25
- "Bipolar disorder": ["bipolar", "manic", "mania", "euphoric", "energetic", "depressed", "hopeless"],
26
- "Schizophrenia": ["schizophrenia", "hallucination", "delusion", "paranoia", "disorganized", "psychotic", "dissociation"],
27
- "PTSD": ["ptsd", "trauma", "nightmare", "flashback", "startled", "avoidance", "hypervigilance"],
28
- "Obsessive-Compulsive Disorder": ["ocd", "obsession", "compulsion", "intrusive thought", "ritual", "cleaning", "checking"],
29
- "Eating disorder": ["eating disorder", "anorexia", "bulimia", "binge eating", "body image", "weight obsession", "purging"],
30
- "Substance use disorder": ["substance use", "drug addiction", "alcoholism", "substance abuse", "withdrawal", "craving", "dependency"],
31
- "Attention-deficit/hyperactivity disorder (ADHD)": ["adhd", "attention deficit", "hyperactivity", "impulsivity", "inattention", "restlessness", "fidgeting"],
32
- "Borderline personality disorder": ["borderline personality", "emotional instability", "impulsivity", "fear of abandonment", "unstable relationships", "self-harm", "mood swings"],
33
- "Postpartum depression": ["postpartum depression", "baby blues", "motherhood depression", "crying spells", "irritability", "hopelessness", "fatigue"],
34
- "Social anxiety disorder": ["social anxiety", "fear of social situations", "embarrassment", "humiliation", "avoidance", "self-consciousness", "panic"]
35
- }
36
-
37
- # Count occurrences of keywords for each mental condition
38
- condition_counts = {condition: sum(message.lower().count(keyword) for message in user_messages for keyword in keywords)
39
- for condition, keywords in mental_conditions.items()}
40
-
41
- # Determine the condition with the highest count
42
- diagnosis = max(condition_counts, key=condition_counts.get)
43
-
44
- return diagnosis
45
-
46
  # Define model links for the Addiction Recovery and Mental Health models
47
  model_links = {
48
  "Addiction recovery AI": "NousResearch/Nous-Hermes-2-Yi-34B",
@@ -105,20 +76,9 @@ def interact_with_together_api(messages, model_link):
105
  st.error(f"Error communicating with the API: {e}")
106
  return None
107
 
108
- # Initialize chat history
109
- if "messages" not in st.session_state:
110
- st.session_state.messages = []
111
-
112
  # Create sidebar with model selection dropdown and reset button
113
  selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys()))
114
- reset_button = st.sidebar.button('Reset Chat', on_click=reset_conversation)
115
-
116
- # Add diagnostic feature to the sidebar after at least 3 messages are input by the user
117
- if len(st.session_state.messages) >= 3:
118
- diagnosis = analyze_diagnosis(st.session_state.messages, model_links[selected_model])
119
- if diagnosis:
120
- st.sidebar.subheader("Diagnosis")
121
- st.sidebar.write(diagnosis)
122
 
123
  # Add cautionary message about testing phase at the bottom of the sidebar
124
  st.sidebar.markdown("**Note**: This model is still in the beta phase. Responses may be inaccurate or undesired. Use it cautiously, especially for critical issues.")
@@ -127,6 +87,17 @@ st.sidebar.markdown("**Note**: This model is still in the beta phase. Responses
127
  st.sidebar.image("https://assets.isu.pub/document-structure/221118065013-a6029cf3d563afaf9b946bb9497d45d4/v1/2841525b232adaef7bd0efe1da81a4c5.jpeg", width=200)
128
  st.sidebar.write("A product proudly developed by Kisii University")
129
 
 
 
 
 
 
 
 
 
 
 
 
130
  # Accept user input
131
  if prompt := st.chat_input(f"Hi, I'm {selected_model}, let's chat"):
132
  # Display user message in chat message container
@@ -134,6 +105,7 @@ if prompt := st.chat_input(f"Hi, I'm {selected_model}, let's chat"):
134
  st.markdown(prompt)
135
  # Add user message to chat history
136
  st.session_state.messages.append(("user", prompt))
 
137
 
138
  # Interact with the selected model
139
  assistant_response = interact_with_together_api(st.session_state.messages, model_links[selected_model])
@@ -144,13 +116,12 @@ if prompt := st.chat_input(f"Hi, I'm {selected_model}, let's chat"):
144
  st.markdown("AI is typing...")
145
  st.empty()
146
  st.markdown(assistant_response)
147
-
148
  # Check if intervention is needed based on bot response
149
  if any(keyword in prompt.lower() for keyword in ["human", "therapist", "someone", "died", "death", "help", "suicide", "suffering", "crisis", "emergency", "support", "depressed", "anxiety", "lonely", "desperate", "struggling", "counseling", "distressed", "hurt", "pain", "grief", "trauma", "abuse", "danger", "risk", "urgent", "need assistance"]):
150
  # Intervention logic here
151
  if not st.session_state.ask_intervention:
152
  if st.button("After the analysing our session you may need some extra help, so you can reach out to a certified therapist at +25493609747 Name: Ogega feel free to talk"):
153
  st.write("You can reach out to a certified therapist at +25493609747.")
154
-
155
  # Add assistant response to chat history
156
  st.session_state.messages.append(("assistant", assistant_response))
 
1
+
2
  import streamlit as st
3
  import requests
4
  import os
 
14
  st.session_state.messages = []
15
  return None
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Define model links for the Addiction Recovery and Mental Health models
18
  model_links = {
19
  "Addiction recovery AI": "NousResearch/Nous-Hermes-2-Yi-34B",
 
76
  st.error(f"Error communicating with the API: {e}")
77
  return None
78
 
 
 
 
 
79
  # Create sidebar with model selection dropdown and reset button
80
  selected_model = st.sidebar.selectbox("Select Model", list(model_links.keys()))
81
+ st.sidebar.button('Reset Chat', on_click=reset_conversation)
 
 
 
 
 
 
 
82
 
83
  # Add cautionary message about testing phase at the bottom of the sidebar
84
  st.sidebar.markdown("**Note**: This model is still in the beta phase. Responses may be inaccurate or undesired. Use it cautiously, especially for critical issues.")
 
87
  st.sidebar.image("https://assets.isu.pub/document-structure/221118065013-a6029cf3d563afaf9b946bb9497d45d4/v1/2841525b232adaef7bd0efe1da81a4c5.jpeg", width=200)
88
  st.sidebar.write("A product proudly developed by Kisii University")
89
 
90
+ # Initialize chat history
91
+ if "messages" not in st.session_state:
92
+ st.session_state.messages = []
93
+ st.session_state.message_count = 0
94
+ st.session_state.ask_intervention = False
95
+
96
+ # Display chat messages from history on app rerun
97
+ for message in st.session_state.messages:
98
+ with st.chat_message(message[0]):
99
+ st.markdown(message[1])
100
+
101
  # Accept user input
102
  if prompt := st.chat_input(f"Hi, I'm {selected_model}, let's chat"):
103
  # Display user message in chat message container
 
105
  st.markdown(prompt)
106
  # Add user message to chat history
107
  st.session_state.messages.append(("user", prompt))
108
+ st.session_state.message_count += 1
109
 
110
  # Interact with the selected model
111
  assistant_response = interact_with_together_api(st.session_state.messages, model_links[selected_model])
 
116
  st.markdown("AI is typing...")
117
  st.empty()
118
  st.markdown(assistant_response)
 
119
  # Check if intervention is needed based on bot response
120
  if any(keyword in prompt.lower() for keyword in ["human", "therapist", "someone", "died", "death", "help", "suicide", "suffering", "crisis", "emergency", "support", "depressed", "anxiety", "lonely", "desperate", "struggling", "counseling", "distressed", "hurt", "pain", "grief", "trauma", "abuse", "danger", "risk", "urgent", "need assistance"]):
121
  # Intervention logic here
122
  if not st.session_state.ask_intervention:
123
  if st.button("After the analysing our session you may need some extra help, so you can reach out to a certified therapist at +25493609747 Name: Ogega feel free to talk"):
124
  st.write("You can reach out to a certified therapist at +25493609747.")
125
+
126
  # Add assistant response to chat history
127
  st.session_state.messages.append(("assistant", assistant_response))