feat: 更新agent_app.py以自动启动web界面并优化项目配置

This commit is contained in:
2311020116lhh 2026-01-15 21:25:37 +08:00
parent 8a5fd75d0a
commit 317be70b11
5 changed files with 284854 additions and 45 deletions

3
.gitignore vendored
View File

@ -27,9 +27,6 @@ ENV/
*.h5 *.h5
*.hdf5 *.hdf5
*.pb *.pb
data/*.csv
images/*.png
images/*.jpg
# 测试覆盖率 # 测试覆盖率
.coverage .coverage

1
11.txt
View File

@ -1 +0,0 @@
111

View File

@ -31,26 +31,6 @@ ml_course_design/
└── test_*.py └── test_*.py
``` ```
## 快速开始
```bash
# 克隆仓库
git clone <仓库地址>
cd ml_course_design
# 安装依赖(使用 uv
uv sync
# 训练模型
uv run python -m src.train
# 运行 DemoStreamlit
uv run streamlit run src/streamlit_app.py
# 运行测试
uv run pytest tests/
```
## 团队成员 ## 团队成员
| 姓名 | 学号 | 贡献 | | 姓名 | 学号 | 贡献 |

284808
data/creditcard.csv Normal file

File diff suppressed because it is too large Load Diff

View File

@ -239,27 +239,52 @@ def create_agent(model_dir: str = "models", model_name: str = "random_forest") -
if __name__ == "__main__": if __name__ == "__main__":
agent = create_agent() import subprocess
import webbrowser
import time
from pathlib import Path
print("=== 可用工具 ===") print("=== 信用卡欺诈检测系统 ===")
for tool in agent.list_tools(): print("\n正在启动Web界面...\n")
print(f"- {tool['name']}: {tool['description']}")
test_transaction = [ streamlit_app_path = Path(__file__).parent / "streamlit_app.py"
0, -1.3598071336738, -0.0727811733098497, 2.53634673796914, 1.37815522427443,
-0.338320769942518, 0.462387777762292, 0.239598554061257, 0.0986979012610507,
0.363786969611213, 0.0907941719789316, -0.551599533260813, -0.617800855762348,
-0.991389847235408, -0.311169353699879, 1.46817697209427, -0.470400525259478,
0.207971241929242, 0.0257905801985591, 0.403992960255733, 0.251412098239705,
-0.018306777944153, 0.277837575558899, -0.110473910188767, 0.0669280749146731,
0.128539358273528, -0.189114843888824, 0.133558376740387, -0.0210530534538215,
149.62
]
result = agent.process_transaction(test_transaction) if not streamlit_app_path.exists():
print("\n=== 决策结果 ===") print(f"错误: 找不到streamlit_app.py文件: {streamlit_app_path}")
print(f"预测类别: {result.evaluation.class_name}") exit(1)
print(f"欺诈概率: {result.evaluation.fraud_probability:.4f}")
print(f"置信度: {result.evaluation.confidence}") process = subprocess.Popen(
print(f"关键特征数量: {len(result.explanation.key_features)}") ["streamlit", "run", str(streamlit_app_path), "--server.headless", "true"],
print(f"行动建议数量: {len(result.action_plan.actions)}") stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
)
print("✓ Web服务正在启动...")
print("✓ 请等待几秒钟,服务将自动在浏览器中打开\n")
time.sleep(3)
print("=" * 60)
print("🌐 信用卡欺诈检测系统Web界面")
print("=" * 60)
print("\n📋 访问信息:")
print(" 本地访问: http://localhost:8501")
print(" 网络访问: http://0.0.0.0:8501")
print("\n💡 提示:")
print(" - 如果浏览器没有自动打开,请手动访问上述链接")
print(" - 按 Ctrl+C 可以停止服务")
print("=" * 60)
try:
webbrowser.open("http://localhost:8501")
except Exception as e:
print(f"\n⚠️ 无法自动打开浏览器: {e}")
print(" 请手动访问: http://localhost:8501")
try:
process.wait()
except KeyboardInterrupt:
print("\n\n正在停止服务...")
process.terminate()
print("✓ 服务已停止")