lanny xu commited on
Commit
63ec70e
·
1 Parent(s): 52f92a4

resolve conflict

Browse files
Files changed (1) hide show
  1. KAGGLE_INIT.py +148 -0
KAGGLE_INIT.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Kaggle 会话初始化脚本
3
+ 解决 Stop Session 后项目丢失的问题
4
+
5
+ 使用方法:
6
+ 在 Kaggle Notebook 第一个单元格运行:
7
+ exec(open('/kaggle/input/your-dataset/KAGGLE_INIT.py').read())
8
+ 或者直接复制此脚本内容到第一个单元格
9
+ """
10
+
11
+ import os
12
+ import subprocess
13
+ import sys
14
+ from pathlib import Path
15
+
16
+ print("🚀 Kaggle 会话自动初始化")
17
+ print("="*70)
18
+
19
+ # ==================== 配置区域 ====================
20
+ REPO_URL = "https://github.com/LannyCodes/adaptive_RAG.git"
21
+ PROJECT_DIR = "/kaggle/working/adaptive_RAG"
22
+ PREVIOUS_RUN_INPUT = "/kaggle/input/output" # 👈 修改为您保存的 Dataset 名称
23
+
24
+ # ==================== 1. 检查并克隆项目 ====================
25
+ print("\n📦 步骤 1: 检查项目状态...")
26
+
27
+ if os.path.exists(PROJECT_DIR):
28
+ print(f" ✅ 项目已存在: {PROJECT_DIR}")
29
+ print(" ℹ️ 如需更新代码,请运行:")
30
+ print(f" cd {PROJECT_DIR} && git pull origin main")
31
+ else:
32
+ print(f" 📥 项目不存在,开始克隆...")
33
+
34
+ os.chdir('/kaggle/working')
35
+
36
+ result = subprocess.run(
37
+ ['git', 'clone', REPO_URL],
38
+ capture_output=True,
39
+ text=True
40
+ )
41
+
42
+ if result.returncode == 0:
43
+ print(f" ✅ 项目克隆成功")
44
+ else:
45
+ print(f" ❌ 克隆失败:")
46
+ print(f" {result.stderr}")
47
+ print("\n 💡 可能的原因:")
48
+ print(" 1. 网络问题")
49
+ print(" 2. 仓库地址错误")
50
+ print(" 3. 仓库是私有的(需要认证)")
51
+ sys.exit(1)
52
+
53
+ # ==================== 2. 恢复之前的数据 ====================
54
+ print("\n💾 步骤 2: 检查之前的运行数据...")
55
+
56
+ if os.path.exists(PREVIOUS_RUN_INPUT):
57
+ print(f" ✅ 发现之前的数据: {PREVIOUS_RUN_INPUT}")
58
+
59
+ # 列出可恢复的文件
60
+ saved_files = list(Path(PREVIOUS_RUN_INPUT).glob('*'))
61
+
62
+ if saved_files:
63
+ print(f" 📂 可恢复的文件:")
64
+ for file in saved_files[:10]: # 只显示前10个
65
+ print(f" • {file.name}")
66
+
67
+ # 恢复知识图谱(如果存在)
68
+ kg_file = Path(PREVIOUS_RUN_INPUT) / 'knowledge_graph.pkl'
69
+ if kg_file.exists():
70
+ import shutil
71
+ dest = Path(PROJECT_DIR) / 'knowledge_graph.pkl'
72
+ shutil.copy2(kg_file, dest)
73
+ print(f" ✅ 已恢复知识图谱")
74
+
75
+ print(f"\n 💡 如需恢复其他文件,使用:")
76
+ print(f" import shutil")
77
+ print(f" shutil.copy2('{PREVIOUS_RUN_INPUT}/文件名', '{PROJECT_DIR}/文件名')")
78
+ else:
79
+ print(" ⚠️ 数据目录为空")
80
+ else:
81
+ print(" ℹ️ 未发现之前的运行数据(首次运行)")
82
+ print(f" 💡 会话结束时,将 /kaggle/working 保存为 Dataset")
83
+ print(f" 命名为: output")
84
+
85
+ # ==================== 3. 设置工作环境 ====================
86
+ print("\n⚙️ 步骤 3: 设置工作环境...")
87
+
88
+ # 进入项目目录
89
+ os.chdir(PROJECT_DIR)
90
+
91
+ # 添加到 Python 路径
92
+ if PROJECT_DIR not in sys.path:
93
+ sys.path.insert(0, PROJECT_DIR)
94
+
95
+ print(f" ✅ 当前目录: {os.getcwd()}")
96
+ print(f" ✅ Python 路径已更新")
97
+
98
+ # ==================== 4. 显示系统信息 ====================
99
+ print("\n📊 步骤 4: 系统信息...")
100
+
101
+ # Python 版本
102
+ print(f" • Python: {sys.version.split()[0]}")
103
+
104
+ # GPU 状态
105
+ gpu_check = subprocess.run(['nvidia-smi'], capture_output=True, text=True)
106
+ if gpu_check.returncode == 0:
107
+ # 提取 GPU 信息
108
+ for line in gpu_check.stdout.split('\n'):
109
+ if 'Tesla' in line or 'P100' in line or 'T4' in line:
110
+ print(f" • GPU: {line.strip()}")
111
+ break
112
+ else:
113
+ print(" • GPU: 不可用")
114
+
115
+ # 磁盘空间
116
+ disk_check = subprocess.run(['df', '-h', '/kaggle/working'], capture_output=True, text=True)
117
+ if disk_check.returncode == 0:
118
+ lines = disk_check.stdout.strip().split('\n')
119
+ if len(lines) > 1:
120
+ info = lines[1].split()
121
+ print(f" • 可用空间: {info[3]}")
122
+
123
+ # ==================== 5. 快速测试 ====================
124
+ print("\n🧪 步骤 5: 快速测试...")
125
+
126
+ # 检查关键文件
127
+ key_files = [
128
+ 'entity_extractor.py',
129
+ 'graph_indexer.py',
130
+ 'knowledge_graph.py',
131
+ 'config.py'
132
+ ]
133
+
134
+ all_files_exist = True
135
+ for file in key_files:
136
+ if os.path.exists(file):
137
+ print(f" ✅ {file}")
138
+ else:
139
+ print(f" ❌ {file} 缺失")
140
+ all_files_exist = False
141
+
142
+ if not all_files_exist:
143
+ print("\n ⚠️ 部分关键文件缺失,请检查仓库")
144
+
145
+ # ==================== 完成 ====================
146
+ print("\n" + "="*70)
147
+ print("✅ 初始化完成!")
148
+ print("="*70)