- 添加 install.bat Windows一键安装脚本 - 添加 install.sh Linux/Mac一键安装脚本 - 添加 auto_install.py 跨平台自动化安装脚本 - 更新 README.md 添加详细的一键安装说明 - 测试环境检查脚本功能正常
242 lines
6.1 KiB
Python
242 lines
6.1 KiB
Python
"""
|
||
信用卡欺诈检测系统 - 自动化安装脚本
|
||
跨平台支持:Windows、Linux、macOS
|
||
"""
|
||
|
||
import sys
|
||
import subprocess
|
||
import os
|
||
from pathlib import Path
|
||
import time
|
||
|
||
|
||
def print_header(text):
|
||
"""打印标题"""
|
||
print("=" * 60)
|
||
print(text)
|
||
print("=" * 60)
|
||
print()
|
||
|
||
|
||
def print_step(step_num, total_steps, description):
|
||
"""打印步骤信息"""
|
||
print(f"[步骤 {step_num}/{total_steps}] {description}")
|
||
|
||
|
||
def run_command(command, description=""):
|
||
"""运行命令并处理错误"""
|
||
try:
|
||
if description:
|
||
print(f" 正在执行: {description}")
|
||
|
||
result = subprocess.run(
|
||
command,
|
||
shell=True,
|
||
check=True,
|
||
capture_output=True,
|
||
text=True
|
||
)
|
||
|
||
if result.stdout:
|
||
print(result.stdout)
|
||
|
||
return True
|
||
except subprocess.CalledProcessError as e:
|
||
print(f" [错误] 命令执行失败")
|
||
if e.stderr:
|
||
print(f" 错误信息: {e.stderr}")
|
||
return False
|
||
|
||
|
||
def check_python_version():
|
||
"""检查Python版本"""
|
||
print_step(1, 5, "检查Python版本...")
|
||
|
||
version = sys.version_info
|
||
print(f" Python版本: {version.major}.{version.minor}.{version.micro}")
|
||
|
||
if version.major == 3 and version.minor >= 10:
|
||
print(" ✓ Python版本符合要求 (>= 3.10)")
|
||
return True
|
||
else:
|
||
print(" ✗ Python版本不符合要求,需要3.10或更高版本")
|
||
print(" 请从 https://www.python.org/downloads/ 下载最新版本")
|
||
return False
|
||
|
||
|
||
def install_dependencies():
|
||
"""安装Python依赖"""
|
||
print_step(2, 5, "安装Python依赖...")
|
||
print(" 正在安装依赖包,这可能需要几分钟...")
|
||
|
||
# 尝试使用pip安装
|
||
success = run_command(
|
||
f"{sys.executable} -m pip install -r requirements.txt",
|
||
"安装依赖包"
|
||
)
|
||
|
||
if success:
|
||
print(" ✓ 依赖安装完成")
|
||
return True
|
||
else:
|
||
print(" ✗ 依赖安装失败")
|
||
print(" 请检查网络连接或尝试使用国内镜像源:")
|
||
print(f" {sys.executable} -m pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple")
|
||
return False
|
||
|
||
|
||
def check_data_file():
|
||
"""检查数据文件"""
|
||
print_step(3, 5, "检查数据文件...")
|
||
|
||
data_file = Path("data/creditcard.csv")
|
||
|
||
if data_file.exists():
|
||
file_size = data_file.stat().st_size / (1024 * 1024) # MB
|
||
print(f" ✓ 数据文件已存在 (大小: {file_size:.2f} MB)")
|
||
return True
|
||
else:
|
||
print(" ✗ 未找到数据文件 data/creditcard.csv")
|
||
print()
|
||
print(" 请从以下地址下载数据集:")
|
||
print(" https://www.kaggle.com/datasets/mlg-ulb/creditcardfraud")
|
||
print()
|
||
print(" 下载后将 creditcard.csv 文件放入 data/ 目录")
|
||
print()
|
||
|
||
response = input(" 数据文件已准备好吗?(Y/N): ").strip().upper()
|
||
if response == 'Y':
|
||
print(" ✓ 继续安装...")
|
||
return True
|
||
else:
|
||
print(" ✗ 安装已取消")
|
||
return False
|
||
|
||
|
||
def check_model_files():
|
||
"""检查并训练模型"""
|
||
print_step(4, 5, "检查模型文件...")
|
||
|
||
models_dir = Path("models")
|
||
model_file = models_dir / "random_forest_model.joblib"
|
||
|
||
if model_file.exists():
|
||
file_size = model_file.stat().st_size / 1024 # KB
|
||
print(f" ✓ 模型文件已存在 (大小: {file_size:.2f} KB)")
|
||
return True
|
||
else:
|
||
print(" 模型文件不存在,开始训练模型...")
|
||
print(" 这可能需要几分钟,请耐心等待...")
|
||
|
||
success = run_command(
|
||
f"{sys.executable} src/train.py",
|
||
"训练模型"
|
||
)
|
||
|
||
if success:
|
||
print(" ✓ 模型训练完成")
|
||
return True
|
||
else:
|
||
print(" ✗ 模型训练失败")
|
||
return False
|
||
|
||
|
||
def run_environment_check():
|
||
"""运行环境检查"""
|
||
print_step(5, 5, "运行环境检查...")
|
||
|
||
success = run_command(
|
||
f"{sys.executable} check_environment.py",
|
||
"检查环境"
|
||
)
|
||
|
||
if success:
|
||
print(" ✓ 环境检查通过")
|
||
else:
|
||
print(" ⚠ 环境检查发现问题,但将继续启动应用")
|
||
|
||
return True
|
||
|
||
|
||
def launch_application():
|
||
"""启动应用"""
|
||
print()
|
||
print_header("安装完成!正在启动Web界面...")
|
||
print()
|
||
print("提示:")
|
||
print("- Web界面将在浏览器中自动打开")
|
||
print("- 如果没有自动打开,请访问: http://localhost:8501")
|
||
print("- 按 Ctrl+C 可以停止服务")
|
||
print()
|
||
|
||
# 等待几秒让用户看到提示
|
||
time.sleep(2)
|
||
|
||
# 启动应用
|
||
try:
|
||
subprocess.run(
|
||
f"{sys.executable} src/agent_app.py",
|
||
shell=True,
|
||
check=True
|
||
)
|
||
except KeyboardInterrupt:
|
||
print()
|
||
print("应用已停止")
|
||
except Exception as e:
|
||
print(f"启动应用时出错: {e}")
|
||
|
||
|
||
def main():
|
||
"""主函数"""
|
||
print_header("信用卡欺诈检测系统 - 自动化安装脚本")
|
||
|
||
# 检查Python版本
|
||
if not check_python_version():
|
||
input("按回车键退出...")
|
||
sys.exit(1)
|
||
|
||
print()
|
||
|
||
# 安装依赖
|
||
if not install_dependencies():
|
||
input("按回车键退出...")
|
||
sys.exit(1)
|
||
|
||
print()
|
||
|
||
# 检查数据文件
|
||
if not check_data_file():
|
||
input("按回车键退出...")
|
||
sys.exit(1)
|
||
|
||
print()
|
||
|
||
# 检查模型文件
|
||
if not check_model_files():
|
||
input("按回车键退出...")
|
||
sys.exit(1)
|
||
|
||
print()
|
||
|
||
# 运行环境检查
|
||
run_environment_check()
|
||
|
||
# 启动应用
|
||
launch_application()
|
||
|
||
|
||
if __name__ == "__main__":
|
||
try:
|
||
main()
|
||
except KeyboardInterrupt:
|
||
print()
|
||
print("安装已取消")
|
||
sys.exit(1)
|
||
except Exception as e:
|
||
print()
|
||
print(f"安装过程中出现错误: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
input("按回车键退出...")
|
||
sys.exit(1)
|