BULAK/simple_build.py

140 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python3
"""
简化版打包脚本
"""
import os
import sys
def check_dependencies():
"""检查依赖"""
print("检查依赖...")
try:
import streamlit
import openai
import dotenv
print("✅ 依赖检查通过")
return True
except ImportError as e:
print(f"❌ 依赖缺失: {e}")
return False
def create_standalone_app():
"""创建独立应用"""
print("创建独立应用...")
# 创建启动脚本
launcher_content = '''import os
import sys
import subprocess
import tempfile
import shutil
def install_dependencies():
"""安装依赖"""
print("正在安装依赖...")
packages = ["streamlit", "openai", "python-dotenv"]
for package in packages:
try:
subprocess.check_call([sys.executable, "-m", "pip", "install", package])
print(f"{package} 安装成功")
except Exception as e:
print(f"{package} 安装失败: {e}")
return False
return True
def run_app():
"""运行应用"""
# 创建临时目录并复制文件
temp_dir = tempfile.mkdtemp()
print(f"临时目录: {temp_dir}")
# 复制app.py
shutil.copy2("app.py", os.path.join(temp_dir, "app.py"))
# 复制.env文件如果存在
if os.path.exists(".env"):
shutil.copy2(".env", os.path.join(temp_dir, ".env"))
# 切换到临时目录
os.chdir(temp_dir)
# 运行streamlit
print("启动应用...")
print("请在浏览器中打开: http://localhost:8501")
subprocess.call([sys.executable, "-m", "streamlit", "run", "app.py", "--server.headless", "true"])
# 清理临时目录
shutil.rmtree(temp_dir)
if __name__ == "__main__":
if install_dependencies():
run_app()
else:
print("依赖安装失败,请手动安装依赖")
input("按任意键退出...")
'''
with open('launcher.py', 'w', encoding='utf-8') as f:
f.write(launcher_content)
# 创建批处理文件
bat_content = '''@echo off
chcp 65001 >nul
echo 多Agent决策工作坊启动器
echo.
echo 正在检查Python环境...
python --version >nul 2>&1
if errorlevel 1 (
echo 错误未找到Python请先安装Python 3.8+
pause
exit /b 1
)
echo Python环境正常
echo 启动应用...
echo.
echo 应用将在浏览器中打开,请稍候...
echo.
python launcher.py
pause
'''
with open('启动应用.bat', 'w', encoding='gbk') as f:
f.write(bat_content)
print("✅ 启动文件创建成功")
def main():
"""主函数"""
print("🚀 创建多Agent决策工作坊独立应用")
print("=" * 50)
# 检查当前目录
if not os.path.exists('app.py'):
print("❌ 错误:请在项目根目录运行此脚本")
return
# 检查依赖
if not check_dependencies():
print("请先安装依赖: pip install streamlit openai python-dotenv")
return
# 创建独立应用
create_standalone_app()
print("\n🎉 打包完成!")
print("📁 生成的文件:")
print(" - launcher.py (启动脚本)")
print(" - 启动应用.bat (双击运行)")
print("\n🚀 使用方法:")
print(" 1. 双击 '启动应用.bat'")
print(" 2. 等待依赖安装完成")
print(" 3. 在浏览器中打开 http://localhost:8501")
print("\n💡 提示教室电脑需要有Python环境")
if __name__ == "__main__":
main()