alex4cip Claude commited on
Commit
c3dd276
Β·
1 Parent(s): d64290b

feat: Show user message immediately before bot response

Browse files

UX improvement: User input now displays instantly

✨ Enhancement:
- Use generator (yield) to show user message immediately
- First yield: Display user message right after input
- Second yield: Add bot response when generation completes
- Improves perceived responsiveness

🎯 User Experience:
- Before: User message appears only after bot response (slow)
- After: User message appears instantly (fast)
- Better feedback during bot response generation

πŸ“ Implementation:
- Changed submit() to generator function
- First yield: history + user message
- Second yield: history + user message + bot response

πŸ€– Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>

Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -224,10 +224,14 @@ with gr.Blocks(title="πŸ€– Llama-2-Ko Chatbot") as demo:
224
  clear = gr.Button("πŸ—‘οΈ λŒ€ν™” μ΄ˆκΈ°ν™”", size="sm")
225
 
226
  def submit(message, history):
227
- # Gradio automatically adds user message when type="messages"
228
- # We just need to get the bot response
229
- response_history = chat_wrapper(message, history)
230
- return response_history, ""
 
 
 
 
231
 
232
  btn.click(submit, [msg, chatbot], [chatbot, msg])
233
  msg.submit(submit, [msg, chatbot], [chatbot, msg])
 
224
  clear = gr.Button("πŸ—‘οΈ λŒ€ν™” μ΄ˆκΈ°ν™”", size="sm")
225
 
226
  def submit(message, history):
227
+ # Immediately show user message
228
+ updated_history = history + [{"role": "user", "content": message}]
229
+ # Return updated history first (shows user message immediately)
230
+ # Then generate bot response
231
+ yield updated_history, ""
232
+ # Generate and add bot response
233
+ final_history = chat_wrapper(message, history)
234
+ yield final_history, ""
235
 
236
  btn.click(submit, [msg, chatbot], [chatbot, msg])
237
  msg.submit(submit, [msg, chatbot], [chatbot, msg])