-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththerapist.py
More file actions
93 lines (73 loc) · 2.56 KB
/
therapist.py
File metadata and controls
93 lines (73 loc) · 2.56 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
82
83
84
85
86
87
88
89
90
91
92
93
from openai import OpenAI
import streamlit as st
from dotenv import load_dotenv
import os
import shelve
# Simple Web Version Of The Therapist AI ChatBot
# TO DO
# The alignment of the messages will be arranged.
# The previous conservations will be stored in a database.
# Color combinations will be updated.
load_dotenv()
st.title("MelodiCell")
st.markdown(
"""
<style>
/* Upper Rectangle (Header) → Green */
header[data-testid="stHeader"] {
background-color: #FFCC00 !important;
}
/* Original styles below */
section[data-testid="stSidebar"] {
background-color: #ffffff !important;
}
div[data-testid="stAppViewContainer"] {
background-color: #0051A2 !important;
}
div[data-testid="main"] {
background-color: transparent !important;
}
}
</style>
""",
unsafe_allow_html=True,
)
USER_AVATAR = "👤"
BOT_AVATAR = "🧑⚕️"
client = OpenAI(api_key="")
if "openai_model" not in st.session_state:
st.session_state["openai_model"] = "gpt-3.5-turbo"
def load_chat_history():
with shelve.open("chat_history") as db:
return db.get("messages", [])
def save_chat_history(messages):
with shelve.open("chat_history") as db:
db["messages"] = messages
if "messages" not in st.session_state:
st.session_state.messages = load_chat_history()
with st.sidebar:
if st.button("Delete Chat History"):
st.session_state.messages = []
save_chat_history([])
for message in st.session_state.messages:
avatar = USER_AVATAR if message["role"] == "user" else BOT_AVATAR
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message["content"])
if prompt := st.chat_input("How can I help?"):
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user", avatar=USER_AVATAR):
st.markdown(prompt)
with st.chat_message("assistant", avatar=BOT_AVATAR):
message_placeholder = st.empty()
full_response = ""
for response in client.chat.completions.create(
model=st.session_state["openai_model"],
messages=st.session_state["messages"],
stream=True,
):
full_response += response.choices[0].delta.content or ""
message_placeholder.markdown(full_response + "|")
message_placeholder.markdown(full_response)
st.session_state.messages.append({"role": "assistant", "content": full_response})
# Save chat history after each interaction
save_chat_history(st.session_state.messages)