Spaces:
Running
Running
| import os | |
| from huggingface_hub import HfApi | |
| from dotenv import load_dotenv | |
| def push_to_hf(): | |
| load_dotenv() | |
| repo_id = os.getenv("HF_DATASET_REPO") | |
| token = os.getenv("HF_TOKEN_WRITE") | |
| if not repo_id or not token: | |
| print("❌ Error: HF_DATASET_REPO or HF_TOKEN_WRITE not found in .env") | |
| return | |
| api = HfApi() | |
| # Thư mục dữ liệu cần đẩy lên | |
| base_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| data_dir = os.path.join(base_dir, "data") | |
| if not os.path.exists(data_dir): | |
| print(f"ℹ️ Data directory {data_dir} not found, nothing to push.") | |
| return | |
| print(f"🚀 Starting push to HF Dataset: {repo_id}...") | |
| try: | |
| # Đẩy toàn bộ nội dung trong thư mục data lên Dataset | |
| # path_in_repo="" nghĩa là đẩy vào root của dataset | |
| api.upload_folder( | |
| folder_path=data_dir, | |
| repo_id=repo_id, | |
| repo_type="dataset", | |
| token=token, | |
| # Chỉ đẩy các file quan trọng, bỏ qua cache | |
| allow_patterns=["*.db", "user_indexes/*", "system_index/*", "metadata/*"], | |
| delete_not_maintained_files=False # Cẩn thận: nếu True sẽ xóa file trên HF nếu local không có | |
| ) | |
| print("✅ Push to HF Dataset completed successfully.") | |
| except Exception as e: | |
| print(f"⚠️ Push failed: {e}") | |
| if __name__ == "__main__": | |
| push_to_hf() | |