Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import os
|
| 3 |
+
from groq import Groq
|
| 4 |
+
from dotenv import load_dotenv
|
| 5 |
+
|
| 6 |
+
# Load GROQ API key
|
| 7 |
+
load_dotenv()
|
| 8 |
+
GROQ_API_KEY = "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"
|
| 9 |
+
|
| 10 |
+
# Init client
|
| 11 |
+
client = Groq(api_key=GROQ_API_KEY)
|
| 12 |
+
|
| 13 |
+
# App UI Setup
|
| 14 |
+
st.set_page_config(page_title="π§ Code Snippet Generator", page_icon="π‘", layout="wide")
|
| 15 |
+
|
| 16 |
+
# Custom CSS for style
|
| 17 |
+
st.markdown("""
|
| 18 |
+
<style>
|
| 19 |
+
.main {
|
| 20 |
+
background-color: #1e1e2f;
|
| 21 |
+
color: #f2f2f2;
|
| 22 |
+
font-family: 'Segoe UI', sans-serif;
|
| 23 |
+
}
|
| 24 |
+
.stTextArea textarea {
|
| 25 |
+
background-color: #2e2e3f;
|
| 26 |
+
color: white;
|
| 27 |
+
}
|
| 28 |
+
.stButton button {
|
| 29 |
+
background-color: #4CAF50;
|
| 30 |
+
color: white;
|
| 31 |
+
}
|
| 32 |
+
</style>
|
| 33 |
+
""", unsafe_allow_html=True)
|
| 34 |
+
|
| 35 |
+
# Sidebar
|
| 36 |
+
st.sidebar.image("https://cdn-icons-png.flaticon.com/512/911/911408.png", width=100)
|
| 37 |
+
st.sidebar.title("Quick Code Gen π»")
|
| 38 |
+
st.sidebar.markdown("Turn natural language into code + dry run explanation!")
|
| 39 |
+
|
| 40 |
+
# Main interface
|
| 41 |
+
st.title("β‘ Instant Code Snippet Generator")
|
| 42 |
+
st.subheader("Just describe what you want...")
|
| 43 |
+
|
| 44 |
+
prompt = st.text_area("π¬ Describe your code idea:", height=150, placeholder="e.g., Write a Python function to reverse a string")
|
| 45 |
+
|
| 46 |
+
if st.button("π Generate Code"):
|
| 47 |
+
if prompt.strip() == "":
|
| 48 |
+
st.warning("Please enter something first.")
|
| 49 |
+
else:
|
| 50 |
+
full_prompt = f"""
|
| 51 |
+
You are an expert programmer. Based on the input below, do two things:
|
| 52 |
+
1. Generate the full code snippet in Python
|
| 53 |
+
2. Then provide a step-by-step dry run explaining how the code works.
|
| 54 |
+
|
| 55 |
+
Input: {prompt}
|
| 56 |
+
"""
|
| 57 |
+
|
| 58 |
+
with st.spinner("Generating your code..."):
|
| 59 |
+
response = client.chat.completions.create(
|
| 60 |
+
messages=[
|
| 61 |
+
{"role": "user", "content": full_prompt}
|
| 62 |
+
],
|
| 63 |
+
model="llama-3.3-70b-versatile",
|
| 64 |
+
)
|
| 65 |
+
output = response.choices[0].message.content
|
| 66 |
+
st.markdown("### π§© Generated Code + Dry Run:")
|
| 67 |
+
st.code(output, language="python")
|