Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Gemma 3 270M 部署脚本 | |
| 自动检查项目完整性并准备部署到 Hugging Face Spaces | |
| """ | |
| import os | |
| import sys | |
| import subprocess | |
| import json | |
| from pathlib import Path | |
| def check_file_exists(file_path): | |
| """检查文件是否存在""" | |
| if os.path.exists(file_path): | |
| print(f"✅ {file_path} 存在") | |
| return True | |
| else: | |
| print(f"❌ {file_path} 不存在") | |
| return False | |
| def check_python_syntax(file_path): | |
| """检查Python文件语法""" | |
| try: | |
| with open(file_path, 'r', encoding='utf-8') as f: | |
| compile(f.read(), file_path, 'exec') | |
| print(f"✅ {file_path} 语法正确") | |
| return True | |
| except SyntaxError as e: | |
| print(f"❌ {file_path} 语法错误: {e}") | |
| return False | |
| except Exception as e: | |
| print(f"❌ {file_path} 检查失败: {e}") | |
| return False | |
| def check_imports(): | |
| """检查模块导入""" | |
| try: | |
| import config | |
| import model | |
| print("✅ 模块导入成功") | |
| print(f" 模型名称: {config.MODEL_NAME}") | |
| print(f" 默认Token数: {config.DEFAULT_TOKENS}") | |
| return True | |
| except Exception as e: | |
| print(f"❌ 模块导入失败: {e}") | |
| return False | |
| def check_git_status(): | |
| """检查Git状态""" | |
| try: | |
| result = subprocess.run(['git', 'status', '--porcelain'], | |
| capture_output=True, text=True, check=True) | |
| if result.stdout.strip(): | |
| print("📝 检测到未提交的更改:") | |
| print(result.stdout) | |
| return False | |
| else: | |
| print("✅ Git工作区干净") | |
| return True | |
| except subprocess.CalledProcessError: | |
| print("⚠️ Git未初始化或不在Git仓库中") | |
| return False | |
| except FileNotFoundError: | |
| print("⚠️ Git未安装") | |
| return False | |
| def create_gitignore(): | |
| """创建.gitignore文件""" | |
| gitignore_content = """# Python | |
| __pycache__/ | |
| *.py[cod] | |
| *$py.class | |
| *.so | |
| .Python | |
| build/ | |
| develop-eggs/ | |
| dist/ | |
| downloads/ | |
| eggs/ | |
| .eggs/ | |
| lib/ | |
| lib64/ | |
| parts/ | |
| sdist/ | |
| var/ | |
| wheels/ | |
| *.egg-info/ | |
| .installed.cfg | |
| *.egg | |
| # Virtual environments | |
| venv/ | |
| env/ | |
| ENV/ | |
| # IDE | |
| .vscode/ | |
| .idea/ | |
| *.swp | |
| *.swo | |
| # OS | |
| .DS_Store | |
| Thumbs.db | |
| # Logs | |
| *.log | |
| # Temporary files | |
| *.tmp | |
| *.temp | |
| """ | |
| if not os.path.exists('.gitignore'): | |
| with open('.gitignore', 'w', encoding='utf-8') as f: | |
| f.write(gitignore_content) | |
| print("✅ 创建 .gitignore 文件") | |
| else: | |
| print("✅ .gitignore 文件已存在") | |
| def main(): | |
| """主函数""" | |
| print("🚀 Gemma 3 270M 部署检查") | |
| print("=" * 50) | |
| # 检查必需文件 | |
| required_files = [ | |
| 'app.py', | |
| 'config.py', | |
| 'model.py', | |
| 'requirements.txt', | |
| 'README.md', | |
| '.gitattributes' | |
| ] | |
| print("\n📁 检查必需文件:") | |
| all_files_exist = True | |
| for file_path in required_files: | |
| if not check_file_exists(file_path): | |
| all_files_exist = False | |
| if not all_files_exist: | |
| print("\n❌ 缺少必需文件,请检查项目完整性") | |
| return False | |
| # 检查Python语法 | |
| print("\n🐍 检查Python语法:") | |
| python_files = ['app.py', 'config.py', 'model.py'] | |
| syntax_ok = True | |
| for file_path in python_files: | |
| if not check_python_syntax(file_path): | |
| syntax_ok = False | |
| if not syntax_ok: | |
| print("\n❌ 发现语法错误,请修复后重试") | |
| return False | |
| # 检查模块导入 | |
| print("\n📦 检查模块导入:") | |
| if not check_imports(): | |
| print("\n❌ 模块导入失败,请检查依赖") | |
| return False | |
| # 创建.gitignore | |
| print("\n📝 创建.gitignore:") | |
| create_gitignore() | |
| # 检查Git状态 | |
| print("\n🔍 检查Git状态:") | |
| git_clean = check_git_status() | |
| print("\n" + "=" * 50) | |
| if all_files_exist and syntax_ok: | |
| print("🎉 项目检查通过!可以开始部署") | |
| print("\n📋 部署步骤:") | |
| print("1. git add .") | |
| print("2. git commit -m 'Deploy Gemma 3 270M optimized version'") | |
| print("3. git push origin main") | |
| print("\n🌐 部署后访问: https://huggingface.co/spaces/Tom1986/gemma-3-270m") | |
| return True | |
| else: | |
| print("❌ 项目检查失败,请修复问题后重试") | |
| return False | |
| if __name__ == "__main__": | |
| success = main() | |
| sys.exit(0 if success else 1) | |