Spaces:
Sleeping
Sleeping
vickyli777
commited on
Commit
β’
e3a11d1
1
Parent(s):
bf55d48
Add application file
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import openai
|
2 |
+
import time
|
3 |
+
import random
|
4 |
+
import streamlit as st
|
5 |
+
from streamlit_chat import message
|
6 |
+
import os
|
7 |
+
|
8 |
+
from utils import set_main_page, message_border
|
9 |
+
|
10 |
+
st.set_page_config(
|
11 |
+
page_title="streamlit-demo",
|
12 |
+
page_icon="π§",
|
13 |
+
layout="wide",
|
14 |
+
initial_sidebar_state="expanded",
|
15 |
+
menu_items={
|
16 |
+
'Get Help': 'https://github.com/vickyli777',
|
17 |
+
'Report a bug': "https://github.com/vickyli777",
|
18 |
+
'About': "## a simple demo!"
|
19 |
+
}
|
20 |
+
)
|
21 |
+
|
22 |
+
set_main_page()
|
23 |
+
st.sidebar.divider()
|
24 |
+
|
25 |
+
def clear_chat():
|
26 |
+
"""
|
27 |
+
"""
|
28 |
+
st.session_state.messages =[]
|
29 |
+
|
30 |
+
|
31 |
+
st.sidebar.button("clear all chats", on_click=clear_chat, type="primary")
|
32 |
+
st.sidebar.caption("clear histories and start a new chat")
|
33 |
+
|
34 |
+
#storing the chat
|
35 |
+
if 'messages' not in st.session_state:
|
36 |
+
st.session_state['messages'] = []
|
37 |
+
# Display chat messages from history on app rerun
|
38 |
+
for msg in st.session_state.messages:
|
39 |
+
with st.chat_message(msg["role"]):
|
40 |
+
chat_box = message_border(mes["role"],msg["content"])
|
41 |
+
st.markdown(chat_box,unsafe_allow_html=True)
|
42 |
+
|
43 |
+
# React to user input
|
44 |
+
if question := st.chat_input("your AI assistant is here, ask me something"):
|
45 |
+
# Display user message in chat message container
|
46 |
+
with st.chat_message(name="user"):
|
47 |
+
|
48 |
+
cur_border = message_border("user",question)
|
49 |
+
st.markdown(cur_border,unsafe_allow_html=True) #paragraph
|
50 |
+
|
51 |
+
# st.markdown(prompt)
|
52 |
+
# Add user message to chat history
|
53 |
+
st.session_state.messages.append({"role": "user", "content": question})
|
54 |
+
|
55 |
+
# response = f"Echo: {prompt}"
|
56 |
+
# Display assistant response in chat message container
|
57 |
+
with st.chat_message(name="assistant"):
|
58 |
+
message_placeholder = st.empty()
|
59 |
+
full_response = ""
|
60 |
+
response = random.choice(
|
61 |
+
[
|
62 |
+
"Hello there! How can I assist you today?",
|
63 |
+
"Hi, human! Is there anything I can help you with?",
|
64 |
+
"Do you need help?",
|
65 |
+
]
|
66 |
+
)
|
67 |
+
# Simulate stream of response with milliseconds delay
|
68 |
+
for chunk in response.split():
|
69 |
+
full_response += chunk + " "
|
70 |
+
# ans_border = message_border(full_response+ "β")
|
71 |
+
time.sleep(0.5)
|
72 |
+
# Add a blinking cursor to simulate typing
|
73 |
+
# message_placeholder.markdown("""
|
74 |
+
# <p style="border: 1px solid red;
|
75 |
+
# padding: 5px;
|
76 |
+
# border-radius: 5px">
|
77 |
+
# {}
|
78 |
+
# </p>
|
79 |
+
# """.format(full_response +"β"))
|
80 |
+
ans_border = message_border("assistant",full_response)
|
81 |
+
message_placeholder.markdown(ans_border, unsafe_allow_html=True)
|
82 |
+
# message_placeholder.markdown(ans_border, unsafe_allow_html=True)
|
83 |
+
# st.markdown(f':blue[{response}]')
|
84 |
+
# Add assistant response to chat history
|
85 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|