Update app.py
Browse files
app.py
CHANGED
@@ -346,18 +346,30 @@ def clear_expired_sessions():
|
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
356 |
sessions = c.fetchall()
|
357 |
conn.close()
|
358 |
-
|
359 |
-
|
360 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
361 |
return gr.update(choices=formatted_sessions)
|
362 |
except Exception as e:
|
363 |
print(f"Error updating session list: {e}")
|
@@ -367,17 +379,44 @@ 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
373 |
# ์ฑํ
ํ์์ผ๋ก ๋ณํ
|
374 |
-
|
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,7 +438,7 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
|
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:
|
@@ -412,7 +451,7 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
|
412 |
)
|
413 |
session_history = legacy.Chatbot(
|
414 |
show_label=False,
|
415 |
-
height=
|
416 |
elem_classes="session-chatbot"
|
417 |
)
|
418 |
close_btn = antd.Button(
|
@@ -566,25 +605,6 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
|
566 |
with antd.Tabs.Item(key="render"):
|
567 |
sandbox = gr.HTML(elem_classes="html_content")
|
568 |
|
569 |
-
# Code ์คํ ๋ฒํผ ์ด๋ฒคํธ ํธ๋ค๋ฌ
|
570 |
-
def execute_code(query: str):
|
571 |
-
if not query or query.strip() == '':
|
572 |
-
return None, gr.update(active_key="empty")
|
573 |
-
|
574 |
-
try:
|
575 |
-
# HTML ์ฝ๋ ๋ธ๋ก ํ์ธ
|
576 |
-
if '```html' in query and '```' in query:
|
577 |
-
# HTML ์ฝ๋ ๋ธ๋ก ์ถ์ถ
|
578 |
-
code = remove_code_block(query)
|
579 |
-
else:
|
580 |
-
# ์
๋ ฅ๋ ํ
์คํธ๋ฅผ ๊ทธ๋๋ก ์ฝ๋๋ก ์ฌ์ฉ
|
581 |
-
code = query.strip()
|
582 |
-
|
583 |
-
return send_to_sandbox(code), gr.update(active_key="render")
|
584 |
-
except Exception as e:
|
585 |
-
print(f"Error executing code: {str(e)}")
|
586 |
-
return None, gr.update(active_key="empty")
|
587 |
-
|
588 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ค
|
589 |
execute_btn.click(
|
590 |
fn=execute_code,
|
@@ -592,18 +612,30 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
|
592 |
outputs=[sandbox, state_tab]
|
593 |
)
|
594 |
|
595 |
-
codeBtn.click(
|
596 |
-
|
597 |
-
|
598 |
-
|
599 |
-
|
600 |
-
|
601 |
-
|
602 |
-
open=False),
|
603 |
-
|
604 |
-
|
|
|
605 |
|
|
|
|
|
|
|
|
|
|
|
606 |
|
|
|
|
|
|
|
|
|
|
|
|
|
607 |
# ์ธ์
๊ด๋ จ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ค
|
608 |
sessionBtn.click(
|
609 |
lambda: (gr.update(open=True), update_session_list()),
|
@@ -611,7 +643,6 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
|
611 |
outputs=[session_drawer, session_list]
|
612 |
)
|
613 |
|
614 |
-
# session_drawer ๋ซ๊ธฐ ์ด๋ฒคํธ ์ถ๊ฐ
|
615 |
session_drawer.close(
|
616 |
lambda: (gr.update(open=False), gr.update(choices=[]), []),
|
617 |
outputs=[session_drawer, session_list, session_history]
|
@@ -622,10 +653,8 @@ with gr.Blocks(css_paths="app.css",theme=theme) as demo:
|
|
622 |
outputs=[session_drawer, session_list, session_history]
|
623 |
)
|
624 |
|
625 |
-
|
626 |
-
|
627 |
session_list.change(
|
628 |
-
load_session_history,
|
629 |
inputs=[session_list],
|
630 |
outputs=[session_history]
|
631 |
)
|
|
|
346 |
finally:
|
347 |
conn.close()
|
348 |
|
|
|
|
|
349 |
def update_session_list():
|
350 |
try:
|
351 |
conn = sqlite3.connect('chat_history.db')
|
352 |
c = conn.cursor()
|
353 |
+
# ์ธ์
๊ณผ ๊ฐ์ฅ ์ต๊ทผ ๋ํ ์๊ฐ์ ํจ๊ป ๊ฐ์ ธ์ด
|
354 |
+
c.execute("""
|
355 |
+
SELECT s.session_id, MAX(ch.timestamp) as last_chat
|
356 |
+
FROM sessions s
|
357 |
+
LEFT JOIN chat_history ch ON s.session_id = ch.session_id
|
358 |
+
GROUP BY s.session_id
|
359 |
+
ORDER BY last_chat DESC NULLS LAST
|
360 |
+
""")
|
361 |
sessions = c.fetchall()
|
362 |
conn.close()
|
363 |
+
|
364 |
+
# ์ธ์
ID์ ์๊ฐ์ ํฌ๋งทํ
|
365 |
+
formatted_sessions = []
|
366 |
+
for session_id, last_chat in sessions:
|
367 |
+
if last_chat:
|
368 |
+
time_str = datetime.strptime(last_chat, '%Y-%m-%d %H:%M:%S.%f').strftime('%Y-%m-%d %H:%M:%S')
|
369 |
+
formatted_sessions.append(f"{session_id} ({time_str})")
|
370 |
+
else:
|
371 |
+
formatted_sessions.append(session_id)
|
372 |
+
|
373 |
return gr.update(choices=formatted_sessions)
|
374 |
except Exception as e:
|
375 |
print(f"Error updating session list: {e}")
|
|
|
379 |
try:
|
380 |
if not selected_session:
|
381 |
return []
|
382 |
+
# ์ธ์
ID ์ถ์ถ
|
383 |
+
session_id = selected_session.split(" (")[0] if " (" in selected_session else selected_session
|
384 |
+
|
385 |
+
conn = sqlite3.connect('chat_history.db')
|
386 |
+
c = conn.cursor()
|
387 |
+
c.execute("""
|
388 |
+
SELECT prompt, response, timestamp
|
389 |
+
FROM chat_history
|
390 |
+
WHERE session_id = ?
|
391 |
+
ORDER BY timestamp
|
392 |
+
""", (session_id,))
|
393 |
+
history = c.fetchall()
|
394 |
+
conn.close()
|
395 |
+
|
396 |
# ์ฑํ
ํ์์ผ๋ก ๋ณํ
|
397 |
+
return [[prompt, response] for prompt, response, _ in history]
|
|
|
|
|
|
|
398 |
except Exception as e:
|
399 |
print(f"Error loading session history: {e}")
|
400 |
return []
|
401 |
+
|
402 |
+
|
403 |
+
def save_chat(session_id, prompt, response):
|
404 |
+
try:
|
405 |
+
conn = sqlite3.connect('chat_history.db')
|
406 |
+
c = conn.cursor()
|
407 |
+
current_time = datetime.now()
|
408 |
+
c.execute("""
|
409 |
+
INSERT INTO chat_history (session_id, prompt, response, timestamp)
|
410 |
+
VALUES (?, ?, ?, ?)
|
411 |
+
""", (session_id, prompt, response, current_time))
|
412 |
+
conn.commit()
|
413 |
+
print(f"Chat saved for session {session_id}")
|
414 |
+
except Exception as e:
|
415 |
+
print(f"Error saving chat: {e}")
|
416 |
+
finally:
|
417 |
+
if 'conn' in locals():
|
418 |
+
conn.close()
|
419 |
+
|
420 |
# Demo ์ธ์คํด์ค ๋จผ์ ์์ฑ
|
421 |
demo_instance = Demo()
|
422 |
|
|
|
438 |
with antd.Drawer(
|
439 |
open=False,
|
440 |
title="Session History",
|
441 |
+
placement="right", # ์ค๋ฅธ์ชฝ์ผ๋ก ๋ณ๊ฒฝ
|
442 |
width="900px",
|
443 |
elem_classes="session-drawer"
|
444 |
) as session_drawer:
|
|
|
451 |
)
|
452 |
session_history = legacy.Chatbot(
|
453 |
show_label=False,
|
454 |
+
height=700,
|
455 |
elem_classes="session-chatbot"
|
456 |
)
|
457 |
close_btn = antd.Button(
|
|
|
605 |
with antd.Tabs.Item(key="render"):
|
606 |
sandbox = gr.HTML(elem_classes="html_content")
|
607 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
608 |
# ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ค
|
609 |
execute_btn.click(
|
610 |
fn=execute_code,
|
|
|
612 |
outputs=[sandbox, state_tab]
|
613 |
)
|
614 |
|
615 |
+
codeBtn.click(
|
616 |
+
lambda: gr.update(open=True),
|
617 |
+
inputs=[],
|
618 |
+
outputs=[code_drawer]
|
619 |
+
)
|
620 |
+
|
621 |
+
code_drawer.close(
|
622 |
+
lambda: gr.update(open=False),
|
623 |
+
inputs=[],
|
624 |
+
outputs=[code_drawer]
|
625 |
+
)
|
626 |
|
627 |
+
historyBtn.click(
|
628 |
+
history_render,
|
629 |
+
inputs=[history],
|
630 |
+
outputs=[history_drawer, history_output]
|
631 |
+
)
|
632 |
|
633 |
+
history_drawer.close(
|
634 |
+
lambda: gr.update(open=False),
|
635 |
+
inputs=[],
|
636 |
+
outputs=[history_drawer]
|
637 |
+
)
|
638 |
+
|
639 |
# ์ธ์
๊ด๋ จ ์ด๋ฒคํธ ํธ๋ค๋ฌ๋ค
|
640 |
sessionBtn.click(
|
641 |
lambda: (gr.update(open=True), update_session_list()),
|
|
|
643 |
outputs=[session_drawer, session_list]
|
644 |
)
|
645 |
|
|
|
646 |
session_drawer.close(
|
647 |
lambda: (gr.update(open=False), gr.update(choices=[]), []),
|
648 |
outputs=[session_drawer, session_list, session_history]
|
|
|
653 |
outputs=[session_drawer, session_list, session_history]
|
654 |
)
|
655 |
|
|
|
|
|
656 |
session_list.change(
|
657 |
+
load_session_history,
|
658 |
inputs=[session_list],
|
659 |
outputs=[session_history]
|
660 |
)
|