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