bluenevus commited on
Commit
c156e9e
·
1 Parent(s): 9a3a044

Update app.py via AI Editor

Browse files
Files changed (1) hide show
  1. app.py +0 -15
app.py CHANGED
@@ -257,7 +257,6 @@ def user_input_card():
257
  style={"width": "100%", "height": "60px", "resize": "vertical", "wordWrap": "break-word"},
258
  wrap="soft",
259
  maxLength=1000,
260
- n_submit=0,
261
  n_blur=0,
262
  ),
263
  dcc.Store(id="enter-triggered", data=False),
@@ -296,7 +295,6 @@ app.layout = html.Div([
296
  dcc.Store(id="enter-pressed", data=False)
297
  ])
298
 
299
- # JS callback to intercept Enter/Shift+Enter for dcc.Textarea
300
  app.clientside_callback(
301
  """
302
  function(n, value) {
@@ -325,7 +323,6 @@ def _is_supported_doc(filename):
325
  return ext in [".txt", ".pdf", ".md", ".docx"]
326
 
327
  def _extract_text_from_upload(filepath, ext):
328
- # Only .txt and .md are extracted for now
329
  if ext in [".txt", ".md"]:
330
  try:
331
  with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
@@ -383,7 +380,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
383
  chat_histories = state.get("chat_histories", [])
384
  uploads = state.get("uploads", [])
385
 
386
- # Handle clickable chat history loading
387
  history_index_clicked = None
388
  if trigger == "" and chat_history_clicks:
389
  for idx, n in enumerate(chat_history_clicks):
@@ -402,7 +398,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
402
  state["streaming"] = False
403
  save_session_state(session_id)
404
  logger.info(f"Session {session_id}: Loaded chat history index {history_index_clicked}")
405
- # Rebuild downstream outputs with this dialog
406
  chat_cards = []
407
  for msg in state["messages"]:
408
  chat_cards.append(chat_message_card(msg['content'], is_user=(msg['role'] == "user")))
@@ -430,7 +425,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
430
  history_index_clicked
431
  )
432
 
433
- # Handle File Upload
434
  file_was_uploaded_and_sent = False
435
  if trigger == "file-upload" and file_contents and file_names:
436
  uploads = []
@@ -456,12 +450,10 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
456
  save_session_state(session_id)
457
  logger.info(f"Session {session_id}: Uploaded files {[u['name'] for u in uploads]}")
458
 
459
- # Determine if send was triggered (via send-btn, hidden-send, or enter)
460
  send_triggered = False
461
  if trigger == "send-btn" or trigger == "hidden-send":
462
  send_triggered = True
463
 
464
- # Handle Send
465
  if send_triggered and user_input and user_input.strip():
466
  question = user_input.strip()
467
  state["messages"].append({"role": "user", "content": question})
@@ -472,10 +464,8 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
472
  def run_stream(session_id, messages, question):
473
  try:
474
  system_prompt = load_system_prompt()
475
- # Retrieve relevant context from global RAG
476
  rag_chunks = []
477
  try:
478
- # Search global docs
479
  global_embeds = []
480
  global_texts = []
481
  global_fnames = []
@@ -487,7 +477,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
487
  if global_rag:
488
  for r in global_rag:
489
  rag_chunks.append(f"Global doc [{r['filename']}]:\n{r['text'][:1000]}")
490
- # Search user docs
491
  user_embeds, user_texts, user_fnames = get_user_embeddings(session_id)
492
  user_rag = semantic_search(question, user_embeds, user_texts, user_fnames, top_k=2)
493
  if user_rag:
@@ -544,7 +533,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
544
  threading.Thread(target=run_stream, args=(session_id, list(state["messages"]), question), daemon=True).start()
545
  start_streaming = True
546
 
547
- # Handle New Chat button logic: auto-name and reset
548
  if trigger == "new-chat-btn":
549
  chat_dialog = list(state.get("messages", []))
550
  if not chat_dialog:
@@ -561,7 +549,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
561
  save_session_state(session_id)
562
  logger.info(f"Session {session_id}: Saved chat history '{chat_title}'")
563
 
564
- # Handle polling for streaming
565
  if trigger == "stream-interval":
566
  chat_history = state.get("messages", [])
567
  chat_cards = []
@@ -621,7 +608,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
621
  selected_history
622
  )
623
 
624
- # Default: Build Uploads, Chat History and Chat Window
625
  chat_history = state.get("messages", [])
626
  uploads = state.get("uploads", [])
627
  chat_histories = state.get("chat_histories", [])
@@ -645,7 +631,6 @@ def main_callback(session_id, send_clicks, file_contents, new_chat_clicks, strea
645
  if state.get("stream_buffer", ""):
646
  chat_cards.append(chat_message_card(state["stream_buffer"], is_user=False))
647
  return upload_cards, chat_history_items, chat_cards, error, False, 0, "", selected_history
648
- # Always clear input after send
649
  if send_triggered:
650
  return upload_cards, chat_history_items, chat_cards, error, (not state.get("streaming", False)), 0, "", selected_history
651
  return upload_cards, chat_history_items, chat_cards, error, (not state.get("streaming", False)), 0, user_input or "", selected_history
 
257
  style={"width": "100%", "height": "60px", "resize": "vertical", "wordWrap": "break-word"},
258
  wrap="soft",
259
  maxLength=1000,
 
260
  n_blur=0,
261
  ),
262
  dcc.Store(id="enter-triggered", data=False),
 
295
  dcc.Store(id="enter-pressed", data=False)
296
  ])
297
 
 
298
  app.clientside_callback(
299
  """
300
  function(n, value) {
 
323
  return ext in [".txt", ".pdf", ".md", ".docx"]
324
 
325
  def _extract_text_from_upload(filepath, ext):
 
326
  if ext in [".txt", ".md"]:
327
  try:
328
  with open(filepath, "r", encoding="utf-8", errors="ignore") as f:
 
380
  chat_histories = state.get("chat_histories", [])
381
  uploads = state.get("uploads", [])
382
 
 
383
  history_index_clicked = None
384
  if trigger == "" and chat_history_clicks:
385
  for idx, n in enumerate(chat_history_clicks):
 
398
  state["streaming"] = False
399
  save_session_state(session_id)
400
  logger.info(f"Session {session_id}: Loaded chat history index {history_index_clicked}")
 
401
  chat_cards = []
402
  for msg in state["messages"]:
403
  chat_cards.append(chat_message_card(msg['content'], is_user=(msg['role'] == "user")))
 
425
  history_index_clicked
426
  )
427
 
 
428
  file_was_uploaded_and_sent = False
429
  if trigger == "file-upload" and file_contents and file_names:
430
  uploads = []
 
450
  save_session_state(session_id)
451
  logger.info(f"Session {session_id}: Uploaded files {[u['name'] for u in uploads]}")
452
 
 
453
  send_triggered = False
454
  if trigger == "send-btn" or trigger == "hidden-send":
455
  send_triggered = True
456
 
 
457
  if send_triggered and user_input and user_input.strip():
458
  question = user_input.strip()
459
  state["messages"].append({"role": "user", "content": question})
 
464
  def run_stream(session_id, messages, question):
465
  try:
466
  system_prompt = load_system_prompt()
 
467
  rag_chunks = []
468
  try:
 
469
  global_embeds = []
470
  global_texts = []
471
  global_fnames = []
 
477
  if global_rag:
478
  for r in global_rag:
479
  rag_chunks.append(f"Global doc [{r['filename']}]:\n{r['text'][:1000]}")
 
480
  user_embeds, user_texts, user_fnames = get_user_embeddings(session_id)
481
  user_rag = semantic_search(question, user_embeds, user_texts, user_fnames, top_k=2)
482
  if user_rag:
 
533
  threading.Thread(target=run_stream, args=(session_id, list(state["messages"]), question), daemon=True).start()
534
  start_streaming = True
535
 
 
536
  if trigger == "new-chat-btn":
537
  chat_dialog = list(state.get("messages", []))
538
  if not chat_dialog:
 
549
  save_session_state(session_id)
550
  logger.info(f"Session {session_id}: Saved chat history '{chat_title}'")
551
 
 
552
  if trigger == "stream-interval":
553
  chat_history = state.get("messages", [])
554
  chat_cards = []
 
608
  selected_history
609
  )
610
 
 
611
  chat_history = state.get("messages", [])
612
  uploads = state.get("uploads", [])
613
  chat_histories = state.get("chat_histories", [])
 
631
  if state.get("stream_buffer", ""):
632
  chat_cards.append(chat_message_card(state["stream_buffer"], is_user=False))
633
  return upload_cards, chat_history_items, chat_cards, error, False, 0, "", selected_history
 
634
  if send_triggered:
635
  return upload_cards, chat_history_items, chat_cards, error, (not state.get("streaming", False)), 0, "", selected_history
636
  return upload_cards, chat_history_items, chat_cards, error, (not state.get("streaming", False)), 0, user_input or "", selected_history