Spaces:
Runtime error
Runtime error
Commit
·
126af06
1
Parent(s):
c59a926
Upload psychochat.py
Browse files- psychochat.py +141 -0
psychochat.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import os
|
3 |
+
import pickle
|
4 |
+
import time
|
5 |
+
import tempfile
|
6 |
+
import os
|
7 |
+
|
8 |
+
import requests
|
9 |
+
|
10 |
+
|
11 |
+
|
12 |
+
st.markdown(
|
13 |
+
"""
|
14 |
+
<style>
|
15 |
+
.title {
|
16 |
+
text-align: center;
|
17 |
+
font-size: 2em;
|
18 |
+
font-weight: bold;
|
19 |
+
}
|
20 |
+
</style>
|
21 |
+
<div class="title">🧠 Phsycadalleics Chatbot</div>
|
22 |
+
""",
|
23 |
+
unsafe_allow_html=True
|
24 |
+
)
|
25 |
+
if 'chat_history' not in st.session_state:
|
26 |
+
st.session_state.chat_history = [] # Define chat_history in the session state
|
27 |
+
|
28 |
+
# Load and Save Conversations
|
29 |
+
conversations_file = "conversations.pkl"
|
30 |
+
|
31 |
+
if not os.path.exists(conversations_file):
|
32 |
+
# Create the file
|
33 |
+
with open(conversations_file, 'w') as f:
|
34 |
+
pass # The file is created with this line, no need to write anything to it
|
35 |
+
|
36 |
+
|
37 |
+
@st.cache_data
|
38 |
+
def load_conversations():
|
39 |
+
try:
|
40 |
+
with open(conversations_file, "rb") as f:
|
41 |
+
return pickle.load(f)
|
42 |
+
except (FileNotFoundError, EOFError):
|
43 |
+
return []
|
44 |
+
|
45 |
+
def save_conversations(conversations):
|
46 |
+
temp_conversations_file = "temp_" + conversations_file
|
47 |
+
with open(temp_conversations_file, "wb") as f:
|
48 |
+
pickle.dump(conversations, f)
|
49 |
+
os.replace(temp_conversations_file, conversations_file)
|
50 |
+
|
51 |
+
|
52 |
+
if 'conversations' not in st.session_state:
|
53 |
+
st.session_state.conversations = load_conversations()
|
54 |
+
|
55 |
+
if 'current_conversation' not in st.session_state:
|
56 |
+
st.session_state.current_conversation = [{"role": "assistant", "content": "How may I assist you today?"}]
|
57 |
+
|
58 |
+
|
59 |
+
def truncate_string(s, length=30):
|
60 |
+
return s[:length].rstrip() + "..." if len(s) > length else s
|
61 |
+
|
62 |
+
|
63 |
+
def display_chats_sidebar():
|
64 |
+
with st.sidebar.container():
|
65 |
+
st.header('Settings')
|
66 |
+
col1, col2 = st.columns([1, 1])
|
67 |
+
|
68 |
+
with col1:
|
69 |
+
if col1.button('Start New Chat', key="new_chat"):
|
70 |
+
st.session_state.current_conversation = []
|
71 |
+
st.session_state.conversations.append(st.session_state.current_conversation)
|
72 |
+
st.session_state.chat_history = []
|
73 |
+
|
74 |
+
with col2:
|
75 |
+
if col2.button('Clear All Chats', key="clear_all"):
|
76 |
+
st.session_state.conversations = []
|
77 |
+
st.session_state.current_conversation = []
|
78 |
+
|
79 |
+
with st.sidebar.container():
|
80 |
+
st.header('Conversations')
|
81 |
+
for idx, conversation in enumerate(st.session_state.conversations):
|
82 |
+
if conversation:
|
83 |
+
chat_title_raw = next((msg["content"] for msg in conversation if msg["role"] == "user"), "New Chat")
|
84 |
+
chat_title = truncate_string(chat_title_raw)
|
85 |
+
if st.sidebar.button(f"{chat_title}", key=f"chat_button_{idx}"):
|
86 |
+
st.session_state.current_conversation = st.session_state.conversations[idx]
|
87 |
+
|
88 |
+
|
89 |
+
def dummy_response(prompt_input, chat_history):
|
90 |
+
api_url = 'http://b5c9-34-125-138-225.ngrok.io/api/chat'
|
91 |
+
|
92 |
+
data_payload = {
|
93 |
+
'query': prompt_input,
|
94 |
+
'chat_history': chat_history
|
95 |
+
}
|
96 |
+
|
97 |
+
response = requests.post(api_url, json=data_payload)
|
98 |
+
|
99 |
+
if response.status_code == 200:
|
100 |
+
response_data = response.json()
|
101 |
+
result = response_data.get('response')
|
102 |
+
st.session_state.chat_history = response_data.get('chat_history')
|
103 |
+
else:
|
104 |
+
# Handle the case where the API does not return a successful response
|
105 |
+
result = f"An error occurred: {response.text}"
|
106 |
+
|
107 |
+
return result, chat_history
|
108 |
+
|
109 |
+
|
110 |
+
|
111 |
+
def main_app():
|
112 |
+
for message in st.session_state.current_conversation:
|
113 |
+
with st.chat_message(message["role"]):
|
114 |
+
st.write(message["content"])
|
115 |
+
|
116 |
+
if prompt := st.chat_input('Send a Message'):
|
117 |
+
st.session_state.current_conversation.append({"role": "user", "content": prompt})
|
118 |
+
print(' PROMPT :', prompt)
|
119 |
+
with st.chat_message("user"):
|
120 |
+
st.write(prompt)
|
121 |
+
|
122 |
+
with st.chat_message("assistant"):
|
123 |
+
with st.spinner("Thinking..."):
|
124 |
+
|
125 |
+
|
126 |
+
response, st.session_state.chat_history = dummy_response(prompt, st.session_state.chat_history)
|
127 |
+
full_response = ''
|
128 |
+
placeholder = st.empty()
|
129 |
+
for item in response:
|
130 |
+
full_response += item
|
131 |
+
time.sleep(0.003)
|
132 |
+
placeholder.markdown(full_response)
|
133 |
+
placeholder.markdown(full_response)
|
134 |
+
print(' RESPONSE :', full_response)
|
135 |
+
|
136 |
+
st.session_state.current_conversation.append({"role": "assistant", "content": response})
|
137 |
+
save_conversations(st.session_state.conversations)
|
138 |
+
|
139 |
+
|
140 |
+
display_chats_sidebar()
|
141 |
+
main_app()
|