Skip to content

Commit 18fd125

Browse files
committed
show response generation status
1 parent fa01dbf commit 18fd125

File tree

1 file changed

+36
-14
lines changed

1 file changed

+36
-14
lines changed

ragadoc/ui_chat.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,15 +119,10 @@ def generate_response_with_ui(prompt, current_chat):
119119
reasoning_placeholder = st.empty()
120120
answer_placeholder = st.empty()
121121
stop_container = st.container()
122+
loading_placeholder = st.empty()
122123

123-
# Show stop button
124-
with stop_container:
125-
stop_button_placeholder = st.empty()
126-
with stop_button_placeholder.container():
127-
col1, col2 = st.columns([10, 1])
128-
with col2:
129-
if st.button("⏹", key=f"stop_gen_{hash(prompt)}", help="Stop generation"):
130-
st.session_state.stop_generation = True
124+
# Prepare for aligned loading message and stop button
125+
stop_button_placeholder = st.empty()
131126

132127
# Generate response with streaming
133128
full_response = ""
@@ -136,6 +131,7 @@ def generate_response_with_ui(prompt, current_chat):
136131
reasoning_started = False
137132
in_reasoning = False
138133
generation_stopped = False
134+
first_chunk_received = False
139135

140136
# Use direct ollama streaming with RAG context
141137
system_prompt = PromptBuilder.create_system_prompt(context_text, is_rag=True)
@@ -148,6 +144,7 @@ def generate_response_with_ui(prompt, current_chat):
148144
ollama_base_url = get_ollama_base_url()
149145
in_docker = is_running_in_docker()
150146

147+
# Create chat stream
151148
if in_docker:
152149
client = ollama.Client(host=ollama_base_url)
153150
chat_stream = client.chat(
@@ -162,11 +159,30 @@ def generate_response_with_ui(prompt, current_chat):
162159
stream=True
163160
)
164161

162+
# Show loading message and stop button aligned
163+
with loading_placeholder.container():
164+
col1, col2 = st.columns([10, 1])
165+
with col1:
166+
st.write("⏳ Processing...")
167+
with col2:
168+
if st.button("⏹", key=f"stop_gen_{hash(prompt)}", help="Stop generation"):
169+
st.session_state.stop_generation = True
170+
165171
# Handle streaming response with reasoning support
166172
chunk_count = 0
173+
has_content = False
167174
for chunk in chat_stream:
168175
chunk_count += 1
169176

177+
# Check if this chunk has actual content
178+
if chunk['message']['content']:
179+
has_content = True
180+
181+
# Clear loading spinner on first chunk with actual content
182+
if has_content and not first_chunk_received:
183+
first_chunk_received = True
184+
loading_placeholder.empty() # Clear the spinner
185+
170186
# Check if user wants to stop on every chunk for responsiveness
171187
if st.session_state.get('stop_generation', False):
172188
generation_stopped = True
@@ -218,6 +234,10 @@ def generate_response_with_ui(prompt, current_chat):
218234

219235
# Handle stopped generation
220236
if generation_stopped or st.session_state.get('stop_generation', False):
237+
# Clear loading spinner if still showing when stopped
238+
if not first_chunk_received:
239+
loading_placeholder.empty()
240+
221241
final_answer = answer_content if reasoning_started else full_response
222242
if final_answer.strip():
223243
final_answer += "\n\n*[Generation stopped by user]*"
@@ -230,14 +250,11 @@ def generate_response_with_ui(prompt, current_chat):
230250
elif not reasoning_started:
231251
answer_placeholder.markdown(final_answer)
232252

233-
# Show stopped message and remove stop button
253+
# Show stopped message
234254
with stop_container:
235-
stop_button_placeholder.empty()
236255
st.info("🛑 Generation stopped by user")
237-
else:
238-
# Clear stop button when generation completes normally
239-
with stop_container:
240-
stop_button_placeholder.empty()
256+
# Clear the entire loading container (which includes the stop button)
257+
# This happens for both stopped and completed generation
241258

242259
# Show method information
243260
st.info("🔍 Response generated using RAG (semantic search)")
@@ -255,12 +272,17 @@ def generate_response_with_ui(prompt, current_chat):
255272
return final_answer
256273

257274
except Exception as e:
275+
# Clear loading spinner if still showing in case of error
276+
if not first_chunk_received:
277+
loading_placeholder.empty()
258278
st.error(f"Error generating response: {e}")
259279
return ""
260280
finally:
261281
# Reset generation state
262282
st.session_state.generating = False
263283
st.session_state.stop_generation = False
284+
# Ensure loading spinner is cleared
285+
loading_placeholder.empty()
264286

265287

266288
def render_chat_interface():

0 commit comments

Comments
 (0)