{"id":50648858,"url":"https://github.com/jensenzhong/Construction-Safety-Risk-Analysis-System-Based-on-RAG-LLM","last_synced_at":"2026-06-24T12:00:31.072Z","repository":{"id":360591330,"uuid":"1250303667","full_name":"jensenzhong/Construction-Safety-Risk-Analysis-System-Based-on-RAG-LLM","owner":"jensenzhong","description":"基于 RAG + LLM 的施工安全风险智能分析系统 ，基于53000+真实事故案例实现语义检索与智能预评估| Construction safety risk analysis powered by RAG retrieval and DepSeek LLM with 53,000+   OSHA accident records","archived":false,"fork":false,"pushed_at":"2026-05-27T04:21:59.000Z","size":1798,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-05-27T05:09:25.093Z","etag":null,"topics":["construction-saty","faiss","llm","nlp","osha","python","rag","risk-assessment","streamlit"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jensenzhong.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2026-05-26T13:54:46.000Z","updated_at":"2026-05-27T04:22:03.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/jensenzhong/Construction-Safety-Risk-Analysis-System-Based-on-RAG-LLM","commit_stats":null,"previous_names":["jensenzhong/construction-safety-risk-analysis-system-based-on-rag-llm"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/jensenzhong/Construction-Safety-Risk-Analysis-System-Based-on-RAG-LLM","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jensenzhong","download_url":"https://codeload.github.com/jensenzhong/Construction-Safety-Risk-Analysis-System-Based-on-RAG-LLM/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34731256,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-24T02:00:07.484Z","response_time":106,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["construction-saty","faiss","llm","nlp","osha","python","rag","risk-assessment","streamlit"],"created_at":"2026-06-07T16:00:22.266Z","updated_at":"2026-06-24T12:00:31.064Z","avatar_url":"https://github.com/jensenzhong.png","language":"Python","funding_links":[],"categories":["What's Inside"],"sub_categories":[],"readme":"\u003cdiv align=\"center\"\u003e\n\n# :construction: 施工安全风险智能分析系统\n\n**Construction Safety Risk Analysis System Based on RAG + LLM**\n\n基于检索增强生成与大语言模型的施工安全风险智能决策平台\n\n![](https://img.shields.io/badge/方法-RAG%20%2B%20LLM-FF6F61?style=for-the-badge\u0026labelColor=2D2D2D)\n![](https://img.shields.io/badge/数据-53000%2B%20OSHA事故-4A90D9?style=for-the-badge\u0026labelColor=2D2D2D)\n![](https://img.shields.io/badge/分析-因果推断%20PSM-50C878?style=for-the-badge\u0026labelColor=2D2D2D)\n\n![](https://img.shields.io/badge/Python-3.12+-3776AB?style=flat-square\u0026logo=python\u0026logoColor=white)\n![](https://img.shields.io/badge/Streamlit-1.55+-FF4B4B?style=flat-square\u0026logo=streamlit\u0026logoColor=white)\n![](https://img.shields.io/badge/FAISS-Vector_Search-0467DF?style=flat-square\u0026logo=meta\u0026logoColor=white)\n![](https://img.shields.io/badge/DeepSeek-LLM-5A67D8?style=flat-square\u0026logo=openai\u0026logoColor=white)\n![](https://img.shields.io/badge/Sentence_Transformers-Embedding-FF9900?style=flat-square\u0026logo=huggingface\u0026logoColor=white)\n![](https://img.shields.io/badge/PyInstaller-Windows_Packaging-13AA52?style=flat-square\u0026logo=windows\u0026logoColor=white)\n![](https://img.shields.io/badge/Plotly-Visualization-3F4F75?style=flat-square\u0026logo=plotly\u0026logoColor=white)\n![](https://img.shields.io/badge/License-CC%20BY--NC%204.0-EF9421?style=flat-square\u0026logo=creativecommons\u0026logoColor=white)\n\n\u003cbr/\u003e\n\n\u003c/div\u003e\n\n---\n\n## :bookmark_tabs: 目录\n\n\u003cdetails open\u003e\n\u003csummary\u003e\u003cb\u003e展开 / 收起\u003c/b\u003e\u003c/summary\u003e\n\u003cbr/\u003e\n\n- [:star2: 项目简介](#star2-项目简介)\n- [:brain: 核心技术](#brain-核心技术)\n- [:building_construction: 系统架构](#building_construction-系统架构)\n- [:camera: 系统截图](#camera-系统截图)\n- [:jigsaw: 功能模块](#jigsaw-功能模块)\n- [:open_file_folder: 项目结构](#open_file_folder-项目结构)\n- [:wrench: 技术栈](#wrench-技术栈)\n- [:rocket: 快速开始](#rocket-快速开始)\n- [:bar_chart: 数据与索引](#bar_chart-数据与索引)\n- [:test_tube: 测试](#test_tube-测试)\n- [:package: Windows 打包分发](#package-windows-打包分发)\n- [:question: 常见问题](#question-常见问题)\n- [:lock: 安全说明](#lock-安全说明)\n- [:world_map: Roadmap](#world_map-roadmap)\n- [:balance_scale: 声明](#balance_scale-声明)\n\n\u003c/details\u003e\n\n\u003cbr/\u003e\n\n---\n\n## :star2: 项目简介\n\n本系统是一个面向 **建筑工程施工安全** 领域的智能风险分析平台，以 **53,000+ 条 OSHA 真实事故记录** 为知识库，结合 **检索增强生成（RAG）** 与 **大语言模型（DeepSeek）**，为施工现场提供智能化的安全决策支持。\n\n系统核心能力：\n\n- :dart: **事故预评估** — 输入施工计划、工种、环境参数，自动输出结构化风险报告\n- :mag: **语义案例检索** — 基于 FAISS 向量索引的毫秒级相似事故检索\n- :chart_with_upwards_trend: **可视化仪表盘** — 实时环境风险监测与事故趋势分析\n- :bar_chart: **因果推断分析** — 基于 PSM（倾向得分匹配）的环境因素影响量化\n- :desktop_computer: **一键部署** — PyInstaller 打包，无需 Python 环境即可运行\n\n\u003cbr/\u003e\n\n---\n\n## :brain: 核心技术\n\n\u003e **RAG（检索增强生成）**：将大规模事故知识库与 LLM 推理能力结合，让模型基于真实案例给出有据可依的安全建议，而非凭空生成。\n\n```\n传统方式：  人工查阅规范 → 经验判断 → 主观评估\n                  ↓\n本系统：    语义检索历史案例 → LLM 结构化分析 → 量化风险评估\n```\n\n| 传统模式 | 本系统 |\n|:--------:|:------:|\n| 人工查阅事故报告 | FAISS 毫秒级语义检索 |\n| 经验主观判断 | LLM 结构化推理 |\n| 单一因素分析 | 多维环境因素关联 |\n| 事后总结 | 事前预评估 |\n| 静态报表 | 实时可视化仪表盘 |\n\n\u003cbr/\u003e\n\n---\n\n## :building_construction: 系统架构\n\n```\n┌─────────────────────────────────────────────────────────────────────┐\n│                    施工安全风险智能分析系统                            │\n├──────────────────┬──────────────────┬───────────────────────────────┤\n│   数据层          │    分析层         │    展示层                      │\n│                  │                  │                               │\n│  OSHA 事故数据   │  RAG 语义检索     │  Streamlit Web UI             │\n│  53,000+ 记录    │  DeepSeek LLM    │  Plotly 可视化                 │\n│  FAISS 向量索引  │  PSM 因果推断     │  风险仪表盘                    │\n│  环境参数数据    │  本地兜底评估     │  结构化报告                    │\n│                  │                  │                               │\n│   📦 存储        │    🧠 推理        │    📊 交互                     │\n└──────────────────┴──────────────────┴───────────────────────────────┘\n```\n\n**数据流向**：\n\n```\n用户输入（工种/环境/计划）\n        ↓\n  语义向量化（sentence-transformers）\n        ↓\n  FAISS 相似案例检索 + 关键词混合打分\n        ↓\n  构建 Prompt（检索结果 + 用户输入）\n        ↓\n  DeepSeek LLM 结构化推理\n        ↓\n  输出：风险等级 / 危险源 / 预防措施 / PPE / 引用案例\n```\n\n\n## :jigsaw: 功能模块\n\n### :one: 事故预评估与风险建议\n\n\u003e 输入施工计划、工种、环境参数（温度/风速/湿度），系统自动输出结构化风险报告\n\n| 输出项 | 说明 |\n|:------:|:----:|\n| :warning: 风险等级 | 1-4 级量化评估 |\n| :boom: 潜在危险源 | 基于历史案例识别 |\n| :clipboard: 管理薄弱点 | 制度与流程缺陷 |\n| :shield: 预防措施 | 针对性安全建议 |\n| :rescue_worker_helmet: 必备 PPE | 个人防护装备清单 |\n| :books: 引用案例 | 相似历史事故参考 |\n\n---\n\n### :two: RAG 语义检索引擎\n\n\u003e 混合检索策略：语义相似度 + 关键词匹配，双重打分确保召回质量\n\n- :zap: **向量化**：`sentence-transformers/all-MiniLM-L6-v2` 生成 384 维语义向量\n- :mag_right: **索引**：FAISS 向量索引，支持毫秒级 Top-K 检索\n- :dart: **混合打分**：`hybrid_score = α × semantic + β × keyword`\n- :globe_with_meridians: **多语言扩展**：中英关键词提示扩展（高处/坠落/触电 ↔ fall/electrocution）\n\n---\n\n### :three: 可视化风险仪表盘\n\n\u003e 实时环境监测 + 事故画像 + 趋势分析\n\n- :thermometer: **环境风险仪表** — 温度、风速、湿度三维监测\n- :chart_with_upwards_trend: **事故月度趋势** — 时间序列分析\n- :world_map: **事故热力图** — 地理分布可视化\n- :busts_in_silhouette: **事故画像** — 工种、危害类型、季节分布\n- :link: **环境关联** — 温度/风速与事故率相关性\n\n---\n\n### :four: PSM 因果推断分析\n\n\u003e 基于倾向得分匹配（Propensity Score Matching）量化环境因素对事故严重程度的因果效应\n\n- 控制混杂变量（工种、季节、地区）\n- 估计处理效应（ATE / ATT）\n- 输出匹配样本与效应报告\n\n\u003cbr/\u003e\n\n---\n\n## :camera: 系统截图\n\n\u003cdiv align=\"center\"\u003e\n  \n### 数据管理与风险仪表盘（温度 / 风速 / 湿度）\n\u003cimg width=\"3169\" height=\"3562\" alt=\"PixPin_2026-03-14_12-47-57 - 副本\" src=\"https://github.com/user-attachments/assets/9e8e9c97-dbd2-40bc-8fab-54bf8f414439\" /\u003e\n\n\n### 事故预评估与RAG 历史案例检索\n\u003cimg width=\"3148\" height=\"3024\" alt=\"PixPin_2026-03-14_12-50-13\" src=\"https://github.com/user-attachments/assets/45e04c5e-d87a-461b-83e5-c5951010560c\" /\u003e\n\n\n### 月度事故趋势、事故热力图与环境因素关联分析\n\u003cimg width=\"3153\" height=\"4149\" alt=\"image\" src=\"https://github.com/user-attachments/assets/a85731b1-bba2-40a4-9dd8-966430ba4149\" /\u003e\n\u003cbr/\u003e\n\n\u003c/div\u003e\n\n## :open_file_folder: 项目结构\n\n```\nConstruction-Safety-Risk-Analysis-System/\n├── app.py                              # 🖥️ Streamlit 主应用（Web UI）\n├── launcher.py                         # 🚀 打包后启动入口\n├── main.py                             # 📋 批量结构化抽取脚本\n├── requirements.txt                    # 📦 Python 依赖清单\n├── ConstructionSafetyAssistant.spec    # 🔧 PyInstaller 打包规格\n├── settings.example.json              # ⚙️ 配置模板（不含密钥）\n├── run_app.bat                         # ▶️ Windows 一键启动\n├── stop_app.bat                        # ⏹️ 停止服务\n├── .gitignore                          # 🔒 Git 忽略规则\n├── LICENSE                             # 📄 MIT 许可证\n│\n├── rag/                                # 🔍 RAG 检索模块\n│   ├── config.py                       #    ├── 数据与索引路径配置\n│   ├── index_builder.py                #    ├── FAISS 索引构建\n│   ├── retrieval.py                    #    ├── 检索与混合打分\n│   ├── prompting.py                    #    ├── 提示词构建\n│   ├── extraction_schema.py            #    ├── 结构化输出校验\n│   └── local_pre_assessment.py         #    └── 本地兜底预评估\n│\n├── llm/                                # 🧠 LLM 调用模块\n│   └── client.py                       #    └── DeepSeek 配置解析与调用\n│\n├── analysis/                           # 📊 因果分析模块\n│   └── causal_psm.py                   #    └── 倾向得分匹配（PSM）\n│\n├── sensors/                            # 📡 传感器模块\n│   └── api.py                          #    └── 传感器读取接口（当前 mock）\n│\n├── scripts/                            # 🛠️ 工具脚本\n│   ├── start_app.py                    #    ├── 开发态启动逻辑\n│   ├── stop_app.ps1                    #    ├── 停止服务\n│   ├── build_windows_installer.py      #    ├── 一键生成安装器\n│   └── installer_bootstrap.py          #    └── 安装器引导脚本\n│\n├── tests/                              # ✅ 测试用例（12 项）\n├── indexes/                            # 🗂️ FAISS 索引与元数据\n├── results/                            # 📈 分析结果输出\n├── release/                            # 📦 发布产物（安装器）\n├── docs/screenshots/                   # 📸 系统截图\n├── 文档信息归档/                         # 📚 项目文档归档\n└── Injury Severity.CSV                 # 🗃️ OSHA 事故数据集（53,000+）\n```\n\n\u003cbr/\u003e\n\n---\n\n## :wrench: 技术栈\n\n| 类别 | 技术 | 说明 |\n|:---:|:---:|:---:|\n| :globe_with_meridians: Web 框架 | Streamlit | 快速构建数据应用 |\n| :brain: 大语言模型 | DeepSeek Chat | OpenAI 兼容接口 |\n| :mag: 向量检索 | FAISS | Meta 开源向量数据库 |\n| :abc: 文本嵌入 | sentence-transformers | all-MiniLM-L6-v2 |\n| :chart_with_upwards_trend: 可视化 | Plotly | 交互式图表 |\n| :bar_chart: 数据处理 | Pandas / NumPy | 数据分析与处理 |\n| :test_tube: 因果推断 | statsmodels / scipy | PSM 倾向得分匹配 |\n| :package: 打包分发 | PyInstaller | Windows 安装器生成 |\n| :snake: 运行环境 | Python 3.12+ | 主语言 |\n\n\u003cbr/\u003e\n\n---\n\n## :rocket: 快速开始\n\n### 1. 克隆仓库\n\n```bash\ngit clone https://github.com/jensenzhong/Construction-Safety-Risk-Analysis-System-Based-on-RAG-LLM.git\ncd Construction-Safety-Risk-Analysis-System-Based-on-RAG-LLM\n```\n\n### 2. 创建虚拟环境\n\n```bash\npython -m venv .venv\n\n# Windows PowerShell\n.venv\\Scripts\\Activate.ps1\n\n# Windows CMD\n.venv\\Scripts\\activate.bat\n```\n\n### 3. 安装依赖\n\n```bash\npip install -r requirements.txt\n```\n\n### 4. 配置 API Key\n\n复制示例配置文件并填入你的 DeepSeek API Key：\n\n```bash\ncp settings.example.json settings.json\n```\n\n编辑 `settings.json`：\n\n```json\n{\n  \"deepseek\": {\n    \"api_key\": \"your_api_key_here\",\n    \"base_url\": \"https://api.deepseek.com/v1\",\n    \"model\": \"deepseek-chat\"\n  }\n}\n```\n\n或使用环境变量：\n\n```bash\n# Windows PowerShell\n$env:DEEPSEEK_API_KEY=\"your_api_key\"\n\n# Linux / macOS\nexport DEEPSEEK_API_KEY=\"your_api_key\"\n```\n\n### 5. 启动应用\n\n```bash\n# 方式 A：使用项目脚本（推荐）\nrun_app.bat\n\n# 方式 B：直接运行\nstreamlit run app.py --server.address 127.0.0.1 --server.port 8501\n```\n\n\u003e :bulb: **提示**：浏览器访问 `http://127.0.0.1:8501`\n\n\u003cbr/\u003e\n\n---\n\n## :bar_chart: 数据与索引\n\n### 数据集\n\n默认使用 `Injury Severity.CSV`，包含 **53,000+** 条 OSHA 真实事故记录。\n\n| 字段 | 说明 |\n|:---:|:---:|\n| `abstract` | 事故描述文本（用于检索与分析） |\n| `event_keyword` | 事故关键词分类 |\n| `degree_of_inj_x` | 伤害严重程度 |\n| `date` | 事故发生日期 |\n| `temp` / `wind_speed` / `wind_deg` | 环境参数 |\n\n### 构建索引\n\n```bash\npython -m rag.index_builder --device cpu\n```\n\n生成文件：\n- `indexes/faiss.index` — FAISS 向量索引\n- `indexes/metadata.parquet` — 结构化元数据\n\n\u003cbr/\u003e\n\n---\n\n## :test_tube: 测试\n\n```bash\npytest -q\n```\n\n测试覆盖（12 项）：\n\n| 模块 | 测试内容 |\n|:---:|:---:|\n| RAG | 引用过滤与约束验证 |\n| 预评估 | 输出结构完整性 |\n| LLM | DeepSeek 配置解析 |\n| 启动器 | 配置播种逻辑 |\n| PSM | 因果分析健全性 |\n\n\u003cbr/\u003e\n\n---\n\n## :package: Windows 打包分发\n\n### 一键打包安装器\n\n```bash\npython scripts/build_windows_installer.py\n```\n\n产物：`release/ConstructionSafetyAssistant-Installer.exe`\n\n### 安装后运行\n\n1. 运行安装器，按提示完成安装\n2. 桌面 / 开始菜单启动 **Construction Safety Assistant**\n3. 浏览器自动打开 `http://127.0.0.1:8501`\n\n\u003e :bulb: 打包版本已内置 Python 运行时和所有依赖，无需额外安装。\n\n\u003cbr/\u003e\n\n---\n\n## :question: 常见问题\n\n| 问题 | 解决方案 |\n|:---:|:---:|\n| 启动后浏览器没打开 | 手动访问 `http://127.0.0.1:8501` |\n| 提示缺少索引文件 | 运行 `python -m rag.index_builder --device cpu` |\n| DeepSeek 报 API Key 缺失 | 检查 `settings.json` 或环境变量 `DEEPSEEK_API_KEY` |\n| 查看启动日志 | `%LOCALAPPDATA%\\ConstructionSafetyAssistant\\launcher.log` |\n| 如何停止服务 | 运行 `stop_app.bat` 或关闭终端窗口 |\n\n\u003cbr/\u003e\n\n---\n\n## :lock: 安全说明\n\n本项目已配置 `.gitignore` 保护敏感文件：\n\n- :white_check_mark: `settings.json`（含 API Key）**不会**被提交\n- :white_check_mark: `.env` 文件**不会**被提交\n- :white_check_mark: `settings.example.json` 提供安全配置模板\n\n\u003e :rotating_light: **请勿将真实 API Key 提交到公开仓库。**\n\n\u003cbr/\u003e\n\n---\n\n## :world_map: Roadmap\n\n- [ ] 接入真实传感器（MQTT / HTTP / 串口）\n- [ ] 增加多工种风险模板库\n- [ ] 引入用户权限与审计日志\n- [ ] 支持多项目 / 多工地数据隔离\n- [ ] 增强检索评估指标与可解释性\n- [ ] 支持更多 LLM 后端（GPT-4、Claude、通义千问）\n- [ ] 移动端适配与小程序版本\n\n\u003cbr/\u003e\n\n---\n\n## :balance_scale: 许可证与声明\n\n本项目采用 [**CC BY-NC 4.0**](https://creativecommons.org/licenses/by-nc/4.0/) 许可证。\n\n| 允许 | 禁止 |\n|:---:|:---:|\n| :white_check_mark: 学习与研究使用 | :x: 商业用途 |\n| :white_check_mark: 修改与二次创作 | :x: 未署名使用 |\n| :white_check_mark: 非商业分享与传播 | :x: 去除版权声明 |\n\n\u003e :warning: **商业使用须获得作者书面授权。** 如需商业合作请联系作者。\n\n\u003e :information_source: 本项目基于公开的 OSHA 事故数据集进行研究开发。系统中的传感器数据为模拟数据，仅用于展示系统设计理念和核心功能。\n\n\u003cbr/\u003e\n\n\u003cdiv align=\"center\"\u003e\n\n**施工安全风险智能分析系统** · 2025\n\nCopyright \u0026copy; 2025 Jensen Zhong. All commercial rights reserved.\n\nMade with :heart: by Jensen Zhong\n\n\u003c/div\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjensenzhong%2FConstruction-Safety-Risk-Analysis-System-Based-on-RAG-LLM/lists"}