File size: 7,199 Bytes
c23d83a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
151
152
153
154
155
156
157
158
159
import streamlit as st
import pandas as pd
from groq import Groq
import nltk
from nltk.sentiment import SentimentIntensityAnalyzer

# Download NLTK data
nltk.download('vader_lexicon')

# Initialize the Sentiment Analyzer
sia = SentimentIntensityAnalyzer()

# Initialize the Groq client with your API key
client = Groq(api_key="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")


# Define the system message for the model
system_message = {
    "role": "system",
    "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."
}

# Function to reset the chat
def reset_chat():
    st.session_state.messages = []
    st.session_state.chat_title = "New Chat"

# Initialize session state variables
if 'messages' not in st.session_state:
    st.session_state.messages = []
    st.session_state.chat_title = "CLARIMIND"
if 'questionnaire_open' not in st.session_state:
    st.session_state.questionnaire_open = False
if 'questionnaire_submitted' not in st.session_state:
    st.session_state.questionnaire_submitted = False

# Sidebar for user inputs
with st.sidebar:
    st.header("User Inputs")
    name = st.text_input("Name")
    age = st.number_input("Age", min_value=1, max_value=100, value=25)
    location = st.text_input("Location")
    gender = st.selectbox("Gender", options=["Male", "Female", "Other"])

    if st.button("Reset Chat"):
        reset_chat()

# Button to open the questionnaire
if st.button("Please fill the questionnaire"):
    st.session_state.questionnaire_open = True

# Display the questionnaire if the button was clicked
if st.session_state.questionnaire_open and not st.session_state.questionnaire_submitted:
    st.header("Mental Health Screening Questionnaire")
    st.write("Please answer the following questions to help us understand your condition.")

    # Questionnaire for mental health screening
    attention_span = st.radio("Do you find it difficult to concentrate for extended periods?", ["Yes", "No"])
    restlessness = st.radio("Do you often feel restless or unable to sit still?", ["Yes", "No"])
    intrusive_memories = st.radio("Do you experience intrusive or distressing memories of traumatic events?", ["Yes", "No"])
    flashbacks = st.radio("Do you frequently relive past traumatic events (e.g., through flashbacks)?", ["Yes", "No"])
    paranoia = st.radio("Do you feel like others are watching or trying to harm you?", ["Yes", "No"])
    auditory_hallucinations = st.radio("Do you hear voices that others cannot hear?", ["Yes", "No"])
    difficulty_completing_tasks = st.radio("Do you often start tasks but fail to complete them?", ["Yes", "No"])
    emotional_numbness = st.radio("Do you feel emotionally numb or detached from people?", ["Yes", "No"])
    delusions = st.radio("Do you hold beliefs that others find unusual or illogical?", ["Yes", "No"])
    sleep_disturbances = st.radio("Do you experience difficulty falling or staying asleep?", ["Yes", "No"])
    hypervigilance = st.radio("Are you easily startled or always on guard?", ["Yes", "No"])
    impulsivity = st.radio("Do you act on impulses without considering the consequences?", ["Yes", "No"])

# After submitting the questionnaire
if st.button("Submit Questionnaire"):
    patient_persona = (
        f"Patient Profile:\n"
        f"- Name: {name}\n"
        f"- Age: {age}\n"
        f"- Location: {location}\n"
        f"- Gender: {gender}\n"
        f"- Symptoms:\n"
        f"  * Attention Span Issues: {'Yes' if attention_span == 'Yes' else 'No'}\n"
        f"  * Restlessness: {'Yes' if restlessness == 'Yes' else 'No'}\n"
        f"  * Intrusive Memories: {'Yes' if intrusive_memories == 'Yes' else 'No'}\n"
        f"  * Flashbacks: {'Yes' if flashbacks == 'Yes' else 'No'}\n"
        f"  * Paranoia: {'Yes' if paranoia == 'Yes' else 'No'}\n"
        f"  * Auditory Hallucinations: {'Yes' if auditory_hallucinations == 'Yes' else 'No'}\n"
        f"  * Difficulty Completing Tasks: {'Yes' if difficulty_completing_tasks == 'Yes' else 'No'}\n"
        f"  * Emotional Numbness: {'Yes' if emotional_numbness == 'Yes' else 'No'}\n"
        f"  * Delusions: {'Yes' if delusions == 'Yes' else 'No'}\n"
        f"  * Sleep Disturbances: {'Yes' if sleep_disturbances == 'Yes' else 'No'}\n"
        f"  * Hypervigilance: {'Yes' if hypervigilance == 'Yes' else 'No'}\n"
        f"  * Impulsivity: {'Yes' if impulsivity == 'Yes' else 'No'}\n"
    )

    st.session_state.patient_persona = patient_persona

    questionnaire_data = {
        "Name": name,
        "Age": age,
        "Location": location,
        "Gender": gender,
        "Attention Span": attention_span,
        "Restlessness": restlessness,
        "Intrusive Memories": intrusive_memories,
        "Flashbacks": flashbacks,
        "Paranoia": paranoia,
        "Auditory Hallucinations": auditory_hallucinations,
        "Difficulty Completing Tasks": difficulty_completing_tasks,
        "Emotional Numbness": emotional_numbness,
        "Delusions": delusions,
        "Sleep Disturbances": sleep_disturbances,
        "Hypervigilance": hypervigilance,
        "Impulsivity": impulsivity
    }

    df = pd.DataFrame([questionnaire_data])  
    df.to_csv("mental_health_responses.csv", mode='a', header=not pd.io.common.file_exists("mental_health_responses.csv"), index=False)

    st.session_state.questionnaire_open = False
    st.session_state.questionnaire_submitted = True
    st.success("Thank you for completing the questionnaire! Your patient profile has been created.")

# Chat functionality
st.title(st.session_state.chat_title)

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

user_input = st.chat_input("How can I assist you today?")
if user_input:
    sentiment_scores = sia.polarity_scores(user_input)
    sentiment = "Neutral"
    if sentiment_scores['compound'] > 0.05:
        sentiment = "Positive"
    elif sentiment_scores['compound'] < -0.05:
        sentiment = "Negative"

    st.session_state.messages.append({"role": "user", "content": f"{user_input} (Sentiment: {sentiment})"})

    persona = st.session_state.patient_persona if 'patient_persona' in st.session_state else "Patient has not filled the questionnaire."
    messages = [system_message, {"role": "user", "content": persona}] + st.session_state.messages

    try:
        completion = client.chat.completions.create(
            model="llama3-8b-8192",
            messages=messages,
            temperature=1,
            max_tokens=1024,
            top_p=1,
            stream=False,
        )
        response_content = completion.choices[0].message.content if completion.choices else "Sorry, I couldn't generate a response."
    except Exception as e:
        response_content = f"Error: {str(e)}"

    st.session_state.messages.append({"role": "assistant", "content": response_content})
    with st.chat_message("assistant"):
        st.markdown(response_content)