Afeefa123 commited on
Commit
35c501d
·
verified ·
1 Parent(s): 562f59a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -13
app.py CHANGED
@@ -2,17 +2,27 @@ import os
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
@@ -25,21 +35,27 @@ 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()
 
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()