#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 清理脚本:准备上传到 Hugging Face 该脚本会: 1. 清理不必要的文件(__pycache__, .pyc, .pyo等) 2. 检查大文件 3. 创建上传检查清单 """ import os import shutil from pathlib import Path from typing import List, Tuple def find_and_remove_patterns(root_dir: str, patterns: List[str]) -> List[str]: """ 查找并删除匹配模式的文件/文件夹 Args: root_dir: 根目录 patterns: 文件/文件夹模式列表 Returns: 已删除的文件/文件夹列表 """ removed = [] root_path = Path(root_dir) for pattern in patterns: for item in root_path.rglob(pattern): if item.exists(): try: if item.is_file(): item.unlink() removed.append(str(item)) elif item.is_dir(): shutil.rmtree(item) removed.append(str(item)) except Exception as e: print(f"警告: 无法删除 {item}: {e}") return removed def find_large_files(root_dir: str, size_mb: int = 50) -> List[Tuple[str, float]]: """ 查找大文件 Args: root_dir: 根目录 size_mb: 文件大小阈值(MB) Returns: (文件路径, 大小MB) 列表 """ large_files = [] root_path = Path(root_dir) size_bytes = size_mb * 1024 * 1024 for item in root_path.rglob('*'): if item.is_file(): try: size = item.stat().st_size if size > size_bytes: size_mb_actual = size / (1024 * 1024) large_files.append((str(item), size_mb_actual)) except Exception as e: print(f"警告: 无法检查 {item}: {e}") return large_files def check_gitignore(root_dir: str) -> bool: """ 检查是否存在 .gitignore 文件 Args: root_dir: 根目录 Returns: 是否存在 .gitignore """ gitignore_path = Path(root_dir) / '.gitignore' return gitignore_path.exists() def create_upload_checklist(root_dir: str) -> str: """ 创建上传检查清单 Args: root_dir: 根目录 Returns: 检查清单内容 """ checklist = """# Hugging Face 上传检查清单 ## ✅ 清理完成 ### 已删除的文件类型 - `__pycache__/` 文件夹 - `*.pyc`, `*.pyo`, `*.pyd` 文件 - `.DS_Store` 文件(macOS) - `.vscode/`, `.idea/` 文件夹 - `*.swp`, `*.swo` 文件 ### 需要手动检查的项目 1. **大文件检查** - 检查是否有超过50MB的文件 - 考虑使用 Git LFS 或排除这些文件 2. **敏感信息检查** - 检查是否有API密钥、密码等敏感信息 - 检查配置文件中的敏感数据 3. **数据文件** - 检查 `examples/data/` 目录 - 如果数据文件很大,考虑排除或使用外部链接 4. **模型文件** - 检查是否有预训练模型文件 - 大模型文件应使用 Git LFS 或 Hugging Face Model Hub 5. **日志文件** - 确保没有日志文件被包含 - 检查 `log/`, `logs/` 目录 ## 📦 上传到 Hugging Face ### 方法1: 使用 Hugging Face CLI ```bash # 安装 Hugging Face CLI pip install huggingface_hub # 登录 huggingface-cli login # 创建仓库(如果还没有) # 在 https://huggingface.co/new 创建新仓库 # 上传文件 cd /path/to/EasyTemporalPointProcess-main huggingface-cli upload / . --repo-type dataset ``` ### 方法2: 使用 Git ```bash # 初始化 Git 仓库(如果还没有) git init git add . git commit -m "Initial commit" # 添加 Hugging Face 远程仓库 git remote add origin https://huggingface.co// # 推送 git push origin main ``` ### 方法3: 使用 Web 界面 1. 访问 https://huggingface.co/new 2. 创建新的 Dataset 或 Space 3. 使用 Web 界面上传文件 ## 📝 文件结构说明 ``` EasyTemporalPointProcess-main/ ├── easy_tpp/ # 核心库代码 ├── examples/ # 示例代码 ├── notebooks/ # Jupyter notebooks ├── tests/ # 测试代码 ├── docs/ # 文档 ├── compute_cascade_metrics.py # 新增:级联指标计算脚本 ├── COMPUTE_METRICS_README.md # 新增:指标计算说明 ├── requirements.txt # 基础依赖 ├── requirements_compute_metrics.txt # 新增:指标计算依赖 ├── setup.py # 安装脚本 └── README.md # 项目说明 ``` ## ⚠️ 注意事项 1. **不要上传大文件到 Git 仓库** - 使用 Git LFS 或 Hugging Face 的存储系统 - 考虑使用外部链接引用大文件 2. **检查许可证** - 确保所有代码都有适当的许可证 - 检查第三方依赖的许可证兼容性 3. **README 文件** - 确保 README.md 清晰说明项目用途 - 包含安装和使用说明 4. **依赖管理** - 确保 requirements.txt 是最新的 - 考虑使用 `pip freeze` 生成精确版本 ## 🔍 验证上传 上传后,检查: - [ ] 所有文件都已上传 - [ ] 文件大小合理 - [ ] 没有敏感信息泄露 - [ ] README 显示正确 - [ ] 代码可以正常下载和使用 """ return checklist def main(): """主函数""" root_dir = os.path.dirname(os.path.abspath(__file__)) print("=" * 60) print("清理脚本:准备上传到 Hugging Face") print("=" * 60) # 要删除的模式 patterns_to_remove = [ '__pycache__', '*.pyc', '*.pyo', '*.pyd', '.DS_Store', '.vscode', '.idea', '*.swp', '*.swo', '*.log', '.pytest_cache', '.mypy_cache', '.ruff_cache', ] print("\n1. 清理不必要的文件...") removed = find_and_remove_patterns(root_dir, patterns_to_remove) if removed: print(f" 已删除 {len(removed)} 个文件/文件夹") for item in removed[:10]: # 只显示前10个 print(f" - {item}") if len(removed) > 10: print(f" ... 还有 {len(removed) - 10} 个文件/文件夹") else: print(" 没有找到需要删除的文件") # 检查大文件 print("\n2. 检查大文件(>50MB)...") large_files = find_large_files(root_dir, size_mb=50) if large_files: print(f" 找到 {len(large_files)} 个大文件:") for file_path, size_mb in large_files: print(f" - {file_path} ({size_mb:.2f} MB)") print("\n ⚠️ 建议:大文件应使用 Git LFS 或排除在上传之外") else: print(" ✅ 没有找到大文件") # 检查 .gitignore print("\n3. 检查 .gitignore...") if check_gitignore(root_dir): print(" ✅ .gitignore 文件存在") else: print(" ⚠️ 警告: .gitignore 文件不存在") # 创建检查清单 print("\n4. 创建上传检查清单...") checklist_content = create_upload_checklist(root_dir) checklist_path = Path(root_dir) / 'UPLOAD_CHECKLIST.md' with open(checklist_path, 'w', encoding='utf-8') as f: f.write(checklist_content) print(f" ✅ 已创建: {checklist_path}") print("\n" + "=" * 60) print("清理完成!") print("=" * 60) print("\n下一步:") print("1. 查看 UPLOAD_CHECKLIST.md 了解上传步骤") print("2. 检查是否有敏感信息需要移除") print("3. 按照检查清单上传到 Hugging Face") if __name__ == '__main__': main()