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
|
| 6 |
+
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 7 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
def review_code(code, language):
|
| 11 |
+
if code.strip() == "":
|
| 12 |
+
return "Please paste some code to review."
|
| 13 |
+
|
| 14 |
+
prompt = f"""
|
| 15 |
+
You are an expert senior software engineer performing a professional code review.
|
| 16 |
+
|
| 17 |
+
Analyze the following {language} code based on:
|
| 18 |
+
|
| 19 |
+
1. Readability
|
| 20 |
+
2. Structure and modularity
|
| 21 |
+
3. Maintainability
|
| 22 |
+
4. Best practices
|
| 23 |
+
5. Potential bugs
|
| 24 |
+
6. Performance improvements
|
| 25 |
+
|
| 26 |
+
Return the response in the following format:
|
| 27 |
+
|
| 28 |
+
Code Quality Scores (0-10):
|
| 29 |
+
Readability:
|
| 30 |
+
Structure:
|
| 31 |
+
Maintainability:
|
| 32 |
+
Best Practices:
|
| 33 |
+
|
| 34 |
+
Issues Found:
|
| 35 |
+
- bullet points
|
| 36 |
+
|
| 37 |
+
Suggested Improvements:
|
| 38 |
+
- bullet points
|
| 39 |
+
|
| 40 |
+
Refactored Code:
|
| 41 |
+
Provide improved version of the code.
|
| 42 |
+
|
| 43 |
+
Code to review:
|
| 44 |
+
{code}
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
try:
|
| 48 |
+
completion = client.chat.completions.create(
|
| 49 |
+
model="llama3-70b-8192",
|
| 50 |
+
messages=[
|
| 51 |
+
{"role": "system", "content": "You are a helpful code review assistant."},
|
| 52 |
+
{"role": "user", "content": prompt},
|
| 53 |
+
],
|
| 54 |
+
temperature=0.2,
|
| 55 |
+
)
|
| 56 |
+
|
| 57 |
+
return completion.choices[0].message.content
|
| 58 |
+
|
| 59 |
+
except Exception as e:
|
| 60 |
+
return f"Error occurred: {str(e)}"
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def explain_code(code):
|
| 64 |
+
if code.strip() == "":
|
| 65 |
+
return "Please paste some code."
|
| 66 |
+
|
| 67 |
+
prompt = f"""
|
| 68 |
+
Explain the following code in a simple and clear way.
|
| 69 |
+
Describe what the code does step-by-step.
|
| 70 |
+
|
| 71 |
+
Code:
|
| 72 |
+
{code}
|
| 73 |
+
"""
|
| 74 |
+
|
| 75 |
+
try:
|
| 76 |
+
completion = client.chat.completions.create(
|
| 77 |
+
model="llama3-70b-8192",
|
| 78 |
+
messages=[
|
| 79 |
+
{"role": "user", "content": prompt}],
|
| 80 |
+
temperature=0.3,
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
return completion.choices[0].message.content
|
| 84 |
+
|
| 85 |
+
except Exception as e:
|
| 86 |
+
return f"Error occurred: {str(e)}"
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
with gr.Blocks(title="Smart Code Reviewer") as demo:
|
| 90 |
+
|
| 91 |
+
gr.Markdown("# 🤖 Smart Code Reviewer")
|
| 92 |
+
gr.Markdown(
|
| 93 |
+
"AI-powered assistant that reviews your code for readability, structure, maintainability, and best practices."
|
| 94 |
+
)
|
| 95 |
+
|
| 96 |
+
with gr.Row():
|
| 97 |
+
|
| 98 |
+
with gr.Column():
|
| 99 |
+
|
| 100 |
+
language = gr.Dropdown(
|
| 101 |
+
["Python", "JavaScript", "Java", "C++", "Go", "Other"],
|
| 102 |
+
value="Python",
|
| 103 |
+
label="Programming Language"
|
| 104 |
+
)
|
| 105 |
+
|
| 106 |
+
code_input = gr.Code(
|
| 107 |
+
label="Paste Your Code",
|
| 108 |
+
language="python",
|
| 109 |
+
lines=20
|
| 110 |
+
)
|
| 111 |
+
|
| 112 |
+
review_btn = gr.Button("🔍 Review Code", variant="primary")
|
| 113 |
+
explain_btn = gr.Button("📖 Explain Code")
|
| 114 |
+
|
| 115 |
+
with gr.Column():
|
| 116 |
+
|
| 117 |
+
output = gr.Markdown(label="AI Review Output")
|
| 118 |
+
|
| 119 |
+
review_btn.click(
|
| 120 |
+
fn=review_code,
|
| 121 |
+
inputs=[code_input, language],
|
| 122 |
+
outputs=output
|
| 123 |
+
)
|
| 124 |
+
|
| 125 |
+
explain_btn.click(
|
| 126 |
+
fn=explain_code,
|
| 127 |
+
inputs=code_input,
|
| 128 |
+
outputs=output
|
| 129 |
+
)
|
| 130 |
+
|
| 131 |
+
gr.Markdown(
|
| 132 |
+
"""
|
| 133 |
+
---
|
| 134 |
+
### Features
|
| 135 |
+
- AI-powered code review
|
| 136 |
+
- Code quality scoring
|
| 137 |
+
- Bug detection
|
| 138 |
+
- Refactoring suggestions
|
| 139 |
+
- Code explanation
|
| 140 |
+
|
| 141 |
+
Built with **Groq + Llama3 + Gradio**
|
| 142 |
+
"""
|
| 143 |
+
)
|
| 144 |
+
|
| 145 |
+
|
| 146 |
+
if __name__ == "__main__":
|
| 147 |
+
demo.launch()
|