Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -56,18 +56,17 @@ import sqlite3
|
|
| 56 |
from datetime import datetime
|
| 57 |
|
| 58 |
def init_db():
|
|
|
|
|
|
|
| 59 |
try:
|
| 60 |
conn = sqlite3.connect('chat_history.db')
|
| 61 |
c = conn.cursor()
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
c.execute("DROP TABLE IF EXISTS chat_history")
|
| 65 |
-
c.execute("DROP TABLE IF EXISTS sessions")
|
| 66 |
-
|
| 67 |
-
# 테이블 새로 생성
|
| 68 |
c.execute('''CREATE TABLE IF NOT EXISTS sessions
|
| 69 |
(session_id TEXT PRIMARY KEY,
|
| 70 |
created_at TIMESTAMP)''')
|
|
|
|
| 71 |
c.execute('''CREATE TABLE IF NOT EXISTS chat_history
|
| 72 |
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 73 |
session_id TEXT,
|
|
@@ -75,15 +74,36 @@ def init_db():
|
|
| 75 |
response TEXT,
|
| 76 |
timestamp TIMESTAMP,
|
| 77 |
FOREIGN KEY (session_id) REFERENCES sessions(session_id))''')
|
|
|
|
| 78 |
conn.commit()
|
|
|
|
|
|
|
| 79 |
except sqlite3.Error as e:
|
| 80 |
-
print(f"Database error: {e}")
|
| 81 |
raise
|
| 82 |
finally:
|
| 83 |
if conn:
|
| 84 |
conn.close()
|
| 85 |
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
def create_session():
|
| 88 |
max_attempts = 5
|
| 89 |
for attempt in range(max_attempts):
|
|
@@ -108,14 +128,7 @@ def create_session():
|
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
-
|
| 112 |
-
def save_chat(session_id, prompt, response):
|
| 113 |
-
conn = sqlite3.connect('chat_history.db')
|
| 114 |
-
c = conn.cursor()
|
| 115 |
-
c.execute("INSERT INTO chat_history (session_id, prompt, response, timestamp) VALUES (?, ?, ?, ?)",
|
| 116 |
-
(session_id, prompt, response, datetime.now()))
|
| 117 |
-
conn.commit()
|
| 118 |
-
conn.close()
|
| 119 |
|
| 120 |
# 세션별 히스토리 조회
|
| 121 |
def get_session_history(session_id):
|
|
@@ -282,8 +295,19 @@ class Demo:
|
|
| 282 |
collected_content = content
|
| 283 |
|
| 284 |
if collected_content:
|
| 285 |
-
|
| 286 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 287 |
|
| 288 |
_history = messages_to_history([
|
| 289 |
{'role': Role.SYSTEM, 'content': system_message}
|
|
@@ -399,22 +423,41 @@ def load_session_history(selected_session):
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 408 |
c.execute("""
|
| 409 |
INSERT INTO chat_history (session_id, prompt, response, timestamp)
|
| 410 |
VALUES (?, ?, ?, ?)
|
| 411 |
-
""", (session_id, prompt, response,
|
|
|
|
| 412 |
conn.commit()
|
| 413 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
except Exception as e:
|
| 415 |
-
print(f"
|
|
|
|
|
|
|
|
|
|
| 416 |
finally:
|
| 417 |
-
if
|
| 418 |
conn.close()
|
| 419 |
|
| 420 |
# Demo 인스턴스 먼저 생성
|
|
|
|
| 56 |
from datetime import datetime
|
| 57 |
|
| 58 |
def init_db():
|
| 59 |
+
print("Initializing database...")
|
| 60 |
+
conn = None
|
| 61 |
try:
|
| 62 |
conn = sqlite3.connect('chat_history.db')
|
| 63 |
c = conn.cursor()
|
| 64 |
|
| 65 |
+
# 테이블 생성
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
c.execute('''CREATE TABLE IF NOT EXISTS sessions
|
| 67 |
(session_id TEXT PRIMARY KEY,
|
| 68 |
created_at TIMESTAMP)''')
|
| 69 |
+
|
| 70 |
c.execute('''CREATE TABLE IF NOT EXISTS chat_history
|
| 71 |
(id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 72 |
session_id TEXT,
|
|
|
|
| 74 |
response TEXT,
|
| 75 |
timestamp TIMESTAMP,
|
| 76 |
FOREIGN KEY (session_id) REFERENCES sessions(session_id))''')
|
| 77 |
+
|
| 78 |
conn.commit()
|
| 79 |
+
print("Database initialized successfully")
|
| 80 |
+
|
| 81 |
except sqlite3.Error as e:
|
| 82 |
+
print(f"Database initialization error: {e}")
|
| 83 |
raise
|
| 84 |
finally:
|
| 85 |
if conn:
|
| 86 |
conn.close()
|
| 87 |
|
| 88 |
+
def check_db_status():
|
| 89 |
+
try:
|
| 90 |
+
conn = sqlite3.connect('chat_history.db')
|
| 91 |
+
c = conn.cursor()
|
| 92 |
+
|
| 93 |
+
# 테이블 존재 여부 확인
|
| 94 |
+
c.execute("SELECT name FROM sqlite_master WHERE type='table'")
|
| 95 |
+
tables = c.fetchall()
|
| 96 |
+
print(f"Existing tables: {tables}")
|
| 97 |
+
|
| 98 |
+
# 데이터 개수 확인
|
| 99 |
+
c.execute("SELECT COUNT(*) FROM chat_history")
|
| 100 |
+
chat_count = c.fetchone()[0]
|
| 101 |
+
print(f"Number of chat records: {chat_count}")
|
| 102 |
+
|
| 103 |
+
conn.close()
|
| 104 |
+
except Exception as e:
|
| 105 |
+
print(f"Error checking database status: {e}")
|
| 106 |
+
|
| 107 |
def create_session():
|
| 108 |
max_attempts = 5
|
| 109 |
for attempt in range(max_attempts):
|
|
|
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 132 |
|
| 133 |
# 세션별 히스토리 조회
|
| 134 |
def get_session_history(session_id):
|
|
|
|
| 295 |
collected_content = content
|
| 296 |
|
| 297 |
if collected_content:
|
| 298 |
+
try:
|
| 299 |
+
print(f"Attempting to save chat with session_id: {self.current_session}")
|
| 300 |
+
print(f"Query: {query}")
|
| 301 |
+
print(f"Content length: {len(collected_content)}")
|
| 302 |
+
|
| 303 |
+
# 채팅 내용 저장
|
| 304 |
+
save_chat(self.current_session, query, collected_content)
|
| 305 |
+
print("Chat saved successfully")
|
| 306 |
+
|
| 307 |
+
except Exception as save_error:
|
| 308 |
+
print(f"Error saving chat: {save_error}")
|
| 309 |
+
|
| 310 |
+
|
| 311 |
|
| 312 |
_history = messages_to_history([
|
| 313 |
{'role': Role.SYSTEM, 'content': system_message}
|
|
|
|
| 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}")
|
| 428 |
+
conn = None
|
| 429 |
try:
|
| 430 |
conn = sqlite3.connect('chat_history.db')
|
| 431 |
c = conn.cursor()
|
| 432 |
+
|
| 433 |
+
# 세션이 존재하는지 확인
|
| 434 |
+
c.execute("SELECT 1 FROM sessions WHERE session_id = ?", (session_id,))
|
| 435 |
+
if not c.fetchone():
|
| 436 |
+
print(f"Session {session_id} not found, creating new session")
|
| 437 |
+
c.execute("INSERT INTO sessions (session_id, created_at) VALUES (?, ?)",
|
| 438 |
+
(session_id, datetime.now()))
|
| 439 |
+
|
| 440 |
+
# 채팅 저장
|
| 441 |
c.execute("""
|
| 442 |
INSERT INTO chat_history (session_id, prompt, response, timestamp)
|
| 443 |
VALUES (?, ?, ?, ?)
|
| 444 |
+
""", (session_id, prompt, response, datetime.now()))
|
| 445 |
+
|
| 446 |
conn.commit()
|
| 447 |
+
print(f"Successfully saved chat for session {session_id}")
|
| 448 |
+
|
| 449 |
+
except sqlite3.Error as e:
|
| 450 |
+
print(f"Database error: {e}")
|
| 451 |
+
if conn:
|
| 452 |
+
conn.rollback()
|
| 453 |
+
raise
|
| 454 |
except Exception as e:
|
| 455 |
+
print(f"Unexpected error: {e}")
|
| 456 |
+
if conn:
|
| 457 |
+
conn.rollback()
|
| 458 |
+
raise
|
| 459 |
finally:
|
| 460 |
+
if conn:
|
| 461 |
conn.close()
|
| 462 |
|
| 463 |
# Demo 인스턴스 먼저 생성
|