seawolf2357 commited on
Commit
6ce4ef1
·
verified ·
1 Parent(s): c86076e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -11
app.py CHANGED
@@ -346,19 +346,38 @@ def clear_expired_sessions():
346
  finally:
347
  conn.close()
348
 
349
- def update_session_list():
350
- conn = sqlite3.connect('chat_history.db')
351
- c = conn.cursor()
352
- c.execute("SELECT session_id FROM sessions ORDER BY created_at DESC")
353
- sessions = [row[0] for row in c.fetchall()]
354
- conn.close()
355
- return gr.update(choices=sessions)
356
 
357
- def load_session_history(session_id):
358
- history = get_session_history(session_id)
359
- return [[p, r] for p, r, _ in history]
360
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
361
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
362
  # Demo 인스턴스 먼저 생성
363
  demo_instance = Demo()
364
 
@@ -380,7 +399,7 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
380
  with antd.Drawer(
381
  open=False,
382
  title="Session History",
383
- placement="left",
384
  width="900px",
385
  elem_classes="session-drawer"
386
  ) as session_drawer:
@@ -582,6 +601,9 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
582
  history_drawer.close(lambda: gr.update(
583
  open=False), inputs=[], outputs=[history_drawer])
584
 
 
 
 
585
  # 세션 관련 이벤트 핸들러들
586
  sessionBtn.click(
587
  lambda: (gr.update(open=True), update_session_list()),
@@ -600,6 +622,8 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
600
  outputs=[session_drawer, session_list, session_history]
601
  )
602
 
 
 
603
  session_list.change(
604
  load_session_history,
605
  inputs=[session_list],
 
346
  finally:
347
  conn.close()
348
 
 
 
 
 
 
 
 
349
 
 
 
 
350
 
351
+ def update_session_list():
352
+ try:
353
+ conn = sqlite3.connect('chat_history.db')
354
+ c = conn.cursor()
355
+ c.execute("SELECT session_id, created_at FROM sessions ORDER BY created_at DESC")
356
+ sessions = c.fetchall()
357
+ conn.close()
358
+ # 날짜와 함께 보기 좋게 포맷팅
359
+ formatted_sessions = [f"{sid} ({datetime.strptime(ctime, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S')})"
360
+ for sid, ctime in sessions]
361
+ return gr.update(choices=formatted_sessions)
362
+ except Exception as e:
363
+ print(f"Error updating session list: {e}")
364
+ return gr.update(choices=[])
365
 
366
+ def load_session_history(selected_session):
367
+ try:
368
+ if not selected_session:
369
+ return []
370
+ # 세션 ID 추출 (날짜 부분 제거)
371
+ session_id = selected_session.split(" (")[0]
372
+ history = get_session_history(session_id)
373
+ # 채팅 형식으로 변환
374
+ chat_history = []
375
+ for prompt, response, timestamp in history:
376
+ chat_history.append([prompt, response])
377
+ return chat_history
378
+ except Exception as e:
379
+ print(f"Error loading session history: {e}")
380
+ return []
381
  # Demo 인스턴스 먼저 생성
382
  demo_instance = Demo()
383
 
 
399
  with antd.Drawer(
400
  open=False,
401
  title="Session History",
402
+ placement="right",
403
  width="900px",
404
  elem_classes="session-drawer"
405
  ) as session_drawer:
 
601
  history_drawer.close(lambda: gr.update(
602
  open=False), inputs=[], outputs=[history_drawer])
603
 
604
+
605
+
606
+
607
  # 세션 관련 이벤트 핸들러들
608
  sessionBtn.click(
609
  lambda: (gr.update(open=True), update_session_list()),
 
622
  outputs=[session_drawer, session_list, session_history]
623
  )
624
 
625
+
626
+
627
  session_list.change(
628
  load_session_history,
629
  inputs=[session_list],