Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,67 +19,105 @@ def get_db_connection():
|
|
| 19 |
# Đường dẫn tạm thời để lưu database.db
|
| 20 |
temp_db_path = '/tmp/database.db'
|
| 21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
# Thông tin dataset trên Hugging Face
|
| 23 |
repo_id = "huylaughmad/web"
|
| 24 |
filename = "database.db"
|
| 25 |
repo_type = "dataset"
|
| 26 |
|
| 27 |
-
# Token Hugging Face
|
| 28 |
hf_token = os.getenv("HF_TOKEN")
|
| 29 |
if not hf_token:
|
| 30 |
raise RuntimeError("HF_TOKEN không được thiết lập trong biến môi trường")
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
try:
|
| 35 |
-
logger.info(f"Kiểm tra file {filename} trên dataset {repo_id}")
|
| 36 |
-
downloaded_path = hf_hub_download(
|
| 37 |
-
repo_id=repo_id,
|
| 38 |
-
filename=filename,
|
| 39 |
-
repo_type=repo_type,
|
| 40 |
-
token=hf_token
|
| 41 |
-
)
|
| 42 |
-
logger.info(f"Tải file thành công từ {downloaded_path}")
|
| 43 |
-
|
| 44 |
-
# Sao chép file từ đường dẫn tải về sang /tmp/database.db
|
| 45 |
-
with open(downloaded_path, 'rb') as src, open(temp_db_path, 'wb') as dst:
|
| 46 |
-
dst.write(src.read())
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
except Exception as e:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
)
|
| 68 |
-
logger.info(f"File {filename} đã được đẩy lên dataset thành công")
|
| 69 |
-
|
| 70 |
-
# Kiểm tra quyền ghi vào /tmp/
|
| 71 |
-
if not os.access(os.path.dirname(temp_db_path), os.W_OK):
|
| 72 |
-
raise RuntimeError(f"Không có quyền ghi tại {os.path.dirname(temp_db_path)}")
|
| 73 |
-
|
| 74 |
-
# Kết nối với SQLite
|
| 75 |
-
conn = sqlite3.connect(temp_db_path)
|
| 76 |
-
conn.row_factory = sqlite3.Row # Trả về dữ liệu dưới dạng dictionary
|
| 77 |
-
return conn
|
| 78 |
-
|
| 79 |
-
except sqlite3.OperationalError as sql_error:
|
| 80 |
-
raise RuntimeError(f"Không thể mở file cơ sở dữ liệu tại {temp_db_path}: {str(sql_error)}")
|
| 81 |
except Exception as e:
|
| 82 |
-
|
| 83 |
|
| 84 |
# Tạo các bảng nếu chưa tồn tại
|
| 85 |
def init_db():
|
|
@@ -737,6 +775,7 @@ def add_case():
|
|
| 737 |
request.form.get('case-tag', '')
|
| 738 |
))
|
| 739 |
conn.commit()
|
|
|
|
| 740 |
conn.close()
|
| 741 |
return redirect(url_for('cms'))
|
| 742 |
|
|
|
|
| 19 |
# Đường dẫn tạm thời để lưu database.db
|
| 20 |
temp_db_path = '/tmp/database.db'
|
| 21 |
|
| 22 |
+
# Thư mục cache tùy chỉnh mà ứng dụng có quyền ghi
|
| 23 |
+
cache_dir = '/tmp/hf_cache'
|
| 24 |
+
os.makedirs(cache_dir, exist_ok=True) # Tạo thư mục cache nếu chưa tồn tại
|
| 25 |
+
|
| 26 |
+
# Thiết lập biến môi trường cho huggingface_hub để sử dụng cache_dir
|
| 27 |
+
os.environ['HF_HOME'] = cache_dir
|
| 28 |
+
|
| 29 |
# Thông tin dataset trên Hugging Face
|
| 30 |
repo_id = "huylaughmad/web"
|
| 31 |
filename = "database.db"
|
| 32 |
repo_type = "dataset"
|
| 33 |
|
| 34 |
+
# Token Hugging Face
|
| 35 |
hf_token = os.getenv("HF_TOKEN")
|
| 36 |
if not hf_token:
|
| 37 |
raise RuntimeError("HF_TOKEN không được thiết lập trong biến môi trường")
|
| 38 |
|
| 39 |
+
# Khởi tạo Hugging Face API
|
| 40 |
+
api = HfApi()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
# Khóa file để tránh xung đột giữa các worker
|
| 43 |
+
lock_file = '/tmp/database.lock'
|
| 44 |
+
with open(lock_file, 'a'):
|
| 45 |
+
import fcntl
|
| 46 |
+
fcntl.flock(lock_file, fcntl.LOCK_EX)
|
| 47 |
+
try:
|
| 48 |
+
# Kiểm tra xem file database.db đã tồn tại cục bộ chưa
|
| 49 |
+
if os.path.exists(temp_db_path):
|
| 50 |
+
logger.info(f"File {temp_db_path} đã tồn tại cục bộ, sử dụng file này")
|
| 51 |
+
else:
|
| 52 |
+
# Kiểm tra xem file database.db có trên dataset không
|
| 53 |
+
try:
|
| 54 |
+
logger.info(f"Kiểm tra file {filename} trên dataset {repo_id}")
|
| 55 |
+
downloaded_path = hf_hub_download(
|
| 56 |
+
repo_id=repo_id,
|
| 57 |
+
filename=filename,
|
| 58 |
+
repo_type=repo_type,
|
| 59 |
+
token=hf_token,
|
| 60 |
+
cache_dir=cache_dir
|
| 61 |
+
)
|
| 62 |
+
logger.info(f"Tải file thành công từ {downloaded_path}")
|
| 63 |
+
|
| 64 |
+
# Sao chép file từ đường dẫn tải về sang /tmp/database.db
|
| 65 |
+
with open(downloaded_path, 'rb') as src, open(temp_db_path, 'wb') as dst:
|
| 66 |
+
dst.write(src.read())
|
| 67 |
+
|
| 68 |
+
except Exception as e:
|
| 69 |
+
logger.warning(f"Không tìm thấy file {filename} trên dataset hoặc lỗi tải: {str(e)}")
|
| 70 |
+
# Tạo file database.db mới trong /tmp/
|
| 71 |
+
logger.info(f"Tạo file database mới tại {temp_db_path}")
|
| 72 |
+
open(temp_db_path, 'a').close() # Tạo file rỗng
|
| 73 |
+
|
| 74 |
+
# Gọi init_db() để tạo các bảng và điền dữ liệu mẫu
|
| 75 |
+
logger.info("Khởi tạo cơ sở dữ liệu với các bảng và dữ liệu mẫu")
|
| 76 |
+
init_db()
|
| 77 |
+
|
| 78 |
+
# Đẩy file database.db mới tạo lên dataset
|
| 79 |
+
logger.info(f"Đẩy file {temp_db_path} lên dataset {repo_id}")
|
| 80 |
+
upload_file(
|
| 81 |
+
path_or_fileobj=temp_db_path,
|
| 82 |
+
path_in_repo=filename,
|
| 83 |
+
repo_id=repo_id,
|
| 84 |
+
repo_type=repo_type,
|
| 85 |
+
token=hf_token
|
| 86 |
+
)
|
| 87 |
+
logger.info(f"File {filename} đã được đẩy lên dataset thành công")
|
| 88 |
+
|
| 89 |
+
# Kiểm tra quyền ghi vào /tmp/
|
| 90 |
+
if not os.access(os.path.dirname(temp_db_path), os.W_OK):
|
| 91 |
+
raise RuntimeError(f"Không có quyền ghi tại {os.path.dirname(temp_db_path)}")
|
| 92 |
+
|
| 93 |
+
# Kết nối với SQLite
|
| 94 |
+
conn = sqlite3.connect(temp_db_path)
|
| 95 |
+
conn.row_factory = sqlite3.Row # Trả về dữ liệu dưới dạng dictionary
|
| 96 |
+
return conn
|
| 97 |
+
|
| 98 |
+
except sqlite3.OperationalError as sql_error:
|
| 99 |
+
raise RuntimeError(f"Không thể mở file cơ sở dữ liệu tại {temp_db_path}: {str(sql_error)}")
|
| 100 |
except Exception as e:
|
| 101 |
+
raise RuntimeError(f"Lỗi khi xử lý cơ sở dữ liệu: {str(e)}")
|
| 102 |
+
finally:
|
| 103 |
+
fcntl.flock(lock_file, fcntl.LOCK_UN) # Mở khóa
|
| 104 |
+
|
| 105 |
+
def update_db_to_huggingface():
|
| 106 |
+
try:
|
| 107 |
+
cache_dir = '/tmp/hf_cache'
|
| 108 |
+
os.makedirs(cache_dir, exist_ok=True)
|
| 109 |
+
os.environ['HF_HOME'] = cache_dir
|
| 110 |
+
|
| 111 |
+
upload_file(
|
| 112 |
+
path_or_fileobj="/tmp/database.db",
|
| 113 |
+
path_in_repo="database.db",
|
| 114 |
+
repo_id="huylaughmad/web",
|
| 115 |
+
repo_type="dataset",
|
| 116 |
+
token=os.getenv("HF_TOKEN")
|
| 117 |
+
)
|
| 118 |
+
logger.info("Cập nhật database.db lên dataset thành công")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
except Exception as e:
|
| 120 |
+
logger.error(f"Lỗi khi cập nhật database.db lên dataset: {str(e)}")
|
| 121 |
|
| 122 |
# Tạo các bảng nếu chưa tồn tại
|
| 123 |
def init_db():
|
|
|
|
| 775 |
request.form.get('case-tag', '')
|
| 776 |
))
|
| 777 |
conn.commit()
|
| 778 |
+
update_db_to_huggingface() # Đẩy file lên dataset sau khi ghi
|
| 779 |
conn.close()
|
| 780 |
return redirect(url_for('cms'))
|
| 781 |
|