File size: 1,416 Bytes
e0e8e53
 
 
 
35c501d
 
 
 
 
 
 
 
 
92b895e
4e22759
 
92b895e
e0e8e53
35c501d
 
 
e0e8e53
35c501d
e0e8e53
35c501d
e0e8e53
4e22759
 
 
 
e0e8e53
4e22759
e0e8e53
92b895e
e0e8e53
 
 
35c501d
 
0fd8a52
35c501d
 
 
 
 
 
 
e0e8e53
35c501d
 
e0e8e53
 
35c501d
4e22759
 
 
 
35c501d
4e22759
e0e8e53
35c501d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import os
import gradio as gr
from groq import Groq

# Get API key from Hugging Face Secrets
api_key = os.environ.get("GROQ_API_KEY")

# Initialize client only if key exists
if api_key:
    client = Groq(api_key=api_key)
else:
    client = None


def review_code(code):
    if not code.strip():
        return "Please paste some code."

    if client is None:
        return "Groq API key not found. Please add GROQ_API_KEY in Hugging Face Space Secrets."

    prompt = f"""
You are an expert software engineer acting as a code reviewer.

Analyze the following code and give feedback on:

1. Readability
2. Structure
3. Maintainability
4. Best Practices

Also suggest an improved version of the code.

Code:
{code}
"""

    try:
        completion = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[
                {"role": "user", "content": prompt}
            ],
            temperature=0.2
        )

        return completion.choices[0].message.content

    except Exception as e:
        return f"Error while reviewing code: {str(e)}"


demo = gr.Interface(
    fn=review_code,
    inputs=gr.Code(label="Paste your code here", language="python"),
    outputs=gr.Markdown(label="AI Code Review"),
    title="Smart Code Reviewer",
    description="AI assistant that reviews code for readability, structure, maintainability, and best practices."
)

demo.launch()