| | from groq import Groq |
| | import gradio as gr |
| |
|
| | |
| | GROQ_API_KEY = "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" |
| |
|
| | |
| | client = Groq(api_key=GROQ_API_KEY) |
| |
|
| | |
| | def analyze_code(user_code): |
| | try: |
| | chat_completion = client.chat.completions.create( |
| | messages=[ |
| | { |
| | "role": "user", |
| | "content": f""" |
| | You are an expert software security and code mentor. |
| | |
| | 1. Carefully analyze the following code for possible bugs, vulnerabilities, or bad practices. |
| | 2. Suggest an improved, secure, and efficient patch for the code. |
| | 3. Explain in simple words (like teaching a beginner) what was wrong and why your patch fixes it. |
| | 4. Provide 2-3 general tips for writing better and safer code. |
| | |
| | Here is the code to analyze: |
| | |
| | {user_code} |
| | """ |
| | } |
| | ], |
| | model="llama-3.3-70b-versatile" |
| | ) |
| | return chat_completion.choices[0].message.content |
| | except Exception as e: |
| | return f"Error: {e}" |
| |
|
| | |
| | demo = gr.Interface( |
| | fn=analyze_code, |
| | inputs=gr.Textbox(lines=12, placeholder="Paste your code here..."), |
| | outputs="text", |
| | title="AI Code Bug Finder & Mentor", |
| | description="Paste your code and get bug detection, secure patch suggestions, and beginner-friendly explanations using the LLaMA-3 model powered by Groq." |
| | ) |
| |
|
| | if __name__ == "__main__": |
| | demo.launch() |
| |
|