Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,81 +2,44 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
|
|
|
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
client = Groq(api_key=GROQ_API_KEY)
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
def review_code(code, language):
|
| 14 |
-
|
| 15 |
-
if code.strip() == "":
|
| 16 |
return "Please paste some code."
|
| 17 |
|
| 18 |
prompt = f"""
|
| 19 |
-
You are a senior software engineer
|
| 20 |
|
| 21 |
-
|
| 22 |
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
-
|
| 26 |
-
Readability
|
| 27 |
-
Structure
|
| 28 |
-
Maintainability
|
| 29 |
-
Best Practices
|
| 30 |
-
|
| 31 |
-
Issues Found
|
| 32 |
-
Suggested Improvements
|
| 33 |
-
Refactored Code
|
| 34 |
|
| 35 |
Code:
|
| 36 |
{code}
|
| 37 |
"""
|
| 38 |
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
{"role": "user", "content": prompt}
|
| 44 |
-
],
|
| 45 |
-
temperature=0.2,
|
| 46 |
-
)
|
| 47 |
-
|
| 48 |
-
return completion.choices[0].message.content
|
| 49 |
-
|
| 50 |
-
except Exception as e:
|
| 51 |
-
return str(e)
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
with gr.Blocks(title="Smart Code Reviewer") as demo:
|
| 55 |
-
|
| 56 |
-
gr.Markdown("# Smart Code Reviewer")
|
| 57 |
-
gr.Markdown("AI assistant that reviews code before human review.")
|
| 58 |
-
|
| 59 |
-
language = gr.Dropdown(
|
| 60 |
-
["Python", "JavaScript", "Java", "C++", "Other"],
|
| 61 |
-
value="Python",
|
| 62 |
-
label="Programming Language"
|
| 63 |
)
|
| 64 |
|
| 65 |
-
|
| 66 |
-
label="Paste Code",
|
| 67 |
-
lines=20
|
| 68 |
-
)
|
| 69 |
|
| 70 |
-
review_btn = gr.Button("Review Code")
|
| 71 |
-
|
| 72 |
-
output = gr.Markdown()
|
| 73 |
-
|
| 74 |
-
review_btn.click(
|
| 75 |
-
fn=review_code,
|
| 76 |
-
inputs=[code_input, language],
|
| 77 |
-
outputs=output
|
| 78 |
-
)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
|
| 82 |
-
demo.launch(server_name="0.0.0.0")
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# Initialize Groq client
|
| 6 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
| 7 |
|
| 8 |
+
def review_code(code):
|
| 9 |
+
if not code.strip():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
return "Please paste some code."
|
| 11 |
|
| 12 |
prompt = f"""
|
| 13 |
+
You are a senior software engineer acting as a code reviewer.
|
| 14 |
|
| 15 |
+
Review the following code and provide feedback in these categories:
|
| 16 |
|
| 17 |
+
1. Readability
|
| 18 |
+
2. Structure
|
| 19 |
+
3. Maintainability
|
| 20 |
+
4. Best Practices
|
| 21 |
|
| 22 |
+
Also suggest an improved version of the code.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
Code:
|
| 25 |
{code}
|
| 26 |
"""
|
| 27 |
|
| 28 |
+
completion = client.chat.completions.create(
|
| 29 |
+
model="llama3-8b-8192",
|
| 30 |
+
messages=[{"role": "user", "content": prompt}],
|
| 31 |
+
temperature=0.2,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
)
|
| 33 |
|
| 34 |
+
return completion.choices[0].message.content
|
|
|
|
|
|
|
|
|
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
interface = gr.Interface(
|
| 38 |
+
fn=review_code,
|
| 39 |
+
inputs=gr.Code(label="Paste your code here", language="python"),
|
| 40 |
+
outputs=gr.Markdown(label="AI Code Review"),
|
| 41 |
+
title="Smart Code Reviewer",
|
| 42 |
+
description="AI assistant that reviews code for readability, structure, maintainability, and best practices.",
|
| 43 |
+
)
|
| 44 |
|
| 45 |
+
interface.launch()
|
|
|