quickstart.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. """
  2. FineTuneX 快速启动脚本
  3. 使用方法:
  4. python quickstart.py
  5. 这个脚本将:
  6. 1. 检查环境
  7. 2. 初始化项目
  8. 3. 运行示例微调
  9. """
  10. import os
  11. import sys
  12. import subprocess
  13. def print_header(text):
  14. """打印标题"""
  15. print("\n" + "=" * 60)
  16. print(text.center(60))
  17. print("=" * 60 + "\n")
  18. def run_command(command, description):
  19. """运行命令"""
  20. print(f"正在 {description}...")
  21. result = subprocess.run(command, shell=True, capture_output=True, text=True)
  22. if result.returncode == 0:
  23. print(f"✓ {description} 完成")
  24. if result.stdout:
  25. print(result.stdout)
  26. return True
  27. else:
  28. print(f"✗ {description} 失败")
  29. if result.stderr:
  30. print(result.stderr)
  31. return False
  32. def check_environment():
  33. """检查环境"""
  34. print_header("步骤 1: 检查环境")
  35. # 检查 Python 版本
  36. python_version = sys.version_info
  37. if python_version.major < 3 or python_version.minor < 9:
  38. print("✗ Python 版本过低,需要 3.9+")
  39. return False
  40. print(f"✓ Python {python_version.major}.{python_version.minor}.{python_version.micro}")
  41. # 检查依赖
  42. try:
  43. import torch
  44. print(f"✓ PyTorch {torch.__version__}")
  45. if torch.cuda.is_available():
  46. print(f"✓ CUDA 可用:{torch.cuda.get_device_name(0)}")
  47. else:
  48. print("⚠ CUDA 不可用,将使用 CPU")
  49. except ImportError:
  50. print("✗ PyTorch 未安装")
  51. return False
  52. return True
  53. def install_dependencies():
  54. """安装依赖"""
  55. print_header("步骤 2: 安装依赖")
  56. if os.path.exists("requirements.txt"):
  57. response = input("是否安装依赖?(y/n): ")
  58. if response.lower() == 'y':
  59. run_command("pip install -r requirements.txt", "安装依赖")
  60. else:
  61. print("⚠ requirements.txt 不存在")
  62. def initialize_project():
  63. """初始化项目"""
  64. print_header("步骤 3: 初始化项目")
  65. if os.path.exists("scripts/init_project.py"):
  66. run_command("python scripts/init_project.py", "初始化项目")
  67. else:
  68. # 手动创建目录
  69. directories = ["outputs", "data", "logs"]
  70. for directory in directories:
  71. os.makedirs(directory, exist_ok=True)
  72. print(f"✓ 创建目录:{directory}")
  73. def run_example():
  74. """运行示例"""
  75. print_header("步骤 4: 运行示例")
  76. print("可运行的示例:")
  77. print("1. Qwen3.5 微调示例")
  78. print("2. 环境检查")
  79. print("3. 运行测试")
  80. choice = input("\n请选择 (1-3): ")
  81. if choice == "1":
  82. if os.path.exists("examples/qwen3.5_0.8b_finetune.py"):
  83. print("\n开始运行 Qwen3.5 微调示例...")
  84. print("注意:这将下载模型并开始训练,可能需要较长时间")
  85. response = input("是否继续?(y/n): ")
  86. if response.lower() == 'y':
  87. run_command("python examples/qwen3.5_0.8b_finetune.py", "运行示例")
  88. else:
  89. print("✗ 示例文件不存在")
  90. elif choice == "2":
  91. if os.path.exists("scripts/check_env.py"):
  92. run_command("python scripts/check_env.py", "环境检查")
  93. elif choice == "3":
  94. if os.path.exists("tests/test_all.py"):
  95. run_command("python tests/test_all.py", "运行测试")
  96. else:
  97. print("无效选择")
  98. def show_next_steps():
  99. """显示下一步"""
  100. print_header("完成!")
  101. print("""
  102. 项目已成功设置!接下来你可以:
  103. 1. 查看文档:
  104. - README.md - 项目概述
  105. - QUICKSTART.md - 快速开始
  106. - docs/usage.md - 详细使用文档
  107. 2. 运行示例:
  108. python examples/qwen3.5_0.8b_finetune.py
  109. 3. 使用自己的数据:
  110. - 准备数据文件 (JSON 格式)
  111. - 修改示例脚本中的数据集路径
  112. - 运行微调
  113. 4. 启动 API 服务:
  114. python scripts/start_api.py
  115. 5. 模型推理:
  116. python scripts/inference.py --model_path ./outputs/xxx --interactive
  117. 祝你使用愉快!
  118. """)
  119. def main():
  120. """主函数"""
  121. print_header("FineTuneX 快速启动")
  122. # 1. 检查环境
  123. if not check_environment():
  124. print("\n请先安装必要的依赖:")
  125. print(" pip install -r requirements.txt")
  126. return
  127. # 2. 安装依赖(可选)
  128. # install_dependencies()
  129. # 3. 初始化项目
  130. initialize_project()
  131. # 4. 运行示例
  132. run_example()
  133. # 5. 显示下一步
  134. show_next_steps()
  135. if __name__ == "__main__":
  136. main()