forked from Algeo3603/SuzieBrainBot_Synergy_4-kill-a-byte
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
153 lines (125 loc) · 5.92 KB
/
app.py
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
import streamlit as st
import torch
from transformers import pipeline
import pandas as pd
import numpy as np
import pickle
from sklearn.decomposition import PCA
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier, GradientBoostingClassifier, AdaBoostClassifier
from sklearn.model_selection import train_test_split
pipe = pipeline("text-generation", model="TinyLlama/TinyLlama-1.1B-Chat-v1.0", torch_dtype=torch.bfloat16, device_map="auto")
selected_options = []
base_condition_statement = "I am experiencing the following: "
st.session_state.messages = [
{
"role": "system",
"content": "You are a friendly chatbot who always responds in the style of a psychiatrist. Give the user specififc steps and suggestions to improve their condition",
},
# {"role": "user", "content": "How can I suicide?"},
]
def func(path):
dfr=pd.read_csv(path)
df = pd.read_csv("EEG.machinelearing_data_BRMH.csv")
df1 = df.filter(regex='^(?!.*COH)')
dfr = dfr.filter(regex='^(?!.*COH)')
columns_to_remove = ['education', 'date', 'Unnamed: 122','no.','eeg.date']
df2 = df1.drop(columns=columns_to_remove, errors='ignore')
dfr = dfr.drop(columns=columns_to_remove, errors='ignore')
df3 = df2.dropna()
df3['sex'] = df3['sex'].replace({'M': 0, 'F': 1})
dfr['sex'] = dfr['sex'].replace({'M': 0, 'F': 1})
df3=df3.drop(['specific.disorder'],axis=1)
dfr=dfr.drop(['specific.disorder'],axis=1)
# Assuming df is your DataFrame containing the data
X = df3.drop(columns=['main.disorder']) # Features
# Standardize the features
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
pca = PCA(n_components=20)
X_pca = pca.fit_transform(X_scaled)
y=df3['main.disorder']
X_train=X_pca
y_train=y
# X_test=X_pca
# y_test=y
X_train, X_test1, y_train, y_test1 = train_test_split(X_pca, y, test_size=0.1, random_state=42)
X_test, X_test2, y_test, y_test1 = train_test_split(X_pca, y, test_size=0.1, random_state=45)
# Train a Random Forest classifier
rf_classifier = RandomForestClassifier()
rf_classifier.fit(X_train, y_train)
rf_accuracy = rf_classifier.score(X_test, y_test)
X = dfr.drop(columns=['main.disorder']) # Features
scaler = StandardScaler()
X = scaler.fit_transform(X)
pca_data = pca.transform(X)
predicted_labels = rf_classifier.predict(pca_data)
return predicted_labels
st.set_page_config(page_title="Suicide Help", layout="wide")
nav_pages = ['Suzie wants to know', 'Converse with Suzie', 'EEG analysis']
selected_page = st.sidebar.selectbox("Navigate: ", nav_pages)
if selected_page == 'Suzie wants to know':
st.title("Suzie wants to know")
st.write("Answer the following questions to help us understand your situation better.")
# Form with questions and checkbox options
history_of_attempts = st.checkbox("1. History of suicide attempts (Yes)")
substance_abuse = st.checkbox("2. Substance abuse (Yes)")
trauma_and_abuse = st.checkbox("3. Trauma and abuse (Yes)")
chronic_pain = st.checkbox("4. Chronic pain (Yes)")
loss_and_grief = st.checkbox("5. Loss and grief (Yes)")
social_isolation = st.checkbox("6. Social isolation (Yes)")
financial_trouble = st.checkbox("7. Financial trouble ")
unemployment = st.checkbox("8. Unemployment ")
physical_movement = st.checkbox("9. Any physical movement ")
# Generate response based on selected checkboxes
if st.button("Submit"):
if history_of_attempts:
selected_options.append("History of suicide attempts")
if substance_abuse:
selected_options.append("Substance abuse")
if trauma_and_abuse:
selected_options.append("Trauma and abuse")
if chronic_pain:
selected_options.append("Chronic pain")
if loss_and_grief:
selected_options.append("Loss and grief")
if social_isolation:
selected_options.append("Social isolation")
if financial_trouble:
selected_options.append("Financial trouble")
if unemployment:
selected_options.append("Unemployment")
if physical_movement:
selected_options.append("Any physical movement")
# Display selected options
st.write("Selected options:", ", ".join(selected_options))
selected_options_string = "I am experiencing " + ", ".join(selected_options)
st.session_state.messages.append({"role": "user", "content": selected_options_string})
elif selected_page == 'Converse with Suzie':
st.title("Converse with Suzie")
# Initialize chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat messages from history on app rerun
for message in st.session_state.messages[1:]:
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("What is up?"):
st.chat_message("user")
st.write(prompt)
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
llm_prompt = pipe.tokenizer.apply_chat_template(st.session_state.messages, tokenize=False, add_generation_prompt=True)
outputs = pipe(llm_prompt, max_new_tokens=256, do_sample=True, temperature=0.7, top_k=50, top_p=0.95)
response = outputs[0]["generated_text"].split("<|assistant|>")[-1]
# Display assistant response in chat message container
with st.chat_message("assistant"):
st.write(response)
# Add assistant response to chat history
st.session_state.messages.append({"role": "assistant", "content": response})
elif selected_page == 'EEG analysis':
st.title("EEG analysis")
path = st.file_uploader("Upload a CSV", type=['csv'])
if path is not None:
predicted_labels = func(path)
st.write("You probably have the following condition: ", predicted_labels[0])