-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page_Two.py
52 lines (42 loc) · 1.71 KB
/
Page_Two.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
from styling import template2_page_style
import streamlit as st
import time
def chat(messages, question):
messages.append({"role": "user", "content":question})
with st.chat_message("user", avatar="👤"):
st.markdown(question)
with st.spinner('Processing...'):
time.sleep(5)
response = "Welcome to Streamlit Template 2."
messages.append({"role": "assistant", "content":response})
with st.chat_message("assistant", avatar="🤖"):
st.markdown(response)
def clear_session(messages):
# Clear necessary session state variables
st.cache_data.clear()
messages.clear()
return messages
def main():
st.title('Streamlit_UI_Template_Two')
if 'messages' not in st.session_state:
st.session_state.messages = []
question = st.chat_input('Input query here...')
avatars = {
"assistant": "🤖", # Avatar for assistant
"user": "👤" # Avatar for user
}
for message in st.session_state.messages:
avatar = avatars.get(message["role"], "❓") # Get the avatar based on the role
with st.chat_message(message["role"], avatar=avatar):
st.markdown(message['content'])
if question:
chat(st.session_state.messages, question)
st.write('-'*50)
clear_chat_placeholder = st.empty()
if clear_chat_placeholder.button('Start New Session'):
st.session_state.messages = clear_session(st.session_state.messages)
clear_chat_placeholder.empty()
st.success("Chat session has been reset.")
if __name__ == "__main__":
template2_page_style()
main()