Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,54 +2,45 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
# Initialize Groq Client
|
| 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
|
| 13 |
|
| 14 |
prompt = f"""
|
| 15 |
-
You are
|
| 16 |
-
|
| 17 |
-
Analyze the following {language} code based on:
|
| 18 |
|
| 19 |
-
|
| 20 |
-
2. Structure and modularity
|
| 21 |
-
3. Maintainability
|
| 22 |
-
4. Best practices
|
| 23 |
-
5. Potential bugs
|
| 24 |
-
6. Performance improvements
|
| 25 |
|
| 26 |
-
Return
|
| 27 |
|
| 28 |
-
Code Quality Scores (0-10)
|
| 29 |
-
Readability
|
| 30 |
-
Structure
|
| 31 |
-
Maintainability
|
| 32 |
-
Best Practices
|
| 33 |
|
| 34 |
-
Issues Found
|
| 35 |
-
|
|
|
|
| 36 |
|
| 37 |
-
|
| 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-
|
| 50 |
messages=[
|
| 51 |
-
{"role": "
|
| 52 |
-
{"role": "user", "content": prompt},
|
| 53 |
],
|
| 54 |
temperature=0.2,
|
| 55 |
)
|
|
@@ -57,64 +48,28 @@ Code to review:
|
|
| 57 |
return completion.choices[0].message.content
|
| 58 |
|
| 59 |
except Exception as e:
|
| 60 |
-
return
|
| 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("#
|
| 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 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
code_input = gr.Code(
|
| 107 |
-
label="Paste Your Code",
|
| 108 |
-
language="python",
|
| 109 |
-
lines=20
|
| 110 |
-
)
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
|
|
|
|
|
|
| 114 |
|
| 115 |
-
|
| 116 |
|
| 117 |
-
|
| 118 |
|
| 119 |
review_btn.click(
|
| 120 |
fn=review_code,
|
|
@@ -122,26 +77,6 @@ with gr.Blocks(title="Smart Code Reviewer") as demo:
|
|
| 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()
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
|
|
|
| 5 |
GROQ_API_KEY = os.getenv("GROQ_API_KEY")
|
| 6 |
+
|
| 7 |
+
if not GROQ_API_KEY:
|
| 8 |
+
raise ValueError("Please set GROQ_API_KEY in Hugging Face Secrets")
|
| 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 performing a professional code review.
|
|
|
|
|
|
|
| 20 |
|
| 21 |
+
Analyze the following {language} code.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
Return:
|
| 24 |
|
| 25 |
+
Code Quality Scores (0-10)
|
| 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 |
try:
|
| 40 |
completion = client.chat.completions.create(
|
| 41 |
+
model="llama3-8b-8192",
|
| 42 |
messages=[
|
| 43 |
+
{"role": "user", "content": prompt}
|
|
|
|
| 44 |
],
|
| 45 |
temperature=0.2,
|
| 46 |
)
|
|
|
|
| 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 |
+
code_input = gr.Textbox(
|
| 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,
|
|
|
|
| 77 |
outputs=output
|
| 78 |
)
|
| 79 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
+
demo.launch(server_name="0.0.0.0")
|