-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
81 lines (65 loc) · 2.76 KB
/
Copy pathmain.py
File metadata and controls
81 lines (65 loc) · 2.76 KB
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
import openai
import streamlit as st
import textwrap
import base64
counter = 0
with open('style.css') as f:
st.markdown(f'<style>{f.read()}</style>', unsafe_allow_html=True)
st.title("Mental Health Chatbot")
def set_bg_hack(main_bg):
# set bg name
main_bg_ext = "png"
st.markdown(
f"""
<style>
.stApp {{
background: url(data:image/{main_bg_ext};base64,{base64.b64encode(open(main_bg, "rb").read()).decode()});
background-size: cover
}}
</style>
""",
unsafe_allow_html=True
)
set_bg_hack('magicpattern-grid-pattern-1694581131805.png')
long_text = ""
# Wrap the text to a specified width
wrapped_text = textwrap.fill(long_text, width=10)
#Add wrapped text to the sidebar
with st.sidebar:
st.header("About the Project")
st.write(wrapped_text)
openai.api_key = st.secrets["OPENAI_API_KEY"]
if "messages" not in st.session_state:
st.session_state.messages = []
system_message = {
"role": "system",
"content": "You are a mental health chatbot designed to help users and ask them questions. If the user says anything Suicidal or Homicidal reply with the word 'WARNING'"
}
st.session_state.messages.append(system_message)
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
for message in st.session_state.messages:
if message["role"] != "system":
with st.chat_message(message["role"]):
st.markdown(message["content"])
if prompt := st.chat_input("Say something..."):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
with st.chat_message("assistant"):
message_placeholder = st.empty()
full_response = ""
for response in openai.Completion.create(
engine=st.session_state["openai_model"],
prompt="\n".join([f"{m['role']}: {m['content']}" for m in st.session_state.messages]),
max_tokens=150,
stream=True,
):
full_response += response.choices[0].text.strip()
if "I'm really sorry to hear that you're feeling this way, but I can't provide the help that you need." in str(full_response) and counter == 0:
full_response = full_response.replace("WARNING", " ")
st.warning('Your message has been flagged as a potential sign of mental health issues. Please seek appropriate assistance. Refer to the resources tab for further guidance', icon="⚠️")
counter = 1
message_placeholder.markdown(full_response + "▌")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})