huylaughmad commited on
Commit
5c384ff
·
verified ·
1 Parent(s): 7d67079

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -22
app.py CHANGED
@@ -11,32 +11,49 @@ auth = HTTPBasicAuth()
11
 
12
  # Hàm kết nối với SQLite
13
  def get_db_connection():
14
- db_path = '/app/database.db'
15
- db_dir = os.path.dirname(db_path)
16
 
17
- # Tạo thư mục /app/ nếu chưa tồn tại
18
- if not os.path.exists(db_dir):
19
- try:
20
- os.makedirs(db_dir, exist_ok=True)
21
- except Exception as e:
22
- raise RuntimeError(f"Không thể tạo thư mục {db_dir}: {str(e)}")
23
-
24
- # Kiểm tra quyền ghi
25
- if not os.access(db_dir, os.W_OK):
26
- # Nếu không có quyền ghi, thử sử dụng /tmp/
27
- db_path = '/tmp/database.db'
28
- db_dir = os.path.dirname(db_path)
29
- if not os.path.exists(db_dir):
30
- os.makedirs(db_dir, exist_ok=True)
31
- if not os.access(db_dir, os.W_OK):
32
- raise RuntimeError(f"Không có quyền ghi tại {db_dir} và /tmp/")
33
 
34
  try:
35
- conn = sqlite3.connect(db_path)
36
- conn.row_factory = sqlite3.Row # Để trả về dữ liệu dưới dạng dictionary
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
37
  return conn
38
- except sqlite3.OperationalError as e:
39
- raise RuntimeError(f"Không thể mở file cơ sở dữ liệu tại {db_path}: {str(e)}")
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  # Tạo các bảng nếu chưa tồn tại
42
  def init_db():
 
11
 
12
  # Hàm kết nối với SQLite
13
  def get_db_connection():
14
+ # Đường dẫn tạm thời để lưu database.db
15
+ temp_db_path = '/tmp/database.db'
16
 
17
+ # Thông tin dataset trên Hugging Face
18
+ repo_id = "huylaughmad/web"
19
+ filename = "database.db"
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  try:
22
+ # Tải file database.db từ Hugging Face Dataset
23
+ downloaded_path = hf_hub_download(
24
+ repo_id=repo_id,
25
+ filename=filename,
26
+ repo_type="dataset",
27
+ token=os.getenv("HF_TOKEN", None) # Token nếu dataset yêu cầu xác thực
28
+ )
29
+
30
+ # Sao chép file từ đường dẫn tải về sang /tmp/database.db
31
+ with open(downloaded_path, 'rb') as src, open(temp_db_path, 'wb') as dst:
32
+ dst.write(src.read())
33
+
34
+ # Kiểm tra quyền ghi vào /tmp/
35
+ if not os.access(os.path.dirname(temp_db_path), os.W_OK):
36
+ raise RuntimeError(f"Không có quyền ghi tại {os.path.dirname(temp_db_path)}")
37
+
38
+ # Kết nối với SQLite
39
+ conn = sqlite3.connect(temp_db_path)
40
+ conn.row_factory = sqlite3.Row # Trả về dữ liệu dưới dạng dictionary
41
  return conn
42
+
43
+ except Exception as e:
44
+ # Nếu không tìm thấy file hoặc có lỗi, tạo mới database.db trong /tmp/
45
+ if not os.path.exists(temp_db_path):
46
+ try:
47
+ open(temp_db_path, 'a').close() # Tạo file rỗng
48
+ except Exception as create_error:
49
+ raise RuntimeError(f"Không thể tạo file cơ sở dữ liệu tại {temp_db_path}: {str(create_error)}")
50
+
51
+ try:
52
+ conn = sqlite3.connect(temp_db_path)
53
+ conn.row_factory = sqlite3.Row
54
+ return conn
55
+ except sqlite3.OperationalError as sql_error:
56
+ raise RuntimeError(f"Không thể mở file cơ sở dữ liệu tại {temp_db_path}: {str(sql_error)}")
57
 
58
  # Tạo các bảng nếu chưa tồn tại
59
  def init_db():