JumaRubea commited on
Commit
ecd5551
·
verified ·
1 Parent(s): 17aad32

Update src/chats.py

Browse files
Files changed (1) hide show
  1. src/chats.py +45 -24
src/chats.py CHANGED
@@ -2,32 +2,53 @@ import sqlite3
2
  import datetime
3
  import os
4
 
5
- # Use /data for persistent storage in Hugging Face Spaces
6
- DB_FILE = "/data/chats.db" # Changed from os.getcwd()
7
 
8
  def init_db():
9
- # Ensure the /data directory exists (optional, as /data is typically available)
10
- os.makedirs(os.path.dirname(DB_FILE), exist_ok=True)
11
- with sqlite3.connect(DB_FILE) as conn:
12
- c = conn.cursor()
13
- c.execute('''
14
- CREATE TABLE IF NOT EXISTS chats (
15
- id INTEGER PRIMARY KEY AUTOINCREMENT,
16
- title TEXT,
17
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
18
- )
19
- ''')
20
- c.execute('''
21
- CREATE TABLE IF NOT EXISTS messages (
22
- id INTEGER PRIMARY KEY AUTOINCREMENT,
23
- chat_id INTEGER,
24
- role TEXT,
25
- content TEXT,
26
- timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
27
- FOREIGN KEY(chat_id) REFERENCES chats(id)
28
- )
29
- ''')
30
- conn.commit()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
 
32
  # Rest of the code remains unchanged
33
  def create_new_chat():
 
2
  import datetime
3
  import os
4
 
5
+ # Use /data for persistent storage
6
+ DB_FILE = "/data/chats.db"
7
 
8
  def init_db():
9
+ # Debug: Print the database path and check directory permissions
10
+ print(f"Database path: {DB_FILE}")
11
+ db_dir = os.path.dirname(DB_FILE)
12
+ print(f"Database directory: {db_dir}")
13
+ if not os.path.exists(db_dir):
14
+ print(f"Directory {db_dir} does not exist, attempting to create it")
15
+ try:
16
+ os.makedirs(db_dir, exist_ok=True)
17
+ except Exception as e:
18
+ print(f"Failed to create directory {db_dir}: {str(e)}")
19
+ raise RuntimeError(f"Cannot create directory {db_dir}: {str(e)}")
20
+ if not os.access(db_dir, os.W_OK):
21
+ print(f"Directory {db_dir} is not writable")
22
+ raise RuntimeError(f"Directory {db_dir} is not writable")
23
+
24
+ # Attempt to connect to the database
25
+ print(f"Attempting to connect to database at {DB_FILE}")
26
+ try:
27
+ with sqlite3.connect(DB_FILE) as conn:
28
+ print("Successfully connected to database")
29
+ c = conn.cursor()
30
+ c.execute('''
31
+ CREATE TABLE IF NOT EXISTS chats (
32
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
33
+ title TEXT,
34
+ created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
35
+ )
36
+ ''')
37
+ c.execute('''
38
+ CREATE TABLE IF NOT EXISTS messages (
39
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
40
+ chat_id INTEGER,
41
+ role TEXT,
42
+ content TEXT,
43
+ timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
44
+ FOREIGN KEY(chat_id) REFERENCES chats(id)
45
+ )
46
+ ''')
47
+ conn.commit()
48
+ print("Database tables created successfully")
49
+ except sqlite3.OperationalError as e:
50
+ print(f"SQLite error: {str(e)}")
51
+ raise
52
 
53
  # Rest of the code remains unchanged
54
  def create_new_chat():