seawolf2357 commited on
Commit
d013880
·
verified ·
1 Parent(s): 4fd85ad

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -10
app.py CHANGED
@@ -402,7 +402,8 @@ def update_session_list():
402
  def load_session_history(selected_session):
403
  try:
404
  if not selected_session:
405
- return []
 
406
  # 세션 ID 추출
407
  session_id = selected_session.split(" (")[0] if " (" in selected_session else selected_session
408
 
@@ -412,16 +413,82 @@ def load_session_history(selected_session):
412
  SELECT prompt, response, timestamp
413
  FROM chat_history
414
  WHERE session_id = ?
415
- ORDER BY timestamp
416
  """, (session_id,))
417
  history = c.fetchall()
418
  conn.close()
419
 
420
- # 채팅 형식으로 변환
421
- return [[prompt, response] for prompt, response, _ in history]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
422
  except Exception as e:
423
  print(f"Error loading session history: {e}")
424
- return []
425
 
426
  def save_chat(session_id, prompt, response):
427
  print(f"Starting save_chat with session_id: {session_id}")
@@ -477,11 +544,12 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
477
 
478
  with antd.Drawer(open=False, title="history", placement="left", width="900px") as history_drawer:
479
  history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")
 
480
 
481
  with antd.Drawer(
482
  open=False,
483
  title="Session History",
484
- placement="right", # 오른쪽으로 변경
485
  width="900px",
486
  elem_classes="session-drawer"
487
  ) as session_drawer:
@@ -492,10 +560,9 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
492
  choices=[],
493
  elem_classes="session-list"
494
  )
495
- session_history = legacy.Chatbot(
496
- show_label=False,
497
- height=700,
498
- elem_classes="session-chatbot"
499
  )
500
  close_btn = antd.Button(
501
  "Close",
@@ -503,6 +570,17 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
503
  elem_classes="close-btn"
504
  )
505
 
 
 
 
 
 
 
 
 
 
 
 
506
  # 메인 컨텐츠를 위한 Row
507
  with antd.Row(gutter=[32, 12]) as layout:
508
  # 좌측 패널
 
402
  def load_session_history(selected_session):
403
  try:
404
  if not selected_session:
405
+ return gr.update(), gr.update()
406
+
407
  # 세션 ID 추출
408
  session_id = selected_session.split(" (")[0] if " (" in selected_session else selected_session
409
 
 
413
  SELECT prompt, response, timestamp
414
  FROM chat_history
415
  WHERE session_id = ?
416
+ ORDER BY timestamp DESC
417
  """, (session_id,))
418
  history = c.fetchall()
419
  conn.close()
420
 
421
+ # 프롬프트 카드용 HTML 생성
422
+ cards_html = """
423
+ <style>
424
+ .prompt-grid {
425
+ display: grid;
426
+ grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
427
+ gap: 20px;
428
+ padding: 20px;
429
+ }
430
+ .prompt-card {
431
+ background: white;
432
+ border: 1px solid #eee;
433
+ border-radius: 8px;
434
+ padding: 15px;
435
+ cursor: pointer;
436
+ transition: all 0.3s ease;
437
+ box-shadow: 0 2px 5px rgba(0,0,0,0.1);
438
+ }
439
+ .prompt-card:hover {
440
+ transform: translateY(-2px);
441
+ box-shadow: 0 4px 10px rgba(0,0,0,0.15);
442
+ }
443
+ .timestamp {
444
+ font-size: 0.8em;
445
+ color: #666;
446
+ margin-top: 10px;
447
+ }
448
+ </style>
449
+ <div class="prompt-grid">
450
+ """
451
+
452
+ for i, (prompt, response, timestamp) in enumerate(history):
453
+ # 프롬프트 텍스트를 적절한 길이로 자르기
454
+ short_prompt = prompt[:100] + "..." if len(prompt) > 100 else prompt
455
+ formatted_time = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M')
456
+
457
+ cards_html += f"""
458
+ <div class="prompt-card" onclick="showResponse({i})">
459
+ <div>{short_prompt}</div>
460
+ <div class="timestamp">{formatted_time}</div>
461
+ </div>
462
+ """
463
+
464
+ cards_html += "</div>"
465
+
466
+ # JavaScript 함수 추가
467
+ cards_html += """
468
+ <script>
469
+ function showResponse(index) {
470
+ const responses = %s;
471
+ document.getElementById('code-display').innerHTML = `<pre><code>${responses[index]}</code></pre>`;
472
+ }
473
+ </script>
474
+ """ % json.dumps([response for _, response, _ in history])
475
+
476
+ # 코드 표시 영역 추가
477
+ cards_html += """
478
+ <div id="code-display" style="
479
+ margin-top: 20px;
480
+ padding: 20px;
481
+ background: #f5f5f5;
482
+ border-radius: 8px;
483
+ display: none;
484
+ "></div>
485
+ """
486
+
487
+ return gr.HTML(value=cards_html)
488
+
489
  except Exception as e:
490
  print(f"Error loading session history: {e}")
491
+ return gr.HTML("Error loading history")
492
 
493
  def save_chat(session_id, prompt, response):
494
  print(f"Starting save_chat with session_id: {session_id}")
 
544
 
545
  with antd.Drawer(open=False, title="history", placement="left", width="900px") as history_drawer:
546
  history_output = legacy.Chatbot(show_label=False, flushing=False, height=960, elem_classes="history_chatbot")
547
+
548
 
549
  with antd.Drawer(
550
  open=False,
551
  title="Session History",
552
+ placement="right",
553
  width="900px",
554
  elem_classes="session-drawer"
555
  ) as session_drawer:
 
560
  choices=[],
561
  elem_classes="session-list"
562
  )
563
+ # Chatbot을 HTML로 변경
564
+ session_history = gr.HTML(
565
+ elem_classes="session-history"
 
566
  )
567
  close_btn = antd.Button(
568
  "Close",
 
570
  elem_classes="close-btn"
571
  )
572
 
573
+ # 이벤트 핸들러 수정
574
+ session_list.change(
575
+ load_session_history,
576
+ inputs=[session_list],
577
+ outputs=[session_history]
578
+ )
579
+
580
+
581
+
582
+
583
+
584
  # 메인 컨텐츠를 위한 Row
585
  with antd.Row(gutter=[32, 12]) as layout:
586
  # 좌측 패널