Harshali2628/AI-Code-Review-Assistant

GitHub: Harshali2628/AI-Code-Review-Assistant

一款基于 Gemini AI 的 Python 代码审查助手,集成静态分析、安全扫描、AI 重构和自动测试生成功能,并提供交互式仪表板与 REST API。

Stars: 2 | Forks: 0

# 🤖 AI 代码审查助手 ![Python](https://img.shields.io/badge/Python-3.11-blue?logo=python) ![FastAPI](https://img.shields.io/badge/FastAPI-REST_API-009688?logo=fastapi) ![Streamlit](https://img.shields.io/badge/Streamlit-App-red?logo=streamlit) ![Docker](https://img.shields.io/badge/Docker-Containerized-2496ED?logo=docker) ![Gemini AI](https://img.shields.io/badge/Google-Gemini_AI-4285F4?logo=google) ![License](https://img.shields.io/badge/License-MIT-green) ![Status](https://img.shields.io/badge/Status-Active-success) 一个基于 AI 的 Python 代码审查助手,使用 **Streamlit** 和 **Google Gemini AI** 构建,AI 审查逻辑也通过独立的 **FastAPI REST API** 暴露。该应用程序通过执行语法验证、静态分析、安全扫描、复杂度分析、AI 驱动的代码审查、基于 AI 的代码重构、自动生成 PyTest、测试执行以及生成专业的 PDF 报告,实现了代码分析的自动化,所有这些都封装在一个交互式的 **Streamlit** 仪表板中。 ## 🚀 在线演示 🔗 [https://ai-code-review-assistant-7dwvew6j4ebfvk8mb6avhb.streamlit.app/] ## 🚀 功能 - 📂 上传 Python (.py) 源文件 - ✅ 语法验证 - 🔍 使用 Pylint 进行静态代码分析 - 🛡️ 使用 Bandit 进行安全扫描 - 📈 使用 Radon 进行圈复杂度分析 - 🤖 使用 Google Gemini AI 进行 AI 代码审查 - ✨ AI 代码重构 - 🧪 AI 生成的单元测试创建 - ▶️ 自动执行 PyTest - 📊 交互式仪表板 - 📄 生成 PDF 报告 - ⚡ **FastAPI REST API** (`api.py`) 将 Gemini AI 代码审查作为一个独立的 `/review` endpoint 暴露 - 📑 为 API 自动生成 **Swagger / OpenAPI 文档** (`/docs`) - 🐳 提供 Docker 支持,用于 API 和 UI 的容器化部署 - 📁 支持 Python 文件上传 - 📈 交互式 Streamlit 仪表板 ## 🛠️ 技术栈 | 类别 | 技术 | |-----------|------------| | 语言 | Python | | 后端 / API | **FastAPI**, Uvicorn | | 前端 | Streamlit | | AI 模型 | Google Gemini | | 静态分析 | Pylint | | 安全 | Bandit | | 复杂度 | Radon | | 测试 | PyTest | | PDF | ReportLab | | 容器化 | Docker | | 版本控制 | Git & GitHub | ## 📂 项目结构 ``` AI-Code-Review-Assistant/ │ ├── app.py # Streamlit frontend ├── api.py # FastAPI app (uvicorn entrypoint) ├── Dockerfile ├── .dockerignore ├── requirements.txt ├── README.md ├── LICENSE │ ├── assets/ ├── reports/ ├── screenshots/ ├── uploads/ │ └── utils/ ├── ai_reviewer.py ├── dashboard.py ├── pytest_runner.py ├── report_generator.py ├── score_helper.py ├── static_analysis.py └── syntax_checker.py ``` ## ⚡ FastAPI 集成 AI 代码审查逻辑作为轻量级的 **FastAPI REST API** (`api.py`) 暴露,构建在 `utils/ai_reviewer.py` 中现有的 `review_code()` 函数之上。这使得 Gemini 驱动的审查可以独立于 Streamlit UI 进行触发——通过 `curl`、Postman 或任何其他客户端。 ### 关键端点 | 方法 | endpoint | 描述 | |--------|----------|--------------| | `GET` | `/` | Root endpoint — 确认 API 正在运行 | | `GET` | `/health` | 健康检查 endpoint | | `POST` | `/review` | 接收代码字符串并返回 AI 生成的审查 (Gemini AI) | | `GET` | `/docs` | 交互式 Swagger UI (由 FastAPI 自动生成) | ### 运行 FastAPI 服务器 ``` uvicorn api:app --reload --host 0.0.0.0 --port 8000 ``` 运行后,请访问: - Swagger UI → `http://localhost:8000/docs` - ReDoc → `http://localhost:8000/redoc` ### 请求示例 ``` curl -X POST "http://localhost:8000/review" \ -H "Content-Type: application/json" \ -d '{"code": "def add(a, b):\n return a + b"}' ``` ### 响应示例 ``` { "review": "The function is simple and correct. Consider adding type hints and a docstring for clarity." } ``` ## ⚙️ 安装说明 ### 克隆仓库 ``` git clone https://github.com/Harshali2628/AI-Code-Review-Assistant.git ``` ### 打开文件夹 ``` cd AI-Code-Review-Assistant ``` ### 安装依赖 ``` pip install -r requirements.txt ``` ### 添加 Gemini API Key 创建一个 `.env` 文件。 ``` GEMINI_API_KEY=YOUR_API_KEY ``` ### 运行项目 启动 **FastAPI 后端**: ``` uvicorn api:app --reload ``` 在单独的终端中,启动 **Streamlit 前端**: ``` streamlit run app.py ``` ## 🐳 Docker ### 构建 ``` docker build -t ai-code-review-assistant . ``` ### 运行 (Streamlit UI) ``` docker run -p 8501:8501 --env-file .env ai-code-review-assistant ``` ### 运行 (FastAPI 后端) ``` docker run -p 8000:8000 --env-file .env ai-code-review-assistant uvicorn api:app --host 0.0.0.0 --port 8000 ``` ## 📸 截图 ### 主页 ![主页](https://static.pigsec.cn/wp-content/uploads/repos/cas/af/af79380e13818bab0e29385f4f8102ba3c27c7fb97addd1761e40e60658e3c9f.png) ### 仪表板 ![仪表板](https://static.pigsec.cn/wp-content/uploads/repos/cas/3c/3c4e8d09fb2fef5736fbd6273ac1e3c9850f14dd42ecc221e3eda7bbafab5309.png) ### 上传文件 ![上传文件](https://static.pigsec.cn/wp-content/uploads/repos/cas/e3/e3cfdba4f1a8884a45533410facdcb47fa3fd75960efc6498ac8af0874e37044.png) ## 🏗️ 系统架构 ``` ┌─────────────────────┐ │ User │ └──────────┬──────────┘ │ ┌───────────────┴───────────────┐ ▼ ▼ ┌────────────────────────┐ ┌────────────────────────┐ │ Streamlit UI │ │ External API Client │ │ (File Upload & Inputs) │ │ (curl / Postman / etc) │ └──────────┬──────────────┘ └───────────┬─────────────┘ │ │ │ (direct function calls) │ HTTP POST /review ▼ ▼ ┌─────────────────┐ ┌──────────────────┐ ┌──────────────────┐ │ Syntax Checker │ │ Static Analysis │ │ FastAPI (api.py) │ │ │ │ • Pylint │ │ → review_code() │ │ │ │ • Bandit │ │ → Gemini AI │ │ │ │ • Radon │ │ │ └────────┬────────┘ └────────┬─────────┘ └────────┬─────────┘ │ │ │ └──────────────────────┼──────────────────────┘ ▼ ┌────────────────────────────┐ │ Score & Report Generator │ │ • Quality Score │ │ • Security Score │ │ • Complexity Score │ │ • PDF Report │ └─────────────┬──────────────┘ ▼ ┌─────────────────────────────┐ │ Interactive Dashboard │ │ • Metrics │ │ • Charts │ │ • Download PDF Report │ └─────────────────────────────┘ ``` ### 工作流程 1. 用户通过 Streamlit 界面上传 Python 源代码文件(语法检查、静态分析、安全扫描和复杂度分析直接在应用程序内运行)。 2. 同时,**FastAPI 服务** (`api.py`) 将 Gemini 驱动的 `review_code()` 函数作为独立的 `/review` endpoint 暴露,因此任何外部客户端都可以通过 HTTP 请求 AI 代码审查。 3. Google Gemini AI 生成包含建议和改进的智能代码审查。 4. Streamlit 应用程序结合静态分析结果和 AI 审查来计算质量指标并生成 PDF 报告。 5. 交互式仪表板显示结果并允许用户下载生成的报告。 ## 🚀 未来改进 - 🔹 支持多种编程语言,如 Java、C++、JavaScript 和 Go。 - 🔹 集成 GitHub API 以直接审查 pull request 和仓库。 - 🔹 添加与 GitHub Actions 的 CI/CD 集成以进行自动化代码分析。 - 🔹 通过交互式仪表板提供代码质量趋势和历史分析。 - 🔹 支持多种 AI 模型(OpenAI、Claude、Llama 和 Gemini)并提供模型选择。 - 🔹 通过为静态分析、安全扫描、复杂度分析和 PDF 报告生成添加额外的 endpoint 来扩展 FastAPI 服务。 - 🔹 实现用户身份验证和个性化审查历史(通过 FastAPI JWT 身份验证)。 - 🔹 为公共 FastAPI endpoint 添加速率限制和 API Key 管理。 - 🔹 将应用程序部署在云平台上,例如 AWS、Azure 或 Google Cloud。 - 🔹 生成多种格式(PDF、HTML 和 DOCX)的可下载报告。 - 🔹 添加具有评论和审查共享功能的团队协作功能。 - 🔹 使用异步处理(FastAPI 异步 endpoint)来提高大型代码库的性能。 ## 👩‍💻 作者 **Harshali Panchal** - GitHub: https://github.com/Harshali2628 - LinkedIn: https://www.linkedin.com/in/harshali-panchal-771b6324a ## ⭐ 支持 如果您觉得这个项目有用,请考虑在 GitHub 上给它一个 ⭐。
标签:AI辅助编程, AV绕过, FastAPI, Kubernetes, Python, Streamlit, 云安全监控, 代码审查, 安全扫描, 无后门, 时序注入, 访问控制, 请求拦截, 逆向工具, 静态分析