Spaces:
Running
Running
Commit
·
be92fa1
1
Parent(s):
a0f26fc
Removed extra "Thinking..." and previous message getting repeated
Browse files
app.py
CHANGED
@@ -29,8 +29,17 @@ st.sidebar.markdown(
|
|
29 |
if "chat_history" not in st.session_state:
|
30 |
st.session_state.chat_history = []
|
31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
# Display past chat messages
|
33 |
-
for msg in
|
34 |
with st.chat_message(msg["role"]):
|
35 |
st.markdown(msg["content"])
|
36 |
|
@@ -81,24 +90,29 @@ if user_input:
|
|
81 |
if not asi_api_key:
|
82 |
st.error("Please enter your API key in the sidebar.")
|
83 |
else:
|
|
|
|
|
|
|
84 |
# Add user message to chat history
|
85 |
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
86 |
with st.chat_message("user"):
|
87 |
st.markdown(user_input)
|
88 |
|
89 |
with st.chat_message("assistant"):
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
|
|
|
|
|
29 |
if "chat_history" not in st.session_state:
|
30 |
st.session_state.chat_history = []
|
31 |
|
32 |
+
# ⚠️ Avoid rendering the last assistant message prematurely
|
33 |
+
history_to_render = st.session_state.chat_history.copy()
|
34 |
+
if (
|
35 |
+
history_to_render
|
36 |
+
and history_to_render[-1]["role"] == "assistant"
|
37 |
+
and st.session_state.get("awaiting_response", False)
|
38 |
+
):
|
39 |
+
history_to_render = history_to_render[:-1]
|
40 |
+
|
41 |
# Display past chat messages
|
42 |
+
for msg in history_to_render:
|
43 |
with st.chat_message(msg["role"]):
|
44 |
st.markdown(msg["content"])
|
45 |
|
|
|
90 |
if not asi_api_key:
|
91 |
st.error("Please enter your API key in the sidebar.")
|
92 |
else:
|
93 |
+
# Mark that we're about to stream a new response
|
94 |
+
st.session_state["awaiting_response"] = True
|
95 |
+
|
96 |
# Add user message to chat history
|
97 |
st.session_state.chat_history.append({"role": "user", "content": user_input})
|
98 |
with st.chat_message("user"):
|
99 |
st.markdown(user_input)
|
100 |
|
101 |
with st.chat_message("assistant"):
|
102 |
+
response_stream = stream_asi1_api(
|
103 |
+
asi_api_key, st.session_state.chat_history
|
104 |
+
)
|
105 |
+
placeholder = st.empty()
|
106 |
+
partial_text = ""
|
107 |
+
|
108 |
+
for chunk in response_stream:
|
109 |
+
partial_text += chunk
|
110 |
+
placeholder.markdown(partial_text + "▌")
|
111 |
+
|
112 |
+
placeholder.markdown(partial_text)
|
113 |
+
|
114 |
+
# Save final assistant message to history
|
115 |
+
st.session_state.chat_history.append(
|
116 |
+
{"role": "assistant", "content": partial_text}
|
117 |
+
)
|
118 |
+
st.session_state["awaiting_response"] = False
|