abdullahrehan commited on
Commit
cabb3f4
Β·
verified Β·
1 Parent(s): 7bb24d2

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -0
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")