Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from groq import Groq
|
| 2 |
+
import gradio as gr
|
| 3 |
+
|
| 4 |
+
# Groq API key (hardcoded as requested)
|
| 5 |
+
GROQ_API_KEY = "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"
|
| 6 |
+
|
| 7 |
+
# Initialize Groq client
|
| 8 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 9 |
+
|
| 10 |
+
# Function to analyze code for bugs and suggest patches
|
| 11 |
+
def analyze_code(user_code):
|
| 12 |
+
try:
|
| 13 |
+
chat_completion = client.chat.completions.create(
|
| 14 |
+
messages=[
|
| 15 |
+
{
|
| 16 |
+
"role": "user",
|
| 17 |
+
"content": f"""
|
| 18 |
+
You are an expert software security and code mentor.
|
| 19 |
+
|
| 20 |
+
1. Carefully analyze the following code for possible bugs, vulnerabilities, or bad practices.
|
| 21 |
+
2. Suggest an improved, secure, and efficient patch for the code.
|
| 22 |
+
3. Explain in simple words (like teaching a beginner) what was wrong and why your patch fixes it.
|
| 23 |
+
4. Provide 2-3 general tips for writing better and safer code.
|
| 24 |
+
|
| 25 |
+
Here is the code to analyze:
|
| 26 |
+
|
| 27 |
+
{user_code}
|
| 28 |
+
"""
|
| 29 |
+
}
|
| 30 |
+
],
|
| 31 |
+
model="llama-3.3-70b-versatile"
|
| 32 |
+
)
|
| 33 |
+
return chat_completion.choices[0].message.content
|
| 34 |
+
except Exception as e:
|
| 35 |
+
return f"Error: {e}"
|
| 36 |
+
|
| 37 |
+
# Gradio UI
|
| 38 |
+
demo = gr.Interface(
|
| 39 |
+
fn=analyze_code,
|
| 40 |
+
inputs=gr.Textbox(lines=12, placeholder="Paste your code here..."),
|
| 41 |
+
outputs="text",
|
| 42 |
+
title="AI Code Bug Finder & Mentor",
|
| 43 |
+
description="Paste your code and get bug detection, secure patch suggestions, and beginner-friendly explanations using the LLaMA-3 model powered by Groq."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
demo.launch()
|