Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,17 +2,27 @@ import os
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
-
#
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
def review_code(code):
|
| 9 |
if not code.strip():
|
| 10 |
return "Please paste some code."
|
| 11 |
|
|
|
|
|
|
|
|
|
|
| 12 |
prompt = f"""
|
| 13 |
-
You are
|
| 14 |
|
| 15 |
-
|
| 16 |
|
| 17 |
1. Readability
|
| 18 |
2. Structure
|
|
@@ -25,21 +35,27 @@ Code:
|
|
| 25 |
{code}
|
| 26 |
"""
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
|
| 34 |
-
|
|
|
|
| 35 |
|
| 36 |
|
| 37 |
-
|
| 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 |
-
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from groq import Groq
|
| 4 |
|
| 5 |
+
# Get API key from Hugging Face Secrets
|
| 6 |
+
api_key = os.environ.get("GROQ_API_KEY")
|
| 7 |
+
|
| 8 |
+
# Initialize client only if key exists
|
| 9 |
+
if api_key:
|
| 10 |
+
client = Groq(api_key=api_key)
|
| 11 |
+
else:
|
| 12 |
+
client = None
|
| 13 |
+
|
| 14 |
|
| 15 |
def review_code(code):
|
| 16 |
if not code.strip():
|
| 17 |
return "Please paste some code."
|
| 18 |
|
| 19 |
+
if client is None:
|
| 20 |
+
return "Groq API key not found. Please add GROQ_API_KEY in Hugging Face Space Secrets."
|
| 21 |
+
|
| 22 |
prompt = f"""
|
| 23 |
+
You are an expert software engineer acting as a code reviewer.
|
| 24 |
|
| 25 |
+
Analyze the following code and give feedback on:
|
| 26 |
|
| 27 |
1. Readability
|
| 28 |
2. Structure
|
|
|
|
| 35 |
{code}
|
| 36 |
"""
|
| 37 |
|
| 38 |
+
try:
|
| 39 |
+
completion = client.chat.completions.create(
|
| 40 |
+
model="llama3-8b-8192",
|
| 41 |
+
messages=[
|
| 42 |
+
{"role": "user", "content": prompt}
|
| 43 |
+
],
|
| 44 |
+
temperature=0.2
|
| 45 |
+
)
|
| 46 |
+
|
| 47 |
+
return completion.choices[0].message.content
|
| 48 |
|
| 49 |
+
except Exception as e:
|
| 50 |
+
return f"Error while reviewing code: {str(e)}"
|
| 51 |
|
| 52 |
|
| 53 |
+
demo = gr.Interface(
|
| 54 |
fn=review_code,
|
| 55 |
inputs=gr.Code(label="Paste your code here", language="python"),
|
| 56 |
outputs=gr.Markdown(label="AI Code Review"),
|
| 57 |
title="Smart Code Reviewer",
|
| 58 |
+
description="AI assistant that reviews code for readability, structure, maintainability, and best practices."
|
| 59 |
)
|
| 60 |
|
| 61 |
+
demo.launch()
|