File size: 11,672 Bytes
b7db63d
 
 
ab058b0
b7db63d
 
 
 
 
 
5c6379b
646ba30
5c6379b
646ba30
5c6379b
646ba30
5c6379b
646ba30
 
 
 
b7db63d
5c6379b
 
74dbaef
 
5c6379b
 
74dbaef
 
5c6379b
646ba30
 
 
b7db63d
646ba30
 
 
5c6379b
b7db63d
5c6379b
 
646ba30
b7db63d
5c6379b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646ba30
 
 
 
 
74dbaef
 
 
 
 
b7db63d
646ba30
 
 
 
 
5c6379b
 
646ba30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c6379b
646ba30
 
b7db63d
646ba30
 
 
5c6379b
646ba30
 
b7db63d
 
646ba30
 
b7db63d
 
 
 
5c6379b
646ba30
 
 
 
 
 
5c6379b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646ba30
 
 
b7db63d
 
646ba30
5c6379b
b7db63d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
646ba30
 
 
 
 
 
b7db63d
 
 
 
 
 
646ba30
 
 
5c6379b
 
 
646ba30
b7db63d
 
 
 
 
 
 
 
 
646ba30
 
b7db63d
646ba30
b7db63d
646ba30
b7db63d
 
 
 
 
 
 
 
 
 
 
 
 
646ba30
5c6379b
646ba30
5c6379b
 
 
646ba30
5c6379b
646ba30
5c6379b
646ba30
5c6379b
 
 
 
 
 
 
646ba30
5c6379b
646ba30
5c6379b
 
 
 
 
 
646ba30
5c6379b
646ba30
5c6379b
 
b7db63d
 
 
 
5c6379b
646ba30
b7db63d
5c6379b
 
 
 
 
 
 
 
646ba30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5c6379b
 
 
 
 
 
646ba30
 
 
 
 
5c6379b
646ba30
 
 
5c6379b
 
b7db63d
5c6379b
b7db63d
5c6379b
646ba30
 
5c6379b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
title: AI Code Analyzer
emoji: 🧠
colorFrom: gray
colorTo: green
sdk: docker
app_port: 7860
pinned: false
---

# AI Code Analyzer

A professional AI-powered code analysis tool with a sleek Matrix-inspired interface that leverages multiple Large Language Models (LLMs) to provide comprehensive code reviews, identify issues, and suggest improvements.

