qwendashcoder commited on
Commit
47bdf5e
·
verified ·
1 Parent(s): 49b15e5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -11
app.py CHANGED
@@ -212,7 +212,7 @@ def handle_chat(msg, chatbot, current_model, logged_in_username, user_chats_stat
212
  gr.Warning("⚠️ Load a model first!")
213
  return chatbot
214
 
215
- # Cooldown check
216
  ok, new_time = enforce_cooldown(last_interaction_time.value)
217
  if not ok:
218
  return chatbot
@@ -224,24 +224,24 @@ def handle_chat(msg, chatbot, current_model, logged_in_username, user_chats_stat
224
  internal_history = chatbot[:] if chatbot else []
225
 
226
  try:
227
- stream_gen = chat_stream(msg, internal_history, current_model, logged_in_username, user_chats_state, current_chat_title, mode_radio, auth_globals_state, file_upload, current_chat_context)
 
 
 
 
228
 
229
  for updated_history in stream_gen:
230
- # Add approximate token count to last assistant message
231
- if updated_history and updated_history[-1]["role"] == "assistant":
232
- content = updated_history[-1]["content"]
233
- tokens = len(content) // 4 + 50 # simple but good enough approximation
234
- updated_history[-1]["tokens"] = tokens
235
- yield updated_history
236
 
237
  except Exception as e:
238
- gr.Warning(f"⚠️ Error: {str(e)[:200]}")
 
239
  finally:
240
  is_generating.value = False
241
  stop_btn.visible = False
242
 
243
- # Update token display
244
- return token_display.update(value=update_token_display(user_chats_state, current_chat_title))
245
 
246
 
247
  # ================== GUI ==================
 
212
  gr.Warning("⚠️ Load a model first!")
213
  return chatbot
214
 
215
+ # 3-second cooldown
216
  ok, new_time = enforce_cooldown(last_interaction_time.value)
217
  if not ok:
218
  return chatbot
 
224
  internal_history = chatbot[:] if chatbot else []
225
 
226
  try:
227
+ stream_gen = chat_stream(
228
+ msg, internal_history, current_model, logged_in_username,
229
+ user_chats_state, current_chat_title, mode_radio,
230
+ auth_globals_state, file_upload, current_chat_context
231
+ )
232
 
233
  for updated_history in stream_gen:
234
+ yield updated_history # ← always yield to chatbot
 
 
 
 
 
235
 
236
  except Exception as e:
237
+ gr.Warning(f"⚠️ Error: {str(e)[:250]}")
238
+ yield chatbot # fallback
239
  finally:
240
  is_generating.value = False
241
  stop_btn.visible = False
242
 
243
+ # Update token count AFTER streaming finishes (using .then chain)
244
+ return chatbot # important: return the chatbot itself
245
 
246
 
247
  # ================== GUI ==================