Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
# 2οΈβ£ SET API KEY (SECRET STYLE)
|
| 3 |
+
# π apni API key yahan paste karo
|
| 4 |
+
# =====================================
|
| 5 |
+
import os
|
| 6 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 7 |
+
|
| 8 |
+
# =====================================
|
| 9 |
+
# 3οΈβ£ IMPORTS
|
| 10 |
+
# =====================================
|
| 11 |
+
import gradio as gr
|
| 12 |
+
from groq import Groq
|
| 13 |
+
|
| 14 |
+
# =====================================
|
| 15 |
+
# 4οΈβ£ LOAD API KEY FROM ENV
|
| 16 |
+
# =====================================
|
| 17 |
+
API_KEY = os.environ.get("GROQ_API_KEY")
|
| 18 |
+
|
| 19 |
+
# =====================================
|
| 20 |
+
# 5οΈβ£ CORE FUNCTION
|
| 21 |
+
# =====================================
|
| 22 |
+
def codegenie_chat(user_prompt):
|
| 23 |
+
if not API_KEY:
|
| 24 |
+
return "β GROQ_API_KEY not found."
|
| 25 |
+
|
| 26 |
+
if not user_prompt or user_prompt.strip() == "":
|
| 27 |
+
return "β Please enter a programming request."
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
client = Groq(api_key=API_KEY)
|
| 31 |
+
|
| 32 |
+
system_prompt = """
|
| 33 |
+
You are CodeGenie β an AI Programming Assistant.
|
| 34 |
+
|
| 35 |
+
Capabilities:
|
| 36 |
+
- Automatic programming language detection
|
| 37 |
+
- Multi-language code generation
|
| 38 |
+
- Basic programs and DSA support
|
| 39 |
+
- Simple code explanation
|
| 40 |
+
- Clean and readable output
|
| 41 |
+
"""
|
| 42 |
+
|
| 43 |
+
response = client.chat.completions.create(
|
| 44 |
+
model="llama-3.3-70b-versatile",
|
| 45 |
+
messages=[
|
| 46 |
+
{"role": "system", "content": system_prompt},
|
| 47 |
+
{"role": "user", "content": user_prompt}
|
| 48 |
+
],
|
| 49 |
+
temperature=0.3,
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
return response.choices[0].message.content
|
| 53 |
+
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return f"β Error:\n{str(e)}"
|
| 56 |
+
|
| 57 |
+
# =====================================
|
| 58 |
+
# 6οΈβ£ GRADIO UI
|
| 59 |
+
# =====================================
|
| 60 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 61 |
+
gr.Markdown("""
|
| 62 |
+
# π€ CodeGenie β AI Programming Assistant
|
| 63 |
+
*Code β’ DSA β’ Explanation β’ Multi-language*
|
| 64 |
+
Google Colab Version π
|
| 65 |
+
""")
|
| 66 |
+
|
| 67 |
+
user_input = gr.Textbox(
|
| 68 |
+
label="π» Enter your programming request",
|
| 69 |
+
placeholder="e.g. Write Python code for stack using array with explanation",
|
| 70 |
+
lines=4
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
generate_btn = gr.Button("π Generate Code")
|
| 74 |
+
|
| 75 |
+
output_box = gr.Markdown()
|
| 76 |
+
|
| 77 |
+
generate_btn.click(
|
| 78 |
+
fn=codegenie_chat,
|
| 79 |
+
inputs=user_input,
|
| 80 |
+
outputs=output_box
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
app.launch(share=True)
|