Spaces:
Sleeping
Sleeping
Update chatbot/database.py
Browse files- chatbot/database.py +9 -19
chatbot/database.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
import sqlite3
|
| 2 |
-
from config import DB_PATH
|
| 3 |
|
| 4 |
def create_table():
|
| 5 |
"""Creates the chat history table if it doesn't exist."""
|
|
@@ -8,7 +8,6 @@ def create_table():
|
|
| 8 |
cursor.execute("""
|
| 9 |
CREATE TABLE IF NOT EXISTS chat_history (
|
| 10 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
| 11 |
-
user_id TEXT,
|
| 12 |
session_id TEXT,
|
| 13 |
user_message TEXT,
|
| 14 |
bot_response TEXT,
|
|
@@ -18,33 +17,24 @@ def create_table():
|
|
| 18 |
conn.commit()
|
| 19 |
conn.close()
|
| 20 |
|
| 21 |
-
def
|
| 22 |
-
"""Fetches
|
| 23 |
conn = sqlite3.connect(DB_PATH)
|
| 24 |
cursor = conn.cursor()
|
| 25 |
-
cursor.execute("SELECT
|
| 26 |
-
|
| 27 |
-
conn.close()
|
| 28 |
-
return [session[0] for session in sessions]
|
| 29 |
-
|
| 30 |
-
def get_chat_history(user_id, session_id):
|
| 31 |
-
"""Fetches chat history for a given user and session."""
|
| 32 |
-
conn = sqlite3.connect(DB_PATH)
|
| 33 |
-
cursor = conn.cursor()
|
| 34 |
-
cursor.execute("SELECT user_message, bot_response FROM chat_history WHERE user_id=? AND session_id=? ORDER BY timestamp",
|
| 35 |
-
(user_id, session_id))
|
| 36 |
history = cursor.fetchall()
|
| 37 |
conn.close()
|
| 38 |
return history
|
| 39 |
|
| 40 |
-
def save_chat(
|
| 41 |
"""Stores a chat message in the database."""
|
| 42 |
conn = sqlite3.connect(DB_PATH)
|
| 43 |
cursor = conn.cursor()
|
| 44 |
-
cursor.execute("INSERT INTO chat_history (
|
| 45 |
-
(
|
| 46 |
conn.commit()
|
| 47 |
conn.close()
|
| 48 |
|
| 49 |
# Initialize the database
|
| 50 |
-
create_table()
|
|
|
|
| 1 |
import sqlite3
|
| 2 |
+
from config import DB_PATH # Import database path from config.py
|
| 3 |
|
| 4 |
def create_table():
|
| 5 |
"""Creates the chat history table if it doesn't exist."""
|
|
|
|
| 8 |
cursor.execute("""
|
| 9 |
CREATE TABLE IF NOT EXISTS chat_history (
|
| 10 |
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
| 11 |
session_id TEXT,
|
| 12 |
user_message TEXT,
|
| 13 |
bot_response TEXT,
|
|
|
|
| 17 |
conn.commit()
|
| 18 |
conn.close()
|
| 19 |
|
| 20 |
+
def get_chat_history(session_id):
|
| 21 |
+
"""Fetches chat history for a given session."""
|
| 22 |
conn = sqlite3.connect(DB_PATH)
|
| 23 |
cursor = conn.cursor()
|
| 24 |
+
cursor.execute("SELECT user_message, bot_response FROM chat_history WHERE session_id=? ORDER BY timestamp",
|
| 25 |
+
(session_id,))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
history = cursor.fetchall()
|
| 27 |
conn.close()
|
| 28 |
return history
|
| 29 |
|
| 30 |
+
def save_chat(session_id, user_message, bot_response):
|
| 31 |
"""Stores a chat message in the database."""
|
| 32 |
conn = sqlite3.connect(DB_PATH)
|
| 33 |
cursor = conn.cursor()
|
| 34 |
+
cursor.execute("INSERT INTO chat_history (session_id, user_message, bot_response) VALUES (?, ?, ?)",
|
| 35 |
+
(session_id, user_message, bot_response))
|
| 36 |
conn.commit()
|
| 37 |
conn.close()
|
| 38 |
|
| 39 |
# Initialize the database
|
| 40 |
+
create_table()
|