Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,9 +1,39 @@
|
|
| 1 |
-
import random
|
| 2 |
import streamlit as st
|
| 3 |
|
| 4 |
assistant_icon_path = "chat_icon/assistant.ico"
|
| 5 |
user_icon_path = "chat_icon/user01.png"
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# μΈμ
μνμμ approval_state λ° messages μ΄κΈ°ν
|
| 8 |
if "approval_state" not in st.session_state:
|
| 9 |
st.session_state.approval_state = "require"
|
|
@@ -12,34 +42,39 @@ if "messages" not in st.session_state:
|
|
| 12 |
|
| 13 |
# Display chat messages from history on app rerun
|
| 14 |
for message in st.session_state.messages:
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
# Start with an assistant message
|
| 19 |
if not st.session_state.messages:
|
| 20 |
-
st.
|
| 21 |
-
st.session_state.messages.append({"role": "assistant", "avatar":assistant_icon_path, "content": "type yes to start"})
|
| 22 |
|
| 23 |
if prompt := st.chat_input():
|
| 24 |
if st.session_state.approval_state == "require":
|
| 25 |
-
st.
|
| 26 |
-
|
| 27 |
-
if prompt == "yes":
|
| 28 |
st.session_state.approval_state = "approved"
|
| 29 |
-
st.
|
| 30 |
-
st.session_state.messages.append({"role": "assistant", "avatar":assistant_icon_path, "content": "token approved now you can use ai-tom chatbot"})
|
| 31 |
else:
|
| 32 |
-
st.
|
| 33 |
-
st.session_state.messages.append({"role": "assistant", "avatar":assistant_icon_path, "content": "wrong token try again"})
|
| 34 |
elif st.session_state.approval_state == "approved":
|
| 35 |
-
|
| 36 |
-
st.chat_message("user", avatar=user_icon_path).markdown(prompt)
|
| 37 |
-
st.session_state.messages.append({"role": "user", "avatar":user_icon_path, "content": prompt})
|
| 38 |
-
|
| 39 |
response = f"Echo: {prompt}"
|
| 40 |
-
|
| 41 |
-
with st.chat_message("assistant", avatar=assistant_icon_path):
|
| 42 |
-
st.markdown(response)
|
| 43 |
-
# Add assistant response to chat history
|
| 44 |
-
st.session_state.messages.append({"role": "assistant", "avatar":assistant_icon_path, "content": response})
|
| 45 |
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
|
| 3 |
assistant_icon_path = "chat_icon/assistant.ico"
|
| 4 |
user_icon_path = "chat_icon/user01.png"
|
| 5 |
|
| 6 |
+
# Custom CSS for alignment
|
| 7 |
+
st.markdown("""
|
| 8 |
+
<style>
|
| 9 |
+
.chat-container {
|
| 10 |
+
display: flex;
|
| 11 |
+
width: 100%;
|
| 12 |
+
}
|
| 13 |
+
.chat-left {
|
| 14 |
+
justify-content: flex-start;
|
| 15 |
+
text-align: left;
|
| 16 |
+
}
|
| 17 |
+
.chat-right {
|
| 18 |
+
justify-content: flex-end;
|
| 19 |
+
text-align: right;
|
| 20 |
+
}
|
| 21 |
+
.chat-box {
|
| 22 |
+
max-width: 60%;
|
| 23 |
+
padding: 10px;
|
| 24 |
+
margin: 5px;
|
| 25 |
+
border-radius: 10px;
|
| 26 |
+
display: inline-block;
|
| 27 |
+
}
|
| 28 |
+
.user-box {
|
| 29 |
+
background-color: #d1e7fd;
|
| 30 |
+
}
|
| 31 |
+
.assistant-box {
|
| 32 |
+
background-color: #f1f1f1;
|
| 33 |
+
}
|
| 34 |
+
</style>
|
| 35 |
+
""", unsafe_allow_html=True)
|
| 36 |
+
|
| 37 |
# μΈμ
μνμμ approval_state λ° messages μ΄κΈ°ν
|
| 38 |
if "approval_state" not in st.session_state:
|
| 39 |
st.session_state.approval_state = "require"
|
|
|
|
| 42 |
|
| 43 |
# Display chat messages from history on app rerun
|
| 44 |
for message in st.session_state.messages:
|
| 45 |
+
if message["role"] == "user":
|
| 46 |
+
align_class = "chat-left"
|
| 47 |
+
box_class = "user-box"
|
| 48 |
+
else:
|
| 49 |
+
align_class = "chat-right"
|
| 50 |
+
box_class = "assistant-box"
|
| 51 |
+
|
| 52 |
+
st.markdown(
|
| 53 |
+
f"""
|
| 54 |
+
<div class="chat-container {align_class}">
|
| 55 |
+
<div class="chat-box {box_class}">
|
| 56 |
+
<b>{message["role"].capitalize()}:</b> {message["content"]}
|
| 57 |
+
</div>
|
| 58 |
+
</div>
|
| 59 |
+
""",
|
| 60 |
+
unsafe_allow_html=True
|
| 61 |
+
)
|
| 62 |
|
| 63 |
# Start with an assistant message
|
| 64 |
if not st.session_state.messages:
|
| 65 |
+
st.session_state.messages.append({"role": "assistant", "content": "type yes to start"})
|
|
|
|
| 66 |
|
| 67 |
if prompt := st.chat_input():
|
| 68 |
if st.session_state.approval_state == "require":
|
| 69 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
| 70 |
+
if prompt.lower() == "yes":
|
|
|
|
| 71 |
st.session_state.approval_state = "approved"
|
| 72 |
+
st.session_state.messages.append({"role": "assistant", "content": "token approved now you can use ai-tom chatbot"})
|
|
|
|
| 73 |
else:
|
| 74 |
+
st.session_state.messages.append({"role": "assistant", "content": "wrong token try again"})
|
|
|
|
| 75 |
elif st.session_state.approval_state == "approved":
|
| 76 |
+
st.session_state.messages.append({"role": "user", "content": prompt})
|
|
|
|
|
|
|
|
|
|
| 77 |
response = f"Echo: {prompt}"
|
| 78 |
+
st.session_state.messages.append({"role": "assistant", "content": response})
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
st.rerun() # νλ©΄ κ°±μ
|