![AI Code Analyzer](https://img.shields.io/badge/Python-3.11+-blue.svg)
![Streamlit](https://img.shields.io/badge/Streamlit-1.36.0-red.svg)
![Deployment](https://img.shields.io/badge/Deployment-Render-green.svg)
![License](https://img.shields.io/badge/License-MIT-green.svg)

## ✨ Features

* **πŸ€– Multi-Model Analysis**: Compare insights from OpenAI GPT-4, Anthropic Claude, DeepSeek, and Hugging Face models
* **🎯 Fine-tuned Code Analyzer**: Custom DeepSeek model trained on 59+ code analysis examples
* **🎨 Matrix-Inspired UI**: Sleek dark theme with neon green accents and cyberpunk aesthetics
* **πŸ“Š Comprehensive Code Review**: Get quality scores, identify bugs, security issues, and performance concerns
* **πŸ” Language Auto-Detection**: Automatically detects programming language or manually specify
* **πŸ“ File Upload Support**: Upload code files directly with drag & drop functionality
* **⚑ Fast & Responsive**: Optimized for deployment with professional performance
* **πŸ”’ Secure**: API keys are securely managed through environment variables
* **πŸ“ˆ Real-time Metrics**: Track analysis time, quality scores, and model comparisons
* **🌐 Remote Model Support**: Use fine-tuned models hosted on Hugging Face (always available)

## 🌐 Live Demo

**Local Deployment**: Your AI Code Analyzer is running locally at `http://localhost:8501/`

## πŸ› οΈ Tech Stack

- **Frontend**: Streamlit with custom Matrix-inspired CSS
- **LLM Integration**: OpenAI, Anthropic, DeepSeek, Hugging Face APIs
- **Fine-tuning**: LoRA/QLoRA with Hugging Face Transformers
- **Model Hosting**: Hugging Face Hub & Spaces
- **Language**: Python 3.11+
- **Deployment**: Hugging Face Spaces (recommended for ease of use and free tier)
- **Styling**: Custom CSS with Google Fonts (Share Tech Mono, Orbitron)

## 🎯 Fine-tuned Model

This project includes a custom fine-tuned DeepSeek Coder model trained on 59+ code analysis examples:

- **Base Model**: DeepSeek Coder 1.3B
- **Training Method**: LoRA (Low-Rank Adaptation)
- **Dataset**: 59 high-quality code analysis examples
- **Features**: Quality scores, structured analysis, code improvements
- **Hosting**: Hugging Face Spaces (always online)

### Model Capabilities

The fine-tuned model provides:
- **Quality Scores**: 1-100 rating for code quality
- **Structured Analysis**: Bugs, Performance, Security sections
- **Code Improvements**: Specific suggestions with examples
- **Professional Output**: Consistent, detailed analysis format

## πŸš€ Quick Start

### Prerequisites

* Python 3.11 or higher
* API keys for at least one LLM provider:  
   * OpenAI API Key  
   * Anthropic API Key  
   * DeepSeek API Key
   * **Hugging Face API Key** (recommended for free usage)

### Installation

1. **Clone the repository:**
```bash
git clone https://github.com/arun3676/ai-code-analyzer.git
cd ai-code-analyzer
```

2. **Create a virtual environment:**
```bash
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate
```

3. **Install dependencies:**
```bash
pip install -r requirements.txt
```

4. **Configure environment variables:**
   
   Create a `.env` file in the root directory:
```env
# API Keys - Replace with your actual API keys
HUGGINGFACE_API_KEY=your_huggingface_api_key_here  # Recommended for free usage
OPENAI_API_KEY=your_openai_api_key_here
ANTHROPIC_API_KEY=your_anthropic_api_key_here
DEEPSEEK_API_KEY=your_deepseek_api_key_here
GITHUB_TOKEN=your_github_token_here  # Optional, for higher API limits
```

**πŸ“š For detailed Hugging Face setup instructions, see: [HUGGINGFACE_SETUP_GUIDE.md](HUGGINGFACE_SETUP_GUIDE.md)**

5. **Run the application:**
```bash
# Option 1: Use the startup script (recommended)
python run_app.py

# Option 2: Run directly with Streamlit
python -m streamlit run matrix_final.py --server.port 8501
```

The application will be available at `http://localhost:8501`

## πŸ“‹ Usage

### Code Analysis
1. **Upload a file** or **paste your code** in the main panel
2. **Select a model** from the dropdown (OpenAI, Anthropic, or DeepSeek)
3. **Choose analysis type**: Code Analysis or Multimodal Analysis
4. **Click "Analyze Code"** to get comprehensive insights

### File Upload
- **Drag & drop** code files directly onto the upload area
- **Supported formats**: .py, .js, .java, .cpp, .c, .cs, .go, .rs, .php, .rb, .swift, .kt, .txt
- **File size limit**: 200MB per file

### Analysis Results
- **Quality Score**: 0-100 rating with color-coded indicators
- **Summary**: Clear description of code functionality
- **Issues & Bugs**: Potential problems identified
- **Improvements**: Actionable suggestions for better code
- **Security**: Security vulnerabilities and concerns
- **Performance**: Optimization recommendations

## πŸ—οΈ Project Structure

For a detailed explanation of the project structure, architecture, and data flow, please see [PROJECT_STRUCTURE.md](PROJECT_STRUCTURE.md) and [ARCHITECTURE.md](ARCHITECTURE.md).

```
ai-code-analyzer/
β”œβ”€β”€ .venv/                           # Virtual environment directory
β”œβ”€β”€ analyzer/                          # Core analysis engine
β”‚   β”œβ”€β”€ __init__.py                   # Package initialization
β”‚   β”œβ”€β”€ code_analyzer.py              # Main analysis engine
β”‚   β”œβ”€β”€ llm_clients.py                # LLM API client implementations
β”‚   β”œβ”€β”€ prompts.py                    # Analysis prompt templates
β”‚   └── utils.py                      # Utility functions
β”œβ”€β”€ tests/                             # Automated tests
β”‚   β”œβ”€β”€ test_matrix_final.py          # Tests for matrix_final.py utilities
β”‚   └── test_prompts.py               # Tests for prompt generation
β”œβ”€β”€ .env                              # Environment variables (create this)
β”œβ”€β”€ .gitignore                        # Git ignore file
β”œβ”€β”€ ARCHITECTURE.md                   # Detailed architecture documentation
β”œβ”€β”€ matrix_final.py                    # Main Streamlit application
β”œβ”€β”€ PROJECT_STRUCTURE.md              # High-level project structure
β”œβ”€β”€ README.md                         # This file
β”œβ”€β”€ requirements.txt                   # Python dependencies
β”œβ”€β”€ run_app.py                         # Startup script for easy launching
└── TESTING_GUIDE.md                  # Guide for testing the application
```

## πŸ”§ Configuration

### Supported LLM Providers

| Provider      | Model                    | API Key Environment Variable |
| ------------- | ------------------------ | ---------------------------- |
| Hugging Face  | Mixtral-8x7B-Instruct    | HUGGINGFACE\_API\_KEY        |
| OpenAI        | GPT-4o-mini              | OPENAI\_API\_KEY             |
| Anthropic     | Claude 3 Haiku           | ANTHROPIC\_API\_KEY          |
| DeepSeek      | DeepSeek Chat            | DEEPSEEK\_API\_KEY           |

### Supported Programming Languages

- Python, JavaScript, Java, C++, C#, Go, Rust, PHP, Ruby, Swift, Kotlin
- **Auto-detection** available for most languages
- **Manual selection** option for specific analysis

## πŸ§ͺ Testing

For detailed instructions on how to test the application, please refer to the [TESTING_GUIDE.md](TESTING_GUIDE.md).

To run the automated tests:
```bash
pytest
```

## πŸš€ Deployment

### Deploy to Hugging Face Spaces (Recommended)

This project is configured for easy deployment on **Hugging Face Spaces**:

1.  **Fork this repository** to your GitHub account.
2.  **Create a new Space**: Go to [Hugging Face Spaces](https://huggingface.co/spaces/new) and create a new Space.
    *   Choose "Streamlit" as the Space SDK.
    *   Select "Public" or "Private" as per your preference.
    *   Connect your forked GitHub repository.
3.  **Configure Secrets**: In your Hugging Face Space settings, go to "App settings" -> "Secrets". Add your API keys:
    *   `HUGGINGFACE_API_KEY` (required for Hugging Face models)
    *   `OPENAI_API_KEY` (optional)
    *   `ANTHROPIC_API_KEY` (optional)
    *   `DEEPSEEK_API_KEY` (optional)
    *   `GITHUB_TOKEN` (optional, for higher GitHub API limits)
4.  **Wait for Deployment**: Hugging Face will automatically detect your `requirements.txt` and `matrix_final.py` and deploy your app.
5.  **Access Your App**: Once deployed, your application will be live on your Hugging Face Space URL.

### Manual Deployment

If deploying manually, use these settings:
- **Build Command**: `pip install -r requirements.txt`
- **Start Command**: `streamlit run matrix_final.py --server.port=$PORT --server.address=0.0.0.0 --server.headless=true --server.enableCORS=false`

## πŸ“Š Analysis Output

The tool provides structured analysis including:

- **🎯 Quality Score**: 0-100 rating with visual indicators
- **πŸ“‹ Summary**: Clear description of code functionality  
- **πŸ› Issues**: Potential bugs and logical errors
- **πŸ’‘ Improvements**: Specific actionable suggestions
- **πŸ›‘οΈ Security**: Security vulnerabilities and concerns
- **⚑ Performance**: Optimization opportunities
- **πŸ“ˆ Metrics**: Analysis time, model used, code statistics

## 🎨 UI Features

- **Matrix Theme**: Dark background with neon green accents
- **Responsive Design**: Works on desktop, tablet, and mobile
- **File Upload**: Drag & drop interface with progress indicators
- **Real-time Analysis**: Live progress updates during analysis
- **Professional Layout**: Clean, organized interface
- **Custom Fonts**: Share Tech Mono and Orbitron for cyberpunk feel

## πŸ§ͺ Development

### Running Locally
```bash
# Option 1: Use the startup script (recommended)
python run_app.py

# Option 2: Start the development server directly
python -m streamlit run matrix_final.py --server.port 8501

# Option 3: With auto-reload for development
python -m streamlit run matrix_final.py --server.port 8501 --server.runOnSave true
```

### Code Structure
- **`matrix_final.py`**: Main Streamlit application with UI and routing
- **`analyzer/`**: Core analysis engine and LLM integrations
- **Custom CSS**: Embedded in the main app for Matrix theme
- **Error Handling**: Comprehensive error handling and user feedback

## 🀝 Contributing

1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request

## πŸ“ License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

## πŸ™ Acknowledgments

* **Streamlit** for the amazing framework
* **OpenAI** for GPT models
* **Anthropic** for Claude
* **DeepSeek** for DeepSeek Coder
* **Render** for seamless deployment
* **Google Fonts** for Share Tech Mono and Orbitron fonts

## πŸ“ž Support

If you encounter any issues or have questions:

1. Check the [Issues](https://github.com/arun3676/ai-code-analyzer/issues) page
2. Create a new issue with detailed information
3. Contact the maintainers

## πŸ”— Links

- **Live Demo**: *Your Hugging Face Space URL here*
- **Repository**: [github.com/arun3676/ai-code-analyzer](https://github.com/arun3676/ai-code-analyzer)
- **Hugging Face Spaces**: [huggingface.co/spaces](https://huggingface.co/spaces)

---

**Built with ❀️ by [Arun](https://github.com/arun3676)**