Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,641 +1,80 @@
|
|
1 |
-
import os
|
2 |
import streamlit as st
|
|
|
3 |
from groq import Groq
|
4 |
-
from streamlit_tags import st_tags
|
5 |
-
import time
|
6 |
-
from typing import Optional
|
7 |
|
8 |
-
#
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
THEMES = ["Neon", "Cyberpunk", "Solarized", "Dracula", "Monokai", "Nord", "Ocean", "Matrix"]
|
13 |
-
COMPLEXITY_LEVELS = {
|
14 |
-
"Basic": {
|
15 |
-
"description": "Simple implementation with minimal features",
|
16 |
-
"icon": "π±",
|
17 |
-
"temperature": 0.3
|
18 |
-
},
|
19 |
-
"Medium": {
|
20 |
-
"description": "Well-structured code with comments and basic error handling",
|
21 |
-
"icon": "π",
|
22 |
-
"temperature": 0.5
|
23 |
-
},
|
24 |
-
"Advanced": {
|
25 |
-
"description": "Production-ready with tests, documentation, and robust error handling",
|
26 |
-
"icon": "π",
|
27 |
-
"temperature": 0.7
|
28 |
-
},
|
29 |
-
"Expert": {
|
30 |
-
"description": "Optimized solution with advanced patterns, benchmarks, and scalability",
|
31 |
-
"icon": "π§ ",
|
32 |
-
"temperature": 0.9
|
33 |
-
}
|
34 |
-
}
|
35 |
|
36 |
-
#
|
37 |
-
st.
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
)
|
|
|
|
|
43 |
|
44 |
-
#
|
45 |
-
def
|
46 |
-
|
47 |
-
|
48 |
-
"primary": "#4fffb0",
|
49 |
-
"secondary": "#ff4fd8",
|
50 |
-
"bg": "#1a1a2e",
|
51 |
-
"text": "#ffffff",
|
52 |
-
"accent": "#00ff88",
|
53 |
-
"gradient": "linear-gradient(135deg, #1a1a2e 0%, #16213e 100%)"
|
54 |
-
},
|
55 |
-
"Cyberpunk": {
|
56 |
-
"primary": "#ff2a6d",
|
57 |
-
"secondary": "#05d9e8",
|
58 |
-
"bg": "#1a1a2e",
|
59 |
-
"text": "#d1f7ff",
|
60 |
-
"accent": "#ff9a00",
|
61 |
-
"gradient": "linear-gradient(135deg, #1a1a2e 0%, #0d1b2a 100%)"
|
62 |
-
},
|
63 |
-
"Solarized": {
|
64 |
-
"primary": "#268bd2",
|
65 |
-
"secondary": "#d33682",
|
66 |
-
"bg": "#fdf6e3",
|
67 |
-
"text": "#073642",
|
68 |
-
"accent": "#cb4b16",
|
69 |
-
"gradient": "linear-gradient(135deg, #fdf6e3 0%, #eee8d5 100%)"
|
70 |
-
},
|
71 |
-
"Dracula": {
|
72 |
-
"primary": "#bd93f9",
|
73 |
-
"secondary": "#ff79c6",
|
74 |
-
"bg": "#282a36",
|
75 |
-
"text": "#f8f8f2",
|
76 |
-
"accent": "#50fa7b",
|
77 |
-
"gradient": "linear-gradient(135deg, #282a36 0%, #44475a 100%)"
|
78 |
-
},
|
79 |
-
"Monokai": {
|
80 |
-
"primary": "#a6e22e",
|
81 |
-
"secondary": "#fd971f",
|
82 |
-
"bg": "#272822",
|
83 |
-
"text": "#f8f8f2",
|
84 |
-
"accent": "#f92672",
|
85 |
-
"gradient": "linear-gradient(135deg, #272822 0%, #1e1f1c 100%)"
|
86 |
-
},
|
87 |
-
"Nord": {
|
88 |
-
"primary": "#81a1c1",
|
89 |
-
"secondary": "#d08770",
|
90 |
-
"bg": "#2e3440",
|
91 |
-
"text": "#d8dee9",
|
92 |
-
"accent": "#5e81ac",
|
93 |
-
"gradient": "linear-gradient(135deg, #2e3440 0%, #3b4252 100%)"
|
94 |
-
},
|
95 |
-
"Ocean": {
|
96 |
-
"primary": "#7fdbff",
|
97 |
-
"secondary": "#ff851b",
|
98 |
-
"bg": "#001f3f",
|
99 |
-
"text": "#ffffff",
|
100 |
-
"accent": "#2ecc40",
|
101 |
-
"gradient": "linear-gradient(135deg, #001f3f 0%, #0074d9 100%)"
|
102 |
-
},
|
103 |
-
"Matrix": {
|
104 |
-
"primary": "#00ff41",
|
105 |
-
"secondary": "#008f11",
|
106 |
-
"bg": "#000000",
|
107 |
-
"text": "#00ff41",
|
108 |
-
"accent": "#00ff41",
|
109 |
-
"gradient": "linear-gradient(135deg, #000000 0%, #003b00 100%)"
|
110 |
-
}
|
111 |
-
}
|
112 |
-
|
113 |
-
colors = theme_properties.get(theme, theme_properties["Neon"])
|
114 |
-
|
115 |
-
st.markdown(f"""
|
116 |
-
<style>
|
117 |
-
/* Base styles */
|
118 |
-
.main {{
|
119 |
-
background: {colors['gradient']} !important;
|
120 |
-
color: {colors['text']} !important;
|
121 |
-
}}
|
122 |
-
|
123 |
-
/* Input fields */
|
124 |
-
.stTextInput input, .stSelectbox select, .stTextArea textarea {{
|
125 |
-
color: {colors['primary']} !important;
|
126 |
-
background: rgba(0,0,0,0.2) !important;
|
127 |
-
border-radius: 12px !important;
|
128 |
-
border: 1px solid {colors['secondary']} !important;
|
129 |
-
padding: 10px 15px !important;
|
130 |
-
box-shadow: 0 2px 10px rgba(0,0,0,0.1) !important;
|
131 |
-
transition: all 0.3s ease !important;
|
132 |
-
}}
|
133 |
-
|
134 |
-
.stTextInput input:focus, .stSelectbox select:focus, .stTextArea textarea:focus {{
|
135 |
-
border-color: {colors['accent']} !important;
|
136 |
-
box-shadow: 0 0 0 2px {colors['accent']}33 !important;
|
137 |
-
}}
|
138 |
-
|
139 |
-
/* Buttons */
|
140 |
-
.stButton>button {{
|
141 |
-
background: rgba(0,0,0,0.3) !important;
|
142 |
-
border: 2px solid {colors['primary']} !important;
|
143 |
-
color: {colors['primary']} !important;
|
144 |
-
border-radius: 12px !important;
|
145 |
-
padding: 8px 16px !important;
|
146 |
-
transition: all 0.3s ease !important;
|
147 |
-
font-weight: 600 !important;
|
148 |
-
box-shadow: 0 4px 6px rgba(0,0,0,0.1) !important;
|
149 |
-
}}
|
150 |
-
|
151 |
-
.stButton>button:hover {{
|
152 |
-
background: {colors['primary']}22 !important;
|
153 |
-
transform: translateY(-2px) !important;
|
154 |
-
box-shadow: 0 6px 12px {colors['primary']}33 !important;
|
155 |
-
}}
|
156 |
-
|
157 |
-
/* Code blocks */
|
158 |
-
.code-block {{
|
159 |
-
border: 2px solid {colors['secondary']};
|
160 |
-
border-radius: 15px;
|
161 |
-
padding: 1rem;
|
162 |
-
background: rgba(0,0,0,0.3);
|
163 |
-
margin-bottom: 1rem;
|
164 |
-
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
|
165 |
-
}}
|
166 |
-
|
167 |
-
/* Sidebar */
|
168 |
-
.sidebar .sidebar-content {{
|
169 |
-
background: rgba(0,0,0,0.3) !important;
|
170 |
-
backdrop-filter: blur(10px) !important;
|
171 |
-
border-right: 1px solid {colors['secondary']}33 !important;
|
172 |
-
}}
|
173 |
-
|
174 |
-
/* Headers */
|
175 |
-
h1, h2, h3, h4, h5, h6 {{
|
176 |
-
color: {colors['primary']} !important;
|
177 |
-
text-shadow: 0 2px 4px rgba(0,0,0,0.3);
|
178 |
-
}}
|
179 |
-
|
180 |
-
/* Expander */
|
181 |
-
.st-expander {{
|
182 |
-
background: rgba(0,0,0,0.2) !important;
|
183 |
-
border: 1px solid {colors['secondary']} !important;
|
184 |
-
border-radius: 12px !important;
|
185 |
-
}}
|
186 |
-
|
187 |
-
.st-expander .st-expanderHeader {{
|
188 |
-
color: {colors['primary']} !important;
|
189 |
-
font-weight: 600 !important;
|
190 |
-
}}
|
191 |
-
|
192 |
-
/* Progress bar */
|
193 |
-
.stProgress > div > div > div {{
|
194 |
-
background: {colors['accent']} !important;
|
195 |
-
}}
|
196 |
-
|
197 |
-
/* Custom scrollbar */
|
198 |
-
::-webkit-scrollbar {{
|
199 |
-
width: 8px;
|
200 |
-
height: 8px;
|
201 |
-
}}
|
202 |
-
|
203 |
-
::-webkit-scrollbar-track {{
|
204 |
-
background: rgba(0,0,0,0.1);
|
205 |
-
}}
|
206 |
-
|
207 |
-
::-webkit-scrollbar-thumb {{
|
208 |
-
background: {colors['secondary']};
|
209 |
-
border-radius: 4px;
|
210 |
-
}}
|
211 |
-
|
212 |
-
::-webkit-scrollbar-thumb:hover {{
|
213 |
-
background: {colors['primary']};
|
214 |
-
}}
|
215 |
-
|
216 |
-
/* Custom cards */
|
217 |
-
.custom-card {{
|
218 |
-
background: rgba(0,0,0,0.2) !important;
|
219 |
-
border-radius: 16px !important;
|
220 |
-
padding: 20px !important;
|
221 |
-
border: 1px solid {colors['secondary']}33 !important;
|
222 |
-
box-shadow: 0 8px 16px rgba(0,0,0,0.1) !important;
|
223 |
-
margin-bottom: 20px !important;
|
224 |
-
}}
|
225 |
-
|
226 |
-
/* Tooltips */
|
227 |
-
.stTooltip {{
|
228 |
-
background: {colors['bg']} !important;
|
229 |
-
color: {colors['text']} !important;
|
230 |
-
border: 1px solid {colors['secondary']} !important;
|
231 |
-
border-radius: 8px !important;
|
232 |
-
}}
|
233 |
-
|
234 |
-
/* Tabs */
|
235 |
-
.stTabs [data-baseweb="tab-list"] {{
|
236 |
-
gap: 8px;
|
237 |
-
}}
|
238 |
-
|
239 |
-
.stTabs [data-baseweb="tab"] {{
|
240 |
-
background: rgba(0,0,0,0.2) !important;
|
241 |
-
border-radius: 12px !important;
|
242 |
-
padding: 8px 16px !important;
|
243 |
-
transition: all 0.3s ease !important;
|
244 |
-
}}
|
245 |
-
|
246 |
-
.stTabs [aria-selected="true"] {{
|
247 |
-
background: {colors['primary']}22 !important;
|
248 |
-
color: {colors['primary']} !important;
|
249 |
-
font-weight: 600 !important;
|
250 |
-
border: 1px solid {colors['primary']} !important;
|
251 |
-
}}
|
252 |
-
</style>
|
253 |
-
""", unsafe_allow_html=True)
|
254 |
|
255 |
-
|
256 |
-
|
257 |
-
|
258 |
-
if not api_key:
|
259 |
-
st.error("GROQ_API_KEY not found! Please set it in environment variables or secrets.")
|
260 |
-
return None
|
261 |
-
return Groq(api_key=api_key)
|
262 |
|
263 |
-
|
264 |
-
|
265 |
-
|
266 |
-
|
267 |
-
|
268 |
-
client: Groq,
|
269 |
-
complexity: str,
|
270 |
-
keywords: Optional[list] = None,
|
271 |
-
style: Optional[str] = None
|
272 |
-
) -> Optional[str]:
|
273 |
-
complexity_config = COMPLEXITY_LEVELS.get(complexity, COMPLEXITY_LEVELS["Medium"])
|
274 |
-
|
275 |
-
prompt = f"""
|
276 |
-
{complexity_config['description']} in {language}:
|
277 |
-
{query}
|
278 |
-
|
279 |
-
Requirements:
|
280 |
-
- Use {language} best practices
|
281 |
-
- Include appropriate comments
|
282 |
-
- Follow clean code principles
|
283 |
-
"""
|
284 |
-
|
285 |
-
if keywords:
|
286 |
-
prompt += f"\nKeywords to consider: {', '.join(keywords)}"
|
287 |
-
if style:
|
288 |
-
prompt += f"\nCoding style: {style}"
|
289 |
-
|
290 |
-
if complexity == "Expert":
|
291 |
-
prompt += """
|
292 |
-
Additional requirements:
|
293 |
-
- Include performance benchmarks if applicable
|
294 |
-
- Add scalability considerations
|
295 |
-
- Document trade-offs and alternatives
|
296 |
-
- Include unit tests
|
297 |
-
"""
|
298 |
-
|
299 |
-
prompt += """
|
300 |
-
IMPORTANT: Return ONLY the raw executable code with comments,
|
301 |
-
without any additional explanation before or after the code block.
|
302 |
-
"""
|
303 |
-
|
304 |
-
try:
|
305 |
-
completion = client.chat.completions.create(
|
306 |
-
model=model,
|
307 |
-
messages=[{
|
308 |
-
"role": "user",
|
309 |
-
"content": prompt
|
310 |
-
}],
|
311 |
-
temperature=complexity_config['temperature'],
|
312 |
-
max_tokens=4096,
|
313 |
-
top_p=0.95
|
314 |
-
)
|
315 |
-
|
316 |
-
# Extract just the code block if it's wrapped in markdown
|
317 |
-
raw_content = completion.choices[0].message.content
|
318 |
-
if '```' in raw_content:
|
319 |
-
code = raw_content.split('```')[1]
|
320 |
-
if code.startswith(language.lower()):
|
321 |
-
code = code[len(language.lower()):]
|
322 |
-
return code.strip()
|
323 |
-
return raw_content.strip()
|
324 |
-
except Exception as e:
|
325 |
-
st.error(f"Error generating code: {str(e)}")
|
326 |
-
return None
|
327 |
|
328 |
-
|
329 |
-
|
330 |
-
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
model=PRIMARY_MODEL,
|
335 |
-
messages=[{
|
336 |
-
"role": "user",
|
337 |
-
"content": f"""
|
338 |
-
Explain this {language} code in {complexity.lower()} terms:
|
339 |
-
{code}
|
340 |
-
|
341 |
-
Structure your explanation:
|
342 |
-
1. Overview of what the code does
|
343 |
-
2. Key components/functions
|
344 |
-
3. Flow of execution
|
345 |
-
4. {complexity_config['description']} considerations
|
346 |
-
"""
|
347 |
-
}],
|
348 |
-
temperature=0.3,
|
349 |
-
max_tokens=1024
|
350 |
-
)
|
351 |
-
return explanation.choices[0].message.content
|
352 |
-
except Exception as e:
|
353 |
-
st.error(f"Error generating explanation: {str(e)}")
|
354 |
-
return None
|
355 |
|
356 |
-
#
|
357 |
-
def
|
358 |
-
|
359 |
-
|
360 |
-
|
361 |
-
|
362 |
-
|
363 |
-
|
364 |
-
|
365 |
-
|
366 |
-
|
367 |
-
|
368 |
-
|
369 |
-
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
-
|
375 |
-
}],
|
376 |
-
temperature=0.5,
|
377 |
-
max_tokens=4096
|
378 |
-
)
|
379 |
-
|
380 |
-
raw_content = optimized.choices[0].message.content
|
381 |
-
if '```' in raw_content:
|
382 |
-
optimized_code = raw_content.split('```')[1]
|
383 |
-
if optimized_code.startswith(language.lower()):
|
384 |
-
optimized_code = optimized_code[len(language.lower()):]
|
385 |
-
return optimized_code.strip()
|
386 |
-
return raw_content.strip()
|
387 |
-
except Exception as e:
|
388 |
-
st.error(f"Error optimizing code: {str(e)}")
|
389 |
-
return None
|
390 |
|
391 |
-
# Main
|
392 |
-
|
393 |
-
client = get_groq_client()
|
394 |
-
if not client:
|
395 |
-
return
|
396 |
|
397 |
-
|
398 |
-
|
399 |
-
|
400 |
-
|
401 |
-
|
402 |
-
|
403 |
-
|
404 |
-
|
405 |
-
|
406 |
-
selected_language = st.selectbox("Programming Language", LANGUAGES, index=0)
|
407 |
-
|
408 |
-
# Complexity selection with icons and descriptions
|
409 |
-
complexity = st.selectbox(
|
410 |
-
"Code Complexity",
|
411 |
-
options=list(COMPLEXITY_LEVELS.keys()),
|
412 |
-
format_func=lambda x: f"{COMPLEXITY_LEVELS[x]['icon']} {x}",
|
413 |
-
index=1,
|
414 |
-
help="Select the level of complexity for the generated code"
|
415 |
-
)
|
416 |
-
|
417 |
-
# Display complexity description
|
418 |
-
with st.expander("Complexity Details", expanded=False):
|
419 |
-
st.markdown(f"**{complexity}** {COMPLEXITY_LEVELS[complexity]['icon']}")
|
420 |
-
st.caption(COMPLEXITY_LEVELS[complexity]['description'])
|
421 |
-
|
422 |
-
# Additional options
|
423 |
-
with st.expander("Advanced Options", expanded=False):
|
424 |
-
coding_style = st.selectbox(
|
425 |
-
"Coding Style",
|
426 |
-
["Default", "Functional", "OOP", "Procedural", "Concise", "Verbose"],
|
427 |
-
index=0
|
428 |
-
)
|
429 |
-
|
430 |
-
tags = st_tags(
|
431 |
-
label="Keywords/Tags:",
|
432 |
-
text="Press enter to add more",
|
433 |
-
value=[],
|
434 |
-
key="tags",
|
435 |
-
suggestions=["algorithm", "data structure", "API", "GUI", "CLI"]
|
436 |
-
)
|
437 |
-
|
438 |
-
auto_explain = st.checkbox("Auto-generate explanation", value=True)
|
439 |
-
show_metadata = st.checkbox("Show code metadata", value=False)
|
440 |
-
|
441 |
-
st.markdown("---")
|
442 |
-
st.markdown("### About AstraCode Pro")
|
443 |
-
st.caption("A next-gen AI code generator with advanced customization and theming")
|
444 |
-
|
445 |
-
# Main content area
|
446 |
-
st.title(f"π» AstraCode Pro")
|
447 |
-
st.caption("Generate production-ready code with AI-powered assistance")
|
448 |
-
|
449 |
-
# Layout columns
|
450 |
-
col1, col2 = st.columns([4, 1])
|
451 |
-
|
452 |
-
with col1:
|
453 |
-
# Code description input
|
454 |
-
with st.container(border=True):
|
455 |
-
query = st.text_area(
|
456 |
-
"Describe your coding requirement:",
|
457 |
-
height=150,
|
458 |
-
placeholder="e.g., A REST API endpoint for user authentication with JWT tokens\nor\nA React component for a responsive product carousel",
|
459 |
-
help="Be as specific as possible for better results"
|
460 |
-
)
|
461 |
-
|
462 |
-
# Initialize session state
|
463 |
-
if 'generated_code' not in st.session_state:
|
464 |
-
st.session_state.generated_code = None
|
465 |
-
if 'alternative_code' not in st.session_state:
|
466 |
-
st.session_state.alternative_code = None
|
467 |
-
if 'optimized_code' not in st.session_state:
|
468 |
-
st.session_state.optimized_code = None
|
469 |
-
if 'explanation' not in st.session_state:
|
470 |
-
st.session_state.explanation = None
|
471 |
-
|
472 |
-
# Action buttons
|
473 |
-
action_cols = st.columns([1, 1, 1, 1, 1, 2])
|
474 |
-
|
475 |
-
with action_cols[0]:
|
476 |
-
if st.button("π Generate", use_container_width=True, help="Generate initial code implementation"):
|
477 |
-
if not query:
|
478 |
-
st.warning("Please enter a code description")
|
479 |
-
else:
|
480 |
-
with st.spinner(f"Generating {complexity.lower()} {selected_language} code..."):
|
481 |
-
progress_bar = st.progress(0)
|
482 |
-
|
483 |
-
for i in range(100):
|
484 |
-
time.sleep(0.02)
|
485 |
-
progress_bar.progress(i + 1)
|
486 |
-
|
487 |
-
code = generate_code(
|
488 |
-
query,
|
489 |
-
selected_language,
|
490 |
-
PRIMARY_MODEL,
|
491 |
-
client,
|
492 |
-
complexity,
|
493 |
-
tags,
|
494 |
-
coding_style if coding_style != "Default" else None
|
495 |
-
)
|
496 |
-
|
497 |
-
if not code:
|
498 |
-
st.warning("Trying backup model...")
|
499 |
-
code = generate_code(
|
500 |
-
query,
|
501 |
-
selected_language,
|
502 |
-
BACKUP_MODEL,
|
503 |
-
client,
|
504 |
-
complexity,
|
505 |
-
tags,
|
506 |
-
coding_style if coding_style != "Default" else None
|
507 |
-
)
|
508 |
-
|
509 |
-
if code:
|
510 |
-
st.session_state.generated_code = code
|
511 |
-
st.session_state.alternative_code = None
|
512 |
-
st.session_state.optimized_code = None
|
513 |
-
|
514 |
-
if auto_explain:
|
515 |
-
with st.spinner("Generating explanation..."):
|
516 |
-
st.session_state.explanation = generate_explanation(
|
517 |
-
code,
|
518 |
-
selected_language,
|
519 |
-
client,
|
520 |
-
complexity
|
521 |
-
)
|
522 |
-
|
523 |
-
progress_bar.empty()
|
524 |
-
|
525 |
-
with action_cols[1]:
|
526 |
-
if st.button("π Alternative", use_container_width=True,
|
527 |
-
disabled=not st.session_state.generated_code,
|
528 |
-
help="Generate alternative implementation"):
|
529 |
-
with st.spinner(f"Generating alternative {selected_language} solution..."):
|
530 |
-
alt_code = generate_code(
|
531 |
-
f"Alternative approach for: {query}",
|
532 |
-
selected_language,
|
533 |
-
PRIMARY_MODEL,
|
534 |
-
client,
|
535 |
-
complexity,
|
536 |
-
tags,
|
537 |
-
coding_style if coding_style != "Default" else None
|
538 |
-
)
|
539 |
-
if alt_code:
|
540 |
-
st.session_state.alternative_code = alt_code
|
541 |
-
|
542 |
-
with action_cols[2]:
|
543 |
-
if st.button("β‘ Optimize", use_container_width=True,
|
544 |
-
disabled=not st.session_state.generated_code,
|
545 |
-
help="Optimize the generated code"):
|
546 |
-
with st.spinner(f"Optimizing {selected_language} code..."):
|
547 |
-
optimized = optimize_code(
|
548 |
-
st.session_state.generated_code,
|
549 |
-
selected_language,
|
550 |
-
client
|
551 |
-
)
|
552 |
-
if optimized:
|
553 |
-
st.session_state.optimized_code = optimized
|
554 |
-
|
555 |
-
with action_cols[3]:
|
556 |
-
if st.button("π Explain", use_container_width=True,
|
557 |
-
disabled=not st.session_state.generated_code,
|
558 |
-
help="Generate detailed explanation"):
|
559 |
-
with st.spinner("Generating code explanation..."):
|
560 |
-
st.session_state.explanation = generate_explanation(
|
561 |
-
st.session_state.generated_code,
|
562 |
-
selected_language,
|
563 |
-
client,
|
564 |
-
complexity
|
565 |
-
)
|
566 |
-
|
567 |
-
with action_cols[4]:
|
568 |
-
if st.button("π§Ή Clear", use_container_width=True,
|
569 |
-
help="Clear all generated content"):
|
570 |
-
st.session_state.generated_code = None
|
571 |
-
st.session_state.alternative_code = None
|
572 |
-
st.session_state.optimized_code = None
|
573 |
-
st.session_state.explanation = None
|
574 |
-
|
575 |
-
# Display results in tabs
|
576 |
-
if st.session_state.generated_code:
|
577 |
-
tab1, tab2, tab3 = st.tabs(["Generated Code", "Optimized Code", "Explanation"])
|
578 |
-
|
579 |
-
with tab1:
|
580 |
-
st.subheader(f"Generated {selected_language} Code ({complexity})")
|
581 |
-
|
582 |
-
if show_metadata:
|
583 |
-
with st.expander("Code Metadata", expanded=False):
|
584 |
-
metadata_cols = st.columns(3)
|
585 |
-
with metadata_cols[0]:
|
586 |
-
st.metric("Language", selected_language)
|
587 |
-
with metadata_cols[1]:
|
588 |
-
st.metric("Complexity", complexity)
|
589 |
-
with metadata_cols[2]:
|
590 |
-
st.metric("Style", coding_style)
|
591 |
-
|
592 |
-
st.code(st.session_state.generated_code, language=selected_language.lower())
|
593 |
-
|
594 |
-
if st.session_state.explanation and auto_explain:
|
595 |
-
with st.expander("Auto-generated Explanation", expanded=False):
|
596 |
-
st.markdown(st.session_state.explanation)
|
597 |
-
|
598 |
-
with tab2:
|
599 |
-
if st.session_state.optimized_code:
|
600 |
-
st.subheader(f"Optimized {selected_language} Code")
|
601 |
-
st.code(st.session_state.optimized_code, language=selected_language.lower())
|
602 |
-
else:
|
603 |
-
st.info("Click the 'Optimize' button to generate an optimized version")
|
604 |
-
|
605 |
-
with tab3:
|
606 |
-
if st.session_state.explanation:
|
607 |
-
st.subheader("Code Explanation")
|
608 |
-
st.markdown(st.session_state.explanation)
|
609 |
-
else:
|
610 |
-
st.info("Click the 'Explain' button to generate a code explanation")
|
611 |
-
|
612 |
-
# Alternative solution section
|
613 |
-
if st.session_state.alternative_code:
|
614 |
-
st.markdown("---")
|
615 |
-
with st.container(border=True):
|
616 |
-
st.subheader(f"Alternative {selected_language} Solution")
|
617 |
-
st.code(st.session_state.alternative_code, language=selected_language.lower())
|
618 |
-
|
619 |
-
# Empty state
|
620 |
-
elif not st.session_state.generated_code and not query:
|
621 |
-
with st.container(border=True):
|
622 |
-
st.subheader("Welcome to AstraCode Pro!")
|
623 |
-
st.markdown("""
|
624 |
-
<div style="opacity: 0.8;">
|
625 |
-
<p>Get started by:</p>
|
626 |
-
<ol>
|
627 |
-
<li>Describing your coding requirement in the text area</li>
|
628 |
-
<li>Selecting your preferred language and complexity</li>
|
629 |
-
<li>Clicking "Generate" to create your code</li>
|
630 |
-
</ol>
|
631 |
-
<p>Try these examples:</p>
|
632 |
-
<ul>
|
633 |
-
<li><i>"A Python function to calculate Fibonacci sequence with memoization"</i></li>
|
634 |
-
<li><i>"A React component for a responsive image gallery with lazy loading"</i></li>
|
635 |
-
<li><i>"A REST API in Node.js with Express for user authentication"</i></li>
|
636 |
-
</ul>
|
637 |
-
</div>
|
638 |
-
""", unsafe_allow_html=True)
|
639 |
|
640 |
-
|
641 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
+
import os
|
3 |
from groq import Groq
|
|
|
|
|
|
|
4 |
|
5 |
+
# UI Setup
|
6 |
+
st.set_page_config(page_title="CodeGen Pro", page_icon="π‘", layout="wide")
|
7 |
+
st.markdown("<h1 style='text-align:center;'>β‘ AI Code Generator with Qwen 32B</h1>", unsafe_allow_html=True)
|
8 |
+
st.markdown("Generate clean, documented code with the power of Qwen-32B model by Groq π")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
+
# Sidebar Configuration
|
11 |
+
with st.sidebar:
|
12 |
+
st.header("π οΈ Code Settings")
|
13 |
+
language = st.selectbox("Programming Language", ["Python", "JavaScript", "Go", "Java", "C++", "TypeScript"])
|
14 |
+
style = st.selectbox("Coding Style", ["Default", "Functional", "OOP", "Concise", "Verbose"])
|
15 |
+
tags = st.text_input("π Tags (comma-separated)", placeholder="API, login, JWT, MongoDB")
|
16 |
+
temperature = st.slider("π Temperature", 0.0, 1.0, 0.6)
|
17 |
+
max_tokens = st.slider("π§ Max Tokens", 256, 4096, 2048)
|
18 |
+
st.markdown("β
Make sure your `GROQ_API_KEY` is set as environment variable.")
|
19 |
|
20 |
+
# Prompt builder
|
21 |
+
def build_prompt(user_input, language, style, tags):
|
22 |
+
base_instruction = f"""
|
23 |
+
You are a senior software engineer and your task is to generate clean, well-documented, and executable {language} code. Follow these requirements:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
Language: {language}
|
26 |
+
Style: {style if style != "Default" else "any"}
|
27 |
+
Tags: {tags or "N/A"}
|
|
|
|
|
|
|
|
|
28 |
|
29 |
+
π‘ Instructions:
|
30 |
+
- Use best practices in {language}
|
31 |
+
- Include comments where needed
|
32 |
+
- Avoid unnecessary explanations
|
33 |
+
- The code must be copy-paste ready
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
π User Requirement:
|
36 |
+
\"\"\"
|
37 |
+
{user_input.strip()}
|
38 |
+
\"\"\"
|
39 |
+
"""
|
40 |
+
return base_instruction.strip()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
+
# Streaming generator
|
43 |
+
def stream_code(prompt):
|
44 |
+
client = Groq(api_key=os.getenv("GROQ_API_KEY"))
|
45 |
+
response = client.chat.completions.create(
|
46 |
+
model="qwen/qwen3-32b",
|
47 |
+
messages=[{"role": "user", "content": prompt}],
|
48 |
+
temperature=temperature,
|
49 |
+
max_completion_tokens=max_tokens,
|
50 |
+
top_p=0.95,
|
51 |
+
reasoning_effort="default",
|
52 |
+
stream=True,
|
53 |
+
stop=None,
|
54 |
+
)
|
55 |
+
full_code = ""
|
56 |
+
for chunk in response:
|
57 |
+
token = chunk.choices[0].delta.content or ""
|
58 |
+
full_code += token
|
59 |
+
yield token
|
60 |
+
return full_code
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
+
# Main input area
|
63 |
+
user_input = st.text_area("π§ Describe what you want the code to do:", height=200, placeholder="e.g. Create a FastAPI JWT login endpoint connected to MongoDB...")
|
|
|
|
|
|
|
64 |
|
65 |
+
# Action
|
66 |
+
if st.button("π Generate Code"):
|
67 |
+
if not user_input.strip():
|
68 |
+
st.warning("Please enter a valid code request!")
|
69 |
+
else:
|
70 |
+
prompt = build_prompt(user_input, language, style, tags)
|
71 |
+
st.subheader("π Prompt Sent to Model")
|
72 |
+
with st.expander("π View Full Prompt", expanded=False):
|
73 |
+
st.code(prompt, language="markdown")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
st.subheader("π§Ύ Generated Code")
|
76 |
+
code_area = st.empty()
|
77 |
+
generated_text = ""
|
78 |
+
for token in stream_code(prompt):
|
79 |
+
generated_text += token
|
80 |
+
code_area.code(generated_text, language=language.lower())
|