omnics / scripts /auto-sync.py
qf46578's picture
🦞 添加长期记忆系统和自动同步脚本 (已清理敏感信息)
25a3971 verified
Raw History Blame Contribute Delete
2.98 kB
#!/usr/bin/env python3
"""
🦞 龙虾自动同步脚本
将本地代码和记忆同步到 Hugging Face Space
使用方式:
HF_TOKEN=your_token python3 auto-sync.py
"""
import os
import sys
import json
from datetime import datetime
from pathlib import Path
# 从环境变量读取配置
HF_TOKEN = os.environ.get('HF_TOKEN')
HF_REPO_ID = os.environ.get('HF_REPO_ID', 'qf46578/omnics')
LOCAL_PATH = '/root/.openclaw/workspace/omnics'
MEMORY_PATH = '/root/.openclaw/workspace/omnics/memory'
# 忽略模式
IGNORE_PATTERNS = [
'node_modules/',
'.git/',
'dist/',
'build/',
'coverage/',
'.env',
'*.log',
'__pycache__/',
'.vscode/',
'.idea/',
'.DS_Store',
'*.swp',
'*.swo',
'*~',
'.env.local',
'.env.production',
'memory/compressed/',
'memory/session-state.json',
]
def get_file_count():
"""获取待上传文件数量"""
count = 0
for root, dirs, files in os.walk(LOCAL_PATH):
dirs[:] = [d for d in dirs if d not in ['node_modules', '.git', 'dist', 'build', '__pycache__']]
count += len(files)
return count
def sync_to_hf():
"""同步到 Hugging Face"""
if not HF_TOKEN:
print('❌ 错误: 请设置 HF_TOKEN 环境变量')
print(' 使用方式: HF_TOKEN=your_token python3 auto-sync.py')
return False
print(f'🦞 龙虾开始同步...')
print(f'📁 本地路径: {LOCAL_PATH}')
print(f'🎯 目标仓库: {HF_REPO_ID}')
try:
from huggingface_hub import HfApi
api = HfApi(token=HF_TOKEN)
sync_time = datetime.utcnow().isoformat()
result = api.upload_folder(
folder_path=LOCAL_PATH,
repo_id=HF_REPO_ID,
repo_type='space',
commit_message=f'🦞 龙虾自动同步 - {sync_time}',
ignore_patterns=IGNORE_PATTERNS,
)
print(f'✅ 同步成功!')
print(f'🔗 查看项目: https://huggingface.co/spaces/{HF_REPO_ID}')
# 更新同步状态
sync_state = {
'last_sync': sync_time,
'status': 'success',
'files_synced': get_file_count(),
}
os.makedirs(MEMORY_PATH, exist_ok=True)
with open(os.path.join(MEMORY_PATH, 'hf-sync-state.json'), 'w') as f:
json.dump(sync_state, f, indent=2)
return True
except Exception as e:
print(f'❌ 同步失败: {e}')
return False
def get_sync_status():
"""获取同步状态"""
state_file = os.path.join(MEMORY_PATH, 'hf-sync-state.json')
if os.path.exists(state_file):
with open(state_file, 'r') as f:
return json.load(f)
return {'status': 'never_synced'}
if __name__ == '__main__':
if len(sys.argv) > 1 and sys.argv[1] == 'status':
status = get_sync_status()
print(json.dumps(status, indent=2))
else:
sync_to_hf()