smart_code_review/
│
├── app.py
├── templates/
│ ├── index.html
│ └── result.html
└── uploads/
app.py
import os
from flask import Flask, render_template, request
from pylint import epylint as lint
import openai
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'uploads'
os.makedirs(app.config['UPLOAD_FOLDER'], exist_ok=True)
# 🔑 Set your OpenAI API key
openai.api_key = "YOUR_OPENAI_API_KEY"
def analyze_with_pylint(file_path):
"""Run pylint analysis and return report."""
(pylint_stdout, _) = lint.py_run(file_path + " --disable=R,C", return_std=True)
return pylint_stdout.getvalue()
def analyze_with_ai(code):
"""Send code to OpenAI for smart suggestions."""
prompt = f"""
You are a senior Python reviewer.
Review the following code and provide:
1. Code quality feedback
2. Security issues
3. Performance suggestions
4. Refactoring tips
Code:
{code}
"""
response = openai.ChatCompletion.create(
model="gpt-4o-mini", # or 'gpt-4' if available
messages=[{"role": "user", "content": prompt}],
max_tokens=600,
temperature=0.4,
)
return response.choices[0].message["content"]
@app.route("/", methods=["GET", "POST"])
def index():
if request.method == "POST":
if 'file' not in request.files:
return "No file uploaded"
file = request.files['file']
if file.filename == "":
return "No selected file"
file_path = os.path.join(app.config['UPLOAD_FOLDER'], file.filename)
file.save(file_path)
with open(file_path, "r", encoding="utf-8") as f:
code_content = f.read()
pylint_result = analyze_with_pylint(file_path)
ai_feedback = analyze_with_ai(code_content)
return render_template("result.html",
ai_feedback=ai_feedback,
pylint_result=pylint_result)
return render_template("index.html")
if __name__ == "__main__":
app.run(debug=True)
templates/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Smart Code Review Assistant</title>
<style>
body { font-family: Arial; margin: 50px; background-color: #f9f9f9; }
h1 { color: #333; }
.upload-box { background: white; padding: 30px; border-radius: 10px; width: 400px; }
input[type=file] { margin: 20px 0; }
button { padding: 10px 20px; background: #4CAF50; color: white; border: none; border-radius: 5px; }
</style>
</head>
<body>
<h1>🤖 Smart Code Review Assistant</h1>
<div class="upload-box">
<form method="POST" enctype="multipart/form-data">
<label>Upload your Python file (.py):</label><br>
<input type="file" name="file" accept=".py" required><br>
<button type="submit">Analyze Code</button>
</form>
</div>
</body>
</html>
templates/result.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Review Result</title>
<style>
body { font-family: Arial; margin: 40px; background-color: #f4f4f4; }
pre { background: white; padding: 20px; border-radius: 8px; overflow-x: auto; }
h2 { color: #444; }
</style>
</head>
<body>
<h1>✅ Code Review Results</h1>
<h2>🧠AI Feedback</h2>
<pre>{{ ai_feedback }}</pre>
<h2>🧩 Pylint Static Analysis</h2>
<pre>{{ pylint_result }}</pre>
<a href="/">⬅ Back</a>
</body>
</html>
No comments:
Post a Comment