Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -40,13 +40,19 @@ def load_chat_history(session_id):
|
|
| 40 |
def chat_history(session_id):
|
| 41 |
return SQLChatMessageHistory(
|
| 42 |
session_id=session_id,
|
| 43 |
-
connection="sqlite:///chat_history.db"
|
| 44 |
)
|
| 45 |
|
| 46 |
# Generate unique session ID for each user
|
| 47 |
if "session_id" not in st.session_state:
|
| 48 |
st.session_state.session_id = str(uuid.uuid4())
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
session_id = st.session_state.session_id
|
| 51 |
chat_history_instance = chat_history(session_id)
|
| 52 |
|
|
@@ -71,7 +77,7 @@ chain = chat_prompt | llm | out_parser
|
|
| 71 |
# Define Runnable with message history
|
| 72 |
chat = RunnableWithMessageHistory(
|
| 73 |
chain,
|
| 74 |
-
lambda session: SQLChatMessageHistory(session, "sqlite:///chat_history.db"),
|
| 75 |
input_messages_key="prompt",
|
| 76 |
history_messages_key="history"
|
| 77 |
)
|
|
@@ -80,16 +86,20 @@ chat = RunnableWithMessageHistory(
|
|
| 80 |
st.title("Conversational AI Data Science Tutor")
|
| 81 |
st.write("Ask me anything about Data Science!")
|
| 82 |
|
| 83 |
-
# Load chat history
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
-
# Display chat history
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
st.
|
|
|
|
| 90 |
|
| 91 |
-
# User input
|
| 92 |
-
user_input = st.text_input("
|
| 93 |
|
| 94 |
if user_input:
|
| 95 |
save_message(session_id, "user", user_input)
|
|
@@ -102,5 +112,10 @@ if user_input:
|
|
| 102 |
save_message(session_id, "assistant", response)
|
| 103 |
st.session_state.messages.append(("assistant", response))
|
| 104 |
|
| 105 |
-
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def chat_history(session_id):
|
| 41 |
return SQLChatMessageHistory(
|
| 42 |
session_id=session_id,
|
| 43 |
+
connection="sqlite:///chat_history.db"
|
| 44 |
)
|
| 45 |
|
| 46 |
# Generate unique session ID for each user
|
| 47 |
if "session_id" not in st.session_state:
|
| 48 |
st.session_state.session_id = str(uuid.uuid4())
|
| 49 |
|
| 50 |
+
# "New Chat" button
|
| 51 |
+
if st.button("🆕 New Chat"):
|
| 52 |
+
st.session_state.session_id = str(uuid.uuid4()) # Generate new session ID
|
| 53 |
+
st.session_state.messages = [] # Clear chat history
|
| 54 |
+
st.rerun() # Refresh the app
|
| 55 |
+
|
| 56 |
session_id = st.session_state.session_id
|
| 57 |
chat_history_instance = chat_history(session_id)
|
| 58 |
|
|
|
|
| 77 |
# Define Runnable with message history
|
| 78 |
chat = RunnableWithMessageHistory(
|
| 79 |
chain,
|
| 80 |
+
lambda session: SQLChatMessageHistory(session, "sqlite:///chat_history.db"),
|
| 81 |
input_messages_key="prompt",
|
| 82 |
history_messages_key="history"
|
| 83 |
)
|
|
|
|
| 86 |
st.title("Conversational AI Data Science Tutor")
|
| 87 |
st.write("Ask me anything about Data Science!")
|
| 88 |
|
| 89 |
+
# Load chat history and keep it sticky
|
| 90 |
+
if "messages" not in st.session_state:
|
| 91 |
+
st.session_state.messages = load_chat_history(session_id)
|
| 92 |
+
|
| 93 |
+
chat_container = st.container()
|
| 94 |
|
| 95 |
+
# Display chat history in a sticky format
|
| 96 |
+
with chat_container:
|
| 97 |
+
for role, content in st.session_state.messages:
|
| 98 |
+
with st.chat_message(role):
|
| 99 |
+
st.markdown(content)
|
| 100 |
|
| 101 |
+
# User input stays at the bottom
|
| 102 |
+
user_input = st.text_input("Type your message here:", "", key="user_input")
|
| 103 |
|
| 104 |
if user_input:
|
| 105 |
save_message(session_id, "user", user_input)
|
|
|
|
| 112 |
save_message(session_id, "assistant", response)
|
| 113 |
st.session_state.messages.append(("assistant", response))
|
| 114 |
|
| 115 |
+
# Refresh the chat container but keep history sticky
|
| 116 |
+
with chat_container:
|
| 117 |
+
with st.chat_message("assistant"):
|
| 118 |
+
st.markdown(response)
|
| 119 |
+
|
| 120 |
+
# Clear input field
|
| 121 |
+
st.session_state.user_input = ""
|