File size: 2,269 Bytes
20740ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83

# 2️⃣ SET API KEY (SECRET STYLE)
# πŸ‘‰ apni API key yahan paste karo
# =====================================
import os
os.environ["GROQ_API_KEY"] = "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"

# =====================================
# 3️⃣ IMPORTS
# =====================================
import gradio as gr
from groq import Groq

# =====================================
# 4️⃣ LOAD API KEY FROM ENV
# =====================================
API_KEY = os.environ.get("GROQ_API_KEY")

# =====================================
# 5️⃣ CORE FUNCTION
# =====================================
def codegenie_chat(user_prompt):
    if not API_KEY:
        return "❌ GROQ_API_KEY not found."

    if not user_prompt or user_prompt.strip() == "":
        return "❌ Please enter a programming request."

    try:
        client = Groq(api_key=API_KEY)

        system_prompt = """
You are CodeGenie – an AI Programming Assistant.

Capabilities:
- Automatic programming language detection
- Multi-language code generation
- Basic programs and DSA support
- Simple code explanation
- Clean and readable output
"""

        response = client.chat.completions.create(
            model="llama-3.3-70b-versatile",
            messages=[
                {"role": "system", "content": system_prompt},
                {"role": "user", "content": user_prompt}
            ],
            temperature=0.3,
        )

        return response.choices[0].message.content

    except Exception as e:
        return f"❌ Error:\n{str(e)}"

# =====================================
# 6️⃣ GRADIO UI
# =====================================
with gr.Blocks(theme=gr.themes.Soft()) as app:
    gr.Markdown("""
    # πŸ€– CodeGenie – AI Programming Assistant  
    *Code β€’ DSA β€’ Explanation β€’ Multi-language*  
    Google Colab Version πŸš€
    """)

    user_input = gr.Textbox(
        label="πŸ’» Enter your programming request",
        placeholder="e.g. Write Python code for stack using array with explanation",
        lines=4
    )

    generate_btn = gr.Button("πŸš€ Generate Code")

    output_box = gr.Markdown()

    generate_btn.click(
        fn=codegenie_chat,
        inputs=user_input,
        outputs=output_box
    )

app.launch(share=True)