File size: 2,011 Bytes
cabb3f4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import os
from groq import Groq
from dotenv import load_dotenv

# Load GROQ API key
load_dotenv()
GROQ_API_KEY = "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"

# Init client
client = Groq(api_key=GROQ_API_KEY)

# App UI Setup
st.set_page_config(page_title="🧠 Code Snippet Generator", page_icon="πŸ’‘", layout="wide")

# Custom CSS for style
st.markdown("""
    <style>
    .main {
        background-color: #1e1e2f;
        color: #f2f2f2;
        font-family: 'Segoe UI', sans-serif;
    }
    .stTextArea textarea {
        background-color: #2e2e3f;
        color: white;
    }
    .stButton button {
        background-color: #4CAF50;
        color: white;
    }
    </style>
""", unsafe_allow_html=True)

# Sidebar
st.sidebar.image("https://cdn-icons-png.flaticon.com/512/911/911408.png", width=100)
st.sidebar.title("Quick Code Gen πŸ’»")
st.sidebar.markdown("Turn natural language into code + dry run explanation!")

# Main interface
st.title("⚑ Instant Code Snippet Generator")
st.subheader("Just describe what you want...")

prompt = st.text_area("πŸ’¬ Describe your code idea:", height=150, placeholder="e.g., Write a Python function to reverse a string")

if st.button("πŸš€ Generate Code"):
    if prompt.strip() == "":
        st.warning("Please enter something first.")
    else:
        full_prompt = f"""
You are an expert programmer. Based on the input below, do two things:
1. Generate the full code snippet in Python
2. Then provide a step-by-step dry run explaining how the code works.

Input: {prompt}
"""

        with st.spinner("Generating your code..."):
            response = client.chat.completions.create(
                messages=[
                    {"role": "user", "content": full_prompt}
                ],
                model="llama-3.3-70b-versatile",
            )
            output = response.choices[0].message.content
            st.markdown("### 🧩 Generated Code + Dry Run:")
            st.code(output, language="python")