-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_server_simple.py
More file actions
337 lines (300 loc) · 11 KB
/
api_server_simple.py
File metadata and controls
337 lines (300 loc) · 11 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
"""
Simple Flask API server for AI Code Review
Serves only API endpoints for the React frontend
"""
import os
import sys
import json
import time
import logging
from datetime import datetime
from typing import Dict, Any, Optional
from flask import Flask, request, jsonify
from flask_cors import CORS
# Add src directory to Python path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
try:
from config import Settings
from llm_client import GeminiClient
except ImportError as e:
print(f"❌ Import error: {e}")
print("Please make sure all dependencies are installed: pip install -r requirements.txt")
sys.exit(1)
# Initialize Flask app
app = Flask(__name__)
CORS(app)
# Setup logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Global instances
settings = Settings()
gemini_client = None
def initialize_gemini():
"""Initialize Gemini client"""
global gemini_client
try:
gemini_client = GeminiClient()
logger.info("✅ Gemini client initialized successfully")
return True
except Exception as e:
logger.error(f"❌ Failed to initialize Gemini client: {e}")
return False
def analyze_code_simple(code: str, context: Dict[str, Any]) -> Dict[str, Any]:
"""
Simple code analysis using Gemini AI
"""
try:
# Build a simple prompt for analysis
prompt = f"""
As an expert code reviewer, analyze the following code and provide comprehensive feedback.
Code to analyze:
```
{code}
```
Context: {json.dumps(context, indent=2)}
Please provide your analysis in the following JSON format:
{{
"overall_score": <number between 0-10>,
"summary": "<brief summary of the code quality>",
"criteria_results": [
{{
"criterion": "Code Quality",
"score": <0-10>,
"feedback": "<detailed feedback>",
"suggestions": ["<suggestion1>", "<suggestion2>"],
"severity": "<low|medium|high|critical>"
}},
{{
"criterion": "Security",
"score": <0-10>,
"feedback": "<detailed feedback>",
"suggestions": ["<suggestion1>", "<suggestion2>"],
"severity": "<low|medium|high|critical>"
}},
{{
"criterion": "Performance",
"score": <0-10>,
"feedback": "<detailed feedback>",
"suggestions": ["<suggestion1>", "<suggestion2>"],
"severity": "<low|medium|high|critical>"
}},
{{
"criterion": "Maintainability",
"score": <0-10>,
"feedback": "<detailed feedback>",
"suggestions": ["<suggestion1>", "<suggestion2>"],
"severity": "<low|medium|high|critical>"
}},
{{
"criterion": "Readability",
"score": <0-10>,
"feedback": "<detailed feedback>",
"suggestions": ["<suggestion1>", "<suggestion2>"],
"severity": "<low|medium|high|critical>"
}}
],
"recommendations": ["<recommendation1>", "<recommendation2>"],
"positive_aspects": ["<positive1>", "<positive2>"],
"areas_for_improvement": ["<improvement1>", "<improvement2>"],
"code_quality_metrics": {{
"complexity": <0-10>,
"maintainability": <0-10>,
"readability": <0-10>,
"testability": <0-10>
}},
"detected_patterns": ["<pattern1>", "<pattern2>"],
"potential_issues": ["<issue1>", "<issue2>"]
}}
Please ensure the JSON is valid and complete.
"""
response = gemini_client.model.generate_content(prompt)
if not response or not response.text:
logger.error("Empty response from Gemini")
return create_error_response("Empty response from AI")
# Try to parse the JSON response
try:
result = json.loads(response.text)
logger.info("Successfully parsed Gemini response")
return result
except json.JSONDecodeError as e:
logger.error(f"Failed to parse Gemini response as JSON: {e}")
# Return a fallback response
return create_fallback_response(code, response.text)
except Exception as e:
logger.error(f"Error during analysis: {e}")
return create_error_response(str(e))
def create_error_response(error_msg: str) -> Dict[str, Any]:
"""Create a structured error response"""
return {
"overall_score": 0,
"summary": f"Analysis failed: {error_msg}",
"criteria_results": [],
"recommendations": ["Please try again or check your input"],
"positive_aspects": [],
"areas_for_improvement": ["Fix the analysis error"],
"code_quality_metrics": {
"complexity": 0,
"maintainability": 0,
"readability": 0,
"testability": 0
},
"detected_patterns": [],
"potential_issues": [f"Analysis error: {error_msg}"]
}
def create_fallback_response(code: str, raw_response: str) -> Dict[str, Any]:
"""Create a fallback response when JSON parsing fails"""
return {
"overall_score": 5,
"summary": "Analysis completed but response format was invalid. Raw analysis provided.",
"criteria_results": [
{
"criterion": "AI Analysis",
"score": 5,
"feedback": raw_response[:500] + "..." if len(raw_response) > 500 else raw_response,
"suggestions": ["Review the raw analysis feedback"],
"severity": "medium"
}
],
"recommendations": ["Please try the analysis again"],
"positive_aspects": ["Code was successfully processed"],
"areas_for_improvement": ["AI response formatting needs improvement"],
"code_quality_metrics": {
"complexity": 5,
"maintainability": 5,
"readability": 5,
"testability": 5
},
"detected_patterns": ["Code analysis performed"],
"potential_issues": ["Response parsing failed"]
}
@app.route('/api/health')
def health_check():
"""Health check endpoint"""
return jsonify({
'status': 'healthy',
'timestamp': datetime.now().isoformat(),
'version': '1.0.0',
'services': {
'gemini_api': 'connected' if gemini_client else 'disconnected',
'flask_server': 'running'
}
})
@app.route('/api/analyze', methods=['POST'])
def analyze_code():
"""
Analyze code or git diff and return comprehensive feedback
"""
try:
start_time = time.time()
# Get request data
data = request.get_json()
if not data or 'code' not in data:
return jsonify({'error': 'No code provided'}), 400
code = data['code'].strip()
if not code:
return jsonify({'error': 'Empty code provided'}), 400
logger.info(f"Analyzing code of length {len(code)} characters")
if not gemini_client:
return jsonify({'error': 'AI service not available'}), 503
# Prepare context
context = {
'format': 'git-diff' if 'diff --git' in code else 'raw-code',
'analysis_timestamp': datetime.now().isoformat(),
'request_id': str(time.time())
}
# Perform analysis
logger.info("Starting AI analysis...")
analysis_result = analyze_code_simple(code, context)
# Add metadata
processing_time = int((time.time() - start_time) * 1000)
analysis_result.update({
'analysis_timestamp': context['analysis_timestamp'],
'processing_time_ms': processing_time,
'input_format': context['format'],
'input_stats': {
'total_lines': len(code.split('\n')),
'total_characters': len(code)
}
})
logger.info(f"Analysis completed in {processing_time}ms")
return jsonify(analysis_result)
except Exception as e:
logger.error(f"Error analyzing code: {e}")
return jsonify({
'error': 'Analysis failed',
'details': str(e),
'timestamp': datetime.now().isoformat()
}), 500
@app.route('/api/examples')
def get_examples():
"""Get example git diffs for testing"""
examples = {
'python_function': {
'title': 'Python Function Improvement',
'description': 'Adding null checks and error handling',
'code': '''diff --git a/src/utils.py b/src/utils.py
index 1234567..abcdefg 100644
--- a/src/utils.py
+++ b/src/utils.py
@@ -10,7 +10,10 @@ def calculate_total(items):
total = 0
for item in items:
- total += item.price
+ if item.price is not None:
+ total += item.price
+ else:
+ print(f"Warning: Item {item.name} has no price")
return total'''
},
'react_component': {
'title': 'React Component Enhancement',
'description': 'Adding props validation and default values',
'code': '''diff --git a/components/UserProfile.tsx b/components/UserProfile.tsx
index 1234567..abcdefg 100644
--- a/components/UserProfile.tsx
+++ b/components/UserProfile.tsx
@@ -15,8 +15,12 @@ const UserProfile: React.FC<Props> = ({ user }) => {
return (
<div className="user-profile">
- <h2>{user.name}</h2>
- <p>{user.email}</p>
+ <h2>{user.name || 'Anonymous User'}</h2>
+ <p>{user.email || 'No email provided'}</p>
+ {user.avatar && (
+ <img src={user.avatar} alt="User avatar" />
+ )}
+ <div>Last seen: {user.lastSeen || 'Never'}</div>
</div>
);
};'''
}
}
return jsonify(examples)
if __name__ == '__main__':
print("""
🤖 AI Code Review Assistant - API Server
═══════════════════════════════════════════════
🚀 Starting Flask API server for React frontend
📡 API endpoints available at http://localhost:5001
═══════════════════════════════════════════════
""")
# Initialize Gemini client
if not initialize_gemini():
print("⚠️ Warning: Gemini client not initialized. Some features may not work.")
print("💡 Make sure your GEMINI_API_KEY is set in the .env file")
# Start the Flask server
port = int(os.environ.get('PORT', 5001))
host = os.environ.get('HOST', '127.0.0.1')
print(f"🌐 API server starting on http://{host}:{port}")
print(f"📋 Available endpoints:")
print(f" GET http://{host}:{port}/api/health")
print(f" POST http://{host}:{port}/api/analyze")
print(f" GET http://{host}:{port}/api/examples")
print("🔗 React frontend should connect to this API")
print("=" * 50)
app.run(
host=host,
port=port,
debug=True,
threaded=True
)