ans123 commited on
Commit
c23d83a
·
verified ·
1 Parent(s): 1a4b836

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +179 -0
app.py ADDED
@@ -0,0 +1,179 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ from groq import Groq
4
+ import nltk
5
+ from nltk.sentiment import SentimentIntensityAnalyzer
6
+
7
+ # Download NLTK data
8
+ nltk.download('vader_lexicon')
9
+
10
+ # Initialize the Sentiment Analyzer
11
+ sia = SentimentIntensityAnalyzer()
12
+
13
+ # Initialize the Groq client with your API key
14
+ client = Groq(api_key="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")
15
+
16
+ # Custom CSS for full-page background image
17
+ def add_fullpage_background_image():
18
+ st.markdown(
19
+ f"""
20
+ <style>
21
+ .stApp {{
22
+ background-image: url("https://raw.githubusercontent.com/ansabb420/ClariMind/main/img.jpg");
23
+ background-size: cover;
24
+ background-repeat: no-repeat;
25
+ background-position: center;
26
+ background-attachment: fixed;
27
+ height: 100vh;
28
+ overflow: hidden;
29
+ }}
30
+ </style>
31
+ """,
32
+ unsafe_allow_html=True
33
+ )
34
+
35
+ # Add background image
36
+ add_fullpage_background_image()
37
+
38
+ # Define the system message for the model
39
+ system_message = {
40
+ "role": "system",
41
+ "content": "You are a mental health assistant trained to identify symptoms of ADHD, PTSD, and schizophrenia based on user responses and provide appropriate recommendations, resources, and prescriptions based on the patient's condition. Start conversations with empathy and stay supportive, like an expert psychotherapist."
42
+ }
43
+
44
+ # Function to reset the chat
45
+ def reset_chat():
46
+ st.session_state.messages = []
47
+ st.session_state.chat_title = "New Chat"
48
+
49
+ # Initialize session state variables
50
+ if 'messages' not in st.session_state:
51
+ st.session_state.messages = []
52
+ st.session_state.chat_title = "CLARIMIND"
53
+ if 'questionnaire_open' not in st.session_state:
54
+ st.session_state.questionnaire_open = False
55
+ if 'questionnaire_submitted' not in st.session_state:
56
+ st.session_state.questionnaire_submitted = False
57
+
58
+ # Sidebar for user inputs
59
+ with st.sidebar:
60
+ st.header("User Inputs")
61
+ name = st.text_input("Name")
62
+ age = st.number_input("Age", min_value=1, max_value=100, value=25)
63
+ location = st.text_input("Location")
64
+ gender = st.selectbox("Gender", options=["Male", "Female", "Other"])
65
+
66
+ if st.button("Reset Chat"):
67
+ reset_chat()
68
+
69
+ # Button to open the questionnaire
70
+ if st.button("Please fill the questionnaire"):
71
+ st.session_state.questionnaire_open = True
72
+
73
+ # Display the questionnaire if the button was clicked
74
+ if st.session_state.questionnaire_open and not st.session_state.questionnaire_submitted:
75
+ st.header("Mental Health Screening Questionnaire")
76
+ st.write("Please answer the following questions to help us understand your condition.")
77
+
78
+ # Questionnaire for mental health screening
79
+ attention_span = st.radio("Do you find it difficult to concentrate for extended periods?", ["Yes", "No"])
80
+ restlessness = st.radio("Do you often feel restless or unable to sit still?", ["Yes", "No"])
81
+ intrusive_memories = st.radio("Do you experience intrusive or distressing memories of traumatic events?", ["Yes", "No"])
82
+ flashbacks = st.radio("Do you frequently relive past traumatic events (e.g., through flashbacks)?", ["Yes", "No"])
83
+ paranoia = st.radio("Do you feel like others are watching or trying to harm you?", ["Yes", "No"])
84
+ auditory_hallucinations = st.radio("Do you hear voices that others cannot hear?", ["Yes", "No"])
85
+ difficulty_completing_tasks = st.radio("Do you often start tasks but fail to complete them?", ["Yes", "No"])
86
+ emotional_numbness = st.radio("Do you feel emotionally numb or detached from people?", ["Yes", "No"])
87
+ delusions = st.radio("Do you hold beliefs that others find unusual or illogical?", ["Yes", "No"])
88
+ sleep_disturbances = st.radio("Do you experience difficulty falling or staying asleep?", ["Yes", "No"])
89
+ hypervigilance = st.radio("Are you easily startled or always on guard?", ["Yes", "No"])
90
+ impulsivity = st.radio("Do you act on impulses without considering the consequences?", ["Yes", "No"])
91
+
92
+ # After submitting the questionnaire
93
+ if st.button("Submit Questionnaire"):
94
+ patient_persona = (
95
+ f"Patient Profile:\n"
96
+ f"- Name: {name}\n"
97
+ f"- Age: {age}\n"
98
+ f"- Location: {location}\n"
99
+ f"- Gender: {gender}\n"
100
+ f"- Symptoms:\n"
101
+ f" * Attention Span Issues: {'Yes' if attention_span == 'Yes' else 'No'}\n"
102
+ f" * Restlessness: {'Yes' if restlessness == 'Yes' else 'No'}\n"
103
+ f" * Intrusive Memories: {'Yes' if intrusive_memories == 'Yes' else 'No'}\n"
104
+ f" * Flashbacks: {'Yes' if flashbacks == 'Yes' else 'No'}\n"
105
+ f" * Paranoia: {'Yes' if paranoia == 'Yes' else 'No'}\n"
106
+ f" * Auditory Hallucinations: {'Yes' if auditory_hallucinations == 'Yes' else 'No'}\n"
107
+ f" * Difficulty Completing Tasks: {'Yes' if difficulty_completing_tasks == 'Yes' else 'No'}\n"
108
+ f" * Emotional Numbness: {'Yes' if emotional_numbness == 'Yes' else 'No'}\n"
109
+ f" * Delusions: {'Yes' if delusions == 'Yes' else 'No'}\n"
110
+ f" * Sleep Disturbances: {'Yes' if sleep_disturbances == 'Yes' else 'No'}\n"
111
+ f" * Hypervigilance: {'Yes' if hypervigilance == 'Yes' else 'No'}\n"
112
+ f" * Impulsivity: {'Yes' if impulsivity == 'Yes' else 'No'}\n"
113
+ )
114
+
115
+ st.session_state.patient_persona = patient_persona
116
+
117
+ questionnaire_data = {
118
+ "Name": name,
119
+ "Age": age,
120
+ "Location": location,
121
+ "Gender": gender,
122
+ "Attention Span": attention_span,
123
+ "Restlessness": restlessness,
124
+ "Intrusive Memories": intrusive_memories,
125
+ "Flashbacks": flashbacks,
126
+ "Paranoia": paranoia,
127
+ "Auditory Hallucinations": auditory_hallucinations,
128
+ "Difficulty Completing Tasks": difficulty_completing_tasks,
129
+ "Emotional Numbness": emotional_numbness,
130
+ "Delusions": delusions,
131
+ "Sleep Disturbances": sleep_disturbances,
132
+ "Hypervigilance": hypervigilance,
133
+ "Impulsivity": impulsivity
134
+ }
135
+
136
+ df = pd.DataFrame([questionnaire_data])
137
+ df.to_csv("mental_health_responses.csv", mode='a', header=not pd.io.common.file_exists("mental_health_responses.csv"), index=False)
138
+
139
+ st.session_state.questionnaire_open = False
140
+ st.session_state.questionnaire_submitted = True
141
+ st.success("Thank you for completing the questionnaire! Your patient profile has been created.")
142
+
143
+ # Chat functionality
144
+ st.title(st.session_state.chat_title)
145
+
146
+ for message in st.session_state.messages:
147
+ with st.chat_message(message["role"]):
148
+ st.markdown(message["content"])
149
+
150
+ user_input = st.chat_input("How can I assist you today?")
151
+ if user_input:
152
+ sentiment_scores = sia.polarity_scores(user_input)
153
+ sentiment = "Neutral"
154
+ if sentiment_scores['compound'] > 0.05:
155
+ sentiment = "Positive"
156
+ elif sentiment_scores['compound'] < -0.05:
157
+ sentiment = "Negative"
158
+
159
+ st.session_state.messages.append({"role": "user", "content": f"{user_input} (Sentiment: {sentiment})"})
160
+
161
+ persona = st.session_state.patient_persona if 'patient_persona' in st.session_state else "Patient has not filled the questionnaire."
162
+ messages = [system_message, {"role": "user", "content": persona}] + st.session_state.messages
163
+
164
+ try:
165
+ completion = client.chat.completions.create(
166
+ model="llama3-8b-8192",
167
+ messages=messages,
168
+ temperature=1,
169
+ max_tokens=1024,
170
+ top_p=1,
171
+ stream=False,
172
+ )
173
+ response_content = completion.choices[0].message.content if completion.choices else "Sorry, I couldn't generate a response."
174
+ except Exception as e:
175
+ response_content = f"Error: {str(e)}"
176
+
177
+ st.session_state.messages.append({"role": "assistant", "content": response_content})
178
+ with st.chat_message("assistant"):
179
+ st.markdown(response_content